forked from jamesmontemagno/mycadence-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.h
89 lines (77 loc) · 2.68 KB
/
device.h
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
//This is standard speed & cadence bluetooth UUID
static BLEUUID serviceUUID("00001816-0000-1000-8000-00805f9b34fb");
// This is the standard notify characteristic UUID
static BLEUUID notifyUUID("00002a5b-0000-1000-8000-00805f9b34fb");
static BLEAdvertisedDevice * devices[20];
static int device_count = 0;
// button handling
#define BUTTON_DEBOUNCE_TIME 50
#define BUTTON_SHORT_PRESS_TIME 400
// Add a device to our device list (deduplicate in the process)
void addDevice(BLEAdvertisedDevice * device) {
for (uint8_t i = 0; i < device_count; i++) {
if(device->getName() == devices[i]->getName()){
return;
}
}
if(!device->haveServiceUUID() || !device->isAdvertisingService(serviceUUID)) {
return;
}
devices[device_count] = device;
device_count++;
}
// Select a device and return it
BLEAdvertisedDevice * selectDevice(void) {
Heltec.display->setFont(ArialMT_Plain_10);
if(device_count == 0) return nullptr;
if(device_count == 1) return devices[0];
int selected = 0;
// Select a device
while(true) {
if(selected > device_count-1) selected = 0;
int start = selected < 2 ? 0 : selected - 1;
Heltec.display->clear();
Heltec.display->setLogBuffer(10, 50);
// Print to the screen
for (int i = start; i < device_count; i++) {
if(i == selected) {
Heltec.display->print("->");
} else {
Heltec.display->print(" ");
}
Heltec.display->println(devices[i]->getName().c_str());
}
Heltec.display->drawLogBuffer(0, 0);
Heltec.display->display();
int lastState = LOW; // the previous state from the input pin
int currentState; // the current reading from the input pin
unsigned long pressedTime = 0;
while(true) {
// read the state of the switch/button:
currentState = digitalRead(KEY_BUILTIN);
if(lastState == HIGH && currentState == LOW) { // button is pressed
pressedTime = millis();
Serial.println("DOWN");
} else if(pressedTime != 0 && lastState == LOW && currentState == HIGH) { // button is released
long pressDuration = millis() - pressedTime;
Serial.println("UP");
if(pressDuration < BUTTON_DEBOUNCE_TIME) {
pressedTime = 0;
} else if(pressDuration < BUTTON_SHORT_PRESS_TIME ) {
Serial.println("A short press is detected");
pressedTime = 0;
selected++;
break;
} else {
Serial.println("A long press is detected");
pressedTime = 0;
return devices[selected];
break;
}
}
// save the the last state
lastState = currentState;
}
}
return nullptr;
}