diff --git a/firmware/apps/buggy.c b/firmware/apps/buggy.c index 080b5ea..d4edfcd 100644 --- a/firmware/apps/buggy.c +++ b/firmware/apps/buggy.c @@ -8,14 +8,19 @@ static void init(void) { pentatonic_direction(ALL_OUT); photons_init(); + + led_on(RIGHT); } static void run(void) { - uint16_t light = photons_measure(); + uint8_t light = photons_measure(); pentatonic_all_led_set(light >> 3); - wait_ms(500); + led_inv(LEFT); + led_inv(RIGHT); + + wait_ms(100); } REGISTER(run, init, NULL); diff --git a/firmware/include/pentabug/hal.h b/firmware/include/pentabug/hal.h index 6549fc6..dd692fb 100644 --- a/firmware/include/pentabug/hal.h +++ b/firmware/include/pentabug/hal.h @@ -34,6 +34,7 @@ void button_reset(uint8_t btn); void led_set(uint8_t led, uint8_t state); void led_on(uint8_t led); void led_off(uint8_t led); +uint8_t led_state(uint8_t led); void led_inv(uint8_t led); // MOTOR diff --git a/firmware/include/pentabug/photons.h b/firmware/include/pentabug/photons.h index b2cd2ce..5f70ce6 100644 --- a/firmware/include/pentabug/photons.h +++ b/firmware/include/pentabug/photons.h @@ -5,6 +5,8 @@ void photons_init(void); +void photons_stop(void); + uint8_t photons_measure(void); #endif /* PHOTONS_H */ diff --git a/firmware/lib/hal.c b/firmware/lib/hal.c index 4d32d4c..15befcf 100644 --- a/firmware/lib/hal.c +++ b/firmware/lib/hal.c @@ -165,6 +165,14 @@ void led_inv(uint8_t led) { } } +uint8_t led_state(uint8_t led) { + if(led == RIGHT) { + return !(PORTC & (1 << 2)); + } else { + return !(PORTD & (1 << 4)); + } +} + void motor_set(uint8_t state) { if(state) { motor_on(); diff --git a/firmware/lib/photons.c b/firmware/lib/photons.c index 20653f0..5d8c506 100644 --- a/firmware/lib/photons.c +++ b/firmware/lib/photons.c @@ -9,11 +9,15 @@ void photons_init(void) { ADCSRA |= (1 << ADEN); } +void photons_stop(void) { + ADCSRA &= ~(1 << ADEN); +} + uint8_t photons_measure(void) { // save old state - uint8_t old_port = PORTC; - uint8_t old_ddr = DDRC; + uint8_t old_right = led_state(RIGHT); + uint8_t old_left = led_state(LEFT); // all leds off @@ -22,6 +26,7 @@ uint8_t photons_measure(void) { // set to ground for discharge + DDRC |= 1 << 3; PORTC &= ~(1 << 3); // wait for discharge @@ -38,6 +43,7 @@ uint8_t photons_measure(void) { // start measurement + // TODO: why can't i move this to the initialization? ADMUX = (1 << REFS0) | (1 << ADLAR); ADCSRA |= (1 << ADPS2) | (1 << ADPS1); @@ -51,14 +57,13 @@ uint8_t photons_measure(void) { uint8_t res = ADCH; - // disable adc - - /*ADCSRA &= ~(1 << ADEN);*/ - // restore state - PORTC = old_port; - DDRC = old_ddr; + PORTC |= 1 << 3; + DDRC |= 1 << 3; + + led_set(RIGHT, old_right); + led_set(LEFT, old_left); // done