UNPKG

microbyte

Version:

A wrapper for bluetooth and USB interactivity between browsers and micro:bits

180 lines 7.03 kB
/** * (c) 2023, Center for Computational Thinking and Design at Aarhus University and contributors * * SPDX-License-Identifier: MIT */ /** * References to the Bluetooth Profile UUIDs. */ declare namespace MBSpecs { /** * The UUIDs of the services available on the micro:bit. */ namespace Services { /** * The UUID of the micro:bit's UART service. */ const UART_SERVICE = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"; /** * The micro:bits accelerometer service. */ const ACCEL_SERVICE = "e95d0753-251d-470a-a062-fa1922dfa9a8"; /** * The device information service. Exposes information about manufacturer, vendor, and firmware version. */ const DEVICE_INFO_SERVICE = "0000180a-0000-1000-8000-00805f9b34fb"; /** * Used for controlling the LEDs on the micro:bit. */ const LED_SERVICE = "e95dd91d-251d-470a-a062-fa1922dfa9a8"; /** * The UUID of the micro:bit's IO service. */ const IO_SERVICE = "e95d127b-251d-470a-a062-fa1922dfa9a8"; /** * Service for buttons on the micro:bit. */ const BUTTON_SERVICE = "e95d9882-251d-470a-a062-fa1922dfa9a8"; } /** * The UUIDs of the characteristics available on the micro:bit. */ namespace Characteristics { /** * Characteristic for the A button. */ const BUTTON_A = "e95dda90-251d-470a-a062-fa1922dfa9a8"; /** * Characteristic for the B button. */ const BUTTON_B = "e95dda91-251d-470a-a062-fa1922dfa9a8"; /** * The accelerometer data characteristic. */ const ACCEL_DATA = "e95dca4b-251d-470a-a062-fa1922dfa9a8"; /** * IO data characteristic. Used for controlling IO pins on the micro:bit. */ const IO_DATA = "e95d8d00-251d-470a-a062-fa1922dfa9a8"; /** * Allows the state of any|all LEDs in the 5x5 grid to be set to on or off with a single GATT operation. * * Octet 0, LED Row 1: bit4 bit3 bit2 bit1 bit0 * * Octet 1, LED Row 2: bit4 bit3 bit2 bit1 bit0 * * Octet 2, LED Row 3: bit4 bit3 bit2 bit1 bit0 * * Octet 3, LED Row 4: bit4 bit3 bit2 bit1 bit0 * * Octet 4, LED Row 5: bit4 bit3 bit2 bit1 bit0 */ const LED_MATRIX_STATE = "e95d7b77-251d-470a-a062-fa1922dfa9a8"; /** * The model number of the micro:bit as a string. */ const MODEL_NUMBER = "00002a24-0000-1000-8000-00805f9b34fb"; /** * The UUID of the micro:bit's UART TX characteristic. * Used to listen for data from the micro:bit. */ const UART_DATA_TX = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"; /** * The UUID of the micro:bit's UART RX characteristic. * Used for sending data to the micro:bit. */ const UART_DATA_RX = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"; } namespace USBSpecs { const PRODUCT_ID = 516; const VENDOR_ID = 3368; const FICR = 268435456; const DEVICE_ID_1 = 100; const MICROBIT_NAME_LENGTH = 5; const MICROBIT_NAME_CODE_LETTERS = 5; } /** * The buttons on the micro:bit. */ enum Button { A = 0, B = 1 } type MBVersion = 1 | 2; /** * The state of the buttons on the micro:bit. Becomes LongPressed when the button is held for more than ~2 second. */ type ButtonState = ButtonStates.Released | ButtonStates.Pressed | ButtonStates.LongPressed; /** * The state of the buttons on the micro:bit. Becomes LongPressed when the button is held for more than ~2 second. */ enum ButtonStates { Released = 0, Pressed = 1, LongPressed = 2 } /** * Ordered list of all IO pins. Such as 0, 2, '3V', 'GND', etc. */ const IO_PIN_LAYOUT: IOPin[]; type IOPin = 3 | 0 | 4 | 5 | 6 | 7 | 1 | 8 | 9 | 10 | 11 | 12 | 2 | 13 | 14 | 15 | 16 | 17 | '3V' | 18 | 19 | 20 | 21 | 'GND' | 24; type UsableIOPin = 0 | 1 | 2; /** * Utilities for working with the micro:bit's Bluetooth Profile. */ class Utility { private static CODEBOOK_USB; /** * This is a version of the microbit codebook, where the original codebook is transposed * and the rows are flipped. This gives an easier to use version for the bluetooth pattern * connection. * This could be done programatically, but having it typed out hopefully helps * with the understanding of pattern <-> friendly name conversion */ private static CODEBOOK_BLUETOOTH; /** * Fetches the model number of the micro:bit. * @param {BluetoothRemoteGATTServer} gattServer The GATT server to read from. * @return {Promise<number>} The model number of the micro:bit. 1 for the original, 2 for the new. */ static getModelNumber(gattServer: BluetoothRemoteGATTServer): Promise<MBSpecs.MBVersion>; /** * Converts a micro:bit serial number to it's corresponding friendly name * @param {number} serialNo The serial number of the micro:bit * @returns {string} the name of the micro:bit. */ static serialNumberToName(serialNo: number): string; static messageToDataview(message: string, delimiter?: string): DataView; /** * Converts a pairing pattern to a name. * See guide on microbit names to understand how a pattern is turned into a name * https://support.microbit.org/support/solutions/articles/19000067679-how-to-find-the-name-of-your-micro-bit * @param {boolean[]} pattern The pattern to convert. * @returns {string} The name of the micro:bit. */ static patternToName(pattern: boolean[]): string; static isPairingPattermValid(pattern: boolean[]): boolean; /** * Converts a name to a pairing pattern. * IMPORTANT: Assumes correct microbit name. Not enough error handling for * incorrect names. * @param {string} name The name of the micro:bit * @returns {boolean[]} The pairing pattern */ static nameToPattern(name: string): boolean[]; /** * Converts a binary number represented as an array of numbers into an octet. * @param {number[]} array Bitmap array to convert. * @returns {number} The octet. */ static arrayToOctet(array: number[]): number; /** * Converts a binary number represented as an array of boolean into an octet. * @param {boolean[]} array Bitmap array to convert. * @returns {number} The octet. */ static arrayToOctet(array: boolean[]): number; } } export default MBSpecs; //# sourceMappingURL=MBSpecs.d.ts.map