Last active
August 29, 2015 14:16
-
-
Save funrep/fcaa3a1dddce34677e12 to your computer and use it in GitHub Desktop.
Datateknik skolprojekt, arduino spelkontroll
This file contains hidden or 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
# funkar bara på linux för tillfället | |
# måste byta ut xdotool till | |
# https://github.com/SavinaRoja/PyUserInput | |
import serial | |
import os | |
from multiprocessing import Process | |
# måste bytas ut mot rätt device | |
# använd skriv så här i kommando prompten | |
# för att hitta rätt device: | |
# $ ls /dev/ttyACM* | |
ser = serial.Serial("/dev/ttyACM1", 9600) | |
# skapa ny process för själva kommandot | |
# så att inte programmet blockerar nästa input | |
def cmd(s): | |
p = Process(target=xdotool, args=(s)) | |
p.start() | |
# själva kommandot, det är detta vi ska | |
# byta ut mot PyUserInput för Windows support | |
def xdotool(s): | |
os.system('xdotool ' + s) | |
while True: | |
try: | |
b = ser.read() # läs en byte från serial porten | |
except: | |
pass | |
print(b) # debugging information ;) | |
if (b == "0"): | |
cmd("key Left") | |
elif (b == "1"): | |
cmd("key Down") | |
elif (b == "2"): | |
pass # centrerad, så vi behöver inte göra nåt | |
elif (b == "3"): | |
cmd("key Up") | |
elif (b == "4"): | |
cmd("key Right") |
This file contains hidden or 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
// baserat på exempel kod från https://www.sparkfun.com/tutorials/171 | |
const byte PIN_ANALOG_X = 0; | |
const byte PIN_ANALOG_Y = 1; | |
// |----------|----|----------| | |
// 0 505 515 1023 | |
const int X_THRESHOLD_LOW = 505; | |
const int X_THRESHOLD_HIGH = 515; | |
const int Y_THRESHOLD_LOW = 500; | |
const int Y_THRESHOLD_HIGH = 510; | |
int x_position; | |
int y_position; | |
int x_direction; | |
int y_direction; | |
// interfacet vi använder till python programmet är: | |
// 0 = pekar åt vänster | |
// 1 = pekar neråt | |
// 2 = centrerad | |
// 3 = pekar uppåt | |
// 4 = pekar åt höger | |
// OBS kanske inte pekar åt det håll som står, | |
// för jag har inte testat än så de är bara som jag | |
// förstått deras tutorial :P | |
void setup() { | |
Serial.begin(9600); | |
} | |
void loop () { | |
x_direction = 0; | |
y_direction = 0; | |
x_position = analogRead(PIN_ANALOG_X); | |
y_position = analogRead(PIN_ANALOG_Y); | |
if (x_position > X_THRESHOLD_HIGH) { | |
x_direction = 1; | |
} else if (x_position < X_THRESHOLD_LOW) { | |
x_direction = -1; | |
} | |
if (y_position > Y_THRESHOLD_HIGH) { | |
y_direction = 1; | |
} else if (y_position < Y_THRESHOLD_LOW) { | |
y_direction = -1; | |
} | |
if (x_direction == -1) { // pekar åt vänster | |
Serial.print(0); | |
} else if (x_direction == 0) { // pekar inte åt sidan | |
if (y_direction == -1) { // pekar neråt | |
Serial.print(1); | |
} else if (y_direction == 0) { // centrerad | |
Serial.print(2); | |
} else { // y_direction är 1, alltså neråt | |
Serial.print(3); | |
} | |
} else { // x_direction är 1, alltså pekar höger | |
Serial.print(4); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment