-
Notifications
You must be signed in to change notification settings - Fork 239
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
dabdoue
wants to merge
1
commit into
Moddable-OpenSource:public
Choose a base branch
from
dabdoue:ttp223-and-x9c-drivers
base: public
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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(); |
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,9 @@ | ||
{ | ||
"include": [ | ||
"$(MODDABLE)/modules/io/manifest.json", | ||
"$(MODDABLE)/modules/drivers/peripherals/x9c/manifest.json" | ||
], | ||
"modules": { | ||
"*": "./main" | ||
} | ||
} |
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,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`); | ||
} | ||
}) |
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,9 @@ | ||
{ | ||
"include": [ | ||
"$(MODDABLE)/modules/io/manifest.json", | ||
"$(MODDABLE)/modules/drivers/sensors/ttp223/manifest.json" | ||
], | ||
"modules": { | ||
"*": "./main" | ||
} | ||
} |
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,8 @@ | ||
{ | ||
"modules": { | ||
"embedded:peripheral/Potentiometer/X9C": "$(MODDABLE)/modules/drivers/peripherals/x9c/x9c" | ||
}, | ||
"preload": [ | ||
"embedded:peripheral/Potentiometer/X9C" | ||
] | ||
} |
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,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); | ||
} | ||
|
||
#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; |
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,9 @@ | ||
{ | ||
"modules": { | ||
"embedded:sensor/Switch/TTP223": "$(MODDABLE)/modules/drivers/sensors/ttp223/ttp223" | ||
}, | ||
"preload": [ | ||
"embedded:sensor/Switch/TTP223" | ||
] | ||
} | ||
|
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,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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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...