This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_legacy.js
204 lines (183 loc) · 5.69 KB
/
_legacy.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
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
/* ===================================== */
/* Not included: Symbols that may be revived or useful, worth keeping in repo */
/*
// Handle to the connected device.
deviceHandle: null,
*/
blend.deviceHandle = null;
/*
// Handles to characteristics and descriptor for reading and
// writing data from/to the Arduino using the BLE shield.
characteristicRead: null,
characteristicWrite: null,
descriptorNotification: null,
*/
blend.characteristicRead = null;
blend.characteristicWrite = null;
blend.descriptorNotification = null;
function checkDoublePress(key, interval) {
var now = new Date().getUTCTime();
if ( dblClickBuffer['key'] == key && now <= dblClickBuffer['stamp'] + interval ) {
// console.log('Double click on aisle ' + key);
return true;
}
else {
dblClickBuffer['key'] = key;
dblClickBuffer['stamp'] = now;
// console.log('Initial click ' + dblClickBuffer['key'] + ' recorded ' + dblClickBuffer['stamp']);
return false;
}
}
// TODO; no longer called but need to incorporate this into journey.start(), then kill
function initialiseGPS(options) {
if ( options === undefined ) {
options = config.geoOptions;
}
if (navigator.geolocation) {
geoWatchID = navigator.geolocation.watchPosition(
function(position) {
// console.log('Geolocation change at (' + position.coords.latitude + ',' + position.coords.longitude + ')');
journey.addPoint([position.coords.longitude, position.coords.latitude])
},
function(error) {
logActivity('Geolocation change error ' + error.code + ': "' + error.message + '"');
},
options
);
}
else {
console.log('no js geo support');
}
}
function keypressHandler(data) {
var val = data[0];
var speed = 1;
if ( val != 0 ) {
if ( checkDoublePress(val, speed) ) {
val += 3;
}
}
// Update background color.
switch (val) {
case 0: // no keys
setPathColor('white');
break;
case 1: // user key
setPathColor('blue');
logPosition(val);
break;
case 2: // power key
setPathColor('red');
logPosition(val);
break;
case 3: // both keys
setPathColor('magenta');
break;
case 4:
case 5:
case 6:
logActivity('Double clicked ' + (val-3).toString() + ' to get ' + val );
logPosition(val);
}
}
// **************************************************
// Journey GPX functions that fail because DOM seems to fail with a security error on some handsets, use JSON equiv for now
Journey.addPoint_GPX = function(lonlat) {
trkseg = this.dom.querySelector("trk trkseg"); // [name='" + title + "']
trkpt = this.dom.createElement('trkpt');
trkpt.setAttribute('lon', lonlat[0]);
trkpt.setAttribute('lat', lonlat[1]);
time = this.dom.createElement('time');
timeValue = this.dom.createTextNode(formatTimestamp(new Date(), 'W3CDTF'));
time.appendChild(timeValue);
trkpt.appendChild(time);
trkseg.appendChild(trkpt);
}
Journey.makeTracks_GPX = function() {
// FIXME: src below, clearly
var src = '<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1" creator="OSMTracker for Android™ - http://osmtracker-android.googlecode.com/" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd ">';
src += '<trk><name>' + title + '</name><trkseg>';
// src += '<trkpt>'; //etc
src += '</trkseg></trk>';
src += '</gpx>';
// this['gpx'] = {};
this['gpx'] = {
dom: (new DOMParser).parseFromString(src, 'application/xml'),
serialise: function() {
// return this.dom.toString();
return (new XMLSerializer()).serializeToString(this.gpx.dom);
}
}
this['geoJSON'] = {};
this['geoJSON'].serialise = function() {
return toGeoJSON.gpx(this.gpx.dom);
}
console.log('ran Journey.maketracks()');
// console.log(this.gpx);
}
blend.on = function() {
logActivity('LED on requested');
app.write(
'writeCharacteristic',
app.deviceHandle,
app.characteristicWrite,
new Uint8Array([1])); // 1 = on
}
blend.off = function() {
logActivity('LED off requested');
app.write(
'writeCharacteristic',
app.deviceHandle,
app.characteristicWrite,
new Uint8Array([0])); // 0 = off
}
blend.write = function(writeFunc, deviceHandle, handle, value) {
if (handle) {
ble[writeFunc](
deviceHandle,
handle,
value,
function() {
console.log(writeFunc + ': ' + handle + ' success.');
},
function(errorCode) {
console.log(writeFunc + ': ' + handle + ' error: ' + errorCode);
}
);
}
}
blend.getServices = function(deviceHandle) {
console.log('Scanning for services...');
evothings.ble.readAllServiceData(deviceHandle, function(services) {
// Find handles for characteristics and descriptor needed.
for (var si in services) {
var service = services[si];
for (var ci in service.characteristics) {
var characteristic = service.characteristics[ci];
if (characteristic.uuid == '713d0002-503e-4c75-ba94-3148f18d941e') {
app.characteristicRead = characteristic.handle;
}
else if (characteristic.uuid == '713d0003-503e-4c75-ba94-3148f18d941e') {
app.characteristicWrite = characteristic.handle;
}
for (var di in characteristic.descriptors) {
var descriptor = characteristic.descriptors[di];
if (characteristic.uuid == '713d0002-503e-4c75-ba94-3148f18d941e' &&
descriptor.uuid == '00002902-0000-1000-8000-00805f9b34fb') {
app.descriptorNotification = descriptor.handle;
}
}
}
}
if (app.characteristicRead && app.characteristicWrite && app.descriptorNotification) {
console.log('RX/TX services found.');
// app.startReading(deviceHandle);
}
else {
console.log('ERROR: RX/TX services not found!');
}
},
function(errorCode) {
console.log('readAllServiceData error: ' + errorCode);
});
}