arduino stuffs
Diffstat (limited to 'car_driver/processing/processing.pde')
-rw-r--r--car_driver/processing/processing.pde45
1 files changed, 45 insertions, 0 deletions
diff --git a/car_driver/processing/processing.pde b/car_driver/processing/processing.pde
new file mode 100644
index 0000000..5e10e54
--- /dev/null
+++ b/car_driver/processing/processing.pde
@@ -0,0 +1,45 @@
+import processing.serial.*;
+
+private static char STOP = 'v';
+private static char HEADER = 'H';
+
+char current = STOP;
+
+Serial port;
+
+void setup() {
+ frameRate(5);
+ printArray(Serial.list());
+ port = new Serial(this, Serial.list()[0], 9600);
+}
+
+
+void draw() {} // Empty draw() needed to keep the program running
+
+void keyPressed() {
+ updateKeys(key, true);
+}
+
+void keyReleased() {
+ updateKeys(key, false);
+}
+
+boolean isValidKey(char key) {
+ return key == 'a' || key == 'w' || key == 's' || key == 'd';
+}
+
+void write(char key) {
+ char data[] = {HEADER, key};
+ String towrite = new String(data);
+ println(towrite);
+ port.write(towrite);
+}
+
+void updateKeys(char key, boolean on) {
+ if (isValidKey(key)) {
+ if (!on || key != current) {
+ current = on ? key : STOP;
+ write(current);
+ }
+ }
+}