PROGRAM=lights
-SRC=main.c logging.c adc.c pwm.c tmr.c pwmled.c gpio.c
+SRC=main.c logging.c adc.c pwm.c tmr.c pwmled.c gpio.c ambient.c
OBJ=$(SRC:.c=.o)
_BV(REFS1) | _BV(MUX2) | _BV(MUX0),
};
+#define AMBIENT_ADC N_PWMLEDS
+
#define LAST_ADC (sizeof(adc_mux)/sizeof(char))
volatile static unsigned char current_adc = LAST_ADC;
// test if current_adc should be measured
if (current_adc < N_PWMLEDS && pwmled_is_on(current_adc))
goto found;
- // TODO ambient light, battery sense, etc.
+ if (current_adc == AMBIENT_ADC)
+ goto found;
+ // TODO battery sense, etc.
}
// all ADCs have been handled
if (current_adc < N_PWMLEDS)
pwmled_adc(current_adc, adcval);
- // TODO ambient light, battery sense, etc.
+ if (current_adc == AMBIENT_ADC)
+ ambient_adc(adcval);
+ // TODO battery sense, etc.
start_next_adc();
}
--- /dev/null
+#include <avr/io.h>
+
+#include "lights.h"
+
+static uint16_t ambient_val;
+unsigned char ambient_zone;
+unsigned char ambient_zone_set;
+
+static uint16_t ambient_zones[] = {
+ 1, 2, 10, 20, 256
+};
+#define N_AMBIENT_ZONES (sizeof(ambient_zones)/sizeof(ambient_zones[0]))
+
+void ambient_init()
+{
+ ambient_val = 0;
+ ambient_zone = 0;
+ ambient_zone_set = 0;
+}
+
+void ambient_zone_changed()
+{
+ log_byte(0xCC);
+ log_byte(ambient_zone);
+ log_word(ambient_val);
+}
+
+void ambient_adc(uint16_t adcval)
+{
+ unsigned char newzone;
+
+ if (!ambient_zone_set)
+ ambient_val = adcval << 4;
+
+ // running sum
+ ambient_val += adcval - (ambient_val >> 4);
+
+ newzone = 0;
+ while (newzone < N_AMBIENT_ZONES-1
+ && ambient_zones[newzone] <= ambient_val)
+ newzone++;
+
+ if (!ambient_zone_set || newzone != ambient_zone) {
+ ambient_zone = newzone;
+ ambient_zone_changed();
+ ambient_zone_set = 1;
+ }
+}
+
+
+
void gpio_init();
void gpio_set(unsigned char n, unsigned char on);
+/* ambient.c */
+void ambient_init();
+extern unsigned char ambient_zone;
+void ambient_adc(uint16_t adc_val);
+
#endif /* !LIGHTS_H__ */
pwmled_init();
gpio_init();
+ ambient_init();
log_set_state(3);