-
Notifications
You must be signed in to change notification settings - Fork 0
/
tomato.arduino.js
125 lines (108 loc) · 3.28 KB
/
tomato.arduino.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
/*jshint node:true */
"use strict";
var fs = require('fs');
var serialport = require('serialport');
var color = require('cli-color');
var util = require('util');
var events = require('eventemitter2');
function Arduino (serialportDev) {
events.EventEmitter2.call(this);
this.serialportDev = serialportDev || undefined;
}
util.inherits(Arduino, events.EventEmitter2);
Arduino.prototype.detectSerialport = function () {
if (this.serialportDev) {
console.log("Using hardcoded SERIALPORT for Arduino dev: ", this.serialportDev);
return;
}
var devs = fs.readdirSync('/dev/');
var possibleArduinoDevs = [];
for (var i in devs) {
if (devs[i].match(/^tty.*?(USB|ACM).*?/i)) {
possibleArduinoDevs.push(devs[i]);
}
}
if (possibleArduinoDevs.length > 0) {
console.log("Found possible Arduinos:");
for (var j in possibleArduinoDevs) {
console.log(" ", possibleArduinoDevs[j]);
}
console.log("");
var arduinoDev = possibleArduinoDevs[0];
console.log("Using: ", arduinoDev);
console.log("");
this.serialportDev = "/dev/"+arduinoDev;
} else {
console.error(color.redBright("!!! NO Arduino DEVICE FOUND!!!\nPlease make sure your Arduino is plugged in and has you rock_paper_arduino sketch uploaded."));
this.serialportDev = undefined;
}
};
Arduino.prototype.openSerialport = function () {
this.serialport = new serialport.SerialPort(this.serialportDev, {
parser: serialport.parsers.readline("\r\n")
});
this.bindInputProcessor();
};
Arduino.prototype.bindInputProcessor = function () {
var arduino = this;
this.serialport.on("data", function (data) {
var input = data.toString();
arduino.processInput(input);
});
};
Arduino.prototype.writeEvent = function(event, arg) {
if (arg)
this.writeToSerialport("> "+event+" "+arg);
else
this.writeToSerialport("> "+event);
};
Arduino.prototype.writeState = function(state) {
this.writeToSerialport("= "+state);
};
Arduino.prototype.writeToSerialport = function(data) {
console.log(color.blackBright(">> [TO ARDUINO]"), util.inspect(data, true, null, true));
this.serialport.write(data+"\n");
this.serialport.flush(); // might not be necessary?
};
Arduino.prototype.processInput = function (input) {
console.log(color.blackBright("<< [FROM ARDUINO]"), util.inspect(input, true, null, true));
console.log(input);
var c = input.split(" ");
console.log(c);
var ctrl = c[0];
var ev = c[1];
var arg = c[2];
if (input === '~') {
this.emit('online');
//rpa.prepare();
} else if (ctrl === '<') {
// NOTE: arg can be undefined
this.emit(ev, arg);
//rpa.fsm.handle(cmd, arg);
} else {
console.warn(color.red("!!! Unknown message from Arduino: "), input.toString());
}
// switch (cmd) {
// case '~':
// rpa.enterDojo();
// break;
// case 'n':
// rpa.bow();
// break;
// case 'r':
// case 'p':
// case 's':
// rpa.setYourWeapon(cmd);
// break;
// default:
// var match;
// if (match = cmd.match(/\[(\d)/)) {
// arduinoStateChange(parseInt(match[1]));
// } else if (match = cmd.match(/!(.+)/)) {
// arduinoInvalidTransition(match[1]);
// } else {
// console.warn(color.red("!!! Unknown message from Arduino: "), cmd.toString());
// }
// }
};
exports.Arduino = Arduino;