Move the lux sensor reader and start on the real display code
This commit is contained in:
parent
9daaab9e9e
commit
ba2e5b84dc
|
@ -1,103 +1,15 @@
|
|||
#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 },
|
||||
.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 },
|
||||
},
|
||||
};
|
||||
|
||||
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");
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
avrdude -p $1 -c usbtiny -U flash:w:$2:i
|
|
@ -0,0 +1,103 @@
|
|||
#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;
|
||||
}
|
Loading…
Reference in New Issue