-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
198 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
backend/leaderboard.json | ||
backend/leaderboard.json | ||
package-lock.json |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <ezButton.h> | ||
|
||
#define BUTTON0 2 | ||
#define BUTTON1 3 | ||
#define RESETBUTTON 4 | ||
#define LED 13 | ||
|
||
ezButton button0(BUTTON0); | ||
ezButton button1(BUTTON1); | ||
ezButton resetButton(RESETBUTTON); | ||
|
||
void setup() { | ||
Serial.begin(9600); | ||
pinMode(LED, OUTPUT); | ||
button0.setDebounceTime(50); | ||
button1.setDebounceTime(50); | ||
resetButton.setDebounceTime(50); | ||
} | ||
|
||
#define LOW 0 | ||
#define HIGH 1 | ||
#define BLINKING 2 | ||
|
||
int ledState = LOW; | ||
int blinkTime = 1000; | ||
unsigned long lastTime = 0; | ||
|
||
void loop() { | ||
button0.loop(); | ||
button1.loop(); | ||
resetButton.loop(); | ||
|
||
if (button0.isPressed()) { | ||
Serial.println("button-0-1"); | ||
} | ||
|
||
if (button0.isReleased()) { | ||
Serial.println("button-0-0"); | ||
} | ||
|
||
if (button1.isPressed()) { | ||
Serial.println("button-1-1"); | ||
} | ||
|
||
if (button1.isReleased()) { | ||
Serial.println("button-1-0"); | ||
} | ||
|
||
if (resetButton.isPressed()) { | ||
Serial.println("reset-1"); | ||
} | ||
|
||
if (resetButton.isReleased()) { | ||
Serial.println("reset-0"); | ||
} | ||
|
||
if (Serial.available()) { | ||
String command = Serial.readStringUntil('\n'); | ||
if (command == "led-on") { | ||
ledState = HIGH; | ||
digitalWrite(LED, HIGH); | ||
} else if (command == "led-off") { | ||
ledState = LOW; | ||
digitalWrite(LED, LOW); | ||
} else if (command.startsWith("led-blink")) { | ||
ledState = BLINKING; | ||
blinkTime = command.substring(10).toInt(); | ||
lastTime = millis(); | ||
} | ||
} | ||
|
||
if (ledState == BLINKING) { | ||
if (millis() - lastTime > blinkTime) { | ||
lastTime += blinkTime; | ||
digitalWrite(LED, !digitalRead(LED)); | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { SerialPort } from 'serialport'; | ||
|
||
import config from './config.js'; | ||
|
||
import { timer } from './index.js'; | ||
|
||
export default class SerialIO { | ||
constructor() { | ||
this.lastLedState = 'off'; | ||
this.lastButton1Press = -1000; | ||
this.lastButton2Press = -1000; | ||
this.states = [0, 0]; | ||
this.openTimeout = null; | ||
|
||
this.buffer = ''; | ||
|
||
this.serial = new SerialPort({ | ||
path: config.serialPort, | ||
baudRate: 9600, | ||
autoOpen: false, | ||
}); | ||
|
||
this.serial.on('data', (data) => { | ||
this.buffer += data.toString(); | ||
let lines = this.buffer.split('\n'); | ||
this.buffer = lines.pop(); | ||
for (let line of lines) { | ||
this.handleCommand(line); | ||
} | ||
}); | ||
|
||
timer.on('start', () => { | ||
this.setLed('on'); | ||
}); | ||
timer.on('stop', () => { | ||
this.setLed('off'); | ||
}); | ||
timer.on('reset', () => { | ||
this.setLed('off'); | ||
}); | ||
timer.on('newHighscore', () => { | ||
this.setLed('blink', 200); | ||
}); | ||
|
||
if (config.serialPort) this.begin(); | ||
} | ||
|
||
begin() { | ||
this.serial.open((err) => { | ||
if (err) { | ||
console.error(err); | ||
this.openTimeout = setTimeout(() => { | ||
this.begin(); | ||
}, 1000); | ||
} | ||
}); | ||
} | ||
|
||
handleCommand(command) { | ||
if (command.startsWith('button')) { | ||
let [_, pin, state] = command.split('-'); | ||
this.handleButtonUpdate(parseInt(pin), parseInt(state)); | ||
} else if (command.startsWith('resetButton')) { | ||
let [_, state] = command.split('-'); | ||
this.handleResetButton(parseInt(state)); | ||
} | ||
} | ||
|
||
handleButtonUpdate(pin, state) { | ||
this.states[pin] = state; | ||
this[`lastButton${pin}Press`] = performance.now(); | ||
if (this.timer.isRunning) { | ||
for (let i = 0; i < 2; i++) { | ||
if (this.states[i] === 0 && performance.now() - this[`lastButton${i + 1}Press`] < 500) { // debounce | ||
this.states[i] = 1; | ||
} | ||
} | ||
if (states.every(s => s === 1)) { | ||
timer.stop(); | ||
} | ||
} else { | ||
if (states.every(s => s === 0)) { | ||
timer.start(); | ||
this.lastButton1Press -= 1000; | ||
this.lastButton2Press -= 1000; | ||
} | ||
} | ||
} | ||
|
||
handleResetButton(state) { | ||
if (state === 1) { | ||
timer.reset(); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {('on'|'off'|'blink')} state | ||
* @param {number} interval (optional, only used for 'blink' state) | ||
*/ | ||
setLed(state, interval = 500) { | ||
if (state === 'blink') { | ||
this.lastLedState = 'blink' + interval; | ||
} else { | ||
this.lastLedState = state; | ||
} | ||
this.serial.write('\nled-' + this.lastLedState + '\n'); | ||
} | ||
} |
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
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
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