From 1ab0efb5fb6a877174bdb4d5a2dde8fc2f108f3a Mon Sep 17 00:00:00 2001 From: Daniel Abdoue Date: Tue, 11 Apr 2023 23:40:14 -0500 Subject: [PATCH] adding driver support for x9c pots and ttp223 switch --- .../peripherals/potentiometer/x9c/main.js | 30 +++ .../potentiometer/x9c/manifest.json | 9 + examples/drivers/sensors/ttp223/main.js | 28 +++ examples/drivers/sensors/ttp223/manifest.json | 9 + modules/drivers/peripherals/x9c/manifest.json | 8 + modules/drivers/peripherals/x9c/x9c.js | 172 ++++++++++++++++++ modules/drivers/sensors/ttp223/manifest.json | 9 + modules/drivers/sensors/ttp223/ttp223.js | 63 +++++++ 8 files changed, 328 insertions(+) create mode 100644 examples/drivers/peripherals/potentiometer/x9c/main.js create mode 100644 examples/drivers/peripherals/potentiometer/x9c/manifest.json create mode 100644 examples/drivers/sensors/ttp223/main.js create mode 100644 examples/drivers/sensors/ttp223/manifest.json create mode 100644 modules/drivers/peripherals/x9c/manifest.json create mode 100644 modules/drivers/peripherals/x9c/x9c.js create mode 100644 modules/drivers/sensors/ttp223/manifest.json create mode 100644 modules/drivers/sensors/ttp223/ttp223.js diff --git a/examples/drivers/peripherals/potentiometer/x9c/main.js b/examples/drivers/peripherals/potentiometer/x9c/main.js new file mode 100644 index 0000000000..3fe5c92c49 --- /dev/null +++ b/examples/drivers/peripherals/potentiometer/x9c/main.js @@ -0,0 +1,30 @@ +import DigitalPotentiometer from "embedded:peripheral/Potentiometer/X9C"; +import Timer from "timer" + +const pot = new DigitalPotentiometer({ + increment: { + io: device.io.Digital, + pin: 14 + }, + upDown: { + io: device.io.Digital, + pin: 12 + }, + csNVRAMWrite: { + io: device.io.Digital, + pin: 13 + } +}); + + +for (let i = 25; i <= 33; i++) +{ + pot.configure({ + wiper: i + }); + trace(`Wiper Value: ${pot.wiper}\n`); + Timer.delay(1000); +} + + +pot.close(); diff --git a/examples/drivers/peripherals/potentiometer/x9c/manifest.json b/examples/drivers/peripherals/potentiometer/x9c/manifest.json new file mode 100644 index 0000000000..f9242c2048 --- /dev/null +++ b/examples/drivers/peripherals/potentiometer/x9c/manifest.json @@ -0,0 +1,9 @@ +{ + "include": [ + "$(MODDABLE)/modules/io/manifest.json", + "$(MODDABLE)/modules/drivers/peripherals/x9c/manifest.json" + ], + "modules": { + "*": "./main" + } +} diff --git a/examples/drivers/sensors/ttp223/main.js b/examples/drivers/sensors/ttp223/main.js new file mode 100644 index 0000000000..4aee526e97 --- /dev/null +++ b/examples/drivers/sensors/ttp223/main.js @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 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 + * . + * or send a letter to Creative Commons, PO Box 1866, + * Mountain View, CA 94042, USA. + * + */ + +import Sensor from "embedded:sensor/Switch/TTP223"; + +const SWITCH_PIN = 13; +new Sensor({ + sensor: { + io: device.io.Digital, + pin: SWITCH_PIN + }, + onAlert() + { + const sample = this.sample(); + trace(`Button: ${sample.position}\n`); + } +}) diff --git a/examples/drivers/sensors/ttp223/manifest.json b/examples/drivers/sensors/ttp223/manifest.json new file mode 100644 index 0000000000..f721444c40 --- /dev/null +++ b/examples/drivers/sensors/ttp223/manifest.json @@ -0,0 +1,9 @@ +{ + "include": [ + "$(MODDABLE)/modules/io/manifest.json", + "$(MODDABLE)/modules/drivers/sensors/ttp223/manifest.json" + ], + "modules": { + "*": "./main" + } +} \ No newline at end of file diff --git a/modules/drivers/peripherals/x9c/manifest.json b/modules/drivers/peripherals/x9c/manifest.json new file mode 100644 index 0000000000..308cf9bcb3 --- /dev/null +++ b/modules/drivers/peripherals/x9c/manifest.json @@ -0,0 +1,8 @@ +{ + "modules": { + "embedded:peripheral/Potentiometer/X9C": "$(MODDABLE)/modules/drivers/peripherals/x9c/x9c" + }, + "preload": [ + "embedded:peripheral/Potentiometer/X9C" + ] +} \ No newline at end of file diff --git a/modules/drivers/peripherals/x9c/x9c.js b/modules/drivers/peripherals/x9c/x9c.js new file mode 100644 index 0000000000..c7c52d9167 --- /dev/null +++ b/modules/drivers/peripherals/x9c/x9c.js @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2023 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 . + * + */ + +/* + Renesas X9C Series - Digital Potentiometers - X9C102, X9C103, X9C104, X9C503 + https://www.renesas.com/us/en/products/analog-products/data-converters/digital-controlled-potentiometers-dcp/x9c104-digitally-controlled-potentiometer-xdcp + Datasheet: https://www.renesas.com/us/en/document/dst/x9c102-x9c103-x9c104-x9c503-datasheet + Reference Driver: https://github.com/lucyamy/LapX9C10X/blob/main/src/LapX9C10X.cpp +*/ + + +import Timer from "timer" + + +class X9C +{ + #incrementPin; + #upDownPin; + #csNVRAMWritePin; + #currentWiper; + #minWiperValue = 0; + #maxWiperValue = 99; + + constructor(options) + { + const { increment, upDown, csNVRAMWrite } = options; + try + { + this.#incrementPin = new increment.io({ + mode: increment.io.Output, + pin: increment.pin + }); + this.#upDownPin = new upDown.io({ + mode: upDown.io.Output, + pin: upDown.pin + }); + this.#csNVRAMWritePin = new csNVRAMWrite.io({ + mode: csNVRAMWrite.io.Output, + pin: csNVRAMWrite.pin + }); + } + catch (e) + { + this.close(); + throw e; + } + + this.#reset(1); + } + + #reset(value) + { + if (value > (this.#maxWiperValue / 2) | 0) + { + this.#currentWiper = this.#maxWiperValue; + this.#setWiper(this.#minWiperValue); + } + else + { + this.#currentWiper = this.#minWiperValue; + this.#setWiper(this.#maxWiperValue); + } + + this.#setWiper(value); + + } + + #setWiper(value) + { + let steps; + if (!this.#inRange(value)) + throw new RangeError(`wiper range is 0-99`); + + if (value > this.#currentWiper) + { + this.#upDownPin.write(1); + steps = value - this.#currentWiper; + } else if (value < this.#currentWiper) + { + this.#upDownPin.write(0); + steps = this.#currentWiper - value; + } + else + { + return; + } + this.#incrementPin.write(1); + this.#csNVRAMWritePin.write(0); + + for (let i = 0; i < steps; i++) + this.#pulseInc(); + this.#csNVRAMWritePin.write(0); + + this.#currentWiper = value; + } + + #pulseInc() + { + this.#incrementPin.write(1); + Timer.delay(1); + this.#incrementPin.write(0); + Timer.delay(1); + } + + #inRange(value) + { + if (value < this.#minWiperValue || value > this.#maxWiperValue) + return false + return true + } + + + configure(options) + { + // configuration has been implemented this way for the following reasons: + // * if a user wants to reset, it only makes sense for it to happen first + // as changing the wiper value before a reset would be overriden anyway + // * an offset should only be performed last because either a reset or directly + // setting the wiper value after an offset would override the offset + if (undefined !== options.reset) + this.#reset(options.reset); + + if (undefined !== options.wiper) + { + let value = options.wiper; + this.#setWiper(value); + } + + if (undefined !== options.offset) + { + let value = this.#currentWiper + options.offset; + if (!this.#inRange(value)) + throw new RangeError(`wiper range is 0-99`); + this.#setWiper(value); + } + } + + get wiper() + { + return this.#currentWiper; + } + + close() + { + this.#incrementPin?.close(); + this.#incrementPin = undefined; + this.#upDownPin?.close(); + this.#upDownPin = undefined; + this.#csNVRAMWritePin?.close(); + this.#csNVRAMWritePin = undefined; + this.#currentWiper = undefined; + } + +} +export default X9C; diff --git a/modules/drivers/sensors/ttp223/manifest.json b/modules/drivers/sensors/ttp223/manifest.json new file mode 100644 index 0000000000..45dc574e64 --- /dev/null +++ b/modules/drivers/sensors/ttp223/manifest.json @@ -0,0 +1,9 @@ +{ + "modules": { + "embedded:sensor/Switch/TTP223": "$(MODDABLE)/modules/drivers/sensors/ttp223/ttp223" + }, + "preload": [ + "embedded:sensor/Switch/TTP223" + ] +} + diff --git a/modules/drivers/sensors/ttp223/ttp223.js b/modules/drivers/sensors/ttp223/ttp223.js new file mode 100644 index 0000000000..70210b8c1e --- /dev/null +++ b/modules/drivers/sensors/ttp223/ttp223.js @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023 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 . + * + */ + +/* + Tontek TTP223 - Capactive Touch Switch Sensor + http://hiletgo.com/ProductDetail/1915450.html + Datasheet: https://datasheet.lcsc.com/szlcsc/TTP223-BA6_C80757.pdf + Reference Driver: https://github.com/ac005sheekar/Flex-Force-sensitivity-Gravity-Sensors-Interfacing-with-Arduino-Microcontrollers-and-Firmware/blob/master/TTP223B%20Digital%20Touch%20Sensor%20Capacitive%20Touch.ino +*/ + +class TTP223 +{ + #io; + #onAlert; + + constructor(options) { + const { sensor, onAlert } = options; + if (sensor && onAlert) + { + this.#onAlert = options.onAlert; + this.#io = new sensor.io({ + mode: sensor.io.InputPullUp, + ...sensor, + edge: sensor.io.Rising | sensor.io.Falling, + onReadable: () => this.#onAlert() + }); + } + + } + + configure(options) + { + } + + close() + { + this.#io?.close(); + this.#io = undefined; + } + sample() + { + return { position: this.#io.read() }; + } + +} +export default TTP223;