import { BackgroundErrorData, ConnectionStatusChange, DeviceConnection, FlashDataSource, FlashOptions } from "../device.js"; import { Logging } from "../logging.js"; import { AccelerometerData, ButtonActionData, ButtonData, GestureData, LedMatrix, MagnetometerData, MicrobitEventData, PinData, PinValue, TemperatureData, UartData } from "../service-events.js"; import { DeviceBondState } from "./device-bond-state.js"; export interface MicrobitBluetoothConnectionOptions { logging?: Logging; deviceBondState?: DeviceBondState; } /** * A Bluetooth connection to a micro:bit device. * * Events and methods rely on specific BLE services being present in the * micro:bit's firmware. Which services are available depends on the firmware * build for C++ and for a MakeCode program depends on the service blocks added * from the Bluetooth extension. * * The table below maps each event and method to the BLE service it requires. * If a service is not present, the event will silently not fire (no error is * raised) and methods that depend on it will throw. * * ### Accelerometer Service * - `accelerometerdatachanged` event * - {@link getAccelerometerData}, {@link getAccelerometerPeriod}, {@link setAccelerometerPeriod} * * ### Button Service * - `buttonachanged`, `buttonbchanged` events * * ### Event Service * - `gesturechanged` event — also requires the accelerometer hardware to be * active; this happens automatically if the Accelerometer Service is present * - `buttonaaction`, `buttonbaction`, `buttonabaction` events * - `logoaction` event (V2 only) * - `microbitevent` event * - {@link subscribeToEvent}, {@link sendEvent} * * ### IO Pin Service * - `pinchanged` event * - {@link getAnalogPins}, {@link setAnalogPins} * - {@link getInputPins}, {@link setInputPins} * - {@link readPins}, {@link writePins}, {@link writePinPwm} * * ### LED Service * - {@link setLedText}, {@link getLedScrollingDelay}, {@link setLedScrollingDelay} * - {@link getLedMatrix}, {@link setLedMatrix} * * ### Magnetometer Service * - `magnetometerdatachanged` event * - {@link getMagnetometerData}, {@link getMagnetometerBearing} * - {@link getMagnetometerPeriod}, {@link setMagnetometerPeriod} * - {@link triggerMagnetometerCalibration} * * ### Temperature Service * - `temperaturechanged` event * - {@link getTemperature}, {@link getTemperaturePeriod}, {@link setTemperaturePeriod} * * ### UART Service * - `uartdata` event * - {@link uartWrite} */ export interface MicrobitBluetoothConnection extends DeviceConnection { readonly type: "bluetooth"; addEventListener(type: "status", listener: (data: ConnectionStatusChange) => void): void; addEventListener(type: "backgrounderror", listener: (data: BackgroundErrorData) => void): void; addEventListener(type: "beforerequestdevice", listener: () => void): void; addEventListener(type: "afterrequestdevice", listener: () => void): void; addEventListener(type: "flash", listener: () => void): void; /** Requires: Accelerometer Service. */ addEventListener(type: "accelerometerdatachanged", listener: (data: AccelerometerData) => void): void; /** Requires: Button Service. */ addEventListener(type: "buttonachanged" | "buttonbchanged", listener: (data: ButtonData) => void): void; /** Requires: Magnetometer Service. */ addEventListener(type: "magnetometerdatachanged", listener: (data: MagnetometerData) => void): void; /** Requires: Temperature Service. */ addEventListener(type: "temperaturechanged", listener: (data: TemperatureData) => void): void; /** Requires: IO Pin Service. */ addEventListener(type: "pinchanged", listener: (data: PinData) => void): void; /** Requires: Event Service. The accelerometer hardware must also be active (automatic if the Accelerometer Service is present). */ addEventListener(type: "gesturechanged", listener: (data: GestureData) => void): void; /** Requires: Event Service. */ addEventListener(type: "buttonaaction" | "buttonbaction" | "buttonabaction", listener: (data: ButtonActionData) => void): void; /** Requires: Event Service. V2 only. */ addEventListener(type: "logoaction", listener: (data: ButtonActionData) => void): void; /** * Requires: Event Service. Receives raw micro:bit message bus events * registered via {@link subscribeToEvent}. Higher-level events supported * by the event service, like `gesturechanged` and button actions, are not * included here unless you subscribe to them using {@link subscribeToEvent}. */ addEventListener(type: "microbitevent", listener: (data: MicrobitEventData) => void): void; /** Requires: UART Service. */ addEventListener(type: "uartdata", listener: (data: UartData) => void): void; removeEventListener(type: "status", listener: (data: ConnectionStatusChange) => void): void; removeEventListener(type: "backgrounderror", listener: (data: BackgroundErrorData) => void): void; removeEventListener(type: "beforerequestdevice", listener: () => void): void; removeEventListener(type: "afterrequestdevice", listener: () => void): void; removeEventListener(type: "flash", listener: () => void): void; removeEventListener(type: "accelerometerdatachanged", listener: (data: AccelerometerData) => void): void; removeEventListener(type: "buttonachanged" | "buttonbchanged", listener: (data: ButtonData) => void): void; removeEventListener(type: "magnetometerdatachanged", listener: (data: MagnetometerData) => void): void; removeEventListener(type: "temperaturechanged", listener: (data: TemperatureData) => void): void; removeEventListener(type: "pinchanged", listener: (data: PinData) => void): void; removeEventListener(type: "gesturechanged", listener: (data: GestureData) => void): void; removeEventListener(type: "buttonaaction" | "buttonbaction" | "buttonabaction", listener: (data: ButtonActionData) => void): void; removeEventListener(type: "logoaction", listener: (data: ButtonActionData) => void): void; removeEventListener(type: "microbitevent", listener: (data: MicrobitEventData) => void): void; removeEventListener(type: "uartdata", listener: (data: UartData) => void): void; /** * Sets micro:bit name filter for device requesting. * * @param name The name of the micro:bit. */ setNameFilter(name: string): void; /** * Gets micro:bit accelerometer data. * * Requires: Accelerometer Service. * * @returns accelerometer data. * @throws {DeviceError} with code `not-connected` if there is no connection. */ getAccelerometerData(): Promise; /** * Gets micro:bit accelerometer period. * * Requires: Accelerometer Service. * * @returns accelerometer period. * @throws {DeviceError} with code `not-connected` if there is no connection. */ getAccelerometerPeriod(): Promise; /** * Sets micro:bit accelerometer period. * * Requires: Accelerometer Service. * * @param value The accelerometer period. * @throws {DeviceError} with code `not-connected` if there is no connection. */ setAccelerometerPeriod(value: number): Promise; /** * Sets micro:bit LED text. * * Requires: LED Service. * * @param text The text displayed on micro:bit LED. * @throws {DeviceError} with code `not-connected` if there is no connection. */ setLedText(text: string): Promise; /** * Gets micro:bit LED scrolling delay. * * Requires: LED Service. * * @returns LED scrolling delay in milliseconds. * @throws {DeviceError} with code `not-connected` if there is no connection. */ getLedScrollingDelay(): Promise; /** * Sets micro:bit LED scrolling delay. * * Requires: LED Service. * * @param delayInMillis LED scrolling delay in milliseconds. * @throws {DeviceError} with code `not-connected` if there is no connection. */ setLedScrollingDelay(delayInMillis: number): Promise; /** * Gets micro:bit LED matrix. * * Requires: LED Service. * * @returns a boolean matrix representing the micro:bit LED display. * @throws {DeviceError} with code `not-connected` if there is no connection. */ getLedMatrix(): Promise; /** * Sets micro:bit LED matrix. * * Requires: LED Service. * * @param matrix an boolean matrix representing the micro:bit LED display. * @throws {DeviceError} with code `not-connected` if there is no connection. */ setLedMatrix(matrix: LedMatrix): Promise; /** * Gets micro:bit magnetometer data. * * Requires: Magnetometer Service. * * @returns magnetometer data. * @throws {DeviceError} with code `not-connected` if there is no connection. */ getMagnetometerData(): Promise; /** * Gets micro:bit magnetometer bearing. * * Requires: Magnetometer Service. * * @returns magnetometer bearing. * @throws {DeviceError} with code `not-connected` if there is no connection. */ getMagnetometerBearing(): Promise; /** * Gets micro:bit magnetometer period. * * Requires: Magnetometer Service. * * @returns magnetometer period. * @throws {DeviceError} with code `not-connected` if there is no connection. */ getMagnetometerPeriod(): Promise; /** * Sets micro:bit magnetometer period. * * Requires: Magnetometer Service. * * @param value magnetometer period. * @throws {DeviceError} with code `not-connected` if there is no connection. */ setMagnetometerPeriod(value: number): Promise; /** * Triggers micro:bit magnetometer calibration. * * Requires: Magnetometer Service. * * @throws {DeviceError} with code `not-connected` if there is no connection. */ triggerMagnetometerCalibration(): Promise; /** * Gets the micro:bit temperature in degrees Celsius. * * Requires: Temperature Service. * * @returns temperature in degrees Celsius. * @throws {DeviceError} with code `not-connected` if there is no connection. */ getTemperature(): Promise; /** * Gets the micro:bit temperature sensor period. * * Requires: Temperature Service. * * @returns temperature period in milliseconds. * @throws {DeviceError} with code `not-connected` if there is no connection. */ getTemperaturePeriod(): Promise; /** * Sets the micro:bit temperature sensor period. * * Requires: Temperature Service. * * @param value period in milliseconds. * @throws {DeviceError} with code `not-connected` if there is no connection. */ setTemperaturePeriod(value: number): Promise; /** * Gets which pins are configured as analog. * All other pins are digital (the default). * * Requires: IO Pin Service. * * @returns array of pin numbers (0-18) configured as analog. * @throws {DeviceError} with code `not-connected` if there is no connection. */ getAnalogPins(): Promise; /** * Sets which pins are configured as analog. * All other pins become digital (the default). * * Requires: IO Pin Service. * * @param pins array of pin numbers (0-18) to configure as analog. * @throws {DeviceError} with code `not-connected` if there is no connection. */ setAnalogPins(pins: number[]): Promise; /** * Gets which pins are configured as inputs. * Input pins are monitored and their values reported via notifications. * All other pins are outputs (the default). * * Requires: IO Pin Service. * * @returns array of pin numbers (0-18) configured as inputs. * @throws {DeviceError} with code `not-connected` if there is no connection. */ getInputPins(): Promise; /** * Sets which pins are configured as inputs. * Input pins are monitored and their values reported via notifications. * All other pins become outputs (the default). * * Note: configuring a pin as input overrides any existing pin mode * (e.g. touch sensing used by MakeCode "on pin pressed" blocks). * The two cannot be used on the same pin simultaneously. * * Requires: IO Pin Service. * * @param pins array of pin numbers (0-18) to configure as inputs. * @throws {DeviceError} with code `not-connected` if there is no connection. */ setInputPins(pins: number[]): Promise; /** * Reads current values of input pins. Unlike the `pinchanged` event * (which only includes pins whose values changed), this returns every * pin configured as an input, up to a firmware limit of 10 pins * (lowest-numbered first). * * Requires: IO Pin Service. * * @returns array of pin/value pairs. * @throws {DeviceError} with code `not-connected` if there is no connection. */ readPins(): Promise; /** * Writes pin data for output pins. * * Requires: IO Pin Service. * * @param data array of pin/value pairs. * @throws {DeviceError} with code `not-connected` if there is no connection. */ writePins(data: PinValue[]): Promise; /** * Sets PWM output on a pin. * * Requires: IO Pin Service. * * @param pin Pin number (0-18). * @param options PWM configuration. * @param options.value Analog value (0-1024). * @param options.period Period in microseconds. * @throws {DeviceError} with code `not-connected` if there is no connection. */ writePinPwm(pin: number, options: { value: number; period: number; }): Promise; /** * Register interest in a specific micro:bit message bus event. * Tells the micro:bit to forward matching message bus traffic over BLE. * Matching events are dispatched as `microbitevent`. * Use 0 as the value to match all events from a source. * * For common message bus events, consider the higher-level alternatives: * `gesturechanged`, `buttonaaction`, `buttonbaction`, `buttonabaction`. * * Requires: Event Service. * * @param source Event source ID. * @param value Event value to match, or 0 for any. * @throws {DeviceError} with code `not-connected` if there is no connection. */ subscribeToEvent(source: number, value: number): Promise; /** * Send an event to the micro:bit's message bus. * * Requires: Event Service. * * @param source Event source ID. * @param value Event value. * @throws {DeviceError} with code `not-connected` if there is no connection. */ sendEvent(source: number, value: number): Promise; /** * Write UART messages. * * Requires: UART Service. * * @param data UART message. * @throws {DeviceError} with code `not-connected` if there is no connection. */ uartWrite(data: Uint8Array): Promise; flash(dataSource: FlashDataSource, options: FlashOptions): Promise; } /** * A Bluetooth connection factory. */ export declare const createBluetoothConnection: (options?: MicrobitBluetoothConnectionOptions) => MicrobitBluetoothConnection;