Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add X9C Potentiometers and TTP223 Switch Driver Support #1085

Open
wants to merge 1 commit into
base: public
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/drivers/peripherals/potentiometer/x9c/main.js
Original file line number Diff line number Diff line change
@@ -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();
9 changes: 9 additions & 0 deletions examples/drivers/peripherals/potentiometer/x9c/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"include": [
"$(MODDABLE)/modules/io/manifest.json",
"$(MODDABLE)/modules/drivers/peripherals/x9c/manifest.json"
],
"modules": {
"*": "./main"
}
}
28 changes: 28 additions & 0 deletions examples/drivers/sensors/ttp223/main.js
Original file line number Diff line number Diff line change
@@ -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
* <http://creativecommons.org/licenses/by/4.0>.
* 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`);
}
})
9 changes: 9 additions & 0 deletions examples/drivers/sensors/ttp223/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"include": [
"$(MODDABLE)/modules/io/manifest.json",
"$(MODDABLE)/modules/drivers/sensors/ttp223/manifest.json"
],
"modules": {
"*": "./main"
}
}
8 changes: 8 additions & 0 deletions modules/drivers/peripherals/x9c/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"modules": {
"embedded:peripheral/Potentiometer/X9C": "$(MODDABLE)/modules/drivers/peripherals/x9c/x9c"
},
"preload": [
"embedded:peripheral/Potentiometer/X9C"
]
}
172 changes: 172 additions & 0 deletions modules/drivers/peripherals/x9c/x9c.js
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*/

/*
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if one could pass in an initial value to avoid glitching to 1 if that's not the desired init value...

}

#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;
9 changes: 9 additions & 0 deletions modules/drivers/sensors/ttp223/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"modules": {
"embedded:sensor/Switch/TTP223": "$(MODDABLE)/modules/drivers/sensors/ttp223/ttp223"
},
"preload": [
"embedded:sensor/Switch/TTP223"
]
}

63 changes: 63 additions & 0 deletions modules/drivers/sensors/ttp223/ttp223.js
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*/

/*
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;