Create a gist now

Instantly share code, notes, and snippets.

anonymous /arduino-controller Secret
Created Nov 17, 2014

What would you like to do?
var Arduino = function () {
var self = this;
// constans block
self.LISTENING_MESSGAE = "LISTENING";
self.STOP_GAME_MESSAGE = "STOP";
...
var portIsReady = true;
// init SerialPort
var serialPort = new SerialPort(self.PORT_NUMBER, {
parser: serialport.parsers.readline("\r\n"),
baudrate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false
}, true);
// open connection and listening port
serialPort.on(self.PORT_OPEN_COMMAND, function () {
serialPort.on(self.PORT_RECEIVE_DATA_COMMAND, function (arduinoMessage) {
if (arduinoMessage === self.LISTENING_MESSGAE) { // arduino is ready
self.emit(self.ARDUINO_READY_COMMAND, arduinoMessage);
} else if (arduinoMessage === self.STOP_GAME_MESSAGE) { // stop command or timeout stop
self.emit(self.ARDUINO_IS_STOPPED, arduinoMessage);
} else if (arduinoMessage === self.GOAL_WHITE_MESSAGE || arduinoMessage === self.GOAL_BLUE_MESSAGE) { // goal in white gate (point for blue team)
self.emit(self.ARDUINO_GOAL, arduinoMessage);
}
});
});
serialPort.on(self.PORT_CLOSE_COMMAND, function () {
portIsReady = false;
});
serialPort.on(self.PORT_ERROR_COMMAND, function () {
portIsReady = false;
});
self.on(self.ARDUINO_READY_COMMAND, function () {
portIsReady = true;
});
self.on(self.ARDUINO_START_COMMAND, function () {
if (portIsReady) {
serialPort.write(self.START_GAME_COMMAND);
}
});
self.on(self.ARDUINO_STOP_COMMAND, function () {
if (portIsReady) {
serialPort.write(self.STOP_GAME_COMMAND);
}
});
self.start = function () {
self.emit(self.ARDUINO_START_COMMAND);
};
self.stop = function () {
self.emit(self.ARDUINO_STOP_COMMAND);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment