-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
153 lines (129 loc) · 4.18 KB
/
index.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
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
var name = 'PizzaSquat';
var pizzaServiceUuid = '13333333-3333-3333-3333-333333333337';
var characteristics = {
crust: '13333333-3333-3333-3333-333333330001',
toppings: '13333333-3333-3333-3333-333333330002',
bake: '13333333-3333-3333-3333-333333330003'
};
var PizzaBakeResult = {
HALF_BAKED: 0,
BAKED: 1,
CRISPY: 2,
BURNT: 3,
ON_FIRE: 4
};
var crustSelectEl = document.getElementById('crust-type');
var toppingsEls = document.querySelectorAll('[name=toppings]');
var ovenTempEl = document.getElementById('oven-temperature');
var crustTypeEl = document.getElementById('crust-type');
var outputEl = document.getElementById('output');
// ¯\_(ツ)_/¯
function swap16(val) {
// le to be
return ((val & 0xFF) << 8)
| ((val >> 8) & 0xFF);
}
// store characteristics after retrieval
var cachedCharacteristics = {};
// current bluetooth connection obj
var ovenServer = null;
// connect to bluetooth peripheral
var readyOven = function() {
return navigator.bluetooth.requestDevice({
filters: [{ services: [ pizzaServiceUuid ], name: name }]
}).then(function(device) {
return device.gatt.connect();
}).then(function(server) {
ovenServer = server;
return server.getPrimaryService(pizzaServiceUuid);
}).then(function(service) {
return Promise.all(Object.values(characteristics).map((uuid)=>service.getCharacteristic(uuid)));
}).then(function(characteristicObjs) {
Object.keys(characteristics).forEach((name, i)=> {
cachedCharacteristics[name] = characteristicObjs[i];
});
}).catch(function(err) {
alert('oven (bluetooth) error');
throw err;
});
};
// characteristic setup
var readyCrust = function(crustType) {
var crust = new Uint8Array(1);
crust[0] = crustType;
var pizzaCrustCharacteristic = cachedCharacteristics['crust'];
if(pizzaCrustCharacteristic == null) throw new Error('oven not ready!');
return pizzaCrustCharacteristic.writeValue(crust).catch(function(err) {
alert('crust error');
throw err;
});
};
var readyToppings = function(toppings) {
var toppingsBuff = new Uint8Array(2);
toppingsBuff[0] = toppings.concat(0).reduce((a, b)=>a | b);
var pizzaToppingsCharacteristic = cachedCharacteristics['toppings'];
if(pizzaToppingsCharacteristic == null) throw new Error('oven not ready');
return pizzaToppingsCharacteristic.writeValue(toppingsBuff).catch(function(err) {
alert('toppings error');
throw err;
});
};
var bakePizza = function(temperature) {
var pizzaBakeCharacteristic = cachedCharacteristics['bake'];
if(pizzaBakeCharacteristic == null) throw new Error('oven not ready!');
var tempBuff = new Uint16Array([swap16(temperature)]);
return pizzaBakeCharacteristic.writeValue(tempBuff).then(function() {
result = pizzaBakeCharacteristic.value.getUint8();
log('The result is ' + (
result == PizzaBakeResult.HALF_BAKED ? 'half baked.' :
result == PizzaBakeResult.BAKED ? 'baked.' :
result == PizzaBakeResult.CRISPY ? 'crispy.' :
result == PizzaBakeResult.BURNT ? 'burnt.' :
result == PizzaBakeResult.ON_FIRE ? 'on fire!' : 'unknown?'));
return result;
}).catch(function(err) {
alert('bake error');
throw err;
});
};
// get values from dom
var getCrustType = function() {
return Number(crustSelectEl.value);
};
var getToppings = function() {
var toppings = [];
[].slice.call(toppingsEls).forEach(function(el) {
if(el.checked) toppings.push(Number(el.value));
});
return toppings;
};
var getOvenTemperature = function() {
return ovenTempEl.value;
};
// button listeners
var onStartButtonClick = function(e) {
if(ovenServer != null && ovenServer.connected) {
alert('Already connected...');
return;
}
readyOven().then(function() {
alert('Connection successful!');
});
};
var onBakeButtonClick = function(e) {
if(ovenServer == null || !ovenServer.connected) {
alert('Not connected!');
return;
}
readyCrust(getCrustType())
.then(() => readyToppings(getToppings()))
.then(() => bakePizza(getOvenTemperature()))
};
var log = function(text) {
outputEl.textContent = text;
}
document.addEventListener('DOMContentLoaded', function() {
if(navigator.bluetooth) {
outputEl.textContent = 'ready.';
}
});