adding pins.pulseIn API

This commit is contained in:
Peli de Halleux
2016-08-17 11:18:15 -07:00
parent 269254796d
commit 8e811b913e
8 changed files with 108 additions and 4 deletions

View File

@ -160,11 +160,32 @@ namespace pins {
*/
//% help=pins/pulse-duration
//% blockId=pins_pulse_duration block="pulse duration (µs)"
//% weight=21
//% weight=21 blockGap=8
int pulseDuration() {
return pxt::lastEvent.timestamp;
}
/**
* Returns the duration of a pulse in microseconds
* @param name the pin which measures the pulse
* @param value the value of the pulse (default high)
*/
//% blockId="pins_pulse_in" block="pulse in (µs)|pin %name"
//% weight=20
int pulseIn(DigitalPin name, PulseValue value) {
MicroBitPin* pin = getPin((int)name);
if (!pin) return 0;
int pulse = value == PulseValue::High ? 1 : 0;
while(pin->getDigitalValue() != pulse);
uint64_t start = system_timer_current_time_us();
while(pin->getDigitalValue() == pulse);
uint64_t end = system_timer_current_time_us();
return end - start;
}
/**
* 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

View File

@ -11,7 +11,7 @@ namespace pins {
* @param toLow the lower bound of the value's target range
* @param toHigh the upper bound of the value's target range, eg: 4
*/
//% help=pins/map weight=22
//% help=pins/map weight=23
//% blockId=math_map block="map %value|from low %fromLow|from high %fromHigh|to low %toLow|to high %toHigh"
export function map(value: number, fromLow: number, fromHigh: number, toLow: number, toHigh: number): number {
return ((value - fromLow) * (toHigh - toLow)) / (fromHigh - fromLow) + toLow;

View File

@ -514,9 +514,18 @@ declare namespace pins {
*/
//% help=pins/pulse-duration
//% blockId=pins_pulse_duration block="pulse duration (µs)"
//% weight=21 shim=pins::pulseDuration
//% weight=21 blockGap=8 shim=pins::pulseDuration
function pulseDuration(): number;
/**
* Returns the duration of a pulse in microseconds
* @param name the pin which measures the pulse
* @param value the value of the pulse (default high)
*/
//% blockId="pins_pulse_in" block="pulse in (µs)|pin %name"
//% weight=20 shim=pins::pulseIn
function pulseIn(name: DigitalPin, value: PulseValue): 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