Improve photon stuff

This commit is contained in:
Thammi 2013-09-03 21:25:07 +02:00
parent 9e09d510ac
commit 59933f9158
5 changed files with 31 additions and 10 deletions

View File

@ -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);

View File

@ -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

View File

@ -5,6 +5,8 @@
void photons_init(void);
void photons_stop(void);
uint8_t photons_measure(void);
#endif /* PHOTONS_H */

View File

@ -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();

View File

@ -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