Set up the lux sensor abstraction

This commit is contained in:
Savanni D'Gerinel 2022-08-12 16:27:29 -04:00
parent cbb537b27d
commit a32779774f
1 changed files with 77 additions and 2 deletions

View File

@ -1,6 +1,68 @@
#include <dio.h>
#include <display.h>
#include <stdio.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 = {
@ -11,12 +73,25 @@ int main(void) {
},
};
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];
snprintf(msg, 15, "[%d]", count);
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);