Skip to content

Commit

Permalink
Add RTC bm8563 and sample code for M5 Stick C gen 2
Browse files Browse the repository at this point in the history
  • Loading branch information
wilberforce authored and mkellner committed Oct 30, 2020
1 parent 1a51a95 commit 4dca41c
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 1 deletion.
69 changes: 69 additions & 0 deletions examples/drivers/m5stickc-rtc/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2016-2020 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
* <https://creativecommons.org/licenses/by/4.0>.
* or send a letter to Creative Commons, PO Box 1866,
* Mountain View, CA 94042, USA.
*
*/

import BM8563 from "bm8563";

let rtc = new BM8563;
let enabled=1;

// Main button: enable/disable RTC
button.a.onChanged = function () {
if (button.a.read()) {
return;
}
enabled=!enabled;
rtc.enabled=enabled;
global.power.brightness = 20+enabled*70;
}

// Side button: set time from sntp
button.b.onChanged = function () {
if (button.b.read()) {
return;
}
let d = new Date;
trace('Set time: ', d, '\n');
trace(d.getTime() / 1000, '\n');
rtc.seconds = d.getTime() / 1000;
}

import Timer from "timer";
import parseBMF from "commodetto/parseBMF";
import Poco from "commodetto/Poco";
import Resource from "Resource";

const render = new Poco(screen, {rotation: 90});

let white = render.makeColor(255, 255, 255);
let blue = render.makeColor(0, 0, 255);

let font = parseBMF(new Resource("OpenSans-Semibold-16.bf4"));
let text = "Press button A to set Time";
let textWidth = render.getTextWidth(text, font);
let x = 1;
let y = 1;

render.begin();
render.fillRectangle(blue, 0, 0, render.width, render.height);
render.end();

Timer.repeat(id => {
let now = rtc.seconds;
let e = new Date(now * 1000);
text = e.toString();
render.begin(0, y, render.width, font.height);
render.fillRectangle(blue, 0, 0, render.width, render.height);
render.drawText(e.toString().slice(4,24), font, white, x, y);
render.end();
}, 1000);
29 changes: 29 additions & 0 deletions examples/drivers/m5stickc-rtc/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"config": {
"rotation": 90
},
"include": [
"$(MODDABLE)/examples/manifest_base.json",
"$(MODDABLE)/examples/manifest_net.json",
"$(MODULES)/pins/i2c/manifest.json",
"$(MODDABLE)/examples/manifest_commodetto.json"
],
"modules": {
"pins/smbus": "$(MODULES)/pins/smbus/smbus",
"*": [
"$(MODULES)/drivers/rtc/rtc",
"$(MODULES)/drivers/rtc/bm8563",
"$(MODULES)/network/sntp/*",
"./main"
]
},
"resources": {
"*-mask": "$(MODDABLE)/examples/assets/fonts/OpenSans-Semibold-16"
},
"preload": [
"sntp",
"bm8563",
"rtc",
"smbus"
]
}
6 changes: 6 additions & 0 deletions examples/drivers/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ Reads the [Adafruit LIS3DH Triple-Axis Accelerometer](https://www.adafruit.com/p

***

### [`m5stickc-rtc`](./m5stickc-rtc)

[Commodetto](../../documentation/commodetto/commodetto.md) app for the [M5Stick-C](https://www.adafruit.com/product/4290). Demonstrates using the RTC BM8563 on the version 2 of the hardware.

***

### [`mcp23008`](./mcp23008)

Leverages the [MCP23008 GPIO expander](https://www.adafruit.com/product/593) to control multiple LEDs from a single I<sup>2</sup>C device.
Expand Down
91 changes: 91 additions & 0 deletions modules/drivers/rtc/bm8563.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2016-2020 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
*
*/

/*
bm8563 realtime clock
(c) Wilbeforce 2020
*/

import RTC from "rtc";

const BM8563_ADDR = 0x51;
const BM8563_CONTROL_STATUS1 = 0x00;
const BM8563_CONTROL_STATUS2 = 0x01;
const BM8563_SECONDS = 0x02;
const BM8563_CENTURY_BIT = 0b10000000;
const BM8563_STOP = 0b00100000;

class BM8563 extends RTC {
constructor(dictionary) {
super(Object.assign({
address: BM8563_ADDR
}, dictionary));
// Status reset (twice)
super.writeByte(BM8563_CONTROL_STATUS1, 0x00);
super.writeByte(BM8563_CONTROL_STATUS1, 0x00);
// Status2 reset
super.writeByte(BM8563_CONTROL_STATUS2, 0x00);

}

get _enabled() {
return (super.readByte(BM8563_CONTROL_STATUS1) & BM8563_STOP) != 0;
}

set _enabled(e) {
let c = e ? 0 : BM8563_STOP;
super.writeByte(BM8563_CONTROL_STATUS1, c);
}

_getDate() {
let date = {};
this.block = super.readBlock(BM8563_SECONDS, 7);
let century = (this.block[5] & BM8563_CENTURY_BIT) ? 100 : 0;
date.year = this.bcdToDec(this.block[6]) + century + 1900; // year
date.month = this.bcdToDec(this.block[5] & 0b00011111); // month
date.date = this.bcdToDec(this.block[3]); // day
date.seconds = this.bcdToDec(this.block[0] & 0x7f); // seconds
date.minutes = this.bcdToDec(this.block[1]); // minutes
date.hours = this.bcdToDec(this.block[2] & 0x3f); // hours
return date;
}

_setDate(date) {
let buf = new ArrayBuffer(7 * 1);
let bytes = new Uint8Array(buf);

bytes[0] = this.decToBcd(date.seconds);
bytes[1] = this.decToBcd(date.minutes);
bytes[2] = this.decToBcd(date.hours);
bytes[3] = this.decToBcd(date.date);
bytes[4] = this.decToBcd(date.dow);
bytes[5] = this.decToBcd(date.month);
if (date.year >= 100) {
bytes[5] |= BM8563_CENTURY_BIT;
}
bytes[6] = this.decToBcd(date.year % 100);

super.writeBlock(BM8563_SECONDS, buf);
}

}
Object.freeze(BM8563.prototype);

export default BM8563;
17 changes: 16 additions & 1 deletion modules/drivers/rtc/manifest.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
{
"config": {
"wifi2": {
"ssid": "rhys",
"password": "jeered3242"
},
"wifi": {
"ssid": "Guess",
"password": "Guest-963"
},
}
"include": [
"$(MODDABLE)/examples/manifest_base.json",
"$(MODULES)/pins/i2c/manifest.json"
],
"modules": {
"pins/smbus": "$(MODULES)/pins/smbus/smbus",
"*": [
"$(MODULES)/network/sntp/*",
"$(MODULES)/drivers/rtc/rtc",
"$(MODULES)/drivers/rtc/ds1307",
"$(MODULES)/drivers/rtc/ds3231",
"$(MODULES)/drivers/rtc/rv3028"
"$(MODULES)/drivers/rtc/rv3028",
"$(MODULES)/drivers/rtc/bm8563"
]
},
"preload": [
"sntp",
"ds1307",
"ds3231",
"rv3028",
"bm8563",
"rtc",
"smbus"
]
Expand Down

0 comments on commit 4dca41c

Please sign in to comment.