From ba2e5b84dcb2b439a6f08dfc92aa2c856095f385 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Wed, 24 Aug 2022 23:55:34 -0400 Subject: [PATCH] Move the lux sensor reader and start on the real display code --- display-i2c/main.c | 98 ++----------------------------------- flake.nix | 4 +- flash-tiny | 1 + lux-sensor-reader/main.c | 103 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 95 deletions(-) create mode 100755 flash-tiny create mode 100644 lux-sensor-reader/main.c diff --git a/display-i2c/main.c b/display-i2c/main.c index 44fa776..d9a41b0 100644 --- a/display-i2c/main.c +++ b/display-i2c/main.c @@ -1,103 +1,15 @@ #include #include -#include -#include -#include - -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 }, + .output = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 2 }, + .shift_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 1 }, + .latch_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 0 }, }, }; + display_init(&display); - 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; + display_write_message(&display, "ready"); } diff --git a/flake.nix b/flake.nix index fb42f67..f6ab94e 100644 --- a/flake.nix +++ b/flake.nix @@ -90,7 +90,7 @@ (packages."x86_64-linux"."radio-rx" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; }) (packages."x86_64-linux"."lantern" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; }) (packages."x86_64-linux"."lantern-controller" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; }) - (packages."x86_64-linux"."display-i2c" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; }) + (packages."x86_64-linux"."display-i2c" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags attiny85; avr = true; }) ]; }; @@ -306,7 +306,7 @@ let pkgs = import nixpkgs { system = "x86_64-linux"; }; avr = pkgs.pkgsCross.avr.buildPackages; - in packages."x86_64-linux"."display-i2c" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; }; + in packages."x86_64-linux"."display-i2c" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags attiny85; avr = true; }; packages."x86_64-linux"."display-i2c" = { gcc, cflags, avr }: let diff --git a/flash-tiny b/flash-tiny new file mode 100755 index 0000000..1120711 --- /dev/null +++ b/flash-tiny @@ -0,0 +1 @@ +avrdude -p $1 -c usbtiny -U flash:w:$2:i \ No newline at end of file diff --git a/lux-sensor-reader/main.c b/lux-sensor-reader/main.c new file mode 100644 index 0000000..44fa776 --- /dev/null +++ b/lux-sensor-reader/main.c @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include + +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; +}