-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch-code.cjs
More file actions
62 lines (53 loc) · 2.36 KB
/
Copy pathpatch-code.cjs
File metadata and controls
62 lines (53 loc) · 2.36 KB
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
const fs = require('fs');
const file = 'src/ai/circuitChatAI.js';
let code = fs.readFileSync(file, 'utf8');
const t1 = ` let codeStr = "";
if (codeIncludes.size > 0 || codeSetup.length > 0) {
codeStr += \`\\n\`;
codeIncludes.forEach(inc => codeStr += inc + '\\n');
codeStr += \`\\nvoid setup() {\\n Serial.begin(115200);\\n\`;
codeSetup.forEach(s => codeStr += s + '\\n');
codeStr += \`}\\n\\nvoid loop() {\\n // Auto-generated by TinkerAI Offline Engine\\n}\\n\`;
payload.code = codeStr;
}`;
const t2 = t1.replace(/\n/g, '\r\n');
const r = ` let codeStr = "";
if (codeIncludes.size > 0) {
codeStr += \`\\n\`;
codeIncludes.forEach(inc => codeStr += inc + '\\n');
}
codeStr += \`\\nvoid setup() {\\n Serial.begin(115200);\\n\`;
codeSetup.forEach(s => codeStr += s + '\\n');
const mcuPinSetups = new Set();
payload.wiring.forEach(w => {
let mcuPin = null;
let compId = null;
if (w.toComp === 'mcu') { mcuPin = w.toPin; compId = w.fromComp.split('_')[0]; }
else if (w.fromComp === 'mcu') { mcuPin = w.fromPin; compId = w.toComp.split('_')[0]; }
if (mcuPin && !['GND','5V','3V3','3.3V','VIN','VCC'].includes(mcuPin) && !mcuPin.startsWith('A') && !isNaN(parseInt(mcuPin))) {
let mode = 'OUTPUT';
if (compId.includes('sensor') || compId.includes('button') || compId.includes('switch') || compId.includes('joystick') || compId.includes('ldr') || compId.includes('dht') || compId.includes('hx711') || compId.includes('encoder') || compId.includes('receiver') || compId.includes('keypad')) {
mode = 'INPUT';
}
if (compId.includes('button') || compId.includes('switch')) {
mode = 'INPUT_PULLUP';
}
mcuPinSetups.add(\` pinMode(\${mcuPin}, \${mode}); // \${compId}\`);
} else if (mcuPin && mcuPin.startsWith('A')) {
mcuPinSetups.add(\` pinMode(\${mcuPin}, INPUT); // \${compId}\`);
}
});
mcuPinSetups.forEach(s => codeStr += s + '\\n');
codeStr += \`}\\n\\nvoid loop() {\\n // Auto-generated by TinkerAI Offline Engine\\n}\\n\`;
payload.code = codeStr;`;
if (code.includes(t1)) {
code = code.replace(t1, r);
console.log("Patched LF");
} else if (code.includes(t2)) {
code = code.replace(t2, r.replace(/\n/g, '\r\n'));
console.log("Patched CRLF");
} else {
console.error("Not found!");
process.exit(1);
}
fs.writeFileSync(file, code, 'utf8');