2017-02-28 09:09:22 +03:00
|
|
|
#include "pxt.h"
|
|
|
|
|
2017-10-27 08:57:53 +03:00
|
|
|
/*
|
|
|
|
|
2017-11-08 02:09:49 +03:00
|
|
|
These button events need CODAL work.
|
2017-10-27 08:57:53 +03:00
|
|
|
|
|
|
|
// % block="double click"
|
|
|
|
DoubleClick = DEVICE_BUTTON_EVT_DOUBLE_CLICK,
|
|
|
|
|
|
|
|
// % block="hold"
|
|
|
|
Hold = DEVICE_BUTTON_EVT_HOLD
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2018-12-01 04:47:45 +03:00
|
|
|
* User interaction on buttons
|
|
|
|
*/
|
2017-10-27 08:57:53 +03:00
|
|
|
enum class ButtonEvent {
|
|
|
|
//% block="click"
|
|
|
|
Click = DEVICE_BUTTON_EVT_CLICK,
|
|
|
|
//% block="long click"
|
|
|
|
LongClick = DEVICE_BUTTON_EVT_LONG_CLICK,
|
|
|
|
//% block="up"
|
|
|
|
Up = DEVICE_BUTTON_EVT_UP,
|
|
|
|
//% block="down"
|
|
|
|
Down = DEVICE_BUTTON_EVT_DOWN
|
|
|
|
};
|
|
|
|
|
2019-11-27 06:10:29 +03:00
|
|
|
namespace pxt {
|
2017-10-23 23:48:41 +03:00
|
|
|
//%
|
2020-01-24 03:22:14 +03:00
|
|
|
Button *getButtonByPin(int pin, int flags) {
|
2019-04-29 17:39:41 +03:00
|
|
|
unsigned highflags = (unsigned)pin >> 16;
|
|
|
|
if (highflags & 0xff)
|
|
|
|
flags = highflags & 0xff;
|
|
|
|
|
2020-01-24 03:22:14 +03:00
|
|
|
pin &= 0xffff;
|
|
|
|
|
2017-10-23 23:48:41 +03:00
|
|
|
auto cpid = DEVICE_ID_FIRST_BUTTON + pin;
|
2020-01-24 03:22:14 +03:00
|
|
|
auto btn = (Button *)lookupComponent(cpid);
|
2017-10-23 23:48:41 +03:00
|
|
|
if (btn == NULL) {
|
|
|
|
auto pull = PullMode::None;
|
|
|
|
if ((flags & 0xf0) == 0x10)
|
|
|
|
pull = PullMode::Down;
|
|
|
|
else if ((flags & 0xf0) == 0x20)
|
|
|
|
pull = PullMode::Up;
|
2019-04-29 17:39:41 +03:00
|
|
|
else if ((flags & 0xf0) == 0x30)
|
2017-10-23 23:48:41 +03:00
|
|
|
pull = PullMode::None;
|
|
|
|
else
|
2018-11-13 02:34:52 +03:00
|
|
|
oops(3);
|
|
|
|
// GCTODO
|
2020-01-24 03:22:14 +03:00
|
|
|
btn = new Button(*lookupPin(pin), cpid, DEVICE_BUTTON_ALL_EVENTS,
|
2019-11-27 06:10:29 +03:00
|
|
|
(ButtonPolarity)(flags & 0xf), pull);
|
2017-10-23 23:48:41 +03:00
|
|
|
}
|
|
|
|
return btn;
|
|
|
|
}
|
2017-02-28 09:09:22 +03:00
|
|
|
|
2018-06-12 18:14:21 +03:00
|
|
|
//%
|
2020-01-24 03:22:14 +03:00
|
|
|
Button *getButtonByPinCfg(int key, int flags) {
|
2018-06-12 18:14:21 +03:00
|
|
|
int pin = getConfig(key);
|
2018-12-01 04:47:45 +03:00
|
|
|
if (pin == -1)
|
2021-10-20 01:13:13 +03:00
|
|
|
soft_panic(PANIC_NO_SUCH_CONFIG);
|
2018-06-12 18:14:21 +03:00
|
|
|
return getButtonByPin(pin, flags);
|
|
|
|
}
|
|
|
|
|
2017-10-23 23:48:41 +03:00
|
|
|
MultiButton *getMultiButton(int id, int pinA, int pinB, int flags) {
|
|
|
|
auto btn = (MultiButton *)lookupComponent(id);
|
|
|
|
if (btn == NULL) {
|
|
|
|
auto bA = getButtonByPin(pinA, flags);
|
|
|
|
auto bB = getButtonByPin(pinB, flags);
|
2018-11-13 02:34:52 +03:00
|
|
|
// GCTODO
|
2020-01-24 03:22:14 +03:00
|
|
|
btn = new MultiButton(bA->id, bB->id, id);
|
2017-02-28 09:09:22 +03:00
|
|
|
|
2017-10-23 23:48:41 +03:00
|
|
|
// A user has registered to receive events from the buttonAB multibutton.
|
|
|
|
// Disable click events from being generated by ButtonA and ButtonB, and defer the
|
|
|
|
// control of this to the multibutton handler.
|
|
|
|
//
|
|
|
|
// This way, buttons look independent unless a buttonAB is requested, at which
|
|
|
|
// point button A+B clicks can be correclty handled without breaking
|
|
|
|
// causal ordering.
|
|
|
|
bA->setEventConfiguration(DEVICE_BUTTON_SIMPLE_EVENTS);
|
|
|
|
bB->setEventConfiguration(DEVICE_BUTTON_SIMPLE_EVENTS);
|
|
|
|
btn->setEventConfiguration(DEVICE_BUTTON_ALL_EVENTS);
|
|
|
|
}
|
|
|
|
return btn;
|
|
|
|
}
|
2019-11-27 06:10:29 +03:00
|
|
|
|
|
|
|
// This is for A, B, and AB
|
|
|
|
//%
|
|
|
|
AbstractButton *getButton(int id) {
|
|
|
|
int pa = getConfig(CFG_PIN_BTN_A);
|
|
|
|
int pb = getConfig(CFG_PIN_BTN_B);
|
|
|
|
int flags = getConfig(CFG_DEFAULT_BUTTON_MODE, BUTTON_ACTIVE_LOW_PULL_UP);
|
|
|
|
if (id == 0)
|
|
|
|
return getButtonByPin(pa, flags);
|
|
|
|
else if (id == 1)
|
|
|
|
return getButtonByPin(pb, flags);
|
|
|
|
else if (id == 2)
|
|
|
|
return getMultiButton(DEVICE_ID_BUTTON_AB, pa, pb, flags);
|
|
|
|
else {
|
2021-10-20 01:13:13 +03:00
|
|
|
soft_panic(PANIC_INVALID_ARGUMENT);
|
2019-11-27 06:10:29 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-01 04:47:45 +03:00
|
|
|
} // namespace pxt
|
2017-10-23 23:48:41 +03:00
|
|
|
|
2018-10-05 02:21:32 +03:00
|
|
|
namespace DigitalInOutPinMethods {
|
2017-10-27 08:57:53 +03:00
|
|
|
|
2017-10-23 23:48:41 +03:00
|
|
|
/**
|
|
|
|
* Get the push button (connected to GND) for given pin
|
|
|
|
*/
|
2017-02-28 09:09:22 +03:00
|
|
|
//%
|
2018-10-05 02:21:32 +03:00
|
|
|
Button_ pushButton(DigitalInOutPin pin) {
|
2017-10-23 23:48:41 +03:00
|
|
|
return pxt::getButtonByPin(pin->name, BUTTON_ACTIVE_LOW_PULL_UP);
|
2017-02-28 09:09:22 +03:00
|
|
|
}
|
2017-10-23 23:48:41 +03:00
|
|
|
|
2018-12-01 04:47:45 +03:00
|
|
|
} // namespace DigitalInOutPinMethods
|
2017-10-23 23:48:41 +03:00
|
|
|
|
2017-02-28 09:09:22 +03:00
|
|
|
//% noRefCounting fixedInstances
|
|
|
|
namespace ButtonMethods {
|
2018-12-01 04:47:45 +03:00
|
|
|
/**
|
|
|
|
* Do something when a button (`A`, `B` or both `A` + `B`) is clicked, double clicked, etc...
|
|
|
|
* @param button the button that needs to be clicked or used
|
|
|
|
* @param event the kind of button gesture that needs to be detected
|
|
|
|
* @param body code to run when the event is raised
|
|
|
|
*/
|
|
|
|
//% help=input/button/on-event
|
|
|
|
//% blockId=buttonEvent block="on %button|%event"
|
|
|
|
//% blockNamespace=input
|
|
|
|
//% button.fieldEditor="gridpicker"
|
|
|
|
//% button.fieldOptions.width=220
|
|
|
|
//% button.fieldOptions.columns=3
|
|
|
|
//% weight=96 blockGap=12
|
|
|
|
//% trackArgs=0
|
|
|
|
void onEvent(Button_ button, ButtonEvent ev, Action body) {
|
|
|
|
registerWithDal(button->id, (int)ev, body);
|
|
|
|
}
|
2017-02-28 09:09:22 +03:00
|
|
|
|
|
|
|
/**
|
2017-05-16 07:27:06 +03:00
|
|
|
* Check if a button is pressed or not.
|
2017-02-28 09:09:22 +03:00
|
|
|
* @param button the button to query the request
|
|
|
|
*/
|
2017-10-14 01:19:02 +03:00
|
|
|
//% help=input/button/is-pressed
|
2017-06-08 00:43:44 +03:00
|
|
|
//% block="%button|is pressed"
|
2017-02-28 09:09:22 +03:00
|
|
|
//% blockId=buttonIsPressed
|
|
|
|
//% blockNamespace=input
|
2017-05-11 10:09:22 +03:00
|
|
|
//% button.fieldEditor="gridpicker"
|
|
|
|
//% button.fieldOptions.width=220
|
|
|
|
//% button.fieldOptions.columns=3
|
2017-10-14 01:19:02 +03:00
|
|
|
//% weight=50 blockGap=8
|
2017-10-26 09:47:52 +03:00
|
|
|
//% trackArgs=0
|
2017-08-05 00:58:55 +03:00
|
|
|
bool isPressed(Button_ button) {
|
2017-02-28 09:09:22 +03:00
|
|
|
return button->isPressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-16 07:27:06 +03:00
|
|
|
* See if the button was pressed again since the last time you checked.
|
2017-02-28 09:09:22 +03:00
|
|
|
* @param button the button to query the request
|
|
|
|
*/
|
2017-10-14 01:19:02 +03:00
|
|
|
//% help=input/button/was-pressed
|
2017-06-08 00:43:44 +03:00
|
|
|
//% block="%button|was pressed"
|
2017-02-28 09:09:22 +03:00
|
|
|
//% blockId=buttonWasPressed
|
2017-10-14 01:19:02 +03:00
|
|
|
//% blockNamespace=input
|
2017-05-11 10:09:22 +03:00
|
|
|
//% button.fieldEditor="gridpicker"
|
|
|
|
//% button.fieldOptions.width=220
|
|
|
|
//% button.fieldOptions.columns=3
|
2017-10-14 01:19:02 +03:00
|
|
|
//% group="More" weight=46 blockGap=8
|
2017-10-26 09:47:52 +03:00
|
|
|
//% trackArgs=0
|
2017-08-05 00:58:55 +03:00
|
|
|
bool wasPressed(Button_ button) {
|
2017-02-28 09:09:22 +03:00
|
|
|
return button->wasPressed();
|
|
|
|
}
|
2018-03-16 06:26:12 +03:00
|
|
|
|
|
|
|
/**
|
2019-11-06 16:31:20 +03:00
|
|
|
* Gets the component identifier for the button
|
2018-12-01 04:47:45 +03:00
|
|
|
*/
|
2018-03-16 06:26:12 +03:00
|
|
|
//%
|
|
|
|
int id(Button_ button) {
|
|
|
|
return button->id;
|
|
|
|
}
|
|
|
|
|
2018-12-01 04:47:45 +03:00
|
|
|
} // namespace ButtonMethods
|