@@ -58,6 +58,17 @@ export enum CommandType {
5858 * @since Pybricks Profile v1.3.0
5959 */
6060 WriteStdin = 6 ,
61+ /**
62+ * Requests to write to a buffer that is pre-allocated by a user program.
63+ *
64+ * Parameters:
65+ * - offset: The offset from the buffer base address (16-bit little-endian
66+ * unsigned integer).
67+ * - payload: The data to write.
68+ *
69+ * @since Pybricks Profile v1.4.0
70+ */
71+ WriteAppData = 7 ,
6172}
6273
6374/**
@@ -74,11 +85,22 @@ export function createStopUserProgramCommand(): Uint8Array {
7485/**
7586 * Creates a {@link CommandType.StartUserProgram} message.
7687 *
77- * @since Pybricks Profile v1.2.0
88+ * The optional payload parameter was added in Pybricks Profile v1.4.0.
89+ *
90+ * Parameters:
91+ * - payload: Optional program identifier (one byte). Slots 0--127 are
92+ * reserved for downloaded user programs. Slots 128--255 are
93+ * for builtin user programs. If no program identifier is
94+ * given, the currently active program slot will be started.
95+ *
96+ * @since Pybricks Profile v1.2.0. Program identifier added in Pybricks Profile v1.4.0.
7897 */
79- export function createStartUserProgramCommand ( ) : Uint8Array {
80- const msg = new Uint8Array ( 1 ) ;
98+ export function createStartUserProgramCommand ( slot : number | undefined ) : Uint8Array {
99+ const msg = new Uint8Array ( slot === undefined ? 1 : 2 ) ;
81100 msg [ 0 ] = CommandType . StartUserProgram ;
101+ if ( slot !== undefined ) {
102+ msg [ 1 ] = slot & 0xff ;
103+ }
82104 return msg ;
83105}
84106
@@ -140,6 +162,25 @@ export function createWriteStdinCommand(payload: ArrayBuffer): Uint8Array {
140162 return msg ;
141163}
142164
165+ /**
166+ * Creates a {@link CommandType.WriteAppData} message.
167+ * @param offset The offset from the buffer base address
168+ * @param payload The bytes to write.
169+ *
170+ * @since Pybricks Profile v1.4.0.
171+ */
172+ export function createWriteAppDataCommand (
173+ offset : number ,
174+ payload : ArrayBuffer ,
175+ ) : Uint8Array {
176+ const msg = new Uint8Array ( 1 + 2 + payload . byteLength ) ;
177+ const view = new DataView ( msg . buffer ) ;
178+ view . setUint8 ( 0 , CommandType . WriteAppData ) ;
179+ view . setUint16 ( 1 , offset & 0xffff ) ;
180+ msg . set ( new Uint8Array ( payload ) , 3 ) ;
181+ return msg ;
182+ }
183+
143184/** Events are notifications received from the hub. */
144185export enum EventType {
145186 /**
@@ -156,6 +197,12 @@ export enum EventType {
156197 * @since Pybricks Profile v1.3.0
157198 */
158199 WriteStdout = 1 ,
200+ /**
201+ * Hub wrote to appdata event.
202+ *
203+ * @since Pybricks Profile v1.4.0
204+ */
205+ WriteAppData = 2 ,
159206}
160207
161208/** Status indications received by Event.StatusReport */
@@ -244,6 +291,18 @@ export function parseWriteStdout(msg: DataView): ArrayBuffer {
244291 return msg . buffer . slice ( 1 ) ;
245292}
246293
294+ /**
295+ * Parses the payload of a app data message.
296+ * @param msg The raw message data.
297+ * @returns The bytes that were written.
298+ *
299+ * @since Pybricks Profile v1.4.0
300+ */
301+ export function parseWriteAppData ( msg : DataView ) : ArrayBuffer {
302+ assert ( msg . getUint8 ( 0 ) === EventType . WriteAppData , 'expecting write appdata event' ) ;
303+ return msg . buffer . slice ( 1 ) ;
304+ }
305+
247306/**
248307 * Protocol error. Thrown e.g. when there is a malformed message.
249308 */
@@ -285,6 +344,20 @@ export enum HubCapabilityFlag {
285344 * @since Pybricks Profile v1.3.0
286345 */
287346 UserProgramMultiMpy6Native6p1 = 1 << 2 ,
347+
348+ /**
349+ * Hub supports builtin sensor port view monitoring program.
350+ *
351+ * @since Pybricks Profile v1.4.0.
352+ */
353+ HasPortView = 1 << 3 ,
354+
355+ /**
356+ * Hub supports builtin IMU calibration program.
357+ *
358+ * @since Pybricks Profile v1.4.0.
359+ */
360+ HasIMUCalibration = 1 << 4 ,
288361}
289362
290363/** Supported user program file formats. */
0 commit comments