Skip to content

Commit c4d6fcc

Browse files
cluckjscottleibrand
authored andcommitted
Support for pi-buttons (#22)
* Converted to use node-pi-buttons module and made pi-buttons a separate service. * Bugfixes and improvements (#17) * Fix for low number of BGs and offset Fixes crash when actual number of BGs in monitor/glucose.json is fewer than the number we want to display (72 or 120). Thanks to @mhaeberli for catching & patching! Also fixes graph offset so we have a continuous line of BGs. * customizations for jon's production rig * various personalizations and possible improvements * revert screenoff code and add pump status to screen * status screen improvements and bugfixes * Update README.md * improvements, bugfixes, and exit on display malfunction * Clearer error messages * comments * fixing error handling when only writing to the screen once * bugfixes * stop calling status scripts from the commandline * use socket server to control display * improvements and bugfixes * status screen for cas * nice typo * Remove custom rig code * revert readme * fixes and improvements * Revert "upgrade jon-dev with node pi buttons" * Don't crash if buttons are pressed & screen broken This should help keep openaps-menu from crashing when the screen is broken. * bugfixes and add preferences toggle for status * more preferences, update big_bg_status.js * Update README.md * Update menu.json * Delete casstatus.js * Revert package.json Bryan merged upstream bugfixes... * Remove comma Random comma? * Add preferences switch info * Add 8am-8pm invert display option. * Add 8am-8pm invert display option. * Variable name bugfix * Logic fix for day/night inversion * Logic fix for day/night inversion
1 parent 42c6ac5 commit c4d6fcc

File tree

19 files changed

+484
-763
lines changed

19 files changed

+484
-763
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ This is the repository holding the menu-based software code, which you may choos
44

55
See [here](https://github.com/EnhancedRadioDevices/Explorer-HAT) for more details on the Explorer HAT hardware.
66

7+
You can set your preferred auto-updating status screen using the following setting in your `~/myopenaps/preferences.json`:
8+
9+
`"status_screen": "bigbgstatus"` will display the big BG status screen (no graph).
10+
11+
`"status_screen": "off"` will turn the auto-updating screen off.
12+
13+
14+
By default, the auto-updating status script will invert the display about 50% of the time, to prevent burn-in on the OLED screen. You can turn this off with the following setting in your `~/myopenaps/preferences.json`:
15+
16+
`"wearOLEDevenly": "off"`
17+
18+
Or you can have it invert the display from 8pm to 8am with:
19+
20+
`"wearOLEDevenly": "nightandday"`
21+
22+
723
## Example screen outputs (Note: these are examples. The latest code may yield different menu items and screen displays)
824

925
### Status screen:

config/buttons.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"pins": {
3-
"buttonUp": 11,
4-
"buttonDown": 13
2+
"gpios": {
3+
"buttonUp": 17,
4+
"buttonDown": 27
55
},
66
"options": {
7-
"pressed": 200,
8-
"clicked": 400
7+
"socketPath": "/var/run/pi-buttons.sock",
8+
"reconnectTimeout": 3000
99
}
1010
}

config/menus/menu.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
"menu": [
55
{
66
"label": "Status Graph",
7-
"command": "node scripts/status.js",
8-
"emit": "nothing"
7+
"emit": "showgraphstatus"
8+
},
9+
{
10+
"label": "Big BG Status",
11+
"emit": "showbigBGstatus"
912
},
1013
{
1114
"label": "Set Temp Target",
@@ -54,8 +57,7 @@
5457
},
5558
{
5659
"label": "Unicorn Logo",
57-
"command": "node scripts/unicorn.js",
58-
"emit": "nothing"
60+
"emit": "showlogo"
5961
}
6062
]
6163
},

index.js

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ const i2c = require('i2c-bus');
1010
const path = require('path');
1111
const pngparse = require('pngparse');
1212
const extend = require('extend');
13+
var fs = require('fs');
1314

1415
var i2cBus = i2c.openSync(1);
1516

17+
var openapsDir = "/root/myopenaps"; //if you're using a nonstandard OpenAPS directory, set that here. NOT RECOMMENDED.
18+
1619
// setup the display
1720
var displayConfig = require('./config/display.json');
1821
displayConfig.i2cBus = i2cBus;
19-
var display = require('./lib/display/ssd1306')(displayConfig);
2022

21-
// display the logo
22-
pngparse.parseFile('./static/unicorn.png', function(err, image) {
23-
if(err)
24-
throw err
25-
display.clear();
26-
display.oled.drawBitmap(image.data);
27-
});
23+
try {
24+
var display = require('./lib/display/ssd1306')(displayConfig);
25+
displayImage('./static/unicorn.png'); //display logo
26+
} catch (e) {
27+
console.warn("Could not setup display:", e);
28+
}
2829

2930
// setup battery voltage monitor
3031
var voltageConfig = require('./config/voltage.json')
@@ -44,8 +45,36 @@ socketServer
4445
.on('warning', (warn) => {
4546
console.log('socket-server warning: ', warn.reason)
4647
})
48+
.on('displaystatus', function () {
49+
if (display) {
50+
var preferences;
51+
fs.readFile(openapsDir+'/preferences.json', function (err, data) {
52+
if (err) throw err;
53+
preferences = JSON.parse(data);
54+
if (preferences.status_screen == "bigbgstatus") {
55+
bigBGStatus(display, openapsDir);
56+
} else if (preferences.status_screen == "off") {
57+
//don't auto-update the screen if it's turned off
58+
} else {
59+
graphStatus(display, openapsDir); //default to graph status
60+
}
61+
});
62+
}
63+
})
4764

65+
function displayImage(pathToImage) {
66+
pngparse.parseFile(pathToImage, function(err, image) {
67+
if(err)
68+
throw err
69+
display.clear();
70+
display.oled.drawBitmap(image.data);
71+
});
72+
}
4873

74+
// load up graphical status scripts
75+
const graphStatus = require('./scripts/status.js');
76+
const bigBGStatus = require('./scripts/big_bg_status.js');
77+
// if you want to add your own status display script, it will be easiest to replace one of the above!
4978

5079
// setup the menus
5180
var buttonsConfig = require('./config/buttons.json');
@@ -64,6 +93,15 @@ var hidMenu = require('./lib/hid-menu/hid-menu')(buttonsConfig, menuConfig);
6493
hidMenu
6594
.on('nothing', function () {
6695
})
96+
.on('showgraphstatus', function () {
97+
graphStatus(display, openapsDir);
98+
})
99+
.on('showbigBGstatus', function () {
100+
bigBGStatus(display, openapsDir);
101+
})
102+
.on('showlogo', function () {
103+
displayImage('./static/unicorn.png');
104+
})
67105
.on('showvoltage', function () {
68106
voltage()
69107
.then(function (v) {
@@ -85,16 +123,18 @@ hidMenu
85123

86124
// display the current menu on the display
87125
function showMenu(menu) {
88-
display.clear();
89-
var text = '';
126+
if (display) {
127+
display.clear();
128+
var text = '';
90129

91-
var p = menu.getParentSelect();
92-
text += p ? '[' + p.label + ']\n' : '';
93-
var c = menu.getCurrentSelect();
94-
menu.getActiveMenu().forEach(function (m) {
95-
text += (m.selected ? '>' : ' ') + m.label + '\n';
96-
});
130+
var p = menu.getParentSelect();
131+
text += p ? '[' + p.label + ']\n' : '';
132+
var c = menu.getCurrentSelect();
133+
menu.getActiveMenu().forEach(function (m) {
134+
text += (m.selected ? '>' : ' ') + m.label + '\n';
135+
});
97136

98-
// console.log(text);
99-
display.write(text);
137+
// console.log(text);
138+
display.write(text);
139+
}
100140
}

lib/hid-menu/hid-menu.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,70 +8,71 @@
88
const Menube = require('menube');
99

1010
function createHIDMenu(configButtons, configMenus) {
11-
if (!configButtons.pins || !configButtons.pins.buttonUp || !configButtons.pins.buttonDown) {
11+
if (!configButtons.gpios || !configButtons.gpios.buttonUp || !configButtons.gpios.buttonDown) {
1212
throw new Error('Incomplete pins definition in configuration.');
1313
}
14-
var pins = configButtons.pins;
14+
var gpios = configButtons.gpios;
1515
var buttonOptions = configButtons.options || {};
1616
var onChange = configMenus.onChange;
1717
var menu = Menube(configMenus.menuFile, configMenus.menuSettings);
1818
var displayDirty = false;
19-
// var buttons = require('rpi-gpio-buttons')([pins.buttonUp, pins.buttonDown], buttonOptions);
20-
var piButtons = require('../pi-buttons');
19+
var piButtons = require('node-pi-buttons')(configButtons.options);
2120

2221
menu.on('menu_changed', function () {
2322
displayDirty = false; // the parent will redraw the display
2423
});
2524

26-
// buttons
2725
piButtons
28-
.on('clicked', function (pin) {
26+
.on('clicked', function (gpio, data) {
2927
if (displayDirty) {
3028
// fake menu changed to force redraw
3129
menu.emit('menu_changed');
3230
displayDirty = false;
3331
}
3432
else {
35-
switch(pin) {
36-
case pins.buttonUp:
33+
switch(parseInt(gpio, 10)) {
34+
case gpios.buttonUp:
3735
if (!displayDirty) {
3836
menu.menuUp();
3937
}
4038
break;
4139

42-
case pins.buttonDown:
40+
case gpios.buttonDown:
4341
if (!displayDirty) {
4442
menu.menuDown();
4543
}
4644
break;
4745
}
4846
}
4947
})
50-
.on('double_clicked', function (pin) {
48+
.on('double_clicked', function (gpio, data) {
5149
if (displayDirty) {
5250
// fake menu changed to force redraw
5351
menu.emit('menu_changed');
5452
displayDirty = false;
5553
}
5654
else {
57-
switch (pin) {
58-
case pins.buttonUp:
55+
switch (parseInt(gpio, 10)) {
56+
case gpios.buttonUp:
5957
menu.menuBack();
6058
break;
6159

62-
case pins.buttonDown:
60+
case gpios.buttonDown:
6361
displayDirty = true; // activate may write something to the display
6462
menu.activateSelect();
6563
break;
6664
}
6765
}
6866
})
69-
.on('released', function (pin) {
67+
.on('released', function (gpio, data) {
7068
if (displayDirty) {
7169
// fake menu changed to force redraw
7270
menu.emit('menu_changed');
7371
displayDirty = false;
7472
}
73+
})
74+
.on('error', function (data) {
75+
console.log('ERROR: ', data.error);
7576
});
7677

7778
return menu;

lib/pi-buttons/a.out

-13.8 KB
Binary file not shown.

lib/pi-buttons/build.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)