-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.ts
86 lines (76 loc) · 2.46 KB
/
platform.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
namespace sigfox {
// From platform.h
export const MAX_SENSOR_COUNT: number = 3
export const MAX_PORT_COUNT: number = 4
export const MAX_UART_SEND_MSG_SIZE: number = 35
export const MAX_UART_RESPONSE_MSG_SIZE: number = 36
let uartTaskID = 0
let debugBuffer: string[] = []
const MAX_DEBUG_BUFFER_SIZE = 10
// TODO
export function led_toggle() { }
export function millis(): int32 {
// Number of seconds elapsed since power on.
return input.runningTime()
}
export function F(s: string): string { return s; }
export function debug(p1: string, p2: string = null): void {
debug_println(p1, p2);
debug_flush();
}
export function debug_flush(): void {
if (debugBuffer.length == 0) { return; }
const s = debugBuffer.join("");
debugBuffer = [];
uart_write(s);
}
export function debug_print(p1: string, p2: string = null): void {
const s = p1 + ((p2 === null) ? "" : p2);
internal_print(normalise_text(s));
}
export function debug_println(p1: string, p2: string = null): void {
const s = p1 + ((p2 === null) ? "" : p2);
internal_print(normalise_text(s) + "\n");
}
function internal_print(s: string): void {
debugBuffer.push(s);
if (debugBuffer.length >= MAX_DEBUG_BUFFER_SIZE) {
debug_flush();
}
}
export function set_uart_task_id(task_id: number): void {
uartTaskID = task_id;
}
export function normalise_text(s: string): string {
let result = "";
for (let i = 0; i < s.length; i++) {
const ch = s.charCodeAt(i);
if (ch < 32) {
result = result + "[" + ch + "]";
} else {
result = result + s[i];
}
}
return result;
}
function uart_write(s: string): void {
if (uartTaskID === 0) {
serial.writeLine("***** ERROR: uart_write / missing UART task ID");
return;
}
const uartMsg = <UARTMsg>{
debugMsg: true,
sendData: s,
// Set to default values below, not used.
timeout: 0,
markerChar: 0,
expectedMarkerCount: 0,
successEvent: null,
failureEvent: null,
responseMsg: null,
responseTaskID: 0,
};
const msg = <Msg_t>{ uartMsg: uartMsg };
msg_post(uartTaskID, msg);
}
}