-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisc.ts
217 lines (182 loc) · 5.53 KB
/
misc.ts
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
namespace pxsim {
/**
* Error codes used in the micro:bit runtime.
*/
export enum PanicCode {
// PANIC Codes. These are not return codes, but are terminal conditions.
// These induce a panic operation, where all code stops executing, and a panic state is
// entered where the panic code is diplayed.
// Out out memory error. Heap storage was requested, but is not available.
MICROBIT_OOM = 20,
// Corruption detected in the micro:bit heap space
MICROBIT_HEAP_ERROR = 30,
// Dereference of a NULL pointer through the ManagedType class,
MICROBIT_NULL_DEREFERENCE = 40,
};
export function panic(code: number) {
console.log("PANIC:", code)
throw new Error("PANIC " + code)
}
export interface RuntimeOptions {
theme: string;
}
}
namespace pxsim.basic {
export var pause = thread.pause;
export var forever = thread.forever;
}
namespace pxsim.control {
export var inBackground = thread.runInBackground;
export function reset() {
U.userError("reset not implemented in simulator yet")
}
export function waitMicros(micros: number) {
// TODO
}
export function deviceName(): string {
let b = board();
return b && b.id
? b.id.slice(0, 4)
: "abcd";
}
export function deviceSerialNumber(): number {
let b = board();
return parseInt(b && b.id
? b.id.slice(1)
: "42");
}
export function onEvent(id: number, evid: number, handler: RefAction) {
pxtcore.registerWithDal(id, evid, handler)
}
export function raiseEvent(id: number, evid: number, mode: number) {
// TODO mode?
board().bus.queue(id, evid)
}
export function eventTimestamp() {
return board().bus.getLastEventTime()
}
export function eventValue() {
return board().bus.getLastEventValue()
}
}
namespace pxsim.pxtcore {
export function registerWithDal(id: number, evid: number, handler: RefAction) {
board().bus.listen(id, evid, handler);
}
}
namespace pxsim.input {
export function runningTime(): number {
return runtime.runningTime();
}
export function runningTimeMicros(): number {
return runtime.runningTimeUs();
}
export function calibrateCompass() {
// device calibrates...
}
}
namespace pxsim.pins {
export function onPulsed(name: number, pulse: number, body: RefAction) {
}
export function pulseDuration(): number {
return 0;
}
export function createBuffer(sz: number) {
return pxsim.BufferMethods.createBuffer(sz)
}
export function pulseIn(name: number, value: number, maxDuration: number): number {
let pin = getPin(name);
if (!pin) return 0;
return 5000;
}
export function spiWrite(value: number): number {
// TODO
return 0;
}
export function spiFrequency(f: number): void {
// TODO
}
export function spiFormat(bits: number, mode: number): void {
// TODO
}
export function spiPins(mosi: number, miso: number, sck: number) {
// TODO
}
export function i2cReadBuffer(address: number, size: number, repeat?: boolean): RefBuffer {
// fake reading zeros
return createBuffer(size)
}
export function i2cWriteBuffer(address: number, buf: RefBuffer, repeat?: boolean): void {
// fake - noop
}
// this likely shouldn't be called
export function getPinAddress(name: number) {
return getPin(name)
}
export function setEvents(name: number, event: number) {
}
}
namespace pxsim.devices {
export function tellCameraTo(action: number) {
// TODO
}
export function tellRemoteControlTo(action: number) {
// TODO
}
export function raiseAlertTo(action: number) {
// TODO
}
export function onSignalStrengthChanged(action: number) {
// TODO
}
export function signalStrength(): number {
// TODO
return 0;
}
export function onGamepadButton(button: number, body: RefAction) {
// TODO
}
}
namespace pxsim.bluetooth {
export function startIOPinService(): void {
// TODO
}
export function startLEDService(): void {
// TODO
}
export function startTemperatureService(): void {
// TODO
}
export function startMagnetometerService(): void {
// TODO
}
export function startAccelerometerService(): void {
// TODO
}
export function startButtonService(): void {
// TODO
}
export function startUartService(): void {
// TODO
}
export function uartWriteString(s: string): void {
serial.writeString(s)
}
export function uartReadUntil(del: string): string {
return serial.readUntil(del);
}
export function onUartDataReceived(delimiters: string, handler: RefAction) {
let b = board();
b.bus.listen(DAL.MICROBIT_ID_BLE_UART, DAL.MICROBIT_UART_S_EVT_DELIM_MATCH, handler);
}
export function onBluetoothConnected(a: RefAction) {
// TODO
}
export function onBluetoothDisconnected(a: RefAction) {
// TODO
}
export function advertiseUrl(url: string, power: number, connectable: boolean) { }
export function advertiseUidBuffer(nsAndInstance: RefBuffer, power: number, connectable: boolean) { }
export function stopAdvertising() { }
export function setTransmitPower(power: number) { }
}