Skip to content

Commit

Permalink
Support for pi-buttons (#22)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
cluckj authored and scottleibrand committed Feb 13, 2019
1 parent 42c6ac5 commit c4d6fcc
Show file tree
Hide file tree
Showing 19 changed files with 484 additions and 763 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ This is the repository holding the menu-based software code, which you may choos

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

You can set your preferred auto-updating status screen using the following setting in your `~/myopenaps/preferences.json`:

`"status_screen": "bigbgstatus"` will display the big BG status screen (no graph).

`"status_screen": "off"` will turn the auto-updating screen off.


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`:

`"wearOLEDevenly": "off"`

Or you can have it invert the display from 8pm to 8am with:

`"wearOLEDevenly": "nightandday"`


## Example screen outputs (Note: these are examples. The latest code may yield different menu items and screen displays)

### Status screen:
Expand Down
10 changes: 5 additions & 5 deletions config/buttons.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"pins": {
"buttonUp": 11,
"buttonDown": 13
"gpios": {
"buttonUp": 17,
"buttonDown": 27
},
"options": {
"pressed": 200,
"clicked": 400
"socketPath": "/var/run/pi-buttons.sock",
"reconnectTimeout": 3000
}
}
10 changes: 6 additions & 4 deletions config/menus/menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
"menu": [
{
"label": "Status Graph",
"command": "node scripts/status.js",
"emit": "nothing"
"emit": "showgraphstatus"
},
{
"label": "Big BG Status",
"emit": "showbigBGstatus"
},
{
"label": "Set Temp Target",
Expand Down Expand Up @@ -54,8 +57,7 @@
},
{
"label": "Unicorn Logo",
"command": "node scripts/unicorn.js",
"emit": "nothing"
"emit": "showlogo"
}
]
},
Expand Down
76 changes: 58 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ 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.

// setup the display
var displayConfig = require('./config/display.json');
displayConfig.i2cBus = i2cBus;
var display = require('./lib/display/ssd1306')(displayConfig);

// display the logo
pngparse.parseFile('./static/unicorn.png', function(err, image) {
if(err)
throw err
display.clear();
display.oled.drawBitmap(image.data);
});
try {
var display = require('./lib/display/ssd1306')(displayConfig);
displayImage('./static/unicorn.png'); //display logo
} catch (e) {
console.warn("Could not setup display:", e);
}

// setup battery voltage monitor
var voltageConfig = require('./config/voltage.json')
Expand All @@ -44,8 +45,36 @@ socketServer
.on('warning', (warn) => {
console.log('socket-server warning: ', warn.reason)
})
.on('displaystatus', function () {
if (display) {
var preferences;
fs.readFile(openapsDir+'/preferences.json', function (err, data) {
if (err) throw err;
preferences = JSON.parse(data);
if (preferences.status_screen == "bigbgstatus") {
bigBGStatus(display, openapsDir);
} else if (preferences.status_screen == "off") {
//don't auto-update the screen if it's turned off
} else {
graphStatus(display, openapsDir); //default to graph status
}
});
}
})

function displayImage(pathToImage) {
pngparse.parseFile(pathToImage, function(err, image) {
if(err)
throw err
display.clear();
display.oled.drawBitmap(image.data);
});
}

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

// setup the menus
var buttonsConfig = require('./config/buttons.json');
Expand All @@ -64,6 +93,15 @@ var hidMenu = require('./lib/hid-menu/hid-menu')(buttonsConfig, menuConfig);
hidMenu
.on('nothing', function () {
})
.on('showgraphstatus', function () {
graphStatus(display, openapsDir);
})
.on('showbigBGstatus', function () {
bigBGStatus(display, openapsDir);
})
.on('showlogo', function () {
displayImage('./static/unicorn.png');
})
.on('showvoltage', function () {
voltage()
.then(function (v) {
Expand All @@ -85,16 +123,18 @@ hidMenu

// display the current menu on the display
function showMenu(menu) {
display.clear();
var text = '';
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';
});
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);
// console.log(text);
display.write(text);
}
}
29 changes: 15 additions & 14 deletions lib/hid-menu/hid-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,71 @@
const Menube = require('menube');

function createHIDMenu(configButtons, configMenus) {
if (!configButtons.pins || !configButtons.pins.buttonUp || !configButtons.pins.buttonDown) {
if (!configButtons.gpios || !configButtons.gpios.buttonUp || !configButtons.gpios.buttonDown) {
throw new Error('Incomplete pins definition in configuration.');
}
var pins = configButtons.pins;
var gpios = configButtons.gpios;
var buttonOptions = configButtons.options || {};
var onChange = configMenus.onChange;
var menu = Menube(configMenus.menuFile, configMenus.menuSettings);
var displayDirty = false;
// var buttons = require('rpi-gpio-buttons')([pins.buttonUp, pins.buttonDown], buttonOptions);
var piButtons = require('../pi-buttons');
var piButtons = require('node-pi-buttons')(configButtons.options);

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

// buttons
piButtons
.on('clicked', function (pin) {
.on('clicked', function (gpio, data) {
if (displayDirty) {
// fake menu changed to force redraw
menu.emit('menu_changed');
displayDirty = false;
}
else {
switch(pin) {
case pins.buttonUp:
switch(parseInt(gpio, 10)) {
case gpios.buttonUp:
if (!displayDirty) {
menu.menuUp();
}
break;

case pins.buttonDown:
case gpios.buttonDown:
if (!displayDirty) {
menu.menuDown();
}
break;
}
}
})
.on('double_clicked', function (pin) {
.on('double_clicked', function (gpio, data) {
if (displayDirty) {
// fake menu changed to force redraw
menu.emit('menu_changed');
displayDirty = false;
}
else {
switch (pin) {
case pins.buttonUp:
switch (parseInt(gpio, 10)) {
case gpios.buttonUp:
menu.menuBack();
break;

case pins.buttonDown:
case gpios.buttonDown:
displayDirty = true; // activate may write something to the display
menu.activateSelect();
break;
}
}
})
.on('released', function (pin) {
.on('released', function (gpio, data) {
if (displayDirty) {
// fake menu changed to force redraw
menu.emit('menu_changed');
displayDirty = false;
}
})
.on('error', function (data) {
console.log('ERROR: ', data.error);
});

return menu;
Expand Down
Binary file removed lib/pi-buttons/a.out
Binary file not shown.
5 changes: 0 additions & 5 deletions lib/pi-buttons/build.sh

This file was deleted.

Loading

0 comments on commit c4d6fcc

Please sign in to comment.