-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice-test.html
196 lines (176 loc) · 5.18 KB
/
voice-test.html
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
<html>
<body>
<script src="//www.puck-js.com/puck.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/annyang/2.6.0/annyang.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/SpeechKITT/1.0.0/speechkitt.min.js"></script>
<pre id="log" style="float:right;width:600px"></pre>
<p>
Instructions: Click the microphone icon below to enable.
</p>
<p>
Voice Commands Supported:
<ul>
<li>'Turn on red'</li>
<li>'Turn off red'</li>
<li>'Turn on blue'</li>
<li>'Turn off blue'</li>
<li>'Turn on green'</li>
<li>'Turn off green'</li>
<li>'Magnet On'</li>
<li>'Magnet Off'</li>
<li>'Get Light Sensor'</li>
<li>'Get Temperature'</li>
<li>'Get Battery'</li>
<li>'Scan'</li>
<li>'Disconnect'</li>
<li>'Reconnect'</li>
<li>'Stop Listening'</li>
</ul>
<br />
<br />
<br /><br />
<button id="connectBLE" value="Scan" onclick="scan()">Scan</button>
<button id="disconnectBLE" value="Disconnect" onclick="disconnect()">Disconnect</button>
<button id="reconnectBLE" value="Reconnect" onclick="reconnect()">Reconnect</button>
<br /><br />
Set NFC Url: <input type="text" id="nfcUrl" disabled="disabled" /><button id="setNFC" value="Set" disabled="disabled">Set</button>
</p>
<script>
Puck.debug=3;
var bluetoothDevice;
function log(x) {
console.log(x);
document.getElementById("log").innerHTML += x+"\n";
}
function getLightValue()
{
Puck.eval("Puck.light()", function(v){
log("Command: Light Sensor (value: '"+ v +"')");
});
}
function getTemperature()
{
Puck.eval("E.getTemperature()", function(v) {
log("Command: Get Temperature (value: '"+ v +"ºC')");
});
}
function getBattery()
{
Puck.eval("Puck.getBatteryPercentage()", function(v) {
log("Command: Get Battery (value: '"+ v +"%')");
});
Puck.eval("NRF.getBattery()", function(v) {
log(" Battery Voltage (value: '"+ v +"v')");
});
}
function scan()
{
log("Command: Connect");
bluetoothDevice = null;
log("Requesting Bluetooth Device...");
navigator.bluetooth.requestDevice({
filters: [{ namePrefix: 'TESS' }], //<- Prefer filters to save energy & show relevant devices.
optionalServices: ['generic_access']
})
.then(device => {
bluetoothDevice = device;
bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected);
return connect();
})
.catch(error => {
log('ERROR: ' + error);
});
}
function connect() {
log('Connecting to Bluetooth Device...');
return bluetoothDevice.gatt.connect()
.then(server => {
log('Bluetooth Device connected');
});
}
function onDisconnected(event) {
// Object event.target is Bluetooth Device getting disconnected.
log('Bluetooth Device disconnected ('+ event.target.id +')');
}
function disconnect()
{
log("Command: Disconnect");
if (!bluetoothDevice) {
log('No paired bluetooth device to disconnect.');
return;
}
bluetoothDevice.gatt.disconnect();
}
function reconnect()
{
if (!bluetoothDevice) {
log('No paired bluetooth device to connect to.');
return;
}
if (bluetoothDevice.gatt.connected) {
log('Bluetooth Device is already connected');
return;
}
connect()
.catch(error => {
log('ERROR: ' + error);
});
}
annyang.addCommands({
'turn on red': function() {
log("Command: Turn on red");
Puck.write("LED1.set();\n");
},
'turn off red': function() {
log("Command: Turn off red");
Puck.write("LED1.reset();\n");
},
'turn on green': function() {
log("Command: Turn on green");
Puck.write("LED2.set();\n");
},
'turn off green': function() {
log("Command: Turn off green");
Puck.write("LED2.reset();\n");
},
'turn on blue': function() {
log("Command: Turn on blue");
Puck.write("LED3.set();\n");
},
'turn off blue': function() {
log("Command: Turn off blue");
Puck.write("LED3.reset();\n");
},
'magnet on': function() {
log("Command: magnet on");
Puck.write("Puck.magOn();\n")
//Puck.on('mag', function(xyz) {
// log("Magnetometer: value("+ xyz +")");
//});
},
'magnet off': function() {
log("Command: magnet off");
Puck.write("Puck.magOff();\n");
},
'get light sensor': function() { getLightValue(); },
'get temperature': function() {
var temp = Math.round(getTemperature());
var tempF = ((temp * 9/5) + 32);
log("Temperature: "+ tempF +"ºF");
},
'get battery': function() { getBattery(); },
'scan': function() { scan(); },
'disconnect': function() { disconnect(); },
'reconnect': function() { reconnect(); },
'stop listening': function() { annyang.abort(); }
});
annyang.addCallback('result', function(x) {
console.log("Got: "+x);
});
SpeechKITT.annyang();
SpeechKITT.setStylesheet('//cdnjs.cloudflare.com/ajax/libs/SpeechKITT/1.0.0/themes/flat-pumpkin.css');
SpeechKITT.setInstructionsText('Listening...');
SpeechKITT.rememberStatus(1);
SpeechKITT.render();
</script>
</body>