From e6ca366d20ee995fe01a561e58800a5dc852bce8 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Fri, 3 Jun 2016 23:15:51 -0700 Subject: [PATCH] full support for setPull --- libs/microbit/dal.d.ts | 5 +++-- libs/microbit/enums.d.ts | 10 ++++++++++ libs/microbit/pins.cpp | 36 +++++++++++++++++++++++++----------- libs/microbit/pins.ts | 6 ------ libs/microbit/shims.d.ts | 18 +++++++++--------- sim/libmbit.ts | 20 +++++++++++++------- sim/simsvg.ts | 16 ++++++++-------- sim/state.ts | 7 ++++--- 8 files changed, 72 insertions(+), 46 deletions(-) diff --git a/libs/microbit/dal.d.ts b/libs/microbit/dal.d.ts index 6c924ba1..079b094e 100644 --- a/libs/microbit/dal.d.ts +++ b/libs/microbit/dal.d.ts @@ -223,12 +223,13 @@ declare const enum DAL { MICROBIT_ACCELEROMETER_REST_TOLERANCE = 200, MICROBIT_ACCELEROMETER_TILT_TOLERANCE = 200, MICROBIT_ACCELEROMETER_FREEFALL_TOLERANCE = 400, - MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE = 1000, + MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE = 400, MICROBIT_ACCELEROMETER_3G_TOLERANCE = 3072, MICROBIT_ACCELEROMETER_6G_TOLERANCE = 6144, MICROBIT_ACCELEROMETER_8G_TOLERANCE = 8192, - MICROBIT_ACCELEROMETER_GESTURE_DAMPING = 10, + MICROBIT_ACCELEROMETER_GESTURE_DAMPING = 5, MICROBIT_ACCELEROMETER_SHAKE_DAMPING = 10, + MICROBIT_ACCELEROMETER_SHAKE_RTX = 30, MICROBIT_ACCELEROMETER_SHAKE_COUNT_THRESHOLD = 4, // built/yt/yotta_modules/microbit-dal/inc//drivers/MicroBitButton.h MICROBIT_BUTTON_EVT_DOWN = 1, diff --git a/libs/microbit/enums.d.ts b/libs/microbit/enums.d.ts index c0b922f1..2c687fdd 100644 --- a/libs/microbit/enums.d.ts +++ b/libs/microbit/enums.d.ts @@ -278,6 +278,16 @@ declare namespace led { High = 4, // MICROBIT_PIN_EVT_PULSE_HI Low = 5, // MICROBIT_PIN_EVT_PULSE_LO } + + + declare enum PinPullMode { + //% block="down" + PullDown = 0, + //% block="up" + PullUp = 1, + //% block="none" + PullNone = 2, + } declare namespace pins { } diff --git a/libs/microbit/pins.cpp b/libs/microbit/pins.cpp index 6f04182b..0589aa7c 100644 --- a/libs/microbit/pins.cpp +++ b/libs/microbit/pins.cpp @@ -36,6 +36,15 @@ enum class PulseValue { Low = MICROBIT_PIN_EVT_PULSE_LO }; +enum class PinPullMode { + //% block="down" + PullDown = 0, + //% block="up" + PullUp = 1, + //% block="none" + PullNone = 2 +}; + MicroBitPin *getPin(int id) { switch (id) { case MICROBIT_ID_IO_P0: return &uBit.io.P0; @@ -134,17 +143,6 @@ namespace pins { PINOP(setAnalogPeriodUs(micros)); } - /** - * Configures the pull of this pin. - * @param name pin to set the pull mode on - * @param pull one of the mbed pull configurations: PullUp, PullDown, PullNone - */ - //% help=pins/digital-set-pull weight=21 blockGap=8 - //% blockId=device_set_pull block="digital set pull|pin %pin|to %pull" - void setPull(DigitalPin name, PinMode pull) { - PINOP(setPull(pull)); - } - /** * Configures this pin to a digital input, and generates events where the timestamp is the duration that this pin was either ``high`` or ``low``. */ @@ -225,6 +223,22 @@ namespace pins { } } + + /** + * Configures the pull of this pin. + * @param name pin to set the pull mode on + * @param pull one of the mbed pull configurations: PullUp, PullDown, PullNone + */ + //% help=pins/digital-set-pull weight=3 + //% blockId=device_set_pull block="set pull|pin %pin|to %pull" + void setPull(DigitalPin name, PinPullMode pull) { + PinMode m = pull == PinPullMode::PullDown + ? PinMode::PullDown + : pull == PinPullMode::PullUp ? PinMode::PullUp + : PinMode::PullNone; + PINOP(setPull(m)); + } + /** * Create a new zero-initialized buffer. * @param size number of bytes in the buffer diff --git a/libs/microbit/pins.ts b/libs/microbit/pins.ts index 1258d9c1..3412b8e2 100644 --- a/libs/microbit/pins.ts +++ b/libs/microbit/pins.ts @@ -1,9 +1,3 @@ -enum PinMode { - PullDown, - PullUp, - PullNone -} - /** * Control currents in Pins for analog/digital signals, servos, i2c, ... */ diff --git a/libs/microbit/shims.d.ts b/libs/microbit/shims.d.ts index d49a6adc..2dc8f3a0 100644 --- a/libs/microbit/shims.d.ts +++ b/libs/microbit/shims.d.ts @@ -487,15 +487,6 @@ 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 the pull of this pin. - * @param name pin to set the pull mode on - * @param pull one of the mbed pull configurations: PullUp, PullDown, PullNone - */ - //% help=pins/digital-set-pull weight=21 blockGap=8 - //% blockId=device_set_pull block="digital set pull|pin %pin|to %pull" shim=pins::setPull - function setPull(name: DigitalPin, pull: PinMode): 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``. */ @@ -544,6 +535,15 @@ declare namespace pins { //% help=pins/analog-pitch weight=14 async shim=pins::analogPitch function analogPitch(frequency: number, ms: number): void; + /** + * Configures the pull of this pin. + * @param name pin to set the pull mode on + * @param pull one of the mbed pull configurations: PullUp, PullDown, PullNone + */ + //% help=pins/digital-set-pull weight=3 + //% blockId=device_set_pull block="set pull|pin %pin|to %pull" shim=pins::setPull + function setPull(name: DigitalPin, pull: PinPullMode): void; + /** * Create a new zero-initialized buffer. * @param size number of bytes in the buffer diff --git a/sim/libmbit.ts b/sim/libmbit.ts index 639dac66..52dc76dc 100644 --- a/sim/libmbit.ts +++ b/sim/libmbit.ts @@ -524,29 +524,35 @@ namespace pxsim.pins { export function digitalReadPin(pinId: number): number { let pin = getPin(pinId); if (!pin) return; - pin.mode = PinMode.Digital | PinMode.Input; + pin.mode = PinFlags.Digital | PinFlags.Input; return pin.value > 100 ? 1 : 0; } export function digitalWritePin(pinId: number, value: number) { let pin = getPin(pinId); if (!pin) return; - pin.mode = PinMode.Digital | PinMode.Output; + pin.mode = PinFlags.Digital | PinFlags.Output; pin.value = value > 0 ? 1023 : 0; runtime.queueDisplayUpdate(); } + export function setPull(pinId: number, pull: number) { + let pin = getPin(pinId); + if (!pin) return; + pin.pull = pull; + } + export function analogReadPin(pinId: number): number { let pin = getPin(pinId); if (!pin) return; - pin.mode = PinMode.Analog | PinMode.Input; + pin.mode = PinFlags.Analog | PinFlags.Input; return pin.value || 0; } export function analogWritePin(pinId: number, value: number) { let pin = getPin(pinId); if (!pin) return; - pin.mode = PinMode.Analog | PinMode.Output; + pin.mode = PinFlags.Analog | PinFlags.Output; pin.value = value ? 1 : 0; runtime.queueDisplayUpdate(); } @@ -554,7 +560,7 @@ namespace pxsim.pins { export function analogSetPeriod(pinId: number, micros: number) { let pin = getPin(pinId); if (!pin) return; - pin.mode = PinMode.Analog | PinMode.Output; + pin.mode = PinFlags.Analog | PinFlags.Output; pin.period = micros; runtime.queueDisplayUpdate(); } @@ -580,7 +586,7 @@ namespace pxsim.pins { export function analogPitch(frequency: number, ms: number) { // update analog output let pin = board().pins.filter(pin => !!pin && pin.pitch)[0] || board().pins[0]; - pin.mode = PinMode.Analog | PinMode.Output; + pin.mode = PinFlags.Analog | PinFlags.Output; if (frequency <= 0) { pin.value = 0; pin.period = 0; @@ -598,7 +604,7 @@ namespace pxsim.pins { AudioContextManager.stop(); pin.value = 0; pin.period = 0; - pin.mode = PinMode.Unused; + pin.mode = PinFlags.Unused; runtime.queueDisplayUpdate(); cb() }, ms); diff --git a/sim/simsvg.ts b/sim/simsvg.ts index 45110fa5..67c0fce9 100644 --- a/sim/simsvg.ts +++ b/sim/simsvg.ts @@ -183,20 +183,20 @@ namespace pxsim.micro_bit { if (!pin) return; let text = this.pinTexts[index]; let v = ""; - if (pin.mode & PinMode.Analog) { + if (pin.mode & PinFlags.Analog) { v = Math.floor(100 - (pin.value || 0) / 1023 * 100) + "%"; if (text) text.textContent = (pin.period ? "~" : "") + (pin.value || 0) + ""; } - else if (pin.mode & PinMode.Digital) { + else if (pin.mode & PinFlags.Digital) { v = pin.value > 0 ? '0%' : '100%'; if (text) text.textContent = pin.value > 0 ? "1" : "0"; } - else if (pin.mode & PinMode.Touch) { - v = pin.touched ? '0%' : '100%'; + else if (pin.mode & PinFlags.Touch) { + v = pin.touched ? "0%" : "100%"; if (text) text.textContent = ""; } else { - v = '100%'; - if (text) text.textContent = ''; + v = "100%"; + if (text) text.textContent = ""; } if (v) svg.setGradientValue(this.pinGradients[index], v); } @@ -651,7 +651,7 @@ svg.sim.grayscale { let state = this.board; let pin = state.pins[index]; let svgpin = this.pins[index]; - if (pin.mode & PinMode.Input) { + if (pin.mode & PinFlags.Input) { let cursor = svg.cursorPoint(pt, this.element, ev); let v = (400 - cursor.y) / 40 * 1023 pin.value = Math.max(0, Math.min(1023, Math.floor(v))); @@ -664,7 +664,7 @@ svg.sim.grayscale { let pin = state.pins[index]; let svgpin = this.pins[index]; svg.addClass(svgpin, "touched"); - if (pin.mode & PinMode.Input) { + if (pin.mode & PinFlags.Input) { let cursor = svg.cursorPoint(pt, this.element, ev); let v = (400 - cursor.y) / 40 * 1023 pin.value = Math.max(0, Math.min(1023, Math.floor(v))); diff --git a/sim/state.ts b/sim/state.ts index 1a4d3606..ebfd0e4e 100644 --- a/sim/state.ts +++ b/sim/state.ts @@ -8,7 +8,7 @@ namespace pxsim { greyscale } - export enum PinMode { + export enum PinFlags { Unused = 0, Digital = 0x0001, Analog = 0x0002, @@ -22,11 +22,12 @@ namespace pxsim { touched = false; value = 0; period = 0; - mode = PinMode.Unused; + mode = PinFlags.Unused; pitch = false; + pull = 0; // PullDown isTouched(): boolean { - this.mode = PinMode.Touch; + this.mode = PinFlags.Touch; return this.touched; } }