adding pins.pulseIn API
This commit is contained in:
@ -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
|
||||
|
@ -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;
|
||||
|
11
libs/microbit/shims.d.ts
vendored
11
libs/microbit/shims.d.ts
vendored
@ -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
|
||||
|
Reference in New Issue
Block a user