moving into separate project
This commit is contained in:
3
libs/ultrasonic-sensor/README.md
Normal file
3
libs/ultrasonic-sensor/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Touch sensor
|
||||
|
||||
The library to interact with the Touch Sensor.
|
@ -0,0 +1,6 @@
|
||||
{
|
||||
"sensors.UltraSonicSensor.distance": "Gets the distance from the sonar in millimeters",
|
||||
"sensors.UltraSonicSensor.onEvent": "Registers code to run when the given color is close",
|
||||
"sensors.UltraSonicSensor.onEvent|param|handler": "the code to run when detected",
|
||||
"sensors.UltraSonicSensor.waitUntil": "Waits for the event to occur"
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
{
|
||||
"UltrasonicSensorEvent.ObjectDetected|block": "object detected",
|
||||
"UltrasonicSensorEvent.ObjectNear|block": "object near",
|
||||
"sensors.UltraSonicSensor.distance|block": "`icons.ultrasonicSensor` %sensor|distance",
|
||||
"sensors.UltraSonicSensor.onEvent|block": "on `icons.ultrasonicSensor` %sensor|%event",
|
||||
"sensors.UltraSonicSensor.waitUntil|block": "wait `icons.ultrasonicSensor` %sensor|until %event",
|
||||
"sensors.ultrasonic1|block": "1",
|
||||
"sensors.ultrasonic2|block": "2",
|
||||
"sensors.ultrasonic3|block": "3",
|
||||
"sensors.ultrasonic4|block": "4",
|
||||
"{id:category}Sensors": "Sensors",
|
||||
"{id:group}Ultrasonic Sensor": "Ultrasonic Sensor"
|
||||
}
|
15
libs/ultrasonic-sensor/pxt.json
Normal file
15
libs/ultrasonic-sensor/pxt.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "ultrasonic-sensor",
|
||||
"description": "Ultrasonic Sensor support",
|
||||
"files": [
|
||||
"README.md",
|
||||
"ultrasonic.ts"
|
||||
],
|
||||
"testFiles": [
|
||||
"test.ts"
|
||||
],
|
||||
"public": true,
|
||||
"dependencies": {
|
||||
"core": "file:../core"
|
||||
}
|
||||
}
|
0
libs/ultrasonic-sensor/test.ts
Normal file
0
libs/ultrasonic-sensor/test.ts
Normal file
98
libs/ultrasonic-sensor/ultrasonic.ts
Normal file
98
libs/ultrasonic-sensor/ultrasonic.ts
Normal file
@ -0,0 +1,98 @@
|
||||
enum UltrasonicSensorEvent {
|
||||
//% block="object detected"
|
||||
ObjectDetected = 10,
|
||||
//% block="object near"
|
||||
ObjectNear = sensors.internal.ThresholdState.Low,
|
||||
//% block="object far"
|
||||
ObjectFar = sensors.internal.ThresholdState.High
|
||||
}
|
||||
|
||||
namespace sensors {
|
||||
|
||||
//% fixedInstances
|
||||
export class UltraSonicSensor extends internal.UartSensor {
|
||||
private promixityThreshold: sensors.internal.ThresholdDetector;
|
||||
private movementThreshold: number;
|
||||
|
||||
constructor(port: number) {
|
||||
super(port)
|
||||
this.promixityThreshold = new sensors.internal.ThresholdDetector(this.id(), 0, 255, 10, 100); // range is 0..255cm
|
||||
this.movementThreshold = 1;
|
||||
}
|
||||
|
||||
_deviceType() {
|
||||
return DAL.DEVICE_TYPE_ULTRASONIC
|
||||
}
|
||||
|
||||
_query(): number {
|
||||
return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
|
||||
}
|
||||
|
||||
_update(prev: number, curr: number) {
|
||||
// is there an object near?
|
||||
this.promixityThreshold.setLevel(curr);
|
||||
|
||||
// did something change?
|
||||
if (Math.abs(prev - curr) > this.movementThreshold)
|
||||
control.raiseEvent(this._id, UltrasonicSensorEvent.ObjectDetected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers code to run when the given color is close
|
||||
* @param handler the code to run when detected
|
||||
*/
|
||||
//% help=input/ultrasonic/on
|
||||
//% blockId=ultrasonicOn
|
||||
//% block="on `icons.ultrasonicSensor` %sensor|%event"
|
||||
//% parts="ultrasonicsensor"
|
||||
//% blockNamespace=sensors
|
||||
//% weight=100 blockGap=8
|
||||
//% group="Ultrasonic Sensor"
|
||||
onEvent(event: UltrasonicSensorEvent, handler: () => void) {
|
||||
control.onEvent(this._id, event, handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for the event to occur
|
||||
*/
|
||||
//% help=input/ultrasonic/wait
|
||||
//% block="wait `icons.ultrasonicSensor` %sensor|until %event"
|
||||
//% blockId=ultrasonicWait
|
||||
//% parts="ultrasonicsensor"
|
||||
//% blockNamespace=sensors
|
||||
//% weight=99 blockGap=8
|
||||
//% group="Ultrasonic Sensor"
|
||||
waitUntil(event: UltrasonicSensorEvent) {
|
||||
control.waitForEvent(this._id, event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the distance from the sonar in millimeters
|
||||
* @param sensor the ultrasonic sensor port
|
||||
*/
|
||||
//% help=input/ultrasonic/distance
|
||||
//% block="`icons.ultrasonicSensor` %sensor|distance"
|
||||
//% blockId=sonarGetDistance
|
||||
//% parts="ultrasonicsensor"
|
||||
//% blockNamespace=sensors
|
||||
//% weight=65 blockGap=8
|
||||
//% group="Ultrasonic Sensor"
|
||||
distance(): number {
|
||||
// it supposedly also has an inch mode, but we stick to cm
|
||||
this._setMode(0)
|
||||
return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff; // range is 0..255
|
||||
}
|
||||
}
|
||||
|
||||
//% fixedInstance whenUsed block="1"
|
||||
export const ultrasonic1: UltraSonicSensor = new UltraSonicSensor(1)
|
||||
|
||||
//% fixedInstance whenUsed block="4"
|
||||
export const ultrasonic4: UltraSonicSensor = new UltraSonicSensor(4)
|
||||
|
||||
//% fixedInstance whenUsed block="2"
|
||||
export const ultrasonic2: UltraSonicSensor = new UltraSonicSensor(2)
|
||||
|
||||
//% fixedInstance whenUsed block="3"
|
||||
export const ultrasonic3: UltraSonicSensor = new UltraSonicSensor(3)
|
||||
}
|
Reference in New Issue
Block a user