-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
122 lines (104 loc) · 2.55 KB
/
app.js
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
// place your const, vars, functions or classes here
// clear the screen
g.clear();
class State {
constructor(param) {
this.busy = param.busy
this.isConnected = param.isConnected;
this.gatt = param.gatt;
}
}
const initState = new State({
busy: false,
isConnected: false,
gatt: undefined
});
function setState(updater) {
state = updater.call(this, state);
draw();
}
var state = initState;
var service, characteristic;
// redraw the screen
function draw() {
g.reset().clearRect(Bangle.appRect);
var title = "idle";
if (state.isConnected) title = "GoPro";
if (state.busy) title = "...";
// g.setFont("6x8").setFontAlign(0,0)
// .drawString(title,g.getWidth()/2,g.getHeight()/2 - 20);
g.setFont("Vector",42).setFontAlign(0,0)
.drawString(title,g.getWidth()/2,g.getHeight()/2);
}
function onDisconnect(reason) {
setState((state) => {
state.isConnected = false;
return state;
});
}
function connect() {
if (state.busy) {
console.log("Device busy");
return;
}
setState(state => {
state.busy = true;
return state;
});
var gatt;
if (!state.isConnected) {
// search for GoPro Device
NRF.requestDevice({ filters: [{ service: ['fea6'] }] })
.then(device => {
console.log("Found GoPro: " + JSON.stringify(device));
return device.gatt.connect();
})
.then(g => {
gatt = g;
setState(state => {
state.isConnected = true;
return state;
});
return service||gatt.getPrimaryService("b5f9fea6-aa8d-11e3-9046-0002a5d5c51b");
})
.then(s => {
service = s;
console.log("Service", s);
return characteristic||s.getCharacteristic();
})
.then(c => {
characteristic = c;
// TODO: writeValue, startNotification, ...
return c;
})
.then(() => {
gatt.disconnect();
setState(state => {
state.isConnected = false;
state.busy = false;
return state;
});
})
.catch(err => {
console.log("Error: " + err);
setState(state => {
state.busy = false;
return state;
});
});
}
}
setInterval(connect, 3000);
function findDevices() {
deviceCounter = -1;
if (!state.isConnected && !state.busy) {
NRF.findDevices(function(devices) {
deviceCounter = devices.length;
console.log(devices);
}, {timeout: 3000, filters: [{service: ['fea6']}]});
}
}
setInterval(findDevices, 4000);
// Load widgets
Bangle.loadWidgets();
Bangle.drawWidgets();