diff --git a/src/ssd1306.ts b/src/ssd1306.ts new file mode 100644 index 0000000..b69f651 --- /dev/null +++ b/src/ssd1306.ts @@ -0,0 +1,30 @@ +import Device from "@chirimen/grove-oled-display"; +import { WritableDevice } from "./Device"; +import { I2C, i2c } from "./i2c"; + +export function ssd1306( + bus: I2C = i2c(), + address: number = 0x3c +): WritableDevice { + const device = new Device(bus, address); + + return { + /** @param value Object to be output */ + async write(value: boolean | number | string): Promise { + if (device.i2cSlave == null) { + await device.init(true); + } + await device.clearDisplay(); + const messages = + typeof value === "string" + ? value.split("\n") + : JSON.stringify(value, null, " ").split("\n"); + messages.forEach((message, index) => { + device.drawStringQ(index, 0, message); + }); + await device.playSequence(); + } + }; +} + +export default ssd1308;