forked from jman294/talkback
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·339 lines (310 loc) · 8.2 KB
/
index.js
File metadata and controls
executable file
·339 lines (310 loc) · 8.2 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
var loudness = require('loudness')
var gea = require('gea-sdk')
var adapter = require('gea-adapter-usb')
var say = require('say')
var player = require('play-sound')({})
var gpio = require('rpi-gpio')
var fs = require('fs')
var app = gea.configure({
address: 0xcb,
version: [ 0, 0, 1, 0 ]
})
var PATH = '/sys/class/gpio'
var SOURCE = 0xcb
var WASHER = 0x23
var DRYER = 0x2b
var TIME_SECS = 0x2007
var TIME_MINS = 0x0046
var CYCLE_SELECTED = 0x200A
var END_CYCLE = 0x2002
var WATER_TEMP = 0x2016
var SPIN_LEVEL = 0x2017
var SOIL_LEVEL = 0x2015
var MACHINE_STATUS = 0x2000
var states = [
{
id: WASHER,
pinNo: 4,
oldCycle: 0,
cycleRunAlert: false,
oldMinutesRemaining: 0,
buttonPressed: false,
knobTurned: [false, false, false],
inACycle: false,
name: 'washer'
},
{
id: DRYER,
pinNo: 17,
oldCycle: 0,
cycleRunAlert: false,
oldMinutesRemaining: 0,
buttonPressed: false,
knobTurned: [false, false, false],
inACycle: false,
name: 'dryer'
}
]
setInterval(function () {
states.map(function (state) {
fs.readFile(PATH + '/gpio' + state.pinNo + '/value', function (err, data) {
if (err) throw err
if (data == 0) {
if (!state.buttonPressed && state.inACycle) {
say.speak('About '.concat(state.oldMinutesRemaining).concat(' minutes left on the ').concat(state.name), 'voice_us2_mbrola')
}
state.buttonPressed = true
} else {
state.buttonPressed = false
}
})
})
}, 100)
var encodings = {
"0101": 1,
"0100": 2,
"0000": 3,
"0001": 4,
"0011": 5,
"0010": 6,
"0110": 7,
"0111": 8,
"1111": 9,
"1110": 10,
"1010": 11,
"1011": 12,
"1001": 13,
"1000": 14,
"1100": 15,
"1101": 16
}
setInterval(function () {
var regex = /\n$/
var pin1 = fs.readFileSync(PATH + '/gpio26/value').toString().replace(regex, '')
var pin2 = fs.readFileSync(PATH + '/gpio13/value').toString().replace(regex, '')
var pin3 = fs.readFileSync(PATH + '/gpio6/value').toString().replace(regex, '')
var pin4 = fs.readFileSync(PATH + '/gpio27/value').toString().replace(regex, '')
var num = pin1.concat(pin2).concat(pin3).concat(pin4)
loudness.setVolume(90-encodings[num], function (err) {
if (err) throw err
})
}, 100)
app.bind(adapter, function (bus) {
bus.on('publish', function (erd) {
switch (erd.erd) {
case MACHINE_STATUS:
var machineStatus = erd.data[0]
for (var state in states) {
if (erd.source === states[state].id) {
if (machineStatus === 2) {
states[state].inACycle = true
if (states[state].cycleRunAlert) {
// Pressed Start Button
states[state].cycleRunAlert = false
say.speak(
'Starting '
.concat(getReadableCycleName(states[state].oldCycle))
.concat(', with an estimated ')
.concat(states[state].oldMinutesRemaining)
.concat(' minutes left.')
, 'voice_us2_mbrola')
}
} else {
states[state].inACycle = false
states[state].cycleRunAlert = true
}
}
}
break
case SPIN_LEVEL:
var tempCode = erd.data[0]
for (var state in states) {
if (erd.source === states[state].id) {
console.log(states[state].name, states[state].knobTurned)
if (!states[state].knobTurned[0]) {
say.speak('Spin level, '.concat(getSpinLevelByCode(tempCode)))
}
states[state].knobTurned[0] = false
}
}
break
case SOIL_LEVEL:
//console.log('Soiled it soiled it')
var tempCode = erd.data[0]
for (var state in states) {
if (erd.source === states[state].id) {
console.log(states[state].name, states[state].knobTurned)
if (!states[state].knobTurned[1]) {
say.speak('Soil level, '.concat(getSoilLevelByCode(tempCode)))
}
states[state].knobTurned[1] = false
}
}
break
case WATER_TEMP:
var tempCode = erd.data[0]
for (var state in states) {
if (erd.source === states[state].id) {
console.log(states[state].name, states[state].knobTurned)
if (!states[state].knobTurned[2]) {
say.speak('Water temperature, '.concat(getTempByCode(tempCode)))
}
states[state].knobTurned[2] = false
}
}
break
case TIME_SECS:
var bytes = erd.data
for (var state in states) {
if (erd.source === states[state].id) {
var minutes = Math.round((bytes[0]*255 + bytes[1])/60)
states[state].oldMinutesRemaining = minutes
}
}
break
case TIME_MINS:
var timeRemaining = erd.data[1]
for (var state in states) {
if (erd.source === states[state].id) {
states[state].oldMinutesRemaining = timeRemaining
}
}
break
case CYCLE_SELECTED:
var cycleSelected = erd.data[0]
for (var state in states) {
if (erd.source === states[state].id) {
for (let i in states[state].knobTurned) {
states[state].knobTurned[i] = true
}
say.speak(getReadableCycleName(cycleSelected), 'voice_us2_mbrola')
states[state].oldCycle = cycleSelected
}
}
break
case END_CYCLE:
var cycleEnd = erd.data[0]
for (var state in states) {
if (erd.source === states[state].id) {
}
}
break
}
})
busSubscribe(bus, SOURCE, TIME_SECS, [DRYER])
busSubscribe(bus, SOURCE, TIME_MINS, [WASHER])
busSubscribe(bus, SOURCE, CYCLE_SELECTED, [WASHER, DRYER])
busSubscribe(bus, SOURCE, WATER_TEMP, [WASHER, DRYER])
busSubscribe(bus, SOURCE, MACHINE_STATUS, [WASHER, DRYER])
busSubscribe(bus, SOURCE, SOIL_LEVEL, [WASHER, DRYER])
busSubscribe(bus, SOURCE, SPIN_LEVEL, [WASHER, DRYER])
})
function busSubscribe (bus, source, erd, destinations) {
destinations.map(function (dest) {
bus.subscribe({
erd: erd,
source: source,
destination: dest
})
})
}
var busRead = function (bus, source, erd, destinations) {
destinations.map(function (dest) {
bus.read({
erd: erd,
source: source,
destination: dest
})
})
}
var getSoilLevelByCode = (function () {
var codes = {
0: 'extra light',
1: 'light',
2: 'normal',
3: 'heavy',
4: 'extra heavy'
}
return function (code) {
return codes[code]
}
})()
var getSpinLevelByCode = (function () {
var codes = {
0: 'no spin',
1: 'unused',
2: 'medium',
3: 'high',
4: 'extra high',
5: 'disabled'
}
return function (code) {
return codes[code]
}
})()
var getTempByCode = (function () {
var codes = {
21: 'hot',
20: 'warm',
19: 'colors',
18: 'cool',
17: 'cold',
16: 'tap cold'
}
return function (code) {
return codes[code]
}
})()
var getCycleByCode = (function () {
var codes = {
0: 'blank',
1: 'basket_clean',
2: 'rinse_and_spin',
3: 'quick_rinse',
4: 'bulky_items',
5: 'sanitize',
6: 'towels_and_sheets',
7: 'steam_refresh',
8: 'normal',
9: 'whites',
10: 'darks',
11: 'jeans',
12: 'hand_wash',
13: 'delicates',
14: 'speed_wash',
15: 'heavy_duty',
16: 'allergen',
17: 'power_clean',
18: 'rinse_and_spin',
19: 'single_item',
20: 'colors',
21: 'cold_wash',
128: 'cottons',
129: 'easy_care',
130: 'active_wear',
131: 'time_dry',
132: 'dewrinkle',
133: 'air_fluff',
134: 'steam_refresh',
135: 'steam_dewrinkle',
136: 'speed_dry',
137: 'mixed',
138: 'speed_dry',
139: 'casuals',
140: 'warm_up',
141: 'energy_saver'
}
return function (code) {
console.log(code)
return codes[code]
}
})()
var getReadableCycleName = function (code) {
return getCycleByCode(code)
.replace(/[_-]/g, ' ')
}
var playCycle = (function () {
return function (code) {
player.play('./voices/en/' + getCycleByCode(code) + '.mp3')
}
})()