-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch0_master.ino
233 lines (191 loc) · 5.86 KB
/
watch0_master.ino
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
THIS CODE IS FOR THE "MASTER: WATCH0"
"Hearing Watch Project"
Written by: Evan Thebearge
Version 0.0.2 Beta
Last Updated: 12/21/23 - 08:31 AM
ArduinoBLE GitHub used for referencing
*/
#include <ArduinoBLE.h>
// define pins and button states
const int led = D2;
const int m = D6;
const int b0 = D4;
const int b1 = D5;
int ob0s = LOW;
int ob1s = LOW;
void setup() {
// enable lithium cell battery charging
pinMode(P0_13, OUTPUT);
// enable serial logging
Serial.begin(9600);
// set pins to input/output mode
pinMode(led, OUTPUT);
pinMode(m, OUTPUT);
pinMode(b0, INPUT_PULLUP);
pinMode(b1, INPUT_PULLUP);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
// start scanning for WATCH1
BLE.scanForName("WATCH1");
}
void loop() {
// enable lithium cell battery high current charging
digitalWrite(P0_13, HIGH);
// check if WATCH0 (peripheral) has been discovered
BLEDevice peripheral = BLE.available();
if (peripheral) {
// discovered WATCH0 (peripheral), print out address, local name, and advertised services
Serial.print("Found ");
Serial.print(peripheral.address());
Serial.print(" '");
Serial.print(peripheral.localName());
Serial.print("' ");
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();
// make sure peripheral is WATCH1
if (peripheral.localName() != "WATCH1") {
return;
}
// stop scanning for WATCH1
BLE.stopScan();
// run WATCH scripts
control(peripheral);
// if it disconnects, restart scanning for WATCH1
BLE.scanForName("WATCH1");
}
}
void control(BLEDevice peripheral) {
// connect to the WATCH1 (peripheral)
Serial.println("Connecting ...");
if (peripheral.connect()) {
Serial.println("Connected");
} else {
Serial.println("Failed to connect!");
return;
}
// discover WATCH1 (peripheral) attributes
Serial.println("Discovering attributes ...");
if (peripheral.discoverAttributes()) {
Serial.println("Attributes discovered");
} else {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}
// retrieve the characteristics from WATCH1 and verify
// characteristics for reading peripheral button status to notify master
BLECharacteristic pb0n = peripheral.characteristic("0001");
BLECharacteristic pb1n = peripheral.characteristic("0002");
// characteristics for read of button status from master
BLECharacteristic mb0w = peripheral.characteristic("1001");
BLECharacteristic mb1w = peripheral.characteristic("2002");
// check if it has 1 of these characteristics
if (!pb0n) {
Serial.println("Peripheral does not have readable button characteristic!");
peripheral.disconnect();
return;
} else if (!pb0n.canRead()) {
Serial.println("Peripheral does not have a readable button characteristic!");
peripheral.disconnect();
return;
}
// subscribe to notifcations
Serial.println("Subscribing to pb0n ...");
if (!pb0n) {
Serial.println("characteristic pb0n not found!");
peripheral.disconnect();
return;
} else if (!pb0n.canSubscribe()) {
Serial.println("characteristic pb0n not subscribable!");
peripheral.disconnect();
return;
} else if (!pb0n.subscribe()) {
Serial.println("pb0n subscription failed!");
peripheral.disconnect();
return;
}
Serial.println("Subscribing to pb1n ...");
if (!pb1n) {
Serial.println("characteristic pb1n not found!");
peripheral.disconnect();
return;
} else if (!pb1n.canSubscribe()) {
Serial.println("characteristic pb0n not subscribable!");
peripheral.disconnect();
return;
} else if (!pb1n.subscribe()) {
Serial.println("pb1n subscription failed!");
peripheral.disconnect();
return;
}
// While the WATCH1 (peripheral) is connected
while (peripheral.connected()) {
// check if slave b0 has been updated
if (pb0n.valueUpdated()) {
// yes, get the value, characteristic is 1 byte so use byte value
byte value = 0;
pb0n.readValue(value);
if (value & 0x01) {
// slave b0 is pressed, turn the m on
Serial.println("M on");
digitalWrite(m, HIGH);
delay(2000);
} else { // a 0 value
// slave b0 is released, turn the m off
Serial.println(F("M off"));
digitalWrite(m, LOW);
}
}
// check if slave b1 has been updated
if (pb1n.valueUpdated()) {
// yes, get the value, characteristic is 1 byte so use byte value
byte value = 0;
pb1n.readValue(value);
if (value & 0x01) {
// slave b0 is pressed, turn the LED on
Serial.println("LED on");
digitalWrite(led, HIGH);
delay(2000);
} else { // a 0 value
// slave b0 is released, turn the LED off
Serial.println(F("LED off"));
digitalWrite(led, LOW);
}
}
// Turn on slave m if b0 is pressed
int b0state = digitalRead(b0);
if (ob0s != b0state) {
// b0 changed
ob0s = b0state;
if (b0state) {
Serial.println("b0 pressed");
// b0 is pressed, write 0x01 to turn the slave m on
mb0w.writeValue((byte)0x01);
} else {
Serial.println("b0 released");
// b0 is released, write 0x00 to turn the slave m off
mb0w.writeValue((byte)0x00);
}
}
// Turn on slave LED if b1 is pressed
int b1state = digitalRead(b1);
if (ob1s != b1state) {
// b1 changed
ob1s = b1state;
if (b1state) {
Serial.println("b1 pressed");
// b1 pressed, write 0x01 to turn the slave LED on
mb1w.writeValue((byte)0x01);
} else {
Serial.println("b1 released");
// b1 is released, write 0x00 to turn the slave LED off
mb1w.writeValue((byte)0x00);
}
}
}
Serial.println("WATCH1 disconnected");
}