-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
178 lines (159 loc) · 3.88 KB
/
code.js
File metadata and controls
178 lines (159 loc) · 3.88 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
const ssid = 'WiFi_OBDII';
const host = '192.168.0.10';
const port = '35000';
const wifi = require('Wifi');
const net = require('net');
const logos = require('logos');
const spi = new SPI();
var socket;
var rtc;
var g;
var data = '';
const modes = ['clock', 'date', 'battery'];
var selectedMode = 0;
require("FontDylex7x13").add(Graphics);
Graphics.prototype.drawStringDbl = (txt, px, py, h) => {
let gBuffer = Graphics.createArrayBuffer(128, h, 2, {
msb: true
});
gBuffer.setFontDylex7x13();
let w = gBuffer.stringWidth(txt);
let c = (w + 3) >> 2;
gBuffer.drawString(txt);
let img = {
width: w * 2,
height: 1,
transparent: 0,
buffer: new ArrayBuffer(c)
};
let a = new Uint8Array(img.buffer);
for (let y = 0; y < h; y++) {
a.set(new Uint8Array(gBuffer.buffer, 32 * y, c));
this.drawImage(img, px, py + y * 2);
this.drawImage(img, px, py + 1 + y * 2);
}
};
function initDisplay() {
I2C1.setup({
scl: 5,
sda: 4
});
g = require("SSD1306").connect(I2C1, '', {
height: 32
});
}
function initClock() {
I2C1.setup({
scl: 5,
sda: 4
});
rtc = require("DS3231").connect(I2C1, {
DST: false
});
}
function connectSocket() {
net.connect({
host: host,
port: port
}, (s, err) => {
if (err) {
console.log("Connection error: " + err);
return;
}
socket = s;
console.log('Connected to socket.');
});
}
function connectWiFi() {
console.log('Trying to connect to ' + ssid + '.');
wifi.connect(ssid, {}, () => {
console.log('WiFi connected.');
connectSocket();
});
}
function sendCMDSocket(cmd) {
socket.write(cmd + '\r\n');
return socket.read(0).split('>')[0].trim().replace(cmd, '');
}
function sendCMD(cmd) {
if (socket) {
if (!socket.conn) {
connectSocket();
console.log('Connection to the socket lost. Reconnecting...');
return null;
}
return sendCMDSocket(cmd);
} else {
console.log('Socket has not been initialized yet.');
return null;
}
}
function showClock() {
const time = rtc.readDateTime().split(' ')[1];
const hms = time.split(':');
const hmsStr = hms[0] + ' : ' + hms[1] + ' : ' + hms[2];
if (hms) {
g.clear();
g.drawStringDbl(hmsStr, 12, 3, 32);
g.flip();
}
}
function showDate() {
const date = rtc.readDateTime().split(' ')[0];
const dmy = date.split('/');
const mdy = dmy[1] + '/' + dmy[0] + '/20' + dmy[2];
if (date) {
g.clear();
g.drawStringDbl(mdy, 3, 3, 32);
g.flip();
}
}
function showBattery() {
const tmp = sendCMD('ATRV');
if (tmp) data = tmp;
g.clear();
g.drawImage(logos.battery, 0, 0);
if (data) g.drawStringDbl(data, 42, 3, 32);
g.flip();
}
E.on('init', () => {
pinMode(2, 'input_pullup');
pinMode(0, 'input_pullup');
setWatch(function(e) {
selectedMode = ((selectedMode + 1) === modes.length) ?
0 :
selectedMode + 1;
}, 2, {
repeat: true,
edge: 'rising',
debounce: 50
});
setWatch(function(e) {
const isLongPress = (e.time - e.lastTime) > 0.5;
if (isLongPress) {
console.log('H LONG press');
} else {
console.log('H SHORT press');
}
}, 0, {
repeat: true,
edge: 'rising',
debounce: 50
});
initDisplay();
initClock();
connectWiFi();
setInterval(() => {
switch (modes[selectedMode]) {
case 'clock':
showClock();
break;
case 'date':
showDate();
break;
case 'battery':
showBattery();
break;
}
}, 200);
});