-
-
Save anonymous/b6a382e4b385a79c0a7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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