-
Notifications
You must be signed in to change notification settings - Fork 1
/
Z-Uno-Shield-code-gen.js
547 lines (512 loc) · 26.8 KB
/
Z-Uno-Shield-code-gen.js
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
var codeTemplates = {
"SwitchBinary": {
"note": "PPP5PPP",
"vars": "byte pinXXXSwitchBinaryState = 0;",
"channel": " PPP1PPP(pinXXXSwitchBinaryGetter, pinXXXSwitchBinarySetter)",
"setup": " pinMode(XXX, OUTPUT);",
"loop": " digitalWrite(XXX, pinXXXSwitchBinaryState ? PPP3PPP : PPP4PPP);",
"xetter": "void pinXXXSwitchBinarySetter(byte value) {\n" +
" pinXXXSwitchBinaryState = value;\n" +
"}\n\n" +
"byte pinXXXSwitchBinaryGetter() {\n" +
" return pinXXXSwitchBinaryState;\n" +
"}",
"preAction": function(params, pin) {
switch(params[1]) {
case "switch":
params[1] = "ZUNO_SWITCH_BINARY";
break;
case "doorlock":
params[1] = "ZUNO_DOORLOCK";
break;
case "siren":
params[1] = "ZUNO_SIREN";
break;
case "valve":
params[1] = "ZUNO_FLOWSTOP";
break;
}
params[3] = params[2] == "normal" ? "HIGH" : "LOW";
params[4] = params[2] == "normal" ? "LOW" : "HIGH";
params[5] = [13, 14, 15, 16].indexOf(pin) !== -1 ? "- Make sure that output current do not exceed 5 A per channel or 15 A per all PWM1-4 pins" : "";
return params;
},
"retemplate": function(params) {
if (params[1] === "heatingThermostat" || params[1] === "coolingThermostat") return "Thermostat"
return;
}
},
"Thermostat": {
"note": "- PPP10PPP",
"vars": "byte pinXXXThermostatModeState = 0;\n" +
"signed int pinXXXThermostatTemperatureState = 0;\n" +
"signed int pinXXXThermostatTemperatureCurrent = 0;",
"channel": " ZUNO_THERMOSTAT(THERMOSTAT_FLAGS_OFF | THERMOSTAT_FLAGS_PPP5PPP, THERMOSTAT_UNITS_CELSIUS, THERMOSTAT_RANGE_POS, 4, pinXXXThermostatModeGetter, pinXXXThermostatModeSetter, pinXXXThermostatTemperatureGetter, pinXXXThermostatTemperatureSetter)",
"report": " if (REPORT_TYPE() == CC_SENSOR_MULTILEVEL && REPORT_SENSOR_TYPE() == ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE) {\n" +
" signed int temp;\n" +
" \n" +
" switch (REPORT_SENSOR_VALUE_SIZE()) {\n" +
" case 1:\n" +
" temp = REPORT_VALUE_1B();\n" +
" break;\n" +
" case 2:\n" +
" temp = REPORT_VALUE_2B();\n" +
" break;\n" +
" case 4:\n" +
" temp = REPORT_VALUE_4B();\n" +
" break;\n" +
" }\n" +
" \n" +
" // convert to 0.1 units and to C\n" +
" if (REPORT_SENSOR_VALUE_PRECISION() == 0) {\n" +
" temp *= 10;\n" +
" }\n" +
" if (REPORT_SENSOR_SCALE() != SENSOR_MULTILEVEL_SCALE_CELSIUS) {\n" +
" temp = (temp - 32) * 5 / 9;\n" +
" }\n" +
" if (REPORT_SENSOR_VALUE_PRECISION() == 2) {\n" +
" temp /= 10;\n" +
" }\n" +
" if (REPORT_SENSOR_VALUE_PRECISION() > 2) {\n" +
" return; // ignore bigger precision\n" +
" }\n" +
" \n" +
" pinXXXThermostatTemperatureCurrent = temp;\n" +
" }",
"setup": " pinMode(XXX, OUTPUT);",
"loop": " if (pinXXXThermostatModeState) {PPP7PPP\n" +
" if (pinXXXThermostatTemperatureState < pinXXXThermostatTemperatureCurrent - PPP6PPP) {\n" +
" digitalWrite(XXX, PPP8PPP);\n" +
" }\n" +
" if (pinXXXThermostatTemperatureState > pinXXXThermostatTemperatureCurrent + PPP6PPP) {\n" +
" digitalWrite(XXX, PPP9PPP);\n" +
" }\n" +
" }",
"xetter": "void pinXXXThermostatModeSetter(byte value) {\n" +
" pinXXXThermostatModeState = value;\n" +
"}\n\n" +
"byte pinXXXThermostatModeGetter() {\n" +
" return pinXXXThermostatModeState;\n" +
"}\n\n" +
"void pinXXXThermostatTemperatureSetter(byte mode, signed int value) {\n" +
" pinXXXThermostatTemperatureState = value;\n" +
"}\n\n" +
"signed int pinXXXThermostatTemperatureGetter(byte mode) {\n" +
" return pinXXXThermostatTemperatureState;\n" +
"}",
"preAction": function(params, pin, pins) {
params[5] = params[1] === "heatingThermostat" ? "HEAT" : "COOL"
params[8] = (params[2] === "normal" ^ params[1] === "coolingThermostat") ? "HIGH" : "LOW";
params[9] = (params[2] === "normal" ^ params[1] === "coolingThermostat")? "LOW" : "HIGH";
params[6] = 10; // 1 deg C
var thermostatExternalSensorPins = Object.keys(pins).filter(function(key) {
return pins[key].type === "SwitchBinary" && (pins[key].params[1] === "heatingThermostat" || pins[key].params[1] === "coolingThermostat") && pins[key].params[3] === "external";
});
if (params[3] === "external") {
params[7] = "";
if (thermostatExternalSensorPins[0] === pin) {
params[10] = "Put Z-Uno in the Life Line association group of a temperature sensor. Make sure both devices share same security scheme";
} else {
params.suppressNote = true;
}
} else {
var ds18b20Num = parseInt(params[3].split("ds18b20_")[1]);
params[7] = "\n pinXXXThermostatTemperatureCurrent = temperature[" + (ds18b20Num - 1) + "];"
params[10] = "You need to set up DS18B20 channel with at least " + ds18b20Num + " sensor" + (ds18b20Num > 1 ? "s" : "") + ".";
params.suppressReport = true;
}
return params;
}
},
"SwitchMultilevelPWM0": {
"note": "",
"vars": "byte pinXXXSwitchMultilevelState = 0, _pinXXXSwitchMultilevelState = 1;",
"channel": " ZUNO_SWITCH_MULTILEVEL(pinXXXSwitchMultilevelGetter, pinXXXSwitchMultilevelSetter)",
"setup": " pinMode(XXX, OUTPUT);\n zunoFastPWMInit(0);",
"loop": " if (pinXXXSwitchMultilevelState != _pinXXXSwitchMultilevelState) {\n" +
" _pinXXXSwitchMultilevelState = pinXXXSwitchMultilevelState;\n" +
" zunoFastPWMSet(255 - pinXXXSwitchMultilevelState, pinXXXSwitchMultilevelState);\n" +
" zunoFastPWMEnable(0);\n" +
" zunoFastPWMEnable(1);\n" +
" }",
"xetter": "void pinXXXSwitchMultilevelSetter(byte value) {\n" +
" pinXXXSwitchMultilevelState = value;\n" +
"}\n\n" +
"byte pinXXXSwitchMultilevelGetter() {\n" +
" return pinXXXSwitchMultilevelState;\n" +
"}"
},
"SwitchMultilevel": {
"note": "- Make sure that output current do not exceed 5 A per channel or 15 A per all PWM1-4 pins",
"vars": "byte pinXXXSwitchMultilevelState = 0;",
"channel": " ZUNO_SWITCH_MULTILEVEL(pinXXXSwitchMultilevelGetter, pinXXXSwitchMultilevelSetter)",
"setup": " pinMode(XXX, OUTPUT);",
"loop": " analogWrite(XXX, (word)pinXXXSwitchMultilevelState * 255 / 99);",
"xetter": "void pinXXXSwitchMultilevelSetter(byte value) {\n" +
" pinXXXSwitchMultilevelState = value;\n" +
"}\n\n" +
"byte pinXXXSwitchMultilevelGetter() {\n" +
" return pinXXXSwitchMultilevelState;\n" +
"}",
"retemplate": function(params) {
if (params[1] !== "single") return "SwitchColor";
return;
}
},
"SwitchColor": {
"note": "- Make sure that output current do not exceed 5 A per channel or 15 A per all PWM1-4 pins",
"vars": "byte pinXXXSwitchMultilevelState = 0;",
"channel": " ZUNO_SWITCH_COLOR(PPP2PPP, pinsSwitchColorGetter, pinsSwitchColorSetter)",
"setup": " pinMode(XXX, OUTPUT);",
"loop": " analogWrite(XXX, pinXXXSwitchMultilevelState);",
"xetter": "void pinsSwitchColorSetter(byte color, byte value) {\n" +
"PPP3PPP\n" +
"}\n\n" +
"byte pinsSwitchColorGetter(byte color) {\n" +
"PPP4PPP\n" +
"}",
"preAction": function(params, pin, pins) {
var colorFlags = {
"white": "SWITCH_COLOR_FLAGS_WARM_WHITE",
"red": "SWITCH_COLOR_FLAGS_RED",
"green": "SWITCH_COLOR_FLAGS_GREEN",
"blue": "SWITCH_COLOR_FLAGS_BLUE"
};
var colorModes = {
"white": "SWITCH_COLOR_COMPONENT_WARM_WHITE",
"red": "SWITCH_COLOR_COMPONENT_RED",
"green": "SWITCH_COLOR_COMPONENT_GREEN",
"blue": "SWITCH_COLOR_COMPONENT_BLUE"
};
var colorChannels = Object.keys(pins).map(function(key) {
return (pins[key].type === "SwitchMultilevel" && pins[key].params[1] != "single") ? { key: key, obj: pins[key]} : null;
}).filter(function(x) { return x; });
var colors = colorChannels.map(function(x) { return x.obj.params[1]; });
var colorsFlags = colors.map(function(x) { return colorFlags[x]; });
if (params[1] === colors[0]) {
params[2] = colorsFlags.join(' | ');
params[3] = colorChannels.map(function(x) { return " if (color == " + colorModes[x.obj.params[1]] + ") pin" + x.key + "SwitchMultilevelState = value;"; }).join('\n');
params[4] = colorChannels.map(function(x) { return " if (color == " + colorModes[x.obj.params[1]] + ") return pin" + x.key + "SwitchMultilevelState;"; }).join('\n');
} else {
params.channels = 0;
params.suppressChannel = true;
params.suppressXetter = true;
}
return params;
}
},
"SensorBinary": {
"note": "PPP5PPP",
"vars": "byte pinXXXSensorBinaryState;",
"channel": " ZUNO_SENSOR_BINARY(PPP1PPP, pinXXXSensorBinaryGetter)",
"setup": " pinMode(XXX, PPP4PPP);\n pinXXXSensorBinaryState = PPP2PPP!digitalRead(XXX);",
"loop": " byte _pinXXXSensorBinaryState = PPP2PPPdigitalRead(XXX);\n" +
" if (pinXXXSensorBinaryState != _pinXXXSensorBinaryState) {\n" +
" pinXXXSensorBinaryState = _pinXXXSensorBinaryState;\n" +
" zunoSendReport(NNN);\n" +
" }",
"xetter": "byte pinXXXSensorBinaryGetter() {\n" +
" return pinXXXSensorBinaryState;\n" +
"}",
"preAction": function(params) {
params[1] = {
"general": "ZUNO_SENSOR_BINARY_TYPE_GENERAL_PURPOSE",
"smoke": "ZUNO_SENSOR_BINARY_TYPE_SMOKE",
"co": "ZUNO_SENSOR_BINARY_TYPE_CO",
"co2": "ZUNO_SENSOR_BINARY_TYPE_CO2",
"heat": "ZUNO_SENSOR_BINARY_TYPE_HEAT",
"leakage": "ZUNO_SENSOR_BINARY_TYPE_WATER",
"freeze": "ZUNO_SENSOR_BINARY_TYPE_FREEZE",
"tamper": "ZUNO_SENSOR_BINARY_TYPE_TAMPER",
"door": "ZUNO_SENSOR_BINARY_TYPE_DOOR_WINDOW",
"tilt": "ZUNO_SENSOR_BINARY_TYPE_TILT",
"motion": "ZUNO_SENSOR_BINARY_TYPE_MOTION",
"glassbr": "ZUNO_SENSOR_BINARY_TYPE_GLASSBREAK",
}[params[1]];
params[2] = params[2] === "normal" ? "" : "!";
params[4] = params[3] === "pullup" ? "INPUT_PULLUP" : "INPUT";
params[5] = params[3] === "pullup" ? "" : "- Make sure that input is always pulled to high or low level."
return params;
}
},
"SensorMultilevel": {
"note": "- Reports are sent every 30 seconds",
"vars": "PPP5PPP pinXXXSensorMultilevelState;",
"channel": " ZUNO_SENSOR_MULTILEVEL(PPP4PPP, pinXXXSensorMultilevelGetter)",
"setup": " pinMode(XXX, INPUT);",
"loop": " pinXXXSensorMultilevelState = (PPP5PPP) (PPP1PPP * analogRead(XXX) / PPP2PPP) + PPP3PPP; // Math in integer numbers\n" +
" zunoSendReport(NNN); // report every 30 seconds",
"xetter": "PPP5PPP pinXXXSensorMultilevelGetter() {\n" +
" return pinXXXSensorMultilevelState;\n" +
"}",
"preAction": function(params) {
var precision = {
"percentage": 0,
"temperature": 1,
"luminance": 0,
"ppm": 0,
"humidity": 1,
"voltage": 1,
"current": 1,
"distance": 1,
"pressure": 2,
}[params[4]];
var precision_str = {
0: "SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS",
1: "SENSOR_MULTILEVEL_PRECISION_ONE_DECIMAL",
2: "SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS"
}[precision];
var size = {
"percentage": 1,
"temperature": 2,
"luminance": 2,
"humidity": 1,
"voltage": 2,
"current": 2,
"distance": 2,
"pressure": 4,
"ppm": 2,
}[params[4]];
var voltage_offset = {
"3": 1023,
"5": 831,
"12": 862,
}[params[5]];
var size_str = {
1: "SENSOR_MULTILEVEL_SIZE_ONE_BYTE",
2: "SENSOR_MULTILEVEL_SIZE_TWO_BYTES",
4: "SENSOR_MULTILEVEL_SIZE_FOUR_BYTES"
}[size];
var m = params[1] * Math.pow(10, precision), M = params[2] * Math.pow(10, precision);
var frac = floatToRatio((M - m) / voltage_offset, 64);
params[1] = frac[0];
params[2] = frac[1];
params[3] = Math.round(m);
params[4] = {
"percentage": "ZUNO_SENSOR_MULTILEVEL_TYPE_GENERAL_PURPOSE_VALUE, SENSOR_MULTILEVEL_SCALE_PERCENTAGE_VALUE, " + size_str + ", " + precision_str,
"temperature": "ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, " + size_str + ", " + precision_str,
"luminance": "ZUNO_SENSOR_MULTILEVEL_TYPE_LUMINANCE, SENSOR_MULTILEVEL_SCALE_PERCENTAGE_VALUE, " + size_str + ", " + precision_str,
"humidity": "ZUNO_SENSOR_MULTILEVEL_TYPE_RELATIVE_HUMIDITY, SENSOR_MULTILEVEL_SCALE_PERCENTAGE_VALUE, " + size_str + ", " + precision_str,
"voltage": "ZUNO_SENSOR_MULTILEVEL_TYPE_VOLTAGE, SENSOR_MULTILEVEL_SCALE_VOLT, " + size_str + ", " + precision_str,
"current": "ZUNO_SENSOR_MULTILEVEL_TYPE_CURRENT, SENSOR_MULTILEVEL_SCALE_AMPERE, " + size_str + ", " + precision_str,
"distance": "ZUNO_SENSOR_MULTILEVEL_TYPE_DISTANCE, SENSOR_MULTILEVEL_SCALE_METER, " + size_str + ", " + precision_str,
"pressure": "ZUNO_SENSOR_MULTILEVEL_TYPE_ATMOSPHERIC_PRESSURE, SENSOR_MULTILEVEL_SCALE_KILO_PASCAL, " + size_str + ", " + precision_str,
"ppm": "ZUNO_SENSOR_MULTILEVEL_TYPE_CO2_LEVEL, SENSOR_MULTILEVEL_SCALE_PARTS_PER_MILLION, " + size_str + ", " + precision_str
}[params[4]];
params[5] = {
1: "byte",
2: "word",
4: "dword"
}[size];
return params;
}
},
"DHT": {
"note": "- Connect PPP1PPP sensor.",
"includes": "#include \"ZUNO_DHT.h\"",
"vars": "DHT pinXXXDHT(XXX, PPP1PPP);\n" +
"\n" +
"signed int pinXXXDHTTemperatureState;\n" +
"byte pinXXXDHTHumidityState;",
"channel": " ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_ONE_DECIMAL, pinXXXDHTTemperatureGetter),\n" +
" ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_RELATIVE_HUMIDITY, SENSOR_MULTILEVEL_SCALE_PERCENTAGE_VALUE, SENSOR_MULTILEVEL_SIZE_ONE_BYTE, SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS, pinXXXDHTHumidityGetter)",
"setup": " pinXXXDHT.begin();",
"loop": " pinXXXDHTTemperatureState = pinXXXDHT.readTemperatureC10();\n" +
" pinXXXDHTHumidityState = pinXXXDHT.readHumidity();\n" +
" zunoSendReport(NNN); // report every 30 seconds\n" +
" zunoSendReport(NNN + 1);",
"xetter": "word pinXXXDHTTemperatureGetter() {\n" +
" return pinXXXDHTTemperatureState;\n" +
"}\n" +
"\n" +
"byte pinXXXDHTHumidityGetter() {\n" +
" return pinXXXDHTHumidityState;\n" +
"}",
"preAction": function(params) {
params.channels = 2;
return params;
}
},
"DS18B20": {
"note": "- Connect PPP1PPP DS18B20 sensors.",
"includes": "#include \"ZUNO_DS18B20.h\"",
"vars": "OneWire ow(XXX);\n" +
"DS18B20Sensor ds18b20(&ow);\n" +
"\n" +
"byte addresses[8 * PPP1PPP + 8]; // last one for search\n" +
"byte number_of_sensors; // Number of sensors found (if less than PPP1PPP connected)\n" +
"signed int temperature[PPP1PPP];",
"channel": "PPP2PPP",
"setup": " number_of_sensors = ds18b20.findAllSensors(addresses);",
"loop": "PPP3PPP",
"xetter": "PPP4PPP",
"preAction": function(params) {
params.channels = parseInt(params[1]);
var chDefs = [], codeDefs = [], getterDefs = [];
for (var n = 0; n < params.channels; n++) {
chDefs.push(" ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_ONE_DECIMAL, pinXXXSensorDS18B20Getter_" + (n + 1) + ")");
codeDefs.push(" if (number_of_sensors >= " + (n + 1) + ") {\n temperature[" + n + "] = ds18b20.getTempC100(&addresses[8 * " + n + "])/10;\n zunoSendReport(NNN + " + n + ");\n }");
getterDefs.push("word pinXXXSensorDS18B20Getter_" + (n + 1) + "() {\n return temperature[" + n + "];\n}");
}
params[2] = chDefs.join(",\n");
params[3] = codeDefs.join("\n");
params[4] = getterDefs.join("\n\n");
return params;
}
},
"UART": {
"note": "- No channel is created for UART - define a channel if needed and write the code to send and parse commands.",
"vars": "",
"channel": "",
"setup": " Serial1.begin(PPP1PPP);",
"loop": "",
"funcs": "char readByteUART() {\n" +
" while(Serial1.available() <= 0) delay(1);\n" +
" return Serial1.read();\n" +
"}\n" +
"\n" +
"void writeUART(char b) {\n" +
" Serial1.write(b);\n" +
"}",
"xetter": "",
"preAction": function(params, pin) {
params.channels = 0;
if (pin === "8") params.suppressSetup = params.suppressFuncs = true;
return params;
}
},
"RS485": {
"note": "- No channel is created for RS485 - define a channel if needed and write the code to send and parse commands.\n" +
"- Pin 2 is also occupied by RS485.",
"vars": "",
"channel": "",
"setup": " Serial1.begin(PPP1PPP);\n" +
" pinMode(2, OUTPUT);\n" +
" digitalWrite(2, LOW);",
"loop": "",
"funcs": "char readByteRS485() {\n" +
" while(Serial1.available() <= 0) delay(1);\n" +
" return Serial1.read();\n" +
"}\n" +
"\n" +
"void writeRS485(char b) {\n" +
" digitalWrite(2, HIGH);\n" +
" delay(5);\n" +
" Serial1.write(b);\n" +
" delay(5);\n" +
" digitalWrite(2, LOW);\n" +
"}",
"xetter": "",
"preAction": function(params, pin) {
params.channels = 0;
if (pin === "8") params.suppressSetup = params.suppressFuncs = true;
return params;
}
}
};
function detemplate(template, key, channelNum, assocNum, params) {
if (!template) return template;
Object.keys(params).map(function(paramName) { if (parseInt(paramName)) template = template.replace(new RegExp('PPP' + paramName + 'PPP', 'g'), params[paramName]); });
return template.replace(/XXX/g, key).replace(/NNN/g, channelNum).replace(/AAA/g, assocNum);
}
function pinsToTemplates(pins) {
var templates = [];
var channelNum = 1;
var assocNum = 1;
Object.keys(pins).map(function(key) {
if (pins[key].type === "NC") return; // no channel
var templ = codeTemplates[pins[key].type];
if (!templ) {
alert("Can not find code template for " + pins[key].type);
return;
}
var params = clone(pins[key].params);
if (templ.retemplate) {
var templName = templ.retemplate(params, pins); // change template if needed
if (templName) templ = codeTemplates[templName];
}
if (templ.preAction) params = templ.preAction(params, key, pins); // apply pre action on params
templates.push({
"note": !params.suppressNote && detemplate(templ.note, key, channelNum, assocNum, params),
"includes": !params.suppressIncludes && detemplate(templ.includes, key, channelNum, assocNum, params),
"vars": !params.suppressVars && detemplate(templ.vars, key, channelNum, assocNum, params),
"channel": !params.suppressChannel && detemplate(templ.channel, key, channelNum, assocNum, params),
"report": !params.suppressReport && detemplate(templ.report, key, channelNum, assocNum, params),
"setup": !params.suppressSetup && detemplate(templ.setup, key, channelNum, assocNum, params),
"loop": !params.suppressLoop && detemplate(templ.loop, key, channelNum, assocNum, params),
"xetter": !params.suppressXetter && detemplate(templ.xetter, key, channelNum, assocNum, params),
"funcs": !params.suppressFuncs && detemplate(templ.funcs, key, channelNum, assocNum, params),
"key": key,
});
channelNum += params.channels !== undefined ? params.channels : 1;
assocNum += params.assocs !== undefined ? params.assocs : 0;
});
return templates;
}
function clone(obj) {
var copy = {};
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
copy[prop] = obj[prop];
}
}
return copy;
}
function floatToRatio(x, maxNumerator) {
var tolerance = 1.0E-6;
var h1 = 1, h2 = 0;
var k1 = 0, k2 = 1;
var b = Math.abs(x);
do {
var a = Math.floor(b);
var aux = h1;
if (a*h1+h2 >= maxNumerator) break;
h1 = a * h1 + h2;
h2 = aux;
aux = k1;
k1 = a * k1 + k2;
k2 = aux;
b = 1 / (b - a);
} while (Math.abs(x - h1 / k1) > x * tolerance);
return [h1 * Math.sign(x), k1];
}
function generateCode(pins) {
var templates = pinsToTemplates(pins);
var includes = templates.map(function(ch) { return ch.includes; }).filter(function(value, index, self) { return self.indexOf(value) === index && !!value; }).join('\n');
var vars = templates.map(function(ch) { return ch.vars; } ).filter(function(value) { return !!value; }).join('\n\n');
var channels = templates.map(function(ch) { return ch.channel; } ).filter(function(value) { return !!value; }).join(',\n');
var reports = templates.map(function(ch) { return ch.report; } ).filter(function(value) { return !!value; }).join(',\n');
var setup = templates.map(function(ch) { return ch.setup; } ).filter(function(value) { return !!value; }).join('\n\n');
var loop = templates.map(function(ch) { return ch.loop; } ).filter(function(value) { return !!value; }).join('\n\n');
var xetter = templates.map(function(ch) { return ch.xetter; } ).filter(function(value) { return !!value; }).join('\n\n');
var funcs = templates.map(function(ch) { return ch.funcs; } ).filter(function(value) { return !!value; }).join('\n\n');
var notes = templates.map(function(ch) { return ch.note; } ).filter(function(value) { return !!value; }).join('\n\n');
var keys = templates.map(function(ch) { if (ch.note) return ch.key; } ).filter(function(value) { return !!value; }).join(',');
if (!includes && !vars && !channels && !setup && !loop && !xetter && !notes && !funcs)
return {
"code": "// Please select features",
"notes": "No notes"
};
return {
"code":
"" + includes + (includes ? "\n\n" : "") + "" +
"// Global variables\n\n" +
vars + "\n\n" +
"// Z-Wave channels\n" +
"ZUNO_SETUP_CHANNELS(\n" + channels + "\n);\n\n" +
"" + (reports ? ("ZUNO_REPORTS_HANDLER(reportHandler);\n\n") : "") + "" +
"void setup() {\n" +
setup + "\n" +
"}\n\n" +
"void loop() {\n" +
loop + "\n\n" +
" delay(20);\n" +
"}\n\n" +
"// Getters and setters\n\n" +
xetter + "" +
"" + (reports ? ("\n\nvoid reportHandler(void) {\n" + reports + "\n}") : "") +
"" + (funcs ? ("\n\n// Functions\n" + funcs) : ""),
"notes": notes ? notes : "No notes",
"keys": keys ? keys : "No keys"
};
};