diff --git a/firmware/apps/buggy.c b/firmware/apps/buggy.c new file mode 100644 index 0000000..e04d436 --- /dev/null +++ b/firmware/apps/buggy.c @@ -0,0 +1,21 @@ +#include + +#include +#include +#include +#include + +static void init(void) { + pentatonic_direction(ALL_OUT); + photons_init(); +} + +static void run(void) { + uint16_t light = photons_measure(); + + pentatonic_all_led_set(light >> 7); + + wait_ms(500); +} + +REGISTER(run, init, NULL); diff --git a/firmware/include/pentabug/hal.h b/firmware/include/pentabug/hal.h index 87f2db9..6549fc6 100644 --- a/firmware/include/pentabug/hal.h +++ b/firmware/include/pentabug/hal.h @@ -55,7 +55,7 @@ void buzzer_off(void); // IR -// all this functions control whether a 38kHz modulated IR signal is sent +// all these functions control whether a 38kHz modulated IR signal is sent void ir_on(void); void ir_off(void); void ir_inv(void); diff --git a/firmware/include/pentabug/photons.h b/firmware/include/pentabug/photons.h new file mode 100644 index 0000000..ff2b5cd --- /dev/null +++ b/firmware/include/pentabug/photons.h @@ -0,0 +1,10 @@ +#ifndef PHOTONS_H +#define PHOTONS_H + +#include + +void photons_init(void); + +uint16_t photons_measure(void); + +#endif /* PHOTONS_H */ diff --git a/firmware/lib/hal.c b/firmware/lib/hal.c index f379abb..4d32d4c 100644 --- a/firmware/lib/hal.c +++ b/firmware/lib/hal.c @@ -113,6 +113,10 @@ void reset_hw(void) { // turn ir off ir_off(); + + // disable adc + + ADCSRA &= ~(1 << ADEN); } uint8_t button_state(uint8_t btn) { diff --git a/firmware/lib/photons.c b/firmware/lib/photons.c new file mode 100644 index 0000000..498236c --- /dev/null +++ b/firmware/lib/photons.c @@ -0,0 +1,58 @@ +#include + +#include + +#include +#include + +void photons_init(void) {} + +uint16_t photons_measure(void) { + // save old state + + uint8_t old_port = PORTC; + uint8_t old_ddr = DDRC; + + // all leds off + + led_off(RIGHT); + led_off(LEFT); + + // set direction, no pullups + + DDRC &= ~(1 << 3); + PORTC &= ~(1 << 3); + + // wait for discharge + // TODO: needed? + + wait_ms(10); + + // start measurement + + ADMUX = (1 << REFS0); + ADCSRA = (1 << ADPS2) | (1 << ADPS1); + ADCSRA |= (1 << ADEN); + + ADMUX = (ADMUX & ~(0x1F)) | 3; + ADCSRA |= (1 << ADSC); + + // wait for measurement to finish + + while (ADCSRA & (1 << ADSC)) test_stop_app(); + + uint16_t res = ADCW; + + // disable adc + + ADCSRA &= ~(1 << ADEN); + + // restore state + + PORTC = old_port; + DDRC = old_ddr; + + // done + + return res; +}