-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
168 lines (146 loc) · 4.86 KB
/
index.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
// oaps-hid/index.js
/*jslint node: true */
/*jslint esversion: 6 */
'use strict';
const i2c = require('i2c-bus');
const path = require('path');
const pngparse = require('pngparse');
const extend = require('extend');
var fs = require('fs');
var i2cBus = i2c.openSync(1);
var openapsDir = "/root/myopenaps"; //if you're using a nonstandard OpenAPS directory, set that here. NOT RECOMMENDED.
try {
var preferences = JSON.parse(fs.readFileSync(openapsDir+"/preferences.json"));
} catch (e) {
console.error("Could not load preferences.json", e);
}
// setup the display, depending on its size (Eadiofruit is 128x32 and Explorer HAT is 128x64)
if (preferences.hardwaretype && preferences.hardwaretype == "radiofruit") {
var displayConfig = require('./config/display.json').radiofruit;
} else {
var displayConfig = require('./config/display.json').explorerHat;
}
displayConfig.i2cBus = i2cBus;
try {
var display = require('./lib/display/ssd1306')(displayConfig);
if (preferences.hardwaretype && preferences.hardwaretype == "radiofruit") {
displayImage('./static/unicorn_128x32.png');
} else {
displayImage('./static/unicorn_128x64.png');
}
} catch (e) {
console.warn("Could not setup display:", e);
}
function displayImage(pathToImage) {
pngparse.parseFile(pathToImage, function(err, image) {
if(err)
throw err
display.clear();
display.oled.drawBitmap(image.data);
});
}
// setup battery voltage monitor
var voltageConfig = require('./config/voltage.json')
voltageConfig.i2cBus = i2cBus
var voltage = require('./lib/voltage/voltage')(voltageConfig)
// setup socket server for external commands
var batteryConfig = require('./config/battery.json')
var socketServer = require('./lib/socket-server/socket-server')({
voltage: voltage,
battery: batteryConfig
})
socketServer
.on('error', (err) => {
console.log('socket-server error: ', err.reason)
})
.on('warning', (warn) => {
console.log('socket-server warning: ', warn.reason)
})
.on('displaystatus', function () {
if (display) {
if (preferences.status_screen && preferences.status_screen == "bigbgstatus") {
bigBGStatus(display, openapsDir);
} else if (preferences.status_screen && preferences.status_screen == "off") {
//don't auto-update the screen if it's turned off
} else if (preferences.status_screen && preferences.status_screen == "blank") {
display.clear(true);
} else if (preferences.hardwaretype && preferences.hardwaretype == "radiofruit") {
radiofruitStatus(display, openapsDir); //radiofruit text status script
} else {
graphStatus(display, openapsDir); //default to graph status
}
}
})
// load up graphical status scripts
const graphStatus = require('./scripts/status.js');
const bigBGStatus = require('./scripts/big_bg_status.js');
const radiofruitStatus = require('./scripts/status-radiofruit.js');
// if you want to add your own status display script, it will be easiest to replace one of the above!
// setup the menus
if (preferences.hardwaretype && preferences.hardwaretype == "radiofruit") {
var buttonsConfig = require('./config/buttons-radiofruit.json');
} else {
var buttonsConfig = require('./config/buttons-explorerhat.json');
}
var menuConfig = {
menuFile: process.cwd() + path.sep + './config/menus/menu.json', // file path for the menu definition
onChange: showMenu, // method to call when menu changes
menuSettings: {
displayLines: displayConfig.displayLines - 1, // one line is used for menu title
moreUpLabel: " ^ ^ ^",
moreDownLabel: " v v v"
}
};
var hidMenu = require('./lib/hid-menu/hid-menu')(buttonsConfig, menuConfig);
// configure menu events
hidMenu
.on('nothing', function () {
})
.on('showgraphstatus', function () {
graphStatus(display, openapsDir);
})
.on('showbigBGstatus', function () {
bigBGStatus(display, openapsDir);
})
.on('showRadiofruitStatus', function () {
radiofruitStatus(display, openapsDir);
})
.on('showlogo', function () {
if (preferences.hardwaretype && preferences.hardwaretype == "radiofruit") {
displayImage('./static/unicorn_128x32.png');
} else {
displayImage('./static/unicorn_128x64.png');
}
})
.on('showvoltage', function () {
voltage()
.then(function (v) {
display.clear();
display.write('Voltage: ' + v);
})
.catch(function (e) {
console.log(e.toString());
});
})
.on('menu_changed', function () {
showMenu(hidMenu);
})
.on('showoutput', function (err, stdout, stderr) {
display.clear();
display.write(stdout);
});
// display the current menu on the display
function showMenu(menu) {
if (display) {
display.clear();
var text = '';
var p = menu.getParentSelect();
text += p ? '[' + p.label + ']\n' : '';
var c = menu.getCurrentSelect();
menu.getActiveMenu().forEach(function (m) {
text += (m.selected ? '>' : ' ') + m.label + '\n';
});
// console.log(text);
display.write(text);
}
}