Microbit More data format
Reference
micro:bit side
MbitMoreCommon.h: コマンドなどの定数類
MbitMoreService.cpp: BLE Service UUIDの定義など
source/drivers/MicroBitAccelerometer.cpp : Gesture on micro:bit
inc/drivers/MicroBitAccelerometer.h: Gesture related constant (ex. MICROBIT_ACCELEROMETER_EVT_NONE)
Scrath 3 extension side
dist/microbitMore.mjs: 拡張機能本体, onNotify`, define const
Bluetooth Channels
code:ble.ino
#define MBIT_MORE_SERVICE "0b50f3e4-607f-4151-9091-7d008d6ffc5c" #define MBIT_MORE_CH_COMMAND "0b500100-607f-4151-9091-7d008d6ffc5c" // R&W(20byte) #define MBIT_MORE_CH_STATE "0b500101-607f-4151-9091-7d008d6ffc5c" // R(7byte) #define MBIT_MORE_CH_MOTION "0b500102-607f-4151-9091-7d008d6ffc5c" // R(18byte) :pitch,roll,accel,and gyro #define MBIT_MORE_CH_PIN_EVENT "0b500110-607f-4151-9091-7d008d6ffc5c" // R&N #define MBIT_MORE_CH_ACTION_EVENT "0b500111-607f-4151-9091-7d008d6ffc5c" // R&N(20byte) :Buttons with timestamp #define MBIT_MORE_CH_ANALOG_IN_P0 "0b500120-607f-4151-9091-7d008d6ffc5c" // R #define MBIT_MORE_CH_ANALOG_IN_P1 "0b500121-607f-4151-9091-7d008d6ffc5c" // R #define MBIT_MORE_CH_ANALOG_IN_P2 "0b500122-607f-4151-9091-7d008d6ffc5c" // R #define MBIT_MORE_CH_MESSAGE "0b500130-607f-4151-9091-7d008d6ffc5c" // R&N : only for v2 Read: micro:bit -> Scratch
Notify: micro:bit -> Scratch
Write: Scratch -> micro:bit
COMMAND_CH(00)
pxt-mbit-more-v2/MbitMoreDevice.cpp
void MbitMoreDevice::onCommandReceived(uint8_t *data, size_t length)
20byte
table:read_format
byte type value mean value
0 uint8 0x01 hardware 0x01:micro:bit v1, 0x02:micro:bit v2
1 uint8 0x02 protocol 0x02: for micro:bit v2(only)
2-19 uint8 0x00 zero padding?
table:command
byte type value mean value
0 (high 3bit) uint8 command 0x01:CMD_PIN, 0x02:CMD_DISPLAY, 0x03:CMD_AUDIO,0x04:CMD_DATA,
1 unit8 delay at CMD_DISPLAY
code:MbitMoreCommon.h
CMD_PIN = 0x01,
CMD_DISPLAY = 0x02,
CMD_AUDIO = 0x03,
CMD_DATA = 0x04,
STATE CH(01)
pxt-mbit-more-v2/MbitMoreDevice.cpp
void MbitMoreDevice::updateState(uint8_t *data)
7byte
table:format
byte type mean note
0 GPIO input 0bit:gpio0, 1:gpio1, 2:gpio2, ...
1,2,3 uint32(LE) GPIO?
4 uint8 lightlevel
5 uint8 temperature(+128)
6 uint8 soundlevel
MOTION_EVENT CH(02)
IMU (accel + gyro) related values. (Read)
pxt-mbit-more-v2/MbitMoreDevice.cpp
void MbitMoreDevice::updateMotion(uint8_t *data)
18byte
table:format
byte type mean
0,1 unit16(LE) pitch
2,3 unit16(LE) roll
4,5 unit16(LE) ax
6,7 unit16(LE) ay
8,9 unit16(LE) az
10,11 unit16(LE) Heading
12,13 unit16(LE) gx
14,15 unit16(LE) gy
16,17 unit16(LE) gz
PIN_EVENT CH(10)
pxt-mbit-more-v2/MbitMoreDevice.cpp
MbitMoreCommand::CMD_PIN
8 or 10byte(?)
code:dist/microbitMore.mjs
var MbitMorePinCommand = {
SET_OUTPUT: 0x01,
SET_PWM: 0x02,
SET_SERVO: 0x03,
SET_PULL: 0x04,
SET_EVENT: 0x05
};
table:format_servo
byte type mean note
0 uint8 Pin command SERVO: 0x03
1 uint8 Pin index
2,3 uint16(LE) angle
4,5 unit16(LE) range 未使用?
6,7 uint16(LE) center 未使用?
code:MbitMoreDevice.cpp
} else if (pinCommand == MbitMorePinCommand::SET_SERVO) {
// angle is read as uint16_t little-endian.
uint16_t angle;
memcpy(&angle, &(data2), 2); // range is read as uint16_t little-endian.
uint16_t range;
memcpy(&range, &(data4), 2); // center is read as uint16_t little-endian.
uint16_t center;
memcpy(¢er, &(data6), 2); if (range == 0) {
uBit.io.pinpinIndex.setServoValue(angle); } else if (center == 0) {
uBit.io.pinpinIndex.setServoValue(angle, range); } else {
uBit.io.pinpinIndex.setServoValue(angle, range, center); }
}
ACTION_EVENT CH(11)
Read & Notify
pxt-mbit-more-v2/MbitMoreDevice.cpp
MbitMoreDevice::onButtonChanged(MicroBitEvent evt)
void MbitMoreDevice::onGestureChanged(MicroBitEvent evt)
20byte
table:common header format
byte type data var value
0 uint8 0x01 BUTTON actionEventType BUTTON:0x01, GESTURE: 0x02
table:button_event
byte type data var value
1,2 uint16(LE) 0x0100 BUTTON A buttonName 1:A,2:B,100:P0,101:P1,102:P2,121:LOGO
3 uint8 eventName 1:DOWN, 2:UP, 3:CLICK, 4:LONG_CLICK,
5:HOLD, 6:DOUBLE_CLICK
4,5,6,7 uint32(LE) Timestamp
8-18 0x00 Zero padding?
19 unit8 0x12 ACTION_EVENT dataFormat 0x10:CONFIG,0x11: PIN_EVENT,0x12:ACTION_EVENT,
0x13:DATA_NUMBER,0x14:DATA_TEXT
table:gesture_event
byte type data mean value
1 uint8 gesture name 1:TILT_UP,2:TILT_DOWN,3:TILT_LEFT,4:TILT_RIGHT,5:FACE_UP,
6:FACE_DOWN, 7:FREEFALL,8:G3,9:G6:10:G8,11:SHAKE
2-5 uint32(LE) Timestamp
19 uint8 0x12 ACTION_EVENT
table:data_number
byte type value mean
0-7 char * 8 label
8-11 float32(LE) number
19 uint8 0x13 DATA_NUMBER
table:data_text
byte type value mean
0-7 char * 8 label
8-18 char * 11 text
19 uint8 0x14 DATA_TEXT
ANALOG_IN_P0 CH(20)
pxt-mbit-more-v2/MbitMoreDevice.cpp
void MbitMoreDevice::updateAnalogIn(uint8_t *data, size_t pinIndex)
table:analog
byte type value
0,1 uint16(LE) analog pin0 input (0-1023)
ANALOG_IN_P1 CH(21)
same as ANALOG_IN_P0 CH(20)
ANALOG_IN_P2 CH(22)
same as ANALOG_IN_P0 CH(20)
MESSAGE CH(30)
table:message
value
0-7 char label
19 uint8 NUMBER: 1,TEXT: 2
table:number
8-11 float(4byte) number
table:text
8-18 char text data
Constants
code:const_MbitMoreDevice.h
#define MBIT_MORE_DATA_LABEL_SIZE 8 #define MBIT_MORE_DATA_CONTENT_SIZE 11 #define MBIT_MORE_DATA_FORMAT_INDEX 19 #define MM_CH_BUFFER_SIZE_NOTIFY 20 code:const_microbitMore.mjs
var MbitMoreSendingDataType = {
NUMBER: 1,
TEXT: 2
};
var BLECommand = {
CMD_CONFIG: 0x00,
CMD_PIN: 0x01,
CMD_DISPLAY: 0x02,
CMD_AUDIO: 0x03,
CMD_DATA: 0x04
};
var MbitMoreDataFormat = {
CONFIG: 0x10,
// not used at this version
PIN_EVENT: 0x11,
ACTION_EVENT: 0x12,
DATA_NUMBER: 0x13,
DATA_TEXT: 0x14
};
Code for timestamp
code:timestamp.ino
uint32_t time = (uint32_t)millis();
// Set TimeStamp (Little Endian)
action3 = (time >> 8) & 0xff; action4 = (time >> 16) & 0xff; action5 = (time >> 24) & 0xff;