moving touch stuff into separate projects
This commit is contained in:
3
libs/touch-sensor/README.md
Normal file
3
libs/touch-sensor/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Touch sensor
|
||||
|
||||
The library to interact with the Touch Sensor.
|
@ -0,0 +1,7 @@
|
||||
{
|
||||
"TouchSensorEvent": "Touch sensor interactions",
|
||||
"sensors.TouchSensor.isPressed": "Check if touch sensor is touched.",
|
||||
"sensors.TouchSensor.onEvent": "Do something when a touch sensor is touched...",
|
||||
"sensors.TouchSensor.onEvent|param|body": "code to run when the event is raised",
|
||||
"sensors.TouchSensor.wasPressed": "Check if touch sensor is touched since it was last checked."
|
||||
}
|
14
libs/touch-sensor/_locales/touch-sensor-strings.json
Normal file
14
libs/touch-sensor/_locales/touch-sensor-strings.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"TouchSensorEvent.Bumped|block": "bumped",
|
||||
"TouchSensorEvent.Pressed|block": "pressed",
|
||||
"TouchSensorEvent.Released|block": "released",
|
||||
"sensors.TouchSensor.isPressed|block": "`icons.touchSensor` %sensor|is pressed",
|
||||
"sensors.TouchSensor.onEvent|block": "on `icons.touchSensor` %sensor|%event",
|
||||
"sensors.TouchSensor.wasPressed|block": "`icons.touchSensor` %sensor|was pressed",
|
||||
"sensors.touchSensor1|block": "1",
|
||||
"sensors.touchSensor2|block": "2",
|
||||
"sensors.touchSensor3|block": "3",
|
||||
"sensors.touchSensor4|block": "4",
|
||||
"{id:category}Sensors": "Sensors",
|
||||
"{id:group}Touch Sensor": "Touch Sensor"
|
||||
}
|
15
libs/touch-sensor/pxt.json
Normal file
15
libs/touch-sensor/pxt.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "touch-sensor",
|
||||
"description": "Touch Sensor support",
|
||||
"files": [
|
||||
"README.md",
|
||||
"touch.ts"
|
||||
],
|
||||
"testFiles": [
|
||||
"test.ts"
|
||||
],
|
||||
"public": true,
|
||||
"dependencies": {
|
||||
"core": "file:../core"
|
||||
}
|
||||
}
|
8
libs/touch-sensor/test.ts
Normal file
8
libs/touch-sensor/test.ts
Normal file
@ -0,0 +1,8 @@
|
||||
sensors.touchSensor1.onEvent(TouchSensorEvent.Pressed, function () {
|
||||
})
|
||||
sensors.touchSensor2.onEvent(TouchSensorEvent.Bumped, function () {
|
||||
})
|
||||
sensors.touchSensor3.onEvent(TouchSensorEvent.Released, function () {
|
||||
})
|
||||
sensors.touchSensor4.isPressed();
|
||||
sensors.touchSensor4.wasPressed();
|
93
libs/touch-sensor/touch.ts
Normal file
93
libs/touch-sensor/touch.ts
Normal file
@ -0,0 +1,93 @@
|
||||
// keep TouchSensorEvent in sync with ButtonEvent
|
||||
|
||||
/**
|
||||
* Touch sensor interactions
|
||||
*/
|
||||
const enum TouchSensorEvent {
|
||||
//% block="pressed"
|
||||
Pressed = 4,
|
||||
//% block="bumped"
|
||||
Bumped = 1,
|
||||
//% block="released"
|
||||
Released = 3,
|
||||
}
|
||||
|
||||
namespace sensors {
|
||||
|
||||
//% fixedInstances
|
||||
export class TouchSensor extends internal.AnalogSensor {
|
||||
private button: brick.Button;
|
||||
|
||||
constructor(port: number) {
|
||||
super(port)
|
||||
this.button = new brick.Button();
|
||||
}
|
||||
|
||||
_query() {
|
||||
return this._readPin6() > 2500 ? 1 : 0
|
||||
}
|
||||
|
||||
_update(prev: number, curr: number) {
|
||||
this.button._update(curr > 0)
|
||||
}
|
||||
|
||||
_deviceType() {
|
||||
return DAL.DEVICE_TYPE_TOUCH
|
||||
}
|
||||
|
||||
/**
|
||||
* Do something when a touch sensor is touched...
|
||||
* @param sensor the touch sensor that needs to be clicked or used
|
||||
* @param event the kind of button gesture that needs to be detected
|
||||
* @param body code to run when the event is raised
|
||||
*/
|
||||
//% help=input/touch-sensor/on-event
|
||||
//% blockId=touchEvent block="on `icons.touchSensor` %sensor|%event"
|
||||
//% parts="touch"
|
||||
//% blockNamespace=sensors
|
||||
//% weight=99 blockGap=8
|
||||
//% group="Touch Sensor"
|
||||
onEvent(ev: TouchSensorEvent, body: () => void) {
|
||||
this.button.onEvent(<ButtonEvent><number>ev, body)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if touch sensor is touched.
|
||||
* @param sensor the port to query the request
|
||||
*/
|
||||
//% help=input/touch-sensor/is-pressed
|
||||
//% block="`icons.touchSensor` %sensor|is pressed"
|
||||
//% blockId=touchIsPressed
|
||||
//% parts="touch"
|
||||
//% blockNamespace=sensors
|
||||
//% weight=81 blockGap=8
|
||||
//% group="Touch Sensor"
|
||||
isPressed() {
|
||||
return this.button.isPressed();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if touch sensor is touched since it was last checked.
|
||||
* @param sensor the port to query the request
|
||||
*/
|
||||
//% help=input/touch-sensor/was-pressed
|
||||
//% block="`icons.touchSensor` %sensor|was pressed"
|
||||
//% blockId=touchWasPressed
|
||||
//% parts="touch"
|
||||
//% blockNamespace=sensors
|
||||
//% weight=81 blockGap=8
|
||||
//% group="Touch Sensor"
|
||||
wasPressed() {
|
||||
return this.button.wasPressed();
|
||||
}
|
||||
}
|
||||
|
||||
//% whenUsed block="1" weight=95 fixedInstance
|
||||
export const touchSensor1: TouchSensor = new TouchSensor(1)
|
||||
//% whenUsed block="2" weight=95 fixedInstance
|
||||
export const touchSensor2: TouchSensor = new TouchSensor(2)
|
||||
//% whenUsed block="3" weight=95 fixedInstance
|
||||
export const touchSensor3: TouchSensor = new TouchSensor(3)
|
||||
//% whenUsed block="4" weight=95 fixedInstance
|
||||
export const touchSensor4: TouchSensor = new TouchSensor(4)
|
||||
}
|
Reference in New Issue
Block a user