Last active
December 31, 2015 09:49
-
-
Save vermiculus/7969727 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
/* Blink.ino */ | |
int pin; | |
int state; | |
void do_serial_read ( void ); | |
int read_number ( char * ); | |
void setup ( void ) { | |
Serial.begin(9600); | |
pin = 13; | |
state = 0; | |
} | |
void loop ( void ) { | |
Serial.println("Hello, Pi!"); | |
if (Serial.available()) { | |
do_serial_read(); | |
Serial.print("Updating state of pin "); | |
Serial.print(pin); | |
Serial.print(" to state "); | |
Serial.print(state); | |
Serial.println("."); | |
digitalWrite(pin, state); | |
} | |
} | |
void do_serial_read ( void ) { | |
Serial.println("Trying to read arguments"); | |
char *pin_str, *stt_str; | |
int arg_ind, chr_ind; | |
char in; | |
pin_str = (char *) malloc(4 * sizeof(char)); | |
stt_str = (char *) malloc(4 * sizeof(char)); | |
Serial.println("Memory allocated!"); | |
pin_str[3] = '\0'; | |
stt_str[3] = '\0'; | |
arg_ind = 0; | |
while (Serial.available() > 0) { | |
Serial.println("Reading character."); | |
if (chr_ind > 3) { | |
Serial.println("Unable to parse :: input too long!"); | |
return; | |
} | |
in = Serial.read(); | |
if (in == ':') { | |
arg_ind = 1; | |
chr_ind = 0; | |
Serial.println("Switching to second argument"); | |
} else { | |
(arg_ind ? pin_str : stt_str)[chr_ind++] = in; | |
} | |
} | |
Serial.println("Arguments read."); | |
pin = read_number(pin_str); | |
state = read_number(stt_str); | |
free(pin_str); | |
free(stt_str); | |
} | |
int read_number (char *str) { | |
Serial.println("Parsing."); | |
int n, pt; | |
n = 0; | |
for(pt = 0; str[pt] != '\0'; pt++) { | |
Serial.println("Parsed character"); | |
n += str[pt] - '0'; | |
} | |
return n; | |
} | |
/* Local Variables: */ | |
/* mode: c */ | |
/* End: */ |
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
# Blink.py | |
import sys | |
pin, state = sys.argv[1], sys.argv[2] | |
def immediate(s): | |
print s | |
sys.stdout.flush() | |
try: | |
int(pin) | |
int(state) | |
except: | |
print('Your numbers are bad and you should feel bad.') | |
exit() | |
immediate('Importing Serial library...') | |
import serial | |
immediate('Done.') | |
immediate('Opening connection to Arduino...') | |
tty = '/dev/ttyACM0' | |
arduino = serial.Serial(tty, 9600) | |
immediate('Connection established.') | |
to_arduino = '{}:{}'.format(pin, state) | |
immediate('Writing command `{}` to Arduino on {}...'.format(to_arduino, tty)) | |
arduino.write(to_arduino) | |
immediate('Command sent. Waiting for response...') | |
while True: | |
print ' > ', arduino.readline() | |
immediate('Closing connection Arduino...') | |
arduino.close() | |
immediate('Done') |
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
https://gist.github.com/7969727 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment