-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RTC bm8563 and sample code for M5 Stick C gen 2
- Loading branch information
1 parent
1a51a95
commit 4dca41c
Showing
5 changed files
with
211 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters