Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First Arduino: show score and play sound
- Loading branch information
1 parent
65f1f54
commit c4e473c
Showing
9 changed files
with
145 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <LiquidCrystal.h> | ||
#include "pitches.h" | ||
|
||
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); | ||
|
||
void setup() { | ||
Serial.begin(9600); | ||
lcd.begin(16, 2); // init cols and rows | ||
lcd.print("init"); | ||
} | ||
|
||
void loop() { | ||
if (Serial.available()) { | ||
delay(100); | ||
|
||
// first char declares what we receive | ||
char c = Serial.read(); | ||
if (c == 'S') { // SOUND | ||
c = Serial.read(); | ||
|
||
int t = NOTE_A5; // DEFAULT (HOME) | ||
if (c == 'A') t = NOTE_C5; // AWAY | ||
|
||
tone(2, t, 500); | ||
} else if (c == 'D') { // DISPLAY | ||
lcd.clear(); | ||
while (Serial.available() > 0) { | ||
c = Serial.read(); | ||
if (c == '\n') { | ||
lcd.setCursor(0, 1); // first col, second line | ||
} else { | ||
lcd.write(c); | ||
} | ||
} | ||
} | ||
} | ||
} |
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,12 @@ | ||
import serial | ||
import time | ||
|
||
s = serial.Serial("COM3", 9600) # port is COM3, and baud rate is 9600 | ||
time.sleep(2) # wait for the Serial to initialize | ||
s.write(b'Ready...') | ||
while True: | ||
text = input('Enter text: ') | ||
text = text.strip() | ||
if text == 'exit': | ||
break | ||
s.write(bytes(text, "utf8")) |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
|
@@ -4,4 +4,5 @@ numpy | |
scikit-learn | ||
imutils | ||
scikit-image | ||
flask | ||
flask | ||
pySerial |
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,47 @@ | ||
import serial | ||
import time | ||
|
||
DISPLAY = 'D' | ||
SOUND = 'S' | ||
|
||
|
||
class ArduinoConnector: | ||
def __init__(self, port, config, baud_rate=9600): | ||
self.serial = serial.Serial(port, baud_rate) | ||
self.config = config | ||
|
||
self.last_call = time.time() | ||
|
||
def has_feature(self, feature): | ||
return "features" in self.config and feature in self.config["features"] | ||
|
||
def print_score(self, home, away): | ||
""" | ||
Write D{current scores} to the serial port to show current scores on LCD | ||
:param home: | ||
:param away: | ||
:return: | ||
""" | ||
|
||
# make sure to only write to the LCD every few seconds, | ||
# otherwise the LCD will mix up the received text | ||
now = time.time() | ||
if now - self.last_call < 2: | ||
return | ||
|
||
self.last_call = now | ||
|
||
score_text = f"HOME AWAY\n {home} -- {away}" | ||
self.write(DISPLAY, score_text) | ||
|
||
def goal_scored(self, result): | ||
""" | ||
Write S{TEAM} to the serial port to play a sound when a goal is scored, e.g. SA, when Away team scores. | ||
:param result: | ||
:return: | ||
""" | ||
team = "H" if result["team"] == 0 else "A" | ||
self.write(SOUND, team) | ||
|
||
def write(self, write_type, content=""): | ||
self.serial.write(bytes(f"{write_type}{content}", "utf8")) |
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,20 @@ | ||
class Broadcaster: | ||
def __init__(self): | ||
self.onChange = EventHook() | ||
|
||
|
||
class EventHook(object): | ||
def __init__(self): | ||
self.__handlers = [] | ||
|
||
def __iadd__(self, handler): | ||
self.__handlers.append(handler) | ||
return self | ||
|
||
def __isub__(self, handler): | ||
self.__handlers.remove(handler) | ||
return self | ||
|
||
def fire(self, *args, **keywargs): | ||
for handler in self.__handlers: | ||
handler(*args, **keywargs) |
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