avr/lux-sensor-reader/main.c

104 lines
2.7 KiB
C

#include <dio.h>
#include <display.h>
#include <i2c.h>
#include <stdio.h>
#include <stdlib.h>
typedef enum {
ls_ok,
ls_unrecognized,
ls_not_responding,
ls_powered_down,
} lux_sensor_status_e;
typedef struct lux_sensor_s lux_sensor_t;
struct lux_sensor_s {
uint16_t (*read_total_lux)(void *, lux_sensor_status_e *);
uint16_t (*read_ir)(void *, lux_sensor_status_e *);
uint16_t (*read_visible)(void *, lux_sensor_status_e *);
void *impl;
};
uint16_t tsl2561_read_total_lux(void *sensor, lux_sensor_status_e *status) {
if (*status != ls_ok) return 0;
return 0;
}
uint16_t tsl2561_read_ir(void *sensor, lux_sensor_status_e *status) {
if (*status != ls_ok) return 0;
return 0;
}
uint16_t tsl2561_read_visible(void *sensor, lux_sensor_status_e *status) {
if (*status != ls_ok) return 0;
return 0;
}
lux_sensor_t tsl2561_init(dio_t clock, dio_t data) {
i2c_bus_t *bus = malloc(sizeof(i2c_bus_t));
bus->sda = data;
bus->scl = clock;
return (lux_sensor_t){
.read_total_lux = tsl2561_read_total_lux,
.read_ir = tsl2561_read_ir,
.read_visible = tsl2561_read_visible,
.impl = (void *)bus,
};
}
uint16_t ls_read_total_lux(lux_sensor_t *sensor, lux_sensor_status_e *status) {
return sensor->read_total_lux(sensor->impl, status);
}
uint16_t ls_read_ir(lux_sensor_t *sensor, lux_sensor_status_e *status) {
return sensor->read_ir(sensor->impl, status);
}
uint16_t ls_read_visible(lux_sensor_t *sensor, lux_sensor_status_e *status) {
return sensor->read_visible(sensor->impl, status);
}
int main(void) {
display_t display = {
.reg = {
.output = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 7 },
.shift_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 6 },
.latch_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 5 },
},
};
lux_sensor_t sensor = tsl2561_init(
(dio_t){ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 5 },
(dio_t){ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 6 }
);
int count = 0;
while (1) {
lux_sensor_status_e sensor_status = ls_ok;
display_init(&display);
uint16_t total_lux = ls_read_total_lux(&sensor, &sensor_status);
uint16_t ir = ls_read_ir(&sensor, &sensor_status);
char msg[15];
if (sensor_status == ls_ok) {
snprintf(msg, 20, "[%d] %d %d", count, total_lux, ir);
} else {
snprintf(msg, 20, "[%d] error", count);
}
display_clear(&display);
display_write_message(&display, msg);
_delay_ms(1000);
count++;
}
return 0;
}