-
Notifications
You must be signed in to change notification settings - Fork 0
/
oled-node.js
90 lines (71 loc) · 5.31 KB
/
oled-node.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
/**
* Node script for showing one image from a json file containing:
* [{'content': '01001100','columns': 4}]
* on an Onion Omega2 Plus oled
*
* https://github.com/lisa-fehr/onion-microcontroller
**/
var url = "https://warfehr.com/oled-msg";
var oledExp = require("/usr/bin/node-oled-exp");
var interval = 10 * 1000;
var screen_max = 7; // max pages/rows on the oled
//Initialize
oledExp.init();
// Set mode for pages
oledExp.setMemoryMode(2);
// Set columns to show images vs text
oledExp.setImageColumns();
console.log("Updating message every " + interval + " milliseconds.");
var showPicture = () => {
return new Promise ( function(resolve, reject) {
setInterval( () => {
console.log("Loading message");
oledExp.clear();
var request = require("request");
request(url, function (err, response, data) {
// catch errors
if (err) {
console.log(err);
return;
}
if (response.statusCode !== 200) {
console.log('Error. Status code: ' + response.statusCode);
return;
}
// parse the json contents
var jsonContent = JSON.parse(data);
var item = jsonContent[0];
var content = item.content;
var digits = content.split("");
// split the 0s and 1s by the max columns
var max = item.columns;
var y = 0;
var x = 0;
digits.forEach(function(digit) {
if (digit == 1) {
oledExp.writeByte(0xff);
oledExp.writeByte(0xff);
oledExp.writeByte(0xff);
} else {
oledExp.writeByte(0x00);
oledExp.writeByte(0x00);
oledExp.writeByte(0x00);
}
x+=1;
// reset the colums, then advance the row count and cursor position
if (x == max) {
x = 0;
y+=1;
oledExp.setCursorByPixel(y,0);
}
// prevent errors if the image doesn't fit the screen
if (y == screen_max) {
return;
}
});
});
resolve();
}, interval);
});
};
showPicture();