avr/lux-sensor/main.c

83 lines
2.5 KiB
C

#include <dio.h>
#include <i2c.h>
#include <display.h>
#include <stdio.h>
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 },
},
};
display_init(&display);
i2c_bus_t *bus = i2c_init(
(dio_t){ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 5 },
(dio_t){ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 6 }
);
i2c_error_e status = i2c_ok;
i2c_start(bus, &status);
i2c_write_byte(bus, 0x39 << 1, &status);
i2c_write_byte(bus, 0x80, &status);
i2c_write_byte(bus, 0x03, &status);
i2c_end(bus, &status);
if (status != i2c_ok) {
display_clear(&display);
display_write_message(&display, "early abort");
}
_delay_ms(500);
uint8_t count = 0;
while(1) {
/*
int id = 0;
status = i2c_ok;
i2c_start(bus, &status);
i2c_write_byte(bus, 0x39 << 1, &status);
i2c_write_byte(bus, 0x80, &status);
i2c_restart(bus, &status);
i2c_write_byte(bus, (0x39 << 1) | 1, &status);
id = i2c_read_byte(bus, true, &status) & 0x03;
i2c_end(bus, &status);
*/
int lux = 0;
int ir = 0;
i2c_start(bus, &status);
i2c_write_byte(bus, 0x39 << 1, &status);
i2c_write_byte(bus, 0x8c, &status);
i2c_restart(bus, &status);
i2c_write_byte(bus, 0x39 << 1 | 1, &status);
lux = i2c_read_byte(bus, false, &status);
lux |= i2c_read_byte(bus, true, &status) << 8;
i2c_end(bus, &status);
i2c_start(bus, &status);
i2c_write_byte(bus, 0x39 << 1, &status);
i2c_write_byte(bus, 0x8e, &status);
i2c_restart(bus, &status);
i2c_write_byte(bus, 0x39 << 1 | 1, &status);
ir = ((int)i2c_read_byte(bus, false, &status));
ir |= ((int)i2c_read_byte(bus, true, &status)) << 8;
i2c_end(bus, &status);
char msg[20] = "";
display_clear(&display);
snprintf(msg, 20, "[%d] %x, %x", count, lux, ir);
display_write_message(&display, msg);
display_set_location(&display, 1, 0);
i2c_status_to_string(status, msg);
display_write_message(&display, msg);
_delay_ms(1000);
count++;
}
}