Skip to content

Instantly share code, notes, and snippets.

@phoddie
Last active February 9, 2022 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phoddie/c7b8bf4917e163a7bc3ff3ab96ca949c to your computer and use it in GitHub Desktop.
Save phoddie/c7b8bf4917e163a7bc3ff3ab96ca949c to your computer and use it in GitHub Desktop.
Battery monitor for M5Paper
/*
* Copyright (c) 2016-2022 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK.
*
* This work is licensed under the
* Creative Commons Attribution 4.0 International License.
* To view a copy of this license, visit
* <http://creativecommons.org/licenses/by/4.0>.
* or send a letter to Creative Commons, PO Box 1866,
* Mountain View, CA 94042, USA.
*
*/
import parseBMF from "commodetto/parseBMF";
import Poco from "commodetto/Poco";
import Resource from "Resource";
import Timer from "timer";
let render = new Poco(screen, { displayListLength: 2048 });
let black = render.makeColor(0, 0, 0);
let white = render.makeColor(255, 255, 255);
let gray = render.makeColor(128, 128, 128);
let battery = new device.peripheral.battery.Default;
let font = parseBMF(new Resource("OpenSans-Semibold-66.bf4"));
let lastPercent = undefined;
Timer.repeat(() => {
const percent = Math.round(battery.read() * 100);
if (Math.abs(percent - lastPercent) < 2)
return;
lastPercent = percent;
const percentStr = percent + "%"
render.begin();
render.fillRectangle(white, 0, 0, render.width, render.height);
render.drawText(percentStr, font, black,
(render.width - render.getTextWidth(percentStr, font)) >> 1,
-100 + ((render.height - font.height) >> 1));
render.origin(320, 250);
render.fillRectangle(black, 0, 0, 300, 100);
render.fillRectangle(white, 10, 10, 280, 80);
render.fillRectangle(gray, 10, 10, 280 * (percent / 100), 80);
render.fillRectangle(black, 300, 30, 10, 40);
render.origin();
render.end();
}, 200, 0);
new device.peripheral.button.A({
onPush() {
if (this.pressed) {
let t = black;
black = white;
white = t;
lastPercent = undefined;
}
}
})
{
"include": [
"$(MODDABLE)/examples/manifest_base.json",
"$(MODDABLE)/examples/manifest_commodetto.json"
],
"modules": {
"*": "./main"
},
"resources": {
"*-mask": [
{
"source": "$(MODDABLE)/tools/xsbug/fonts/OpenSans-Semibold",
"size": 66
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment