![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ft. button press reset
Fork of BeaconDemo_RobotCode by
Diff: PsiSwarm/led.cpp
- Revision:
- 0:8a5497a2e366
- Child:
- 6:ff3c66f7372b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PsiSwarm/led.cpp Sat Oct 03 22:48:50 2015 +0000 @@ -0,0 +1,119 @@ +/* University of York Robotics Laboratory PsiSwarm Library: LED Functions Source File + * + * File: led.cpp + * + * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York + * + * PsiSwarm Library Version: 0.2 + * + * September 2015 + * + */ + +#include "psiswarm.h" + +char green_led_states; +char red_led_states; +char center_led_state; + +char held_red_states; +char held_green_states; +Timeout blink_led_timeout; + +unsigned short get_led_states(){ + return (green_led_states << 8) + red_led_states; +} + +void set_leds(char green, char red){ + green_led_states = green; + red_led_states = red; + IF_update_leds(); +} + +void set_base_led(char state){ + IF_set_base_LED(state); +} + +void set_green_leds(char green){ + green_led_states = green; + IF_update_leds(); +} + +void set_red_leds(char red){ + red_led_states = red; + IF_update_leds(); +} + +void set_led(char led, char state){ + if(state % 2 == 1) red_led_states |= 1 << led; + else red_led_states &= ~(1 << led); + if(state / 2) green_led_states |= 1 << led; + else green_led_states &= ~(1 << led); + IF_update_leds(); +} + +void blink_leds(float timeout){ + held_red_states = red_led_states; + held_green_states = green_led_states; + set_leds(0xFF,0xFF); + blink_led_timeout.attach(&IF_restore_led_state, timeout); +} + +void set_center_led(char state){ + set_center_led(state, center_led_brightness); +} + +void set_center_led(char state, float brightness){ + center_led_brightness = brightness; + center_led_state = state; + switch(center_led_state){ + case 0: center_led_red.write(0); + center_led_green.write(0); + break; + case 1: center_led_red.write(center_led_brightness / 4); + center_led_green.write(0); + break; + case 2: center_led_red.write(0); + center_led_green.write(center_led_brightness); + break; + case 3: center_led_red.write(center_led_brightness / 4); + center_led_green.write(center_led_brightness); + break; + } +} + +void set_center_led_brightness(float brightness){ + set_center_led(center_led_state,brightness); +} + +void IF_restore_led_state(){ + set_leds(held_green_states,held_red_states); +} + +void IF_init_leds(){ + green_led_states = 0; + red_led_states = 0; + center_led_red.period_us(100); + center_led_green.period_us(100); + set_center_led(0,0.2); +} + +void IF_update_leds(){ + char led_byte_0 = (((green_led_states & (1 << 3)) == 0) << 7) + + (((green_led_states & (1 << 2)) == 0) << 5) + + (((green_led_states & (1 << 1)) == 0) << 3) + + (((green_led_states & (1)) == 0) << 1) + + (((red_led_states & (1 << 3)) == 0) << 6) + + (((red_led_states & (1 << 2)) == 0) << 4) + + (((red_led_states & (1 << 1)) == 0) << 2) + + (((red_led_states & (1)) == 0)); + char led_byte_1 = (((green_led_states & (1 << 7)) == 0) << 7) + + (((green_led_states & (1 << 6)) == 0) << 5) + + (((green_led_states & (1 << 5)) == 0) << 3) + + (((green_led_states & (1 << 4)) == 0) << 1) + + (((red_led_states & (1 << 7)) == 0) << 6) + + (((red_led_states & (1 << 6)) == 0) << 4) + + (((red_led_states & (1 << 5)) == 0) << 2) + + (((red_led_states & (1 << 4)) == 0)); + IF_write_to_led_ic(led_byte_0,led_byte_1); +} \ No newline at end of file