-
Notifications
You must be signed in to change notification settings - Fork 1
/
bridge.ts
164 lines (146 loc) · 5.6 KB
/
bridge.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
/*
Copyright (c) Bryan Hughes <bryan@nebri.us>
This file is part of RVL Node.
RVL Node is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RVL Node is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RVL Node. If not, see <http://www.gnu.org/licenses/>.
*/
import { readFileSync } from 'fs';
import { join } from 'path';
import { LogLevel } from './types';
import { wait } from './util';
import { IWaveParameters } from './animation';
import { addPacketToQueue } from './nodePlatform';
import Module = require('./output');
interface IStructInfoEntry {
name: string;
path: string[];
type: 'uint8_t' | 'int8_t' | 'uint16_t' | 'int16_t' | 'uint32_t' | 'int32_t';
initialValue: number;
size: number;
index: number;
}
interface IStructInfo {
totalSize: number;
entryDictionary: { [entryName: string]: IStructInfoEntry };
}
const structData: IStructInfo = JSON.parse(readFileSync(join(__dirname, 'structInfo.json')).toString());
const pathSegmentArrayRegex = /^(.*?)\[([0-9]*)\]$/;
for (const entryName in structData.entryDictionary) {
if (!structData.entryDictionary.hasOwnProperty(entryName)) {
continue;
}
const entry = structData.entryDictionary[entryName];
const entryPath = entry.name.split('.');
for (let i = entryPath.length - 1; i >= 0; i--) {
const match = pathSegmentArrayRegex.exec(entryPath[i]);
if (match) {
entryPath.splice(i, 1, match[1], match[2]);
}
}
entry.path = entryPath;
}
let cInit: ((logLevel: number, channel: number, deviceId: number) => void) | undefined;
let cGetWaveSettingsPointer: (() => number) | undefined;
let cUpdateWaveSettings: (() => void) | undefined;
let cUpdatePowerState: ((powerState: boolean) => void) | undefined;
let cUpdateBrightness: ((brightness: number) => void) | undefined;
Module.onRuntimeInitialized = () => {
cInit = Module.cwrap('init', null, ['number', 'number', 'number']);
cGetWaveSettingsPointer = Module.cwrap('getWaveSettingsPointer', 'number', []);
cUpdateWaveSettings = Module.cwrap('updateWaveSettings', null, []);
cUpdatePowerState = Module.cwrap('updatePowerState', null, ['number']);
cUpdateBrightness = Module.cwrap('updateBrightness', null, ['number']);
};
export async function init(logLevel: LogLevel, channel: number, deviceId: number): Promise<void> {
if (!Number.isInteger(channel) || channel < 0 || channel > 7) {
throw new Error(`Channel "${channel} is invalid. The channel must be an integer between 0 and 7 (inclusive)`);
}
while (!cInit) {
await wait(10);
}
cInit(logLevel, channel, deviceId);
}
export function setWaveParameters(newWaveParameters: IWaveParameters): void {
if (!cGetWaveSettingsPointer) {
throw new Error(`Internal Error: cGetWaveSettingsPointer is unexpectedly null`);
}
if (!cUpdateWaveSettings) {
throw new Error(`Internal Error: cGetWaveSettingsPointer is unexpectedly null`);
}
const waveSettingsPointer = cGetWaveSettingsPointer();
const waveSettingsBuffer = Module.HEAPU8.subarray(waveSettingsPointer, waveSettingsPointer + structData.totalSize);
const view = Buffer.from(waveSettingsBuffer.buffer, waveSettingsPointer, structData.totalSize);
function writeValue(path: string, value: any) {
if (typeof value === 'number') {
const structEntry = structData.entryDictionary[path];
if (!structEntry) {
throw new Error(`Internal Error: Uknown struct path "${path}"`);
}
switch (structEntry.type) {
case 'uint8_t':
view.writeUInt8(value, structEntry.index);
break;
case 'int8_t':
view.writeInt8(value, structEntry.index);
break;
case 'uint16_t':
view.writeUInt16BE(value, structEntry.index);
break;
case 'int16_t':
view.writeInt16BE(value, structEntry.index);
break;
case 'uint32_t':
view.writeUInt32BE(value, structEntry.index);
break;
case 'int32_t':
view.writeInt32BE(value, structEntry.index);
break;
default:
throw new Error(`Internal Error: Encountered unexpected struct type "${structEntry.type}"`);
}
} else if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
writeValue(`${path}[${i}]`, value[i]);
}
} else if (typeof value === 'object') {
for (const entry in value) {
if (!value.hasOwnProperty(entry)) {
continue;
}
writeValue(`${path}.${entry}`, value[entry]);
}
} else {
throw new Error(`Internal Error: Unsupported value type `);
}
}
for (const param in newWaveParameters) {
if (!newWaveParameters.hasOwnProperty(param)) {
continue;
}
writeValue(param, (newWaveParameters as any)[param]);
}
cUpdateWaveSettings();
}
export function setPowerState(newPowerState: boolean): void {
if (!cUpdatePowerState) {
throw new Error(`Internal Error: cUpdatePowerState is unexpectedly null`);
}
cUpdatePowerState(newPowerState);
}
export function setBrightness(newBrightness: number): void {
if (!cUpdateBrightness) {
throw new Error(`Internal Error: cUpdateBrightness is unexpectedly null`);
}
cUpdateBrightness(newBrightness);
}
export function receivePacket(packet: Buffer): void {
addPacketToQueue(packet);
}