From 89f09c7f350748b9348622632b9d3012a98caa8b Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Mon, 16 May 2016 16:24:44 -0700 Subject: [PATCH] added pins->on pulsed --- libs/microbit/control.cpp | 22 ++++++++++++++++++++-- libs/microbit/control.ts | 11 +++++------ libs/microbit/enums.d.ts | 6 ++++++ libs/microbit/input.cpp | 4 ++-- libs/microbit/led.ts | 24 ++++++++++++------------ libs/microbit/music.ts | 4 ++-- libs/microbit/pins.cpp | 29 ++++++++++++++++++++++++++++- libs/microbit/pins.ts | 2 +- libs/microbit/shims.d.ts | 37 +++++++++++++++++++++++++++++++++---- pxtarget.json | 2 +- sim/libmbit.ts | 7 +++++++ 11 files changed, 117 insertions(+), 31 deletions(-) diff --git a/libs/microbit/control.cpp b/libs/microbit/control.cpp index 051c512a..07fa9188 100644 --- a/libs/microbit/control.cpp +++ b/libs/microbit/control.cpp @@ -141,7 +141,7 @@ namespace control { * @param value Component specific code indicating the cause of the event. * @param mode optional definition of how the event should be processed after construction (default is CREATE_AND_FIRE). */ - //% weight=21 blockGap=12 blockId="control_raise_event" block="raise event|from source %src=control_event_source|with value %value=control_event_value" blockExternalInputs=1 + //% weight=21 blockGap=12 blockId="control_raise_event" block="raise event|from source %src=control_event_source_id|with value %value=control_event_value_id" blockExternalInputs=1 //% mode.defl=CREATE_AND_FIRE void raiseEvent(int src, int value, EventCreationMode mode) { MicroBitEvent evt(src, value, (MicroBitEventLaunchMode)mode); @@ -150,11 +150,29 @@ namespace control { /** * Raises an event in the event bus. */ - //% weight=20 blockGap=8 blockId="control_on_event" block="on event|from %src=control_event_source|with value %value=control_event_value" + //% weight=20 blockGap=8 blockId="control_on_event" block="on event|from %src=control_event_source_id|with value %value=control_event_value_id" //% blockExternalInputs=1 void onEvent(int src, int value, Action handler) { registerWithDal(src, value, handler); } + + /** + * Gets the value of the last event executed on the bus + */ + //% blockId=control_event_value" block="event value" + //% weight=18 + int eventValue() { + return pxt::lastEvent.value; + } + + /** + * Gets the timestamp of the last event executed on the bus + */ + //% blockId=control_event_timestamp" block="event timestamp" + //% weight=19 blockGap-8 + int eventTimestamp() { + return pxt::lastEvent.timestamp; + } /** * Gets a friendly name for the device derived from the its serial number diff --git a/libs/microbit/control.ts b/libs/microbit/control.ts index 7c479fe3..f2d4ec1a 100644 --- a/libs/microbit/control.ts +++ b/libs/microbit/control.ts @@ -7,15 +7,15 @@ namespace control { /** * Returns the value of a C++ runtime constant */ - //% weight=19 weight=19 blockId="control_event_source" block="%id" - export function eventSource(id: EventBusSource) : number { + //% weight=2 weight=19 blockId="control_event_source_id" block="%id" blockGap=8 + export function eventSourceId(id: EventBusSource): number { return id; } /** * Returns the value of a C++ runtime constant */ - //% weight=19 weight=19 blockId="control_event_value" block="%id" - export function eventValue(id: EventBusValue) : number { + //% weight=1 weight=19 blockId="control_event_value_id" block="%id" + export function eventValueId(id: EventBusValue): number { return id; } @@ -23,8 +23,7 @@ namespace control { * Display specified error code and stop the program. */ //% shim=pxtrt::panic - export function panic(code:number) - { + export function panic(code: number) { } /** diff --git a/libs/microbit/enums.d.ts b/libs/microbit/enums.d.ts index 55fcee26..731ace17 100644 --- a/libs/microbit/enums.d.ts +++ b/libs/microbit/enums.d.ts @@ -262,6 +262,12 @@ declare namespace led { P4 = 11, // MICROBIT_ID_IO_P4 P10 = 17, // MICROBIT_ID_IO_P10 } + + + declare enum PulseValue { + High = 4, // MICROBIT_PIN_EVT_PULSE_HI + Low = 5, // MICROBIT_PIN_EVT_PULSE_LO + } declare namespace pins { } declare namespace serial { diff --git a/libs/microbit/input.cpp b/libs/microbit/input.cpp index 803510df..b4148079 100644 --- a/libs/microbit/input.cpp +++ b/libs/microbit/input.cpp @@ -104,7 +104,7 @@ namespace input { * @param button TODO * @param body TODO */ - //% help=input/on-button-pressed weight=85 + //% help=input/on-button-pressed weight=85 blockGap=8 //% blockId=device_button_event block="on button|%NAME|pressed" icon="\uf192" void onButtonPressed(Button button, Action body) { registerWithDal((int)button, MICROBIT_BUTTON_EVT_CLICK, body); @@ -114,7 +114,7 @@ namespace input { * Attaches code to run when the screen is facing up. * @param body TODO */ - //% help=input/on-gesture weight=84 + //% help=input/on-gesture weight=84 blockGap=8 //% blockId=device_gesture_event block="on |%NAME" icon="\uf135" void onGesture(Gesture gesture, Action body) { registerWithDal(MICROBIT_ID_GESTURE, (int)gesture, body); diff --git a/libs/microbit/led.ts b/libs/microbit/led.ts index 0c556f5b..892c9e5c 100644 --- a/libs/microbit/led.ts +++ b/libs/microbit/led.ts @@ -8,7 +8,7 @@ namespace led { let barGraphHigh = 0; // when was the current high value recorded let barGraphHighLast = 0; - + /** * Displays a vertical bar graph based on the `value` and `high` value. * If `high` is 0, the chart gets adjusted automatically. @@ -17,35 +17,35 @@ namespace led { */ //% help=/led/plot-bar-graph weight=20 //% blockId=device_plot_bar_graph block="plot bar graph of %value |up to %high" icon="\uf080" blockExternalInputs=true - export function plotBarGraph(value: number, high: number): void { + export function plotBarGraph(value: number, high: number): void { let now = input.runningTime(); serial.writeString(value.toString() + "\r\n"); value = Math.abs(value); - + if (high != 0) barGraphHigh = high; else if (value > barGraphHigh || now - barGraphHighLast > 5000) { barGraphHigh = value; barGraphHighLast = now; } - + barGraphHigh = Math.max(barGraphHigh, 16); - + let v = (value * 15) / barGraphHigh; let k = 0; - for(let y = 4; y >= 0; --y) { + for (let y = 4; y >= 0; --y) { for (let x = 0; x < 3; ++x) { if (k > v) { - unplot(2-x,y); - unplot(2+x,y); + unplot(2 - x, y); + unplot(2 + x, y); } else { - plot(2-x, y); - plot(2+x, y); + plot(2 - x, y); + plot(2 + x, y); } ++k; } - } + } } - + /** * Toggles a particular pixel * @param x TODO diff --git a/libs/microbit/music.ts b/libs/microbit/music.ts index 1a7197e9..33e84ed8 100644 --- a/libs/microbit/music.ts +++ b/libs/microbit/music.ts @@ -77,9 +77,9 @@ enum BeatFraction { /** * Generation of music tones through pin ``P0``. */ -//% color=52 weight=33 +//% color=52 weight=98 namespace music { - var beatsPerMinute: number = 120; + let beatsPerMinute: number = 120; /** * Plays a tone through pin ``P0`` for the given duration. diff --git a/libs/microbit/pins.cpp b/libs/microbit/pins.cpp index 20023570..3fc32ea7 100644 --- a/libs/microbit/pins.cpp +++ b/libs/microbit/pins.cpp @@ -31,6 +31,11 @@ enum class AnalogPin { P10 = MICROBIT_ID_IO_P10, }; +enum class PulseValue { + High = MICROBIT_PIN_EVT_PULSE_HI, + Low = MICROBIT_PIN_EVT_PULSE_LO +}; + MicroBitPin *getPin(int id) { switch (id) { case MICROBIT_ID_IO_P0: return &uBit.io.P0; @@ -75,7 +80,6 @@ namespace pins { return getPin(id); } - /** * Read the specified pin or connector as either 0 or 1 * @param name pin to read from @@ -129,6 +133,29 @@ namespace pins { void analogSetPeriod(AnalogPin name, int micros) { PINOP(setAnalogPeriodUs(micros)); } + + /** + * Configures this pin to a digital input, and generates events where the timestamp is the duration that this pin was either ``high`` or ``low``. + */ + //% help=pins/on-pulsed weight=22 blockGap=8 + //% blockId=pins_on_pulsed block="on|pin %pin|pulsed %pulse" + void onPulsed(DigitalPin name, PulseValue pulse, Action body) { + MicroBitPin* pin = getPin((int)name); + if (!pin) return; + + pin->eventOn(MICROBIT_PIN_EVENT_ON_PULSE); + registerWithDal((int)name, (int)pulse, body); + } + + /** + * Gets the duration of the last pulse in micro-seconds. This function should be called from a ``onPulse`` handler. + */ + //% help=pins/pulse-micros + //% blockId=pins_pulse_duration block="pulse duration (us)" + //% weight=21 + int pulseDuration() { + return pxt::lastEvent.timestamp; + } /** * Writes a value to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation. On a continuous rotation servo, this will set the speed of the servo (with ``0`` being full-speed in one direction, ``180`` being full speed in the other, and a value near ``90`` being no movement). diff --git a/libs/microbit/pins.ts b/libs/microbit/pins.ts index 1f171df6..59da3495 100644 --- a/libs/microbit/pins.ts +++ b/libs/microbit/pins.ts @@ -20,7 +20,7 @@ namespace pins { /** * Read one number from 7-bit I2C address. */ - //% help=pins/i2c-read-number + //% help=pins/i2c-read-number blockGap=8 //% blockId=pins_i2c_readnumber block="i2c read number|at address %address|of format %format=i2c_sizeof" weight=7 export function i2cReadNumber(address: number, format: NumberFormat): number { let buf = pins.i2cReadBuffer(address, pins.sizeOf(format)) diff --git a/libs/microbit/shims.d.ts b/libs/microbit/shims.d.ts index aecbd407..0a89784a 100644 --- a/libs/microbit/shims.d.ts +++ b/libs/microbit/shims.d.ts @@ -200,7 +200,7 @@ declare namespace input { * @param button TODO * @param body TODO */ - //% help=input/on-button-pressed weight=85 + //% help=input/on-button-pressed weight=85 blockGap=8 //% blockId=device_button_event block="on button|%NAME|pressed" icon="\uf192" shim=input::onButtonPressed function onButtonPressed(button: Button, body: () => void): void; @@ -208,7 +208,7 @@ declare namespace input { * Attaches code to run when the screen is facing up. * @param body TODO */ - //% help=input/on-gesture weight=84 + //% help=input/on-gesture weight=84 blockGap=8 //% blockId=device_gesture_event block="on |%NAME" icon="\uf135" shim=input::onGesture function onGesture(gesture: Gesture, body: () => void): void; @@ -332,17 +332,31 @@ declare namespace control { * @param value Component specific code indicating the cause of the event. * @param mode optional definition of how the event should be processed after construction (default is CREATE_AND_FIRE). */ - //% weight=21 blockGap=12 blockId="control_raise_event" block="raise event|from source %src=control_event_source|with value %value=control_event_value" blockExternalInputs=1 + //% weight=21 blockGap=12 blockId="control_raise_event" block="raise event|from source %src=control_event_source_id|with value %value=control_event_value_id" blockExternalInputs=1 //% mode.defl=1 shim=control::raiseEvent function raiseEvent(src: number, value: number, mode?: EventCreationMode): void; /** * Raises an event in the event bus. */ - //% weight=20 blockGap=8 blockId="control_on_event" block="on event|from %src=control_event_source|with value %value=control_event_value" + //% weight=20 blockGap=8 blockId="control_on_event" block="on event|from %src=control_event_source_id|with value %value=control_event_value_id" //% blockExternalInputs=1 shim=control::onEvent function onEvent(src: number, value: number, handler: () => void): void; + /** + * Gets the value of the last event executed on the bus + */ + //% blockId=control_event_value" block="event value" + //% weight=18 shim=control::eventValue + function eventValue(): number; + + /** + * Gets the timestamp of the last event executed on the bus + */ + //% blockId=control_event_timestamp" block="event timestamp" + //% weight=19 blockGap-8 shim=control::eventTimestamp + function eventTimestamp(): number; + /** * Gets a friendly name for the device derived from the its serial number */ @@ -473,6 +487,21 @@ declare namespace pins { //% blockId=device_set_analog_period block="analog set period|pin %pin|to (µs)%micros" shim=pins::analogSetPeriod function analogSetPeriod(name: AnalogPin, micros: number): void; + /** + * Configures this pin to a digital input, and generates events where the timestamp is the duration that this pin was either ``high`` or ``low``. + */ + //% help=pins/on-pulsed weight=22 blockGap=8 + //% blockId=pins_on_pulsed block="on|pin %pin|pulsed %pulse" shim=pins::onPulsed + function onPulsed(name: DigitalPin, pulse: PulseValue, body: () => void): void; + + /** + * Gets the duration of the last pulse in micro-seconds. This function should be called from a ``onPulse`` handler. + */ + //% help=pins/pulse-micros + //% blockId=pins_pulse_duration block="pulse duration (us)" + //% weight=21 shim=pins::pulseDuration + function pulseDuration(): number; + /** * Writes a value to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation. On a continuous rotation servo, this will set the speed of the servo (with ``0`` being full-speed in one direction, ``180`` being full speed in the other, and a value near ``90`` being no movement). * @param name pin to write to diff --git a/pxtarget.json b/pxtarget.json index 84cb1434..ca3beb1d 100644 --- a/pxtarget.json +++ b/pxtarget.json @@ -63,7 +63,7 @@ "aspectRatio": 1.22 }, "compileService": { - "gittag": "v0.1.9", + "gittag": "v0.1.10", "serviceId": "ws" }, "serial": { diff --git a/sim/libmbit.ts b/sim/libmbit.ts index 445dbfdb..3c11352d 100644 --- a/sim/libmbit.ts +++ b/sim/libmbit.ts @@ -501,6 +501,13 @@ namespace pxsim.radio { } namespace pxsim.pins { + export function onPulse(name: number, pulse: number, body: RefAction) { + } + + export function pulseDuration(): number { + return 0; + } + export function digitalReadPin(pinId: number): number { let pin = getPin(pinId); if (!pin) return;