added pins->on pulsed

This commit is contained in:
Peli de Halleux 2016-05-16 16:24:44 -07:00
parent a667467bbd
commit 89f09c7f35
11 changed files with 117 additions and 31 deletions

View File

@ -141,7 +141,7 @@ namespace control {
* @param value Component specific code indicating the cause of the event. * @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). * @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 //% mode.defl=CREATE_AND_FIRE
void raiseEvent(int src, int value, EventCreationMode mode) { void raiseEvent(int src, int value, EventCreationMode mode) {
MicroBitEvent evt(src, value, (MicroBitEventLaunchMode)mode); MicroBitEvent evt(src, value, (MicroBitEventLaunchMode)mode);
@ -150,11 +150,29 @@ namespace control {
/** /**
* Raises an event in the event bus. * 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 //% blockExternalInputs=1
void onEvent(int src, int value, Action handler) { void onEvent(int src, int value, Action handler) {
registerWithDal(src, value, 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 * Gets a friendly name for the device derived from the its serial number

View File

@ -7,15 +7,15 @@ namespace control {
/** /**
* Returns the value of a C++ runtime constant * Returns the value of a C++ runtime constant
*/ */
//% weight=19 weight=19 blockId="control_event_source" block="%id" //% weight=2 weight=19 blockId="control_event_source_id" block="%id" blockGap=8
export function eventSource(id: EventBusSource) : number { export function eventSourceId(id: EventBusSource): number {
return id; return id;
} }
/** /**
* Returns the value of a C++ runtime constant * Returns the value of a C++ runtime constant
*/ */
//% weight=19 weight=19 blockId="control_event_value" block="%id" //% weight=1 weight=19 blockId="control_event_value_id" block="%id"
export function eventValue(id: EventBusValue) : number { export function eventValueId(id: EventBusValue): number {
return id; return id;
} }
@ -23,8 +23,7 @@ namespace control {
* Display specified error code and stop the program. * Display specified error code and stop the program.
*/ */
//% shim=pxtrt::panic //% shim=pxtrt::panic
export function panic(code:number) export function panic(code: number) {
{
} }
/** /**

View File

@ -262,6 +262,12 @@ declare namespace led {
P4 = 11, // MICROBIT_ID_IO_P4 P4 = 11, // MICROBIT_ID_IO_P4
P10 = 17, // MICROBIT_ID_IO_P10 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 pins {
} }
declare namespace serial { declare namespace serial {

View File

@ -104,7 +104,7 @@ namespace input {
* @param button TODO * @param button TODO
* @param body 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" //% blockId=device_button_event block="on button|%NAME|pressed" icon="\uf192"
void onButtonPressed(Button button, Action body) { void onButtonPressed(Button button, Action body) {
registerWithDal((int)button, MICROBIT_BUTTON_EVT_CLICK, 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. * Attaches code to run when the screen is facing up.
* @param body TODO * @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" //% blockId=device_gesture_event block="on |%NAME" icon="\uf135"
void onGesture(Gesture gesture, Action body) { void onGesture(Gesture gesture, Action body) {
registerWithDal(MICROBIT_ID_GESTURE, (int)gesture, body); registerWithDal(MICROBIT_ID_GESTURE, (int)gesture, body);

View File

@ -8,7 +8,7 @@ namespace led {
let barGraphHigh = 0; let barGraphHigh = 0;
// when was the current high value recorded // when was the current high value recorded
let barGraphHighLast = 0; let barGraphHighLast = 0;
/** /**
* Displays a vertical bar graph based on the `value` and `high` value. * Displays a vertical bar graph based on the `value` and `high` value.
* If `high` is 0, the chart gets adjusted automatically. * If `high` is 0, the chart gets adjusted automatically.
@ -17,35 +17,35 @@ namespace led {
*/ */
//% help=/led/plot-bar-graph weight=20 //% 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 //% 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(); let now = input.runningTime();
serial.writeString(value.toString() + "\r\n"); serial.writeString(value.toString() + "\r\n");
value = Math.abs(value); value = Math.abs(value);
if (high != 0) barGraphHigh = high; if (high != 0) barGraphHigh = high;
else if (value > barGraphHigh || now - barGraphHighLast > 5000) { else if (value > barGraphHigh || now - barGraphHighLast > 5000) {
barGraphHigh = value; barGraphHigh = value;
barGraphHighLast = now; barGraphHighLast = now;
} }
barGraphHigh = Math.max(barGraphHigh, 16); barGraphHigh = Math.max(barGraphHigh, 16);
let v = (value * 15) / barGraphHigh; let v = (value * 15) / barGraphHigh;
let k = 0; let k = 0;
for(let y = 4; y >= 0; --y) { for (let y = 4; y >= 0; --y) {
for (let x = 0; x < 3; ++x) { for (let x = 0; x < 3; ++x) {
if (k > v) { if (k > v) {
unplot(2-x,y); unplot(2 - x, y);
unplot(2+x,y); unplot(2 + x, y);
} else { } else {
plot(2-x, y); plot(2 - x, y);
plot(2+x, y); plot(2 + x, y);
} }
++k; ++k;
} }
} }
} }
/** /**
* Toggles a particular pixel * Toggles a particular pixel
* @param x TODO * @param x TODO

View File

@ -77,9 +77,9 @@ enum BeatFraction {
/** /**
* Generation of music tones through pin ``P0``. * Generation of music tones through pin ``P0``.
*/ */
//% color=52 weight=33 //% color=52 weight=98
namespace music { namespace music {
var beatsPerMinute: number = 120; let beatsPerMinute: number = 120;
/** /**
* Plays a tone through pin ``P0`` for the given duration. * Plays a tone through pin ``P0`` for the given duration.

View File

@ -31,6 +31,11 @@ enum class AnalogPin {
P10 = MICROBIT_ID_IO_P10, P10 = MICROBIT_ID_IO_P10,
}; };
enum class PulseValue {
High = MICROBIT_PIN_EVT_PULSE_HI,
Low = MICROBIT_PIN_EVT_PULSE_LO
};
MicroBitPin *getPin(int id) { MicroBitPin *getPin(int id) {
switch (id) { switch (id) {
case MICROBIT_ID_IO_P0: return &uBit.io.P0; case MICROBIT_ID_IO_P0: return &uBit.io.P0;
@ -75,7 +80,6 @@ namespace pins {
return getPin(id); return getPin(id);
} }
/** /**
* Read the specified pin or connector as either 0 or 1 * Read the specified pin or connector as either 0 or 1
* @param name pin to read from * @param name pin to read from
@ -129,6 +133,29 @@ namespace pins {
void analogSetPeriod(AnalogPin name, int micros) { void analogSetPeriod(AnalogPin name, int micros) {
PINOP(setAnalogPeriodUs(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). * 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).

View File

@ -20,7 +20,7 @@ namespace pins {
/** /**
* Read one number from 7-bit I2C address. * 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 //% 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 { export function i2cReadNumber(address: number, format: NumberFormat): number {
let buf = pins.i2cReadBuffer(address, pins.sizeOf(format)) let buf = pins.i2cReadBuffer(address, pins.sizeOf(format))

View File

@ -200,7 +200,7 @@ declare namespace input {
* @param button TODO * @param button TODO
* @param body 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 //% blockId=device_button_event block="on button|%NAME|pressed" icon="\uf192" shim=input::onButtonPressed
function onButtonPressed(button: Button, body: () => void): void; function onButtonPressed(button: Button, body: () => void): void;
@ -208,7 +208,7 @@ declare namespace input {
* Attaches code to run when the screen is facing up. * Attaches code to run when the screen is facing up.
* @param body TODO * @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 //% blockId=device_gesture_event block="on |%NAME" icon="\uf135" shim=input::onGesture
function onGesture(gesture: Gesture, body: () => void): void; 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 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). * @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 //% mode.defl=1 shim=control::raiseEvent
function raiseEvent(src: number, value: number, mode?: EventCreationMode): void; function raiseEvent(src: number, value: number, mode?: EventCreationMode): void;
/** /**
* Raises an event in the event bus. * 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 //% blockExternalInputs=1 shim=control::onEvent
function onEvent(src: number, value: number, handler: () => void): void; 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 * 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 //% blockId=device_set_analog_period block="analog set period|pin %pin|to (µs)%micros" shim=pins::analogSetPeriod
function analogSetPeriod(name: AnalogPin, micros: number): void; 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). * 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 * @param name pin to write to

View File

@ -63,7 +63,7 @@
"aspectRatio": 1.22 "aspectRatio": 1.22
}, },
"compileService": { "compileService": {
"gittag": "v0.1.9", "gittag": "v0.1.10",
"serviceId": "ws" "serviceId": "ws"
}, },
"serial": { "serial": {

View File

@ -501,6 +501,13 @@ namespace pxsim.radio {
} }
namespace pxsim.pins { namespace pxsim.pins {
export function onPulse(name: number, pulse: number, body: RefAction) {
}
export function pulseDuration(): number {
return 0;
}
export function digitalReadPin(pinId: number): number { export function digitalReadPin(pinId: number): number {
let pin = getPin(pinId); let pin = getPin(pinId);
if (!pin) return; if (!pin) return;