Skip to content

Instantly share code, notes, and snippets.

@cutalion
Last active April 7, 2016 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cutalion/051882348d5645bb5dd2611aa29f9672 to your computer and use it in GitHub Desktop.
Save cutalion/051882348d5645bb5dd2611aa29f9672 to your computer and use it in GitHub Desktop.
Arduino formatter for rspec
--color
--require spec_helper
--format Fuubar
--format ArduinoFormatter
class ArduinoFormatter
# This registers the notifications this formatter supports, and tells
# us that this was written against the RSpec 3.x formatter API.
RSpec::Core::Formatters.register self, :start, :dump_failures
def initialize(*args)
system 'stty -F /dev/ttyACM0 9600 cs8'
@tty = File.open('/dev/ttyACM0', 'w')
# sleep(2) # if auto-reset is turned ON on the Arduino is on
end
def start(_)
@tty.putc('y')
end
def dump_failures(notification)
if notification.failure_notifications.empty?
@tty.putc('g')
else
@tty.putc('r')
end
@tty.close
end
end
#define RED 10
#define YELLOW 11
#define GREEN 12
int incomingByte = 0;
int leds[3] = { RED, YELLOW, GREEN };
int leds_count = 3;
void setup() {
for(int i = 0; i < 3; i++) {
pinMode(leds[i], OUTPUT);
}
Serial.begin(9600);
}
void loop() {
}
void serialEvent() {
incomingByte = Serial.read();
switch(incomingByte) {
case 103: // g, green
turnOn(GREEN);
break;
case 121: // y, yellow
turnOn(YELLOW);
break;
case 114: // r, red
turnOn(RED);
break;
}
}
void turnOn(int pin) {
for(int i = 0; i < 3; i++) {
digitalWrite(leds[i], pin == leds[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment