forked from google/blockly
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zig_functions.js
133 lines (118 loc) · 3.51 KB
/
zig_functions.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
/**
* @license
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Code Generator Functions for Zig Custom Blocks
* @suppress {checkTypes|visibility}
*/
'use strict';
goog.module('Blockly.Zig.functions');
const Zig = goog.require('Blockly.Zig');
// Generate CBOR Message
Zig['compose_msg'] = function(block) {
// Convert each Message Field to Zig Code
var elements = new Array(block.itemCount_);
for (var i = 0; i < block.itemCount_; i++) {
elements[i] = Blockly.Zig.valueToCode(block, 'ADD' + i,
Blockly.Zig.ORDER_NONE) || '\'\'';
}
// Combine the Message Fields into a CBOR Message
const code = [
'try composeCbor(.{ // Compose CBOR Message',
// Insert the indented elements.
Blockly.Zig.prefixLines(
elements.join('\n'),
Blockly.Zig.INDENT),
'})',
].join('\n');
return [code, Blockly.Zig.ORDER_UNARY_POSTFIX];
};
// Generate a field for CBOR message
Zig['field'] = function(block) {
const name = block.getFieldValue('NAME');
const value = Blockly.Zig.valueToCode(block, 'name', Blockly.JavaScript.ORDER_ATOMIC);
const code = `"${name}", ${value},`;
return [code, Blockly.Zig.ORDER_NONE];
};
// Run this code every N seconds
Zig['every'] = function(block) {
const duration = block.getFieldValue('DURATION');
const stmts = Blockly.Zig.statementToCode(block, 'STMTS');
const code = [
``,
`// Every ${duration} seconds...`,
`while (true) {`,
stmts,
Blockly.Zig.INDENT + `// Wait ${duration} seconds`,
Blockly.Zig.INDENT + `_ = c.sleep(${duration});`,
`}`,
``,
].join('\n');
return code;
};
// Read BME280 Sensor
Zig['bme280'] = function(block) {
// Get the Sensor Data Field: temperature / pressure / humidity
const field = block.getFieldValue('FIELD');
// Get the Sensor Device Path, like "/dev/uorb/sensor_baro0"
// TODO: Validate that path contains "sensor_humi" for humidity
const path = block.getFieldValue('PATH');
// Struct name is "sensor_humi" for humidity, else "sensor_baro"
const struct = (field == 'humidity')
? 'struct_sensor_humi'
: 'struct_sensor_baro';
// Compose the code
const code = alignComments([
`try sen.readSensor( // Read BME280 Sensor`,
Blockly.Zig.INDENT + `c.${struct}, // Sensor Data Struct`,
Blockly.Zig.INDENT + `"${field}", // Sensor Data Field`,
Blockly.Zig.INDENT + `"${path}" // Path of Sensor Device`,
`)`,
]).join('\n');
return [code, Blockly.Zig.ORDER_UNARY_POSTFIX];
};
// Transmit CBOR Message
Zig['transmit_msg'] = function(block) {
const msg = Blockly.Zig.valueToCode(block, 'MSG', Blockly.Zig.ORDER_ATOMIC);
const to = block.getFieldValue('TO');
const code = [
``,
`// Transmit message to LoRaWAN`,
`try transmitLorawan(${msg});`,
``,
].join('\n');
return code;
};
// Align the "//" comments by inserting spaces, except first line
function alignComments(lines) {
// Find the max column of "//"
var col = -1;
var firstLine = true;
for (const line of lines) {
const i = line.indexOf('//');
if (firstLine || i < 0) {
firstLine = false;
continue;
}
col = Math.max(col, i);
}
if (col < 0) { return lines; }
// Insert spaces before "//" to align them
var result = [];
firstLine = true;
for (const line of lines) {
const i = line.indexOf('//');
if (firstLine || i < 0) {
firstLine = false;
result.push(line);
continue;
}
const line2 = line.replace(
'//',
' '.repeat(col - i) + '//'
);
result.push(line2);
}
return result;
}