pxt-calliope/libs/core/pins.cpp

630 lines
22 KiB
C++
Raw Permalink Normal View History

2016-11-30 06:55:37 +01:00
#include "pxt.h"
2016-04-02 06:26:06 +02:00
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
#if MICROBIT_CODAL
#include "Pin.h"
#define PinCompat codal::Pin
#else
#define PinCompat MicroBitPin
#endif
2016-04-02 06:26:06 +02:00
enum class DigitalPin {
2017-01-31 22:36:32 +01:00
P0 = MICROBIT_ID_IO_P12, // edge connector 0
P1 = MICROBIT_ID_IO_P0, // edge connector 1
P2 = MICROBIT_ID_IO_P1, // edge connector 2
P3 = MICROBIT_ID_IO_P16, // edge connector 3
C4 = MICROBIT_ID_IO_P3, // LED matrix C1
C5 = MICROBIT_ID_IO_P4, // LED matrix C2
C6 = MICROBIT_ID_IO_P10, // LED matrix C3
C7 = MICROBIT_ID_IO_P13, // LED matrix C4
C8 = MICROBIT_ID_IO_P14, // LED matrix C5
C9 = MICROBIT_ID_IO_P15, // LED matrix C6
C10 = MICROBIT_ID_IO_P9, // LED matrix C7
C11 = MICROBIT_ID_IO_P7, // LED matrix C8
C12 = MICROBIT_ID_IO_P6, // LED matrix C9
C16 = MICROBIT_ID_IO_P2, // RX
C17 = MICROBIT_ID_IO_P8, // TX
C18 = MICROBIT_ID_IO_P20 // SDA
2016-04-02 06:26:06 +02:00
};
enum class AnalogPin {
P1 = MICROBIT_ID_IO_P0, // edge connector 1
P2 = MICROBIT_ID_IO_P1, // edge connector 2
2017-01-31 22:36:32 +01:00
C4 = MICROBIT_ID_IO_P3, // LED matrix C1
C5 = MICROBIT_ID_IO_P4, // LED matrix C2
C6 = MICROBIT_ID_IO_P10, // LED matrix C3
C16 = MICROBIT_ID_IO_P2, // RX
C17 = MICROBIT_ID_IO_P8, // TX
2017-01-31 22:36:32 +01:00
MIC = MICROBIT_ID_IO_P21 // microphone
2016-04-02 06:26:06 +02:00
};
2016-05-17 01:24:44 +02:00
enum class PulseValue {
//% block=high
2016-05-17 01:24:44 +02:00
High = MICROBIT_PIN_EVT_PULSE_HI,
//% block=low
2016-05-17 01:24:44 +02:00
Low = MICROBIT_PIN_EVT_PULSE_LO
};
2016-06-04 08:15:51 +02:00
enum class PinPullMode {
//% block="down"
PullDown = 0,
//% block="up"
PullUp = 1,
//% block="none"
PullNone = 2
};
2017-01-30 20:19:54 +01:00
enum class PinEventType {
//% block="edge"
Edge = MICROBIT_PIN_EVENT_ON_EDGE,
//% block="pulse"
Pulse = MICROBIT_PIN_EVENT_ON_PULSE,
//% block="touch"
Touch = MICROBIT_PIN_EVENT_ON_TOUCH,
//% block="none"
None = MICROBIT_PIN_EVENT_NONE
};
namespace pxt
{
2016-04-02 06:26:06 +02:00
MicroBitPin *getPin(int id) {
switch (id) {
case MICROBIT_ID_IO_P0: return &uBit.io.P0;
case MICROBIT_ID_IO_P1: return &uBit.io.P1;
case MICROBIT_ID_IO_P2: return &uBit.io.P2;
case MICROBIT_ID_IO_P3: return &uBit.io.P3;
case MICROBIT_ID_IO_P4: return &uBit.io.P4;
case MICROBIT_ID_IO_P5: return &uBit.io.P5;
case MICROBIT_ID_IO_P6: return &uBit.io.P6;
case MICROBIT_ID_IO_P7: return &uBit.io.P7;
2017-01-31 22:36:32 +01:00
case MICROBIT_ID_IO_P8: return &uBit.io.P8;
2016-04-02 06:26:06 +02:00
case MICROBIT_ID_IO_P9: return &uBit.io.P9;
case MICROBIT_ID_IO_P10: return &uBit.io.P10;
case MICROBIT_ID_IO_P11: return &uBit.io.P11;
2017-01-31 22:36:32 +01:00
case MICROBIT_ID_IO_P12: return &uBit.io.P12;
case MICROBIT_ID_IO_P13: return &uBit.io.P13;
case MICROBIT_ID_IO_P14: return &uBit.io.P14;
case MICROBIT_ID_IO_P15: return &uBit.io.P15;
case MICROBIT_ID_IO_P16: return &uBit.io.P16;
2016-04-02 06:26:06 +02:00
case MICROBIT_ID_IO_P19: return &uBit.io.P19;
case MICROBIT_ID_IO_P20: return &uBit.io.P20;
2017-01-31 22:36:32 +01:00
case MICROBIT_ID_IO_P21: return &uBit.io.P21;
#if MICROBIT_CODAL
case 1001: return &uBit.io.usbTx;
case 1002: return &uBit.io.usbRx;
#endif
2016-04-02 06:26:06 +02:00
default: return NULL;
}
}
} // pxt
2016-04-02 06:26:06 +02:00
namespace pins {
#define PINOP(op) \
MicroBitPin *pin = getPin((int)name); \
if (!pin) return; \
pin->op
#define PINREAD(op) \
MicroBitPin *pin = getPin((int)name); \
if (!pin) return 0; \
return pin->op
2016-04-04 02:49:35 +02:00
//%
MicroBitPin *getPinAddress(int id) {
return getPin(id);
}
2016-04-02 06:26:06 +02:00
/**
* Read the specified pin or connector as either 0 or 1
* @param name pin to read from, eg: DigitalPin.P0
2016-04-02 06:26:06 +02:00
*/
2016-04-02 06:27:22 +02:00
//% help=pins/digital-read-pin weight=30
2016-04-02 06:26:06 +02:00
//% blockId=device_get_digital_pin block="digital read|pin %name" blockGap=8
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
//% name.fieldOptions.tooltips="false" name.fieldOptions.width="250"
//% group="Digital"
2016-04-02 06:26:06 +02:00
int digitalReadPin(DigitalPin name) {
PINREAD(getDigitalValue());
}
/**
* Set a pin or connector value to either 0 or 1.
* @param name pin to write to, eg: DigitalPin.P0
2016-04-02 06:26:06 +02:00
* @param value value to set on the pin, 1 eg,0
*/
2016-04-02 06:27:22 +02:00
//% help=pins/digital-write-pin weight=29
2016-04-02 06:26:06 +02:00
//% blockId=device_set_digital_pin block="digital write|pin %name|to %value"
//% value.min=0 value.max=1
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
//% name.fieldOptions.tooltips="false" name.fieldOptions.width="250"
//% group="Digital"
2017-01-30 20:19:54 +01:00
void digitalWritePin(DigitalPin name, int value) {
2016-04-02 06:26:06 +02:00
PINOP(setDigitalValue(value));
}
/**
* Read the connector value as analog, that is, as a value comprised between 0 and 1023.
* @param name pin to write to, eg: AnalogPin.P1
2016-04-02 06:26:06 +02:00
*/
2016-04-02 06:27:22 +02:00
//% help=pins/analog-read-pin weight=25
2017-01-30 20:19:54 +01:00
//% blockId=device_get_analog_pin block="analog read|pin %name" blockGap="8"
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
//% name.fieldOptions.tooltips="false" name.fieldOptions.width="250"
//% group="Analog"
2016-04-02 06:26:06 +02:00
int analogReadPin(AnalogPin name) {
PINREAD(getAnalogValue());
}
/**
* Set the connector value as analog. Value must be comprised between 0 and 1023.
* @param name pin name to write to, eg: AnalogPin.P1
2016-04-02 06:26:06 +02:00
* @param value value to write to the pin between ``0`` and ``1023``. eg:1023,0
*/
2016-04-02 06:27:22 +02:00
//% help=pins/analog-write-pin weight=24
2016-04-02 06:26:06 +02:00
//% blockId=device_set_analog_pin block="analog write|pin %name|to %value" blockGap=8
//% value.min=0 value.max=1023
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
//% name.fieldOptions.tooltips="false" name.fieldOptions.width="250"
//% group="Analog"
2017-01-30 20:19:54 +01:00
void analogWritePin(AnalogPin name, int value) {
2016-04-02 06:26:06 +02:00
PINOP(setAnalogValue(value));
}
/**
* Configure the pulse-width modulation (PWM) period of the analog output in microseconds.
2016-04-02 06:26:06 +02:00
* If this pin is not configured as an analog output (using `analog write pin`), the operation has no effect.
* @param name analog pin to set period to, eg: AnalogPin.P1
2016-04-02 06:26:06 +02:00
* @param micros period in micro seconds. eg:20000
*/
2016-05-25 06:39:57 +02:00
//% help=pins/analog-set-period weight=23 blockGap=8
2017-01-30 20:19:54 +01:00
//% blockId=device_set_analog_period block="analog set period|pin %pin|to (µs)%micros"
//% pin.fieldEditor="gridpicker" pin.fieldOptions.columns=4
//% pin.fieldOptions.tooltips="false"
//% group="Analog"
2017-01-30 20:19:54 +01:00
void analogSetPeriod(AnalogPin name, int micros) {
2016-04-02 06:26:06 +02:00
PINOP(setAnalogPeriodUs(micros));
}
2017-01-30 20:19:54 +01:00
2016-05-17 01:24:44 +02:00
/**
* Configure the pin as a digital input and generate an event when the pin is pulsed either high or low.
* @param name digital pin to register to, eg: DigitalPin.P0
* @param pulse the value of the pulse, eg: PulseValue.High
2016-05-17 01:24:44 +02:00
*/
//% help=pins/on-pulsed weight=22 blockGap=16 advanced=true
2016-05-17 01:24:44 +02:00
//% blockId=pins_on_pulsed block="on|pin %pin|pulsed %pulse"
//% pin.fieldEditor="gridpicker" pin.fieldOptions.columns=4
//% pin.fieldOptions.tooltips="false" pin.fieldOptions.width="250"
//% group="Pulse"
2016-05-17 01:24:44 +02:00
void onPulsed(DigitalPin name, PulseValue pulse, Action body) {
MicroBitPin* pin = getPin((int)name);
if (!pin) return;
2017-01-30 20:19:54 +01:00
pin->eventOn(MICROBIT_PIN_EVENT_ON_PULSE);
2016-05-17 01:24:44 +02:00
registerWithDal((int)name, (int)pulse, body);
}
2017-01-30 20:19:54 +01:00
2016-05-17 01:24:44 +02:00
/**
* Get the duration of the last pulse in microseconds. This function should be called from a ``onPulsed`` handler.
2016-05-17 01:24:44 +02:00
*/
//% help=pins/pulse-duration advanced=true
2016-05-25 06:39:57 +02:00
//% blockId=pins_pulse_duration block="pulse duration (µs)"
2016-08-17 20:18:15 +02:00
//% weight=21 blockGap=8
//% group="Pulse"
2016-05-17 01:24:44 +02:00
int pulseDuration() {
return pxt::lastEvent.timestamp;
}
2016-04-02 06:26:06 +02:00
2016-08-17 20:18:15 +02:00
/**
* Return the duration of a pulse at a pin in microseconds.
* @param name the pin which measures the pulse, eg: DigitalPin.P0
* @param value the value of the pulse, eg: PulseValue.High
* @param maximum duration in microseconds
2017-01-30 20:19:54 +01:00
*/
2016-08-17 20:35:54 +02:00
//% blockId="pins_pulse_in" block="pulse in (µs)|pin %name|pulsed %value"
//% weight=20 advanced=true
//% help=pins/pulse-in
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
//% name.fieldOptions.tooltips="false" name.fieldOptions.width="250"
//% group="Pulse"
2016-08-17 20:35:54 +02:00
int pulseIn(DigitalPin name, PulseValue value, int maxDuration = 2000000) {
2016-08-17 20:18:15 +02:00
MicroBitPin* pin = getPin((int)name);
if (!pin) return 0;
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
#if MICROBIT_CODAL
// set polarity
pin->setPolarity(PulseValue::High == value ? 1 : 0);
// record pulse
int period = pin->getPulseUs(maxDuration);
// timeout
if (DEVICE_CANCELLED == period)
return 0;
// success!
return period;
#else
2016-08-17 20:18:15 +02:00
int pulse = value == PulseValue::High ? 1 : 0;
2017-01-30 20:19:54 +01:00
uint64_t tick = system_timer_current_time_us();
uint64_t maxd = (uint64_t)maxDuration;
2016-08-17 20:35:54 +02:00
while(pin->getDigitalValue() != pulse) {
if(system_timer_current_time_us() - tick > maxd)
2017-01-30 20:19:54 +01:00
return 0;
2016-08-17 20:35:54 +02:00
}
2016-08-17 20:18:15 +02:00
2017-01-30 20:19:54 +01:00
uint64_t start = system_timer_current_time_us();
2016-08-17 20:35:54 +02:00
while(pin->getDigitalValue() == pulse) {
if(system_timer_current_time_us() - tick > maxd)
2017-01-30 20:19:54 +01:00
return 0;
}
uint64_t end = system_timer_current_time_us();
return end - start;
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
#endif
2016-08-17 20:18:15 +02:00
}
// TODO FIX THIS IN THE DAL!
inline void fixMotorIssue(AnalogPin name) {
NRF_TIMER2->SHORTS = TIMER_SHORTS_COMPARE3_CLEAR_Msk;
NRF_TIMER2->INTENCLR = TIMER_INTENCLR_COMPARE3_Msk;
NRF_TIMER2->PRESCALER = 4;
NRF_TIMER2->CC[3] = 20000;
NRF_TIMER2->TASKS_START = 1;
NRF_TIMER2->EVENTS_COMPARE[3] = 0;
PINOP(getDigitalValue());
}
2016-04-02 06:26:06 +02:00
/**
* Write 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, eg: AnalogPin.P1
2016-04-02 06:26:06 +02:00
* @param value angle or rotation speed, eg:180,90,0
*/
2016-04-02 06:27:22 +02:00
//% help=pins/servo-write-pin weight=20
2016-04-02 06:26:06 +02:00
//% blockId=device_set_servo_pin block="servo write|pin %name|to %value" blockGap=8
2016-11-01 16:16:03 +01:00
//% parts=microservo trackArgs=0
//% value.min=0 value.max=180
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
//% name.fieldOptions.tooltips="false" name.fieldOptions.width="250"
//% group="Servo"
2017-01-30 20:19:54 +01:00
void servoWritePin(AnalogPin name, int value) {
2016-04-02 06:26:06 +02:00
PINOP(setServoValue(value));
}
/**
* Specifies that a continuous servo is connected.
*/
//%
//% group="Servo"
void servoSetContinuous(AnalogPin name, bool value) {
// handled in simulator
}
2016-04-02 06:26:06 +02:00
/**
* Configure the IO pin as an analog/pwm output and set a pulse width. The period is 20 ms period and the pulse width is set based on the value given in **microseconds** or `1/1000` milliseconds.
2016-04-02 06:26:06 +02:00
* @param name pin name
* @param micros pulse duration in micro seconds, eg:1500
*/
2016-08-10 22:10:40 +02:00
//% help=pins/servo-set-pulse weight=19
2016-04-02 06:26:06 +02:00
//% blockId=device_set_servo_pulse block="servo set pulse|pin %value|to (µs) %micros"
//% value.fieldEditor="gridpicker" value.fieldOptions.columns=4
//% value.fieldOptions.tooltips="false" value.fieldOptions.width="250"
//% group="Servo"
2017-01-30 20:19:54 +01:00
void servoSetPulse(AnalogPin name, int micros) {
fixMotorIssue(name);
2016-04-02 06:26:06 +02:00
PINOP(setServoPulseUs(micros));
}
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
PinCompat* pitchPin = NULL;
PinCompat* pitchPin2 = NULL;
uint8_t pitchVolume = 0xff;
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
bool analogTonePlaying = false;
2016-04-02 06:26:06 +02:00
/**
* Set the pin used when using analog pitch or music.
* @param name pin to modulate pitch from
2016-04-02 06:26:06 +02:00
*/
2017-01-30 20:19:54 +01:00
//% blockId=device_analog_set_pitch_pin block="analog set pitch pin %name"
//% help=pins/analog-set-pitch-pin weight=3 advanced=true
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
//% name.fieldOptions.tooltips="false" name.fieldOptions.width="250"
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
//% blockHidden=true
//% group="Pitch"
2017-01-30 20:19:54 +01:00
void analogSetPitchPin(AnalogPin name) {
pitchPin = getPin((int)name);
pitchPin2 = NULL;
}
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
void pinAnalogSetPitch(PinCompat* pin, int frequency, int ms) {
if (frequency <= 0 || pitchVolume == 0) {
pin->setAnalogValue(0);
} else {
int v = 1 << (pitchVolume >> 5);
pin->setAnalogValue(v);
pin->setAnalogPeriodUs(1000000/frequency);
}
}
/**
* Sets the volume on the pitch pin
* @param volume the intensity of the sound from 0..255
*/
//% blockId=device_analog_set_pitch_volume block="analog set pitch volume $volume"
//% help=pins/analog-set-pitch-volume weight=3 advanced=true
//% volume.min=0 volume.max=255
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
//% blockHidden=true
//% group="Pitch"
void analogSetPitchVolume(int volume) {
pitchVolume = max(0, min(0xff, volume));
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
if (analogTonePlaying) {
int v = pitchVolume == 0 ? 0 : 1 << (pitchVolume >> 5);
if (NULL != pitchPin)
pitchPin->setAnalogValue(v);
}
}
/**
* Gets the volume the pitch pin from 0..255
*/
//% blockId=device_analog_pitch_volume block="analog pitch volume"
//% help=pins/analog-pitch-volume weight=3 advanced=true
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
//% blockHidden=true
//% group="Pitch"
int analogPitchVolume() {
return pitchVolume;
}
2016-04-02 06:26:06 +02:00
/**
* Emit a plse-width modulation (PWM) signal to the current pitch pin. Use `analog set pitch pin` to define the pitch pin.
* @param frequency frequency to modulate in Hz.
* @param ms duration of the pitch in milli seconds.
*/
2017-01-30 20:19:54 +01:00
//% blockId=device_analog_pitch block="analog pitch %frequency|for (ms) %ms"
//% help=pins/analog-pitch weight=4 async advanced=true blockGap=8
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
//% blockHidden=true
//% group="Pitch"
2017-01-30 20:19:54 +01:00
void analogPitch(int frequency, int ms) {
// init pins if needed
if (NULL == pitchPin) {
pitchPin = getPin((int)DigitalPin::P1);
#ifdef SOUND_MIRROR_EXTENSION
pitchPin2 = &SOUND_MIRROR_EXTENSION;
#endif
}
// set pitch
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
analogTonePlaying = true;
if (NULL != pitchPin)
pinAnalogSetPitch(pitchPin, frequency, ms);
if (NULL != pitchPin2)
pinAnalogSetPitch(pitchPin2, frequency, ms);
// clear pitch
if (ms > 0) {
fiber_sleep(ms);
if (NULL != pitchPin)
pitchPin->setAnalogValue(0);
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
analogTonePlaying = false;
// causes issues with v2 DMA.
// fiber_sleep(5);
}
2016-04-02 06:26:06 +02:00
}
2017-01-30 20:19:54 +01:00
2016-06-04 08:15:51 +02:00
/**
* Configure the pull directiion of of a pin.
* @param name pin to set the pull mode on, eg: DigitalPin.P0
* @param pull one of the mbed pull configurations, eg: PinPullMode.PullUp
2016-06-04 08:15:51 +02:00
*/
//% help=pins/set-pull weight=3 advanced=true
2016-06-04 08:15:51 +02:00
//% blockId=device_set_pull block="set pull|pin %pin|to %pull"
//% pin.fieldEditor="gridpicker" pin.fieldOptions.columns=4
//% pin.fieldOptions.tooltips="false" pin.fieldOptions.width="250"
//% group="Digital"
2016-06-04 08:15:51 +02:00
void setPull(DigitalPin name, PinPullMode pull) {
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
#if MICROBIT_CODAL
codal::PullMode m = pull == PinPullMode::PullDown
? codal::PullMode::Down
: pull == PinPullMode::PullUp ? codal::PullMode::Up
: codal::PullMode::None;
PINOP(setPull(m));
#else
2017-01-30 20:19:54 +01:00
PinMode m = pull == PinPullMode::PullDown
2016-06-04 08:15:51 +02:00
? PinMode::PullDown
2017-01-30 20:19:54 +01:00
: pull == PinPullMode::PullUp ? PinMode::PullUp
2016-06-04 08:15:51 +02:00
: PinMode::PullNone;
PINOP(setPull(m));
#endif
2016-06-04 08:15:51 +02:00
}
2017-01-30 20:19:54 +01:00
/**
* Configure the events emitted by this pin. Events can be subscribed to
2017-01-30 20:19:54 +01:00
* using ``control.onEvent()``.
* @param name pin to set the event mode on, eg: DigitalPin.P0
2017-01-30 20:19:54 +01:00
* @param type the type of events for this pin to emit, eg: PinEventType.Edge
*/
//% help=pins/set-events weight=4 advanced=true
//% blockId=device_set_pin_events block="set pin %pin|to emit %type|events"
//% pin.fieldEditor="gridpicker" pin.fieldOptions.columns=4
//% pin.fieldOptions.tooltips="false" pin.fieldOptions.width="250"
//% group="Digital"
2017-01-30 20:19:54 +01:00
void setEvents(DigitalPin name, PinEventType type) {
getPin((int)name)->eventOn((int)type);
}
2016-04-04 01:52:57 +02:00
/**
* Create a new zero-initialized buffer.
* @param size number of bytes in the buffer
*/
//%
Buffer createBuffer(int size)
{
return mkBuffer(NULL, size);
2016-04-04 01:52:57 +02:00
}
2016-04-02 22:44:29 +02:00
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
/**
* Set the matrix width for Neopixel strip (already assigned to a pin).
* Should be used in conjunction with `set matrix width` from Neopixel package.
* @param name pin of Neopixel strip, eg: DigitalPin.P1
* @param value width of matrix (at least ``2``)
*/
//% help=pins/neopixel-matrix-width weight=3 advanced=true
//% blockId=pin_neopixel_matrix_width block="neopixel matrix width|pin %pin %width" blockGap=8
//% pin.fieldEditor="gridpicker" pin.fieldOptions.columns=4
//% pin.fieldOptions.tooltips="false" pin.fieldOptions.width="250"
//% width.defl=5 width.min=2
//% blockHidden=true
void setMatrixWidth(DigitalPin pin, int width) {}
#if MICROBIT_CODAL
#define BUFFER_TYPE uint8_t*
#else
#define BUFFER_TYPE char*
#endif
/**
* Read `size` bytes from a 7-bit I2C `address`.
*/
2016-04-05 04:11:33 +02:00
//%
//% group="i2c"
2016-04-05 04:11:33 +02:00
Buffer i2cReadBuffer(int address, int size, bool repeat = false)
2016-04-02 22:44:29 +02:00
{
Buffer buf = createBuffer(size);
uBit.i2c.read(address << 1, (BUFFER_TYPE)buf->data, size, repeat);
return buf;
2016-04-02 22:44:29 +02:00
}
2017-01-30 20:19:54 +01:00
/**
* Write bytes to a 7-bit I2C `address`.
*/
2016-04-05 04:11:33 +02:00
//%
//% group="i2c"
int i2cWriteBuffer(int address, Buffer buf, bool repeat = false)
2016-04-02 22:44:29 +02:00
{
return uBit.i2c.write(address << 1, (BUFFER_TYPE)buf->data, buf->length, repeat);
2016-04-02 22:44:29 +02:00
}
2016-08-11 08:26:58 +02:00
SPI* spi = NULL;
SPI* allocSPI() {
if (NULL == spi)
2016-08-11 08:26:58 +02:00
spi = new SPI(MOSI, MISO, SCK);
return spi;
}
/**
* Write to the SPI slave and return the response
* @param value Data to be sent to the SPI slave
*/
//% help=pins/spi-write weight=5 advanced=true
2016-08-11 08:26:58 +02:00
//% blockId=spi_write block="spi write %value"
//% group="spi"
2016-08-11 08:26:58 +02:00
int spiWrite(int value) {
auto p = allocSPI();
return p->write(value);
}
2017-01-30 20:19:54 +01:00
/**
* Write to and read from the SPI slave at the same time
* @param command Data to be sent to the SPI slave (can be null)
* @param response Data received from the SPI slave (can be null)
*/
//% help=pins/spi-transfer argsNullable
//% group="spi"
void spiTransfer(Buffer command, Buffer response) {
if (!command && !response)
target_panic(PANIC_INVALID_ARGUMENT);
if (command && response && command->length != response->length)
target_panic(PANIC_INVALID_ARGUMENT);
auto p = allocSPI();
unsigned len = command ? command->length : response->length;
#if MICROBIT_CODAL
p->transfer(command ? command->data : NULL, command ? len : 0,
response ? response->data : NULL, response ? len : 0);
#else
for (unsigned i = 0; i < len; ++i) {
int v = p->write(command ? command->data[i] : 0);
if (response) response->data[i] = v;
}
#endif
}
/**
* Set the SPI frequency
* @param frequency the clock frequency, eg: 1000000
*/
//% help=pins/spi-frequency weight=4 advanced=true
//% blockId=spi_frequency block="spi frequency %frequency"
//% group="spi"
void spiFrequency(int frequency) {
auto p = allocSPI();
p->frequency(frequency);
}
/**
* Set the SPI bits and mode
* @param bits the number of bits, eg: 8
* @param mode the mode, eg: 3
*/
//% help=pins/spi-format weight=3 advanced=true
//% blockId=spi_format block="spi format|bits %bits|mode %mode"
//% group="spi"
void spiFormat(int bits, int mode) {
auto p = allocSPI();
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
p->format(bits, mode);
}
#if MICROBIT_CODAL
#define PIN_ARG(pin) *(getPin((int)(pin)))
#else
#define PIN_ARG(pin) (getPin((int)(pin)))->name
#endif
/**
* Set the MOSI, MISO, SCK pins used by the SPI connection
*
*/
//% help=pins/spi-pins weight=2 advanced=true
//% blockId=spi_pins block="spi set pins|MOSI %mosi|MISO %miso|SCK %sck"
//% mosi.fieldEditor="gridpicker" mosi.fieldOptions.columns=4
//% mosi.fieldOptions.tooltips="false" mosi.fieldOptions.width="250"
//% miso.fieldEditor="gridpicker" miso.fieldOptions.columns=4
//% miso.fieldOptions.tooltips="false" miso.fieldOptions.width="250"
//% sck.fieldEditor="gridpicker" sck.fieldOptions.columns=4
//% sck.fieldOptions.tooltips="false" sck.fieldOptions.width="250"
//% group="spi"
void spiPins(DigitalPin mosi, DigitalPin miso, DigitalPin sck) {
if (NULL != spi) {
delete spi;
spi = NULL;
}
spi = new SPI(PIN_ARG(mosi), PIN_ARG(miso), PIN_ARG(sck));
}
/**
* Mounts a push button on the given pin
*/
//% help=pins/push-button advanced=true
//% group="Digital"
void pushButton(DigitalPin pin) {
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
new MicroBitButton((PinName)getPin((int)(pin))->name, (int)pin, MICROBIT_BUTTON_ALL_EVENTS, PinMode::PullUp);
}
/**
* Set the pin used when producing sounds and melodies. Default is P0.
* @param name pin to modulate pitch from
*/
//% blockId=pin_set_audio_pin block="set audio pin $name"
//% help=pins/set-audio-pin weight=3
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
//% name.fieldOptions.tooltips="false" name.fieldOptions.width="250"
//% weight=1
//% blockHidden=true
//% group="Pitch"
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
void setAudioPin(AnalogPin name) {
#if MICROBIT_CODAL
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
uBit.audio.setPin(*getPin((int)name));
uBit.audio.setPinEnabled(true);
#else
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
// v1 behavior
pins::analogSetPitchPin(name);
#endif
}
V4 updates for beta testing (#147) * change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin * Fix Sound and replace AnalogPin.P0 * remove approved extensions * V4 Updates from remote Repo * locales * add storage functions * fix storage functions * fix int/float values * decrease decimal precision * reorder blocks * Update BLE Settings and Storage Blocks * Fetch MicroBit changes up to v4.0.18 * Update timing for LED Matrix usage * use 32kb ram (mini v2) * resize gatt table * Revert "use 32kb ram (mini v2)" This reverts commit 4b15592f0f0eacfc1a2e826f34682bc589faf16e. * fix missleading indentation * add support for 32kb and 16kb ram * only MIT extensions in preferredRepos * remove extensions without MIT License file * add updated extensions * add extensions with MIT license Co-authored-by: Juri <gitkraken@juriwolf.de> Co-authored-by: Juri <info@juriwolf.de>
2022-03-22 17:36:19 +01:00
}