-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicrobit.java
More file actions
80 lines (71 loc) · 2.71 KB
/
Copy pathMicrobit.java
File metadata and controls
80 lines (71 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* This class controls a micro:bit via bluetooth. It inherits almost
* all its functionality from the abstract class Robot. The only methods it needs here are constructors,
* which call a function that checks whether or not it is a micro:bit.
*
* Mike Yuan and Bambi Breewer, BirdBrain Technologies LLC
* November 2018
*/
public class Microbit extends Robot {
/** This function tries to read sensor 4 (the Hummingbird battery) to determine whether
* the device is a Hummingbird or a micro:bit. It returns true if the device is a micro:bit. */
private boolean isMicrobit() {
StringBuilder newURL = new StringBuilder(baseUrl);
String testURL = (newURL.append("in/isMicrobit/static/")
.append(deviceInstance)).toString();
String stringResponse = sendHttpRequest(testURL);
if (stringResponse.equals("false")) {
System.out.println("Error: Device " + deviceInstance + " is not a Microbit");
return false;
} else if (stringResponse.equals("Not Connected")) {
System.out.println("Error: Device " + deviceInstance + " is not connected.");
return false;
} else {
return true;
}
/*
try {
StringBuilder newURL = new StringBuilder(baseUrl);
String testURL = (newURL.append("in/")
.append("sensor/4/")
.append(deviceInstance)).toString();
requestUrl = new URL(testURL);
connection = (HttpURLConnection) requestUrl.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
String stringResponse = verifyResponse();
if (stringResponse.equals("255")) return true;
else {
System.out.println("Error: Device "+deviceInstance+" is not a micro:bit");
return false;
}
} catch (IOException e) {
System.out.println("Error: Device " + deviceInstance + " is not connected");
return false;
}
*/
}
/**
* default constructor for the library. Construct the baseUrl and set the default device to be A
*/
public Microbit() {
deviceInstance = "A";
if (!isConnectionValid()) System.exit(0);
if (!isMicrobit()) System.exit(0);
}
/**
* constructor for the library. Construct the baseUrl and set the default device to be input.
*
* @param device the input device that will be specified by the user.
*/
public Microbit(String device) {
if (!((device == "A")||(device == "B")||(device == "C"))) {
System.out.println("Error: Device must be A, B, or C.");
System.exit(0);
} else {
deviceInstance = device;
if (!isConnectionValid()) System.exit(0);
if (!isMicrobit()) System.exit(0);
}
}
}