pxt-calliope/libs/core/control.cpp

203 lines
8.2 KiB
C++
Raw Normal View History

2016-04-02 06:26:06 +02:00
#include "ksbit.h"
2016-03-30 02:11:17 +02:00
2016-04-02 02:45:18 +02:00
/**
* How to create the event.
*/
enum class EventCreationMode {
/**
* MicroBitEvent is initialised, and no further processing takes place.
*/
CreateOnly = CREATE_ONLY,
/**
* MicroBitEvent is initialised, and its event handlers are immediately fired (not suitable for use in interrupts!).
*/
CreateAndFire = CREATE_AND_FIRE,
};
// note the trailing '_' in names - otherwise we get conflict with the pre-processor
// this trailing underscore is removed by enums.d.ts generation process
// TODO shouldn't these be renamed to something more sensible anyways?
enum EventBusSource {
MICROBIT_ID_BUTTON_A_ = MICROBIT_ID_BUTTON_A,
MICROBIT_ID_BUTTON_B_ = MICROBIT_ID_BUTTON_B,
MICROBIT_ID_BUTTON_AB_ = MICROBIT_ID_BUTTON_AB,
MICROBIT_ID_RADIO_ = MICROBIT_ID_RADIO,
MICROBIT_ID_GESTURE_ = MICROBIT_ID_GESTURE,
MICROBIT_ID_ACCELEROMETER_ = MICROBIT_ID_ACCELEROMETER,
MICROBIT_ID_IO_P0_ = MICROBIT_ID_IO_P0,
MICROBIT_ID_IO_P1_ = MICROBIT_ID_IO_P1,
MICROBIT_ID_IO_P2_ = MICROBIT_ID_IO_P2,
MICROBIT_ID_IO_P3_ = MICROBIT_ID_IO_P3,
MICROBIT_ID_IO_P4_ = MICROBIT_ID_IO_P4,
MICROBIT_ID_IO_P5_ = MICROBIT_ID_IO_P5,
MICROBIT_ID_IO_P6_ = MICROBIT_ID_IO_P6,
MICROBIT_ID_IO_P7_ = MICROBIT_ID_IO_P7,
MICROBIT_ID_IO_P8_ = MICROBIT_ID_IO_P8,
MICROBIT_ID_IO_P9_ = MICROBIT_ID_IO_P9,
MICROBIT_ID_IO_P10_ = MICROBIT_ID_IO_P10,
MICROBIT_ID_IO_P11_ = MICROBIT_ID_IO_P11,
MICROBIT_ID_IO_P12_ = MICROBIT_ID_IO_P12,
MICROBIT_ID_IO_P13_ = MICROBIT_ID_IO_P13,
MICROBIT_ID_IO_P14_ = MICROBIT_ID_IO_P14,
MICROBIT_ID_IO_P15_ = MICROBIT_ID_IO_P15,
MICROBIT_ID_IO_P16_ = MICROBIT_ID_IO_P16,
MICROBIT_ID_IO_P19_ = MICROBIT_ID_IO_P19,
MICROBIT_ID_IO_P20_ = MICROBIT_ID_IO_P20,
MES_DEVICE_INFO_ID_ = MES_DEVICE_INFO_ID,
MES_SIGNAL_STRENGTH_ID_ = MES_SIGNAL_STRENGTH_ID,
MES_DPAD_CONTROLLER_ID_ = MES_DPAD_CONTROLLER_ID,
MES_BROADCAST_GENERAL_ID_ = MES_BROADCAST_GENERAL_ID,
};
enum EventBusValue {
MICROBIT_EVT_ANY_ = MICROBIT_EVT_ANY,
MICROBIT_BUTTON_EVT_CLICK_ = MICROBIT_BUTTON_EVT_CLICK,
MICROBIT_RADIO_EVT_DATAGRAM_ = MICROBIT_RADIO_EVT_DATAGRAM,
MICROBIT_ACCELEROMETER_EVT_DATA_UPDATE_ = MICROBIT_ACCELEROMETER_EVT_DATA_UPDATE,
MES_ALERT_EVT_ALARM1_ = MES_ALERT_EVT_ALARM1,
MES_ALERT_EVT_ALARM2_ = MES_ALERT_EVT_ALARM2,
MES_ALERT_EVT_ALARM3_ = MES_ALERT_EVT_ALARM3,
MES_ALERT_EVT_ALARM4_ = MES_ALERT_EVT_ALARM4,
MES_ALERT_EVT_ALARM5_ = MES_ALERT_EVT_ALARM5,
MES_ALERT_EVT_ALARM6_ = MES_ALERT_EVT_ALARM6,
MES_ALERT_EVT_DISPLAY_TOAST_ = MES_ALERT_EVT_DISPLAY_TOAST,
MES_ALERT_EVT_FIND_MY_PHONE_ = MES_ALERT_EVT_FIND_MY_PHONE,
MES_ALERT_EVT_PLAY_RINGTONE_ = MES_ALERT_EVT_PLAY_RINGTONE,
MES_ALERT_EVT_PLAY_SOUND_ = MES_ALERT_EVT_PLAY_SOUND,
MES_ALERT_EVT_VIBRATE_ = MES_ALERT_EVT_VIBRATE,
MES_CAMERA_EVT_LAUNCH_PHOTO_MODE_ = MES_CAMERA_EVT_LAUNCH_PHOTO_MODE,
MES_CAMERA_EVT_LAUNCH_VIDEO_MODE_ = MES_CAMERA_EVT_LAUNCH_VIDEO_MODE,
MES_CAMERA_EVT_START_VIDEO_CAPTURE_ = MES_CAMERA_EVT_START_VIDEO_CAPTURE,
MES_CAMERA_EVT_STOP_PHOTO_MODE_ = MES_CAMERA_EVT_STOP_PHOTO_MODE,
MES_CAMERA_EVT_STOP_VIDEO_CAPTURE_ = MES_CAMERA_EVT_STOP_VIDEO_CAPTURE,
MES_CAMERA_EVT_STOP_VIDEO_MODE_ = MES_CAMERA_EVT_STOP_VIDEO_MODE,
MES_CAMERA_EVT_TAKE_PHOTO_ = MES_CAMERA_EVT_TAKE_PHOTO,
MES_CAMERA_EVT_TOGGLE_FRONT_REAR_ = MES_CAMERA_EVT_TOGGLE_FRONT_REAR,
MES_DEVICE_DISPLAY_OFF_ = MES_DEVICE_DISPLAY_OFF,
MES_DEVICE_DISPLAY_ON_ = MES_DEVICE_DISPLAY_ON,
MES_DEVICE_GESTURE_DEVICE_SHAKEN_ = MES_DEVICE_GESTURE_DEVICE_SHAKEN,
MES_DEVICE_INCOMING_CALL_ = MES_DEVICE_INCOMING_CALL,
MES_DEVICE_INCOMING_MESSAGE_ = MES_DEVICE_INCOMING_MESSAGE,
MES_DEVICE_ORIENTATION_LANDSCAPE_ = MES_DEVICE_ORIENTATION_LANDSCAPE,
MES_DEVICE_ORIENTATION_PORTRAIT_ = MES_DEVICE_ORIENTATION_PORTRAIT,
MES_DPAD_BUTTON_1_DOWN_ = MES_DPAD_BUTTON_1_DOWN,
MES_DPAD_BUTTON_1_UP_ = MES_DPAD_BUTTON_1_UP,
MES_DPAD_BUTTON_2_DOWN_ = MES_DPAD_BUTTON_2_DOWN,
MES_DPAD_BUTTON_2_UP_ = MES_DPAD_BUTTON_2_UP,
MES_DPAD_BUTTON_3_DOWN_ = MES_DPAD_BUTTON_3_DOWN,
MES_DPAD_BUTTON_3_UP_ = MES_DPAD_BUTTON_3_UP,
MES_DPAD_BUTTON_4_DOWN_ = MES_DPAD_BUTTON_4_DOWN,
MES_DPAD_BUTTON_4_UP_ = MES_DPAD_BUTTON_4_UP,
MES_DPAD_BUTTON_A_DOWN_ = MES_DPAD_BUTTON_A_DOWN,
MES_DPAD_BUTTON_A_UP_ = MES_DPAD_BUTTON_A_UP,
MES_DPAD_BUTTON_B_DOWN_ = MES_DPAD_BUTTON_B_DOWN,
MES_DPAD_BUTTON_B_UP_ = MES_DPAD_BUTTON_B_UP,
MES_DPAD_BUTTON_C_DOWN_ = MES_DPAD_BUTTON_C_DOWN,
MES_DPAD_BUTTON_C_UP_ = MES_DPAD_BUTTON_C_UP,
MES_DPAD_BUTTON_D_DOWN_ = MES_DPAD_BUTTON_D_DOWN,
MES_DPAD_BUTTON_D_UP_ = MES_DPAD_BUTTON_D_UP,
MES_REMOTE_CONTROL_EVT_FORWARD_ = MES_REMOTE_CONTROL_EVT_FORWARD,
MES_REMOTE_CONTROL_EVT_NEXTTRACK_ = MES_REMOTE_CONTROL_EVT_NEXTTRACK,
MES_REMOTE_CONTROL_EVT_PAUSE_ = MES_REMOTE_CONTROL_EVT_PAUSE,
MES_REMOTE_CONTROL_EVT_PLAY_ = MES_REMOTE_CONTROL_EVT_PLAY,
MES_REMOTE_CONTROL_EVT_PREVTRACK_ = MES_REMOTE_CONTROL_EVT_PREVTRACK,
MES_REMOTE_CONTROL_EVT_REWIND_ = MES_REMOTE_CONTROL_EVT_REWIND,
MES_REMOTE_CONTROL_EVT_STOP_ = MES_REMOTE_CONTROL_EVT_STOP,
MES_REMOTE_CONTROL_EVT_VOLUMEDOWN_ = MES_REMOTE_CONTROL_EVT_VOLUMEDOWN,
MES_REMOTE_CONTROL_EVT_VOLUMEUP_ = MES_REMOTE_CONTROL_EVT_VOLUMEUP,
};
//% weight=1 color="#333333"
2016-03-30 02:11:17 +02:00
namespace control {
void fiberDone(void *a)
{
decr((Action)a);
release_fiber();
}
/**
* Schedules code that run in the background.
*/
//% help=control/in-background
//% blockId="control_in_background" block="run in background" blockGap=8
void inBackground(Action a) {
2016-04-02 22:44:29 +02:00
runInBackground(a);
2016-03-30 02:11:17 +02:00
}
/**
* Resets the BBC micro:bit.
*/
2016-08-17 02:04:21 +02:00
//% weight=30 async help=control/reset blockGap=8
2016-03-30 02:11:17 +02:00
//% blockId="control_reset" block="reset"
void reset() {
microbit_reset();
2016-03-30 02:11:17 +02:00
}
2016-04-02 02:45:18 +02:00
2016-08-17 02:04:21 +02:00
/**
* Blocks the current fiber for the given microseconds
* @param micros number of micro-seconds to wait. eg: 4
*/
//% help=control/wait-micros weight=29
//% blockId="control_wait_us" block="wait (µs)%micros"
void waitMicros(int micros) {
wait_us(micros);
}
2016-04-02 02:45:18 +02:00
/**
* Raises an event in the event bus.
* @param src ID of the MicroBit Component that generated the event e.g. MICROBIT_ID_BUTTON_A.
* @param value Component specific code indicating the cause of the event.
2016-04-14 23:29:28 +02:00
* @param mode optional definition of how the event should be processed after construction (default is CREATE_AND_FIRE).
2016-04-02 02:45:18 +02:00
*/
2016-05-17 01:24:44 +02:00
//% weight=21 blockGap=12 blockId="control_raise_event" block="raise event|from source %src=control_event_source_id|with value %value=control_event_value_id" blockExternalInputs=1
2016-04-14 23:29:28 +02:00
//% mode.defl=CREATE_AND_FIRE
2016-04-02 04:55:51 +02:00
void raiseEvent(int src, int value, EventCreationMode mode) {
2016-04-02 04:59:14 +02:00
MicroBitEvent evt(src, value, (MicroBitEventLaunchMode)mode);
2016-04-02 04:55:51 +02:00
}
2016-04-02 02:45:18 +02:00
/**
* Raises an event in the event bus.
*/
2016-05-17 01:24:44 +02:00
//% weight=20 blockGap=8 blockId="control_on_event" block="on event|from %src=control_event_source_id|with value %value=control_event_value_id"
2016-04-18 22:31:05 +02:00
//% blockExternalInputs=1
2016-04-02 04:59:14 +02:00
void onEvent(int src, int value, Action handler) {
2016-04-03 05:47:27 +02:00
registerWithDal(src, value, handler);
2016-04-02 04:59:14 +02:00
}
2016-05-17 01:24:44 +02:00
/**
* Gets the value of the last event executed on the bus
*/
//% blockId=control_event_value" block="event value"
//% weight=18
int eventValue() {
return pxt::lastEvent.value;
}
/**
* Gets the timestamp of the last event executed on the bus
*/
//% blockId=control_event_timestamp" block="event timestamp"
2016-05-17 18:36:01 +02:00
//% weight=19 blockGap=8
2016-05-17 01:24:44 +02:00
int eventTimestamp() {
return pxt::lastEvent.timestamp;
}
2016-04-26 22:43:02 +02:00
/**
* Gets a friendly name for the device derived from the its serial number
*/
2016-04-28 19:46:03 +02:00
//% blockId="control_device_name" block="device name" weight=10 blockGap=8
2016-04-26 22:43:02 +02:00
StringData* deviceName() {
return ManagedString(microbit_friendly_name()).leakData();
}
/**
* Derive a unique, consistent serial number of this device from internal data.
*/
2016-04-28 19:46:03 +02:00
//% blockId="control_device_serial_number" block="device serial number" weight=9
int deviceSerialNumber() {
return microbit_serial_number();
}
2016-03-30 02:11:17 +02:00
}