-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
160 lines (134 loc) · 3.87 KB
/
index.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
'use strict';
var _ = require("underscore");
function add_names(d) {
_.keys(d).forEach((k) => {
if (!_.has(d[k], "name")) {
d[k].name = k;
}
});
}
function run_inherit(d) {
var result = {};
_.keys(d).forEach((k) => {
var i = d[k];
var chain = [i];
while (_.has(i, "inherit")) {
var tgt = i.inherit;
if (typeof tgt === 'string') {
if (_.has(d,tgt))
tgt = d[tgt];
else
console.error(tgt, "not found");
}
chain.unshift(tgt);
i = tgt;
}
//console.log(chain.map((i) => i.name));
result[k] = Object.assign({}, ...chain);
delete result[k].inherit;
});
return result;
}
function trim_obj(obj) {
if (!Array.isArray(obj) && typeof obj != 'object') return obj;
return Object.keys(obj).reduce(function(acc, key) {
acc[key] = typeof obj[key] == 'string'? obj[key].trim() : trim_obj(obj[key]);
return acc;
}, Array.isArray(obj)? []:{});
}
var boards = require("./data_boards.js");
var shields = require("./data_shields.js");
var examples = require("./data_examples.js");
add_names(boards);
add_names(shields);
add_names(examples);
boards = run_inherit(boards);
shields = run_inherit(shields);
function generate(board, shield, example, auth_token, tmpl_id, dev_name) {
if (boards[board] === undefined) return "No such board";
if (shields[shield] === undefined) return "No such shield";
if (examples[example] === undefined) return "No such example";
var data = {
auth: auth_token || "YourAuthToken",
tmpl_id: tmpl_id || "TMPxxxxxx",
dev_name: dev_name || "Device",
defines: "\b",
board : {
comment: "\b",
defs: "\b",
inc: "\b",
glob: "\b",
init: "\b",
loop: "\b"
},
example: {
comment: "\b",
defs: "\b",
inc: "\b",
glob: "\b",
init: "\b",
loop: "\b"
}
};
_.extend(data, boards[board]);
_.extend(data.board, shields[shield]);
_.extend(data.example, examples[example]);
if (("need_serial" in data.board) && !("serial_dat" in data)) {
data.serial_dat = "SwSerial";
data.board.inc = `
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
${data.board.inc}`;
}
if ("swap_serial" in data.board) {
var tmp = data.serial_dbg;
data.serial_dbg = data.serial_dat;
data.serial_dat = tmp;
}
data = trim_obj(data);
//return JSON.stringify(data);
var code = data.template;
for (var i = 0; i < 5; i++) {
var t = _.template(code);
code = t(data);
if (code.indexOf("<%") == -1) {
break;
}
}
code = code.replace(/\r\n/g, "\n");
code = code.replace(/\n[\s]*[\b]\n/g, "\n");
code = code.replace(/[\b]/g,"");
//code = code.replace(/\n\n\n/g, "\n\n");
code += "\n\n";
return code;
}
function getBoardShields(board) {
board = boards[board];
var result = [];
if (_.has(board, "builtin")) {
result.push("--- Built In");
Array.prototype.push.apply(result, board.builtin);
}
var all_shields = _.keys(shields);
if (_.has(board, "exclude")) {
var regexs = board.exclude;
all_shields = all_shields.filter(function (text) {
if (text.startsWith("---")) return true;
return !regexs.some(function (regex) {
return regex.test(text);
});
});
}
all_shields = all_shields.filter((k) => (shields[k].embedded !== true));
Array.prototype.push.apply(result, all_shields);
return result;
}
/*************************************************/
/*module.exports.boards = boards
module.exports.shields = shields
module.exports.examples = examples*/
module.exports.listBoards = function() { return Object.keys(boards) };
module.exports.listShields = function() { return Object.keys(shields) };
module.exports.listExamples = function() { return Object.keys(examples) };
module.exports.getBoardShields = getBoardShields;
module.exports.generate = generate;