Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x | /**
* External API types for the postMessage remote control interface.
*
* This module defines the protocol for external websites to control
* the Arduino simulator via window.postMessage.
*/
/** API Version for backward compatibility and feature negotiation. */
export const API_VERSION = "1.1.0";
/**
* All actions supported by the simulator's remote control interface (inbound).
*/
export enum SimulatorActionType {
/** Load code into the editor */
LOAD_CODE = "LOAD_CODE",
/** Compile and start the simulation */
START_SIMULATION = "START_SIMULATION",
/** Stop the running simulation */
STOP_SIMULATION = "STOP_SIMULATION",
/** Set the value of a digital or analog pin */
SET_PIN_STATE = "SET_PIN_STATE",
/** Request the current value of a pin (triggers a RESPONSE message) */
GET_PIN_STATE = "GET_PIN_STATE",
/** Set multiple pins in a single operation (batch) */
BATCH_SET_PIN_STATE = "BATCH_SET_PIN_STATE",
}
/**
* All events emitted by the simulator (outbound).
* These are sent proactively from the simulator to the parent frame.
*/
export enum SimulatorEventType {
/** A pin's state (digital or analog value) has changed */
PIN_STATE_CHANGE_EVENT = "PIN_STATE_CHANGE_EVENT",
/** The simulation's overall state has changed (RUNNING, STOPPED, ERROR) */
SIMULATION_STATE_EVENT = "SIMULATION_STATE_EVENT",
/** Data has been output over the serial interface (Serial.print, etc.) */
SERIAL_OUTPUT_EVENT = "SERIAL_OUTPUT_EVENT",
}
/**
* Payload for LOAD_CODE messages.
*/
export interface LoadCodePayload {
code: string;
}
/**
* Payload for SET_PIN_STATE messages.
*/
export interface SetPinStatePayload {
pin: number;
value: number;
}
/**
* Payload for GET_PIN_STATE messages.
*/
export interface GetPinStatePayload {
pin: number;
}
/**
* Payload for BATCH_SET_PIN_STATE messages.
* Allows setting multiple pins in a single operation for efficiency.
*/
export interface BatchSetPinStatePayload {
pins: Array<{ pin: number; value: number }>;
}
/** Union of all payload types keyed by action type */
type PayloadMap = {
[SimulatorActionType.LOAD_CODE]: LoadCodePayload;
[SimulatorActionType.START_SIMULATION]: undefined;
[SimulatorActionType.STOP_SIMULATION]: undefined;
[SimulatorActionType.SET_PIN_STATE]: SetPinStatePayload;
[SimulatorActionType.GET_PIN_STATE]: GetPinStatePayload;
[SimulatorActionType.BATCH_SET_PIN_STATE]: BatchSetPinStatePayload;
};
/**
* Payload for PIN_STATE_CHANGE_EVENT events.
*/
export interface PinStateChangeEventData {
pin: number;
value: number;
}
/**
* Data for SIMULATION_STATE_EVENT events.
*/
export interface SimulationStateEventData {
state: "RUNNING" | "STOPPED" | "PAUSED" | "ERROR";
message?: string;
}
/**
* Inbound message from an external website.
*
* @example
* window.postMessage({ type: "LOAD_CODE", payload: { code: "void setup() {}" } }, "*");
*/
export type SimulatorMessage<T extends SimulatorActionType = SimulatorActionType> = {
type: T;
payload: PayloadMap[T];
};
/**
* Response message sent back to the parent frame via postMessage.
* Includes version for backward compatibility negotiation.
*/
export interface SimulatorResponse {
/** API version (semantic versioning: major.minor.patch) */
version: string;
/** The action this response corresponds to */
type: string;
/** Whether the action was processed successfully */
success: boolean;
/** Optional data payload (e.g. pin value for GET_PIN_STATE) */
data?: unknown;
/** Error message when success is false */
error?: string;
}
/**
* Event message emitted proactively by the simulator.
* These are sent without request and inform the parent of state changes.
* The `data` field matches the format documented in EXTERNAL_API.md.
*/
export type SimulatorEventMessage<T extends SimulatorEventType = SimulatorEventType> = {
version: string;
type: T;
success: true; // Events always report success
data: T extends SimulatorEventType.PIN_STATE_CHANGE_EVENT
? PinStateChangeEventData
: T extends SimulatorEventType.SIMULATION_STATE_EVENT
? SimulationStateEventData
: T extends SimulatorEventType.SERIAL_OUTPUT_EVENT
? string
: never;
}
|