-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.nut
570 lines (494 loc) · 13.5 KB
/
device.nut
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
local blankString = " "; // String to blank a line.
local buttonSelect = 0; // Selection of destination
local numberStops = 3; // Length of Selector array in agent
// Timer handle
local clockTimer;
class LowLevelLcd {
i2cPort = null;
lcdAddress = null;
// commands
static LCD_CLEARDISPLAY = 0x01;
static LCD_RETURNHOME = 0x02;
static LCD_ENTRYMODESET = 0x04;
static LCD_DISPLAYCONTROL = 0x08;
static LCD_CURSORSHIFT = 0x10;
static LCD_FUNCTIONSET = 0x20;
static LCD_SETCGRAMADDR = 0x40;
static LCD_SETDDRAMADDR = 0x80;
// flags for display entry mode
static LCD_ENTRYRIGHT = 0x00;
static LCD_ENTRYLEFT = 0x02;
static LCD_ENTRYSHIFTINCREMENT = 0x01;
static LCD_ENTRYSHIFTDECREMENT = 0x00;
// flags for display on/off control
static LCD_DISPLAYON = 0x04;
static LCD_DISPLAYOFF = 0x00;
static LCD_CURSORON = 0x02;
static LCD_CURSOROFF = 0x00;
static LCD_BLINKON = 0x01;
static LCD_BLINKOFF = 0x00;
// flags for display/cursor shift
static LCD_DISPLAYMOVE = 0x08;
static LCD_CURSORMOVE = 0x00;
static LCD_MOVERIGHT = 0x04;
static LCD_MOVELEFT = 0x00;
// flags for function set
static LCD_8BITMODE = 0x10;
static LCD_4BITMODE = 0x00;
static LCD_2LINE = 0x08;
static LCD_1LINE = 0x00;
static LCD_5x10DOTS = 0x04;
static LCD_5x8DOTS = 0x00;
static PIN_RS = 0x1; // off=command, on=data
static PIN_RW = 0x2; // off=write, on=read
static PIN_EN = 0x4; // clock
static PIN_LED = 0x8;
/*
* Construct a new ImpLcd instance to talk with a generic I2C controlled LCD
*
* @port - I2C object. One of the hardware.i2c* objects
* @address - integer, base address of the I2C LCD
*/
constructor(port, address)
{
this.i2cPort = port;
this.lcdAddress = address;
this.i2cPort.configure(CLOCK_SPEED_10_KHZ);
// set LCD to 8 bits mode 3 times
for (local i=0; i<3; i++)
{
this.sendPulse(0x03 << 4, 0);
}
// set LCD to 4 bits mode
this.sendPulse(0x02 << 4, 0);
}
/*
* Read `length` bytes from the I2C read address
*
* @length - integer, number of bytes to read
*/
function rawRead(length)
{
return this.i2cPort.read((this.lcdAddress << 1) | 0x1, length);
}
/*
* I2C write wrapper. Write `data` to the I2C write address
*
* @data - string, data to send
*/
function rawWrite(data)
{
return this.i2cPort.write((this.lcdAddress << 1) | 0x0, data);
}
/*
* Wrapper around rawWrite() that accepts a byte instead of a string. It also keeps the LED on all time
*
* @byte - byte, byte to send
*/
function rawWriteByte(byte)
{
return this.rawWrite(format("%c", byte | PIN_LED));
}
/*
* Write a string to the display on the current position
*
* @s - string, string to print
*/
function writeString(s)
{
for (local i=0; i<s.len(); i++)
{
this.send(s[i], PIN_RS);
}
}
/*
* Send a command to the LCD
*
* @value - byte, command to send
*/
function sendCommand(value)
{
this.send(value, 0);
}
/*
* Pulse a value to the LCD
*/
function sendPulse(value, mode)
{
mode = mode | PIN_LED;
this.rawWriteByte(value | mode);
this.rawWriteByte(value | mode | PIN_EN);
imp.sleep(0.0006);
this.rawWriteByte(value | mode);
}
/*
* Push an 8bit value to the LCD with two 4bit pulses
*/
function send(value, mode)
{
local highNib = value & 0xf0;
local lowNib = (value << 4) & 0xf0;
this.sendPulse(highNib, mode);
this.sendPulse(lowNib, mode);
}
}
class ImpLcd
{
_lcd = null;
_functionSet = LowLevelLcd.LCD_4BITMODE | LowLevelLcd.LCD_1LINE | LowLevelLcd.LCD_5x8DOTS;
_displayControl = LowLevelLcd.LCD_DISPLAYON | LowLevelLcd.LCD_CURSOROFF | LowLevelLcd.LCD_BLINKOFF;
/*
* Construct a new ImpLcd instance to talk with a generic I2C controlled LCD
*
* @port - I2C object. One of the hardware.i2c* objects
* @address - integer, base address of the I2C LCD
* @rows - integer, number of rows on the LCD
* @dotSize - integer, when this value is greater than zero, the LCD is configured to display 5x10 dots characters. Otherwise, 5x8 dots characters.
*/
constructor(port, address, rows, dotSize)
{
this._lcd = LowLevelLcd(port, address);
if (rows > 1)
{
this._functionSet = this._functionSet | LowLevelLcd.LCD_2LINE;
}
if (dotSize > 0)
{
this._functionSet = this._functionSet | LowLevelLcd.LCD_5x10DOTS;
}
this.functionSet();
this.displayControl();
this.clear();
}
/*
* Send the current value of _functionSet to the LCD
*/
function functionSet()
{
return this._lcd.sendCommand(LowLevelLcd.LCD_FUNCTIONSET | this._functionSet);
}
/*
* Send the current value of _displayControl to the LCD
*/
function displayControl()
{
return this._lcd.sendCommand(LowLevelLcd.LCD_DISPLAYCONTROL | this._displayControl);
}
/*
* Write a string to the display on the current position
*
* @s - string, string to print
*/
function writeString(s)
{
return this._lcd.writeString(s);
}
/*
* Set the LCD cursor to given position
*
* @col - integer, column number, zero based
* @row - integer, row number, zero based
*/
function setCursor(col, row)
{
local rowOffsets = [ 0x00, 0x40, 0x14, 0x54 ];
return this._lcd.sendCommand(LowLevelLcd.LCD_SETDDRAMADDR | (col + rowOffsets[row]));
}
/*
* Clear the LCD
*/
function clear()
{
return this._lcd.sendCommand(LowLevelLcd.LCD_CLEARDISPLAY);
}
/*
* Turn display off (doesn't turn off the LED)
*/
function noDisplay()
{
this._displayControl = this.displayControl & ~LowLevelLcd.LCD_DISPLAYON;
return this.displayControl();
}
/*
* Turn display on
*/
function display()
{
this._displayControl = this.displayControl | LowLevelLcd.LCD_DISPLAYON;
return this.displayControl();
}
/*
* Turn cursor blinking off
*/
function noBlink()
{
this._displayControl = this.displayControl & ~LowLevelLcd.LCD_BLINKON;
return this.displayControl();
}
/*
* Turn cursor blinking on
*/
function blink()
{
this._displayControl = this.displayControl | LowLevelLcd.LCD_BLINKON;
return this.displayControl();
}
/*
* Turn off the cursor
*/
function noCursor()
{
this._displayControl = this.displayControl & ~LowLevelLcd.LCD_CURSORON;
return this.displayControl();
}
/*
* Turn the cursor on
*/
function cursor()
{
this._displayControl = this.displayControl | LowLevelLcd.LCD_CURSORON;
return this.displayControl();
}
}
// create an ImpLcd instance. Our LCD is connected to I2C on ports 8,9 with a base address of 0x27
lcd <- ImpLcd(hardware.i2c89, 0x27, 2, 0);
function newDataHandeler(busInfo)
{
// update the display screen with the new data
if (busInfo.string.len() == 0 ) {
lcd.setCursor(0, busInfo.line);
lcd.writeString(blankString);
}
else {
server.log(busInfo.string)
lcd.setCursor(0, busInfo.line);
lcd.writeString(busInfo.string);
}
}
function indicatorHandeler(indicator)
{
numberStops = indicator.size;
lcd.setCursor(12, 0);
lcd.writeString(" ");
if (indicator.text.len() > 0) {
lcd.setCursor(12, 0);
lcd.writeString("Stop"+indicator.text);
}
}
function weatherHandeler(forecastString)
{
// update the display screen with the new data
server.log(forecastString);
lcd.setCursor(5, 0);
lcd.writeString(" ");
lcd.setCursor(5, 0);
lcd.writeString(forecastString);
}
// Number of days from start of year to UTC/BST change day
function last_sunday_in_march(t)
{
local days, sunday;
days = 89; // Days from 1st Jan to end of march, less 1 'cos we count from 0
if (t.year%4 ==0) // Add a day if this is a leap year
++days;
sunday = t.year;
sunday *= 5; // Find a position in the week for sunday
sunday /= 4;
sunday += 4;
days -= sunday%7;
return (days);
}
function last_sunday_in_october(t)
{
local days, sunday;
days = 303; // Days from 1st Jan to end of october, less 1 'cos we count from 0
if (t.year%4 ==0) // Add a day if this is a leap year
++days;
sunday = t.year;
sunday *= 5;
sunday /= 4;
sunday += 1;
days -= sunday%7;
return (days);
}
// Returns 1 if the clock should show summer time.
function bst_adjust (t)
{
local march, october;
march = last_sunday_in_march(t);
october = last_sunday_in_october(t);
if ((t.yday > march)
&& (t.yday < october))
return 1;
if ((t.yday == march)
&& (t.min >= 60))
return 1;
if ((t.yday == october)
&& (t.min < 60))
return 1;
return 0;
}
function updateClock()
{
// update the clock again in 1 second
// imp.cancelwakeup(clockTimer); // cancel any previously set timer
// clockTimer = imp.wakeup(1, updateClock);
imp.wakeup(1, updateClock);
local t = date();
local hour = t.hour+bst_adjust(t);
if (hour>23) hour = 0;
// move to the first row, first column
lcd.setCursor(0, 0);
// lcd.writeString(format("%02d:%02d:%02d", hour, t.min, t.sec));
if (t.sec & 1) {
lcd.writeString(format("%02d %02d", hour, t.min));
} else {
lcd.writeString(format("%02d:%02d", hour, t.min));
}
// notify the Agent that we are still alive
agent.send("alive",0);
}
if (hardware.wakereason() == WAKEREASON_POWER_ON || hardware.wakereason() == WAKEREASON_NEW_SQUIRREL)
{
server.log("imp booting")
lcd.clear();
agent.send("reset",0);
}
agent.on("weather",weatherHandeler);
agent.on("busData",newDataHandeler);
agent.on("BusStopInd",indicatorHandeler);
// Debounce controls
local ctlUp = {
count = 0,
state = 0,
seen = 0
}
local ctlDown = {
count = 0,
state = 0,
seen = 0
}
function buttonUpcheck() {
if (hardware.pin1.read()) {
// button not pressed
if (--ctlUp.count <= 0) {
ctlUp.state = 0;
ctlUp.count = 0;
// server.log("buttonUp released");
} else {
imp.wakeup(0.010, buttonUpcheck);
}
} else {
// button is pressed
if (++ctlUp.count >= 8) {
if (ctlUp.state != 1) {
ctlUp.state = 1;
ctlUp.count = 8;
server.log("buttonUp pressed");
// Move up the list of stops
++buttonSelect;
// Wrap at the low and high ends
if (buttonSelect < 0) { buttonSelect = numberStops-1; }
if (buttonSelect >= numberStops) { buttonSelect = 0; }
server.log("buttonSelect: " + buttonSelect);
agent.send("newbus",buttonSelect);
}
} else {
imp.wakeup(0.010, buttonUpcheck);
}
}
}
function buttonDowncheck() {
if (hardware.pin2.read()) {
// button not pressed
if (--ctlDown.count <= 0) {
ctlDown.state = 0;
ctlDown.count = 0;
// server.log("buttonDown released");
} else {
imp.wakeup(0.010, buttonDowncheck);
}
} else {
// button is pressed
if (++ctlDown.count >= 8) {
if (ctlDown.state != 1) {
ctlDown.state = 1;
ctlDown.count = 8;
server.log("buttonDown pressed");
// Move down the list of stops
--buttonSelect;
// Wrap at the low and high ends
if (buttonSelect < 0) { buttonSelect = numberStops-1; }
if (buttonSelect >= numberStops) { buttonSelect = 0; }
server.log("buttonSelect: " + buttonSelect);
agent.send("newbus",buttonSelect);
}
} else {
imp.wakeup(0.010, buttonDowncheck);
}
}
}
hardware.pin1.configure(DIGITAL_IN_PULLUP,buttonUpcheck);
hardware.pin2.configure(DIGITAL_IN_PULLUP,buttonDowncheck);
//Create a zero-volt source at pin 5
ground <- hardware.pin5;
ground.configure(DIGITAL_OUT);
ground.write(0);
// fire updateClock() for the first time
updateClock();
/*
// Push button with debounce
class Button {
state = null;
count = null;
sample = null;
direction = null;
constructor(conn,updown)
{
sample = conn;
// sample.configure(DIGITAL_IN_PULLUP);
direction = updown;
state = 0;
count = 0;
}
function check () {
if (this.sample.read()) {
// button not pressed
if (--this.count == 0) {
this.state = 0;
this.count = 0;
server.log("button released");
} else {
imp.wakeup(0.010, this.check);
}
} else {
// button is pressed
if (++this.count >= 4) {
this.state = 1;
this.count = 4;
server.log("button pressed");
// Move up or down the list of stops
++buttonSelect;
// Wrap at the low and high ends
if (buttonSelect < 0) { buttonSelect = numberStops; }
if (buttonSelect >= numberStops) { buttonSelect = 0; }
server.log("buttonSelect: " + buttonSelect);
agent.send("newbus",buttonSelect);
} else {
imp.wakeup(0.010, this.check);
}
}
} // end function
} // end class
// Instantiate two instances of the class
local button1 = Button(hardware.pin1, 1); // increase
local button2 = Button(hardware.pin2, -1); // decrease
button1.configure(DIGITAL_IN_PULLUP,button1.check);
button2.configure(DIGITAL_IN_PULLUP,button2.check);
//Create a zero-volt source at pin 5
ground <- hardware.pin5;
ground.configure(DIGITAL_OUT);
ground.write(0);
// fire updateClock() for the first time
updateClock();
*/