Attempt to port light detection code, not working yet

This commit is contained in:
Thammi 2013-08-31 22:26:26 +02:00
parent 73132a39d4
commit a51fe1f66d
5 changed files with 94 additions and 1 deletions

21
firmware/apps/buggy.c Normal file
View File

@ -0,0 +1,21 @@
#include <stdlib.h>
#include <pentabug/app.h>
#include <pentabug/hal.h>
#include <pentabug/photons.h>
#include <pentabug/pentatonic.h>
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);

View File

@ -55,7 +55,7 @@ void buzzer_off(void);
// IR // 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_on(void);
void ir_off(void); void ir_off(void);
void ir_inv(void); void ir_inv(void);

View File

@ -0,0 +1,10 @@
#ifndef PHOTONS_H
#define PHOTONS_H
#include <stdint.h>
void photons_init(void);
uint16_t photons_measure(void);
#endif /* PHOTONS_H */

View File

@ -113,6 +113,10 @@ void reset_hw(void) {
// turn ir off // turn ir off
ir_off(); ir_off();
// disable adc
ADCSRA &= ~(1 << ADEN);
} }
uint8_t button_state(uint8_t btn) { uint8_t button_state(uint8_t btn) {

58
firmware/lib/photons.c Normal file
View File

@ -0,0 +1,58 @@
#include <pentabug/photons.h>
#include <avr/io.h>
#include <pentabug/hal.h>
#include <pentabug/lifecycle.h>
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;
}