From 04cda3681f6359594b2dccb4fc39219e9f1de67e Mon Sep 17 00:00:00 2001 From: marko Date: Wed, 9 Sep 2020 15:25:00 -0400 Subject: [PATCH] Add wheel circumference setting --- apps.json | 5 +++-- apps/cscsensor/settings.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 apps/cscsensor/settings.js diff --git a/apps.json b/apps.json index 610638d4c..42fda17db 100644 --- a/apps.json +++ b/apps.json @@ -2181,8 +2181,9 @@ "tags": "outdoors,exercise,ble,bluetooth", "readme": "README.md", "storage": [ - {"name":"cscsensor.app.js","url":"cscsensor.app.js"}, - {"name":"cscsensor.img","url":"cscsensor-icon.js","evaluate":true} + {"name":"cscsensor.app.js","url":"cscsensor.app.js"}, + {"name":"cscsensor.settings.js","url":"settings.js"} + {"name":"cscsensor.img","url":"cscsensor-icon.js","evaluate":true} ] }, { "id": "worldclock", diff --git a/apps/cscsensor/settings.js b/apps/cscsensor/settings.js new file mode 100644 index 000000000..95fe28d42 --- /dev/null +++ b/apps/cscsensor/settings.js @@ -0,0 +1,38 @@ +// This file should contain exactly one function, which shows the app's settings +/** + * @param {function} back Use back() to return to settings menu + */ +(function(back) { + const SETTINGS_FILE = 'cscsensor.json' + // initialize with default settings... + let s = { + 'wheelcirc': 2230, + } + // ...and overwrite them with any saved values + // This way saved values are preserved if a new version adds more settings + const storage = require('Storage') + const saved = storage.readJSON(SETTINGS_FILE, 1) || {} + for (const key in saved) { + s[key] = saved[key]; + } + // creates a function to safe a specific setting, e.g. save('color')(1) + function save(key) { + return function (value) { + s[key] = value; + storage.write(SETTINGS_FILE, s); + } + } + + const menu = { + '': { 'title': 'Cycling speed sensor' }, + '< Back': back, + 'Wheel diameter (mm)': { + value: s.wheelcirc, + min: 800, + max: 2400, + step: 5, + onchange: save('wheelcirc'), + } + } + E.showMenu(menu); +})