Compare commits

...

9 Commits

10 changed files with 834 additions and 1 deletions

15
display-i2c/main.c Normal file
View File

@ -0,0 +1,15 @@
#include <dio.h>
#include <display.h>
int main(void) {
display_t display = {
.reg = {
.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);
display_write_message(&display, "ready");
}

View File

@ -90,6 +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 attiny85; avr = true; })
];
};
@ -194,6 +195,23 @@
];
};
packages."x86_64-linux"."i2c" =
{ gcc, cflags }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in mkLibrary {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "i2c";
psrc = ./i2c;
pbuildInputs = [
(packages."x86_64-linux"."dio" { inherit gcc cflags; })
(packages."x86_64-linux"."shift-register" { inherit gcc cflags; })
(packages."x86_64-linux"."display" { inherit gcc cflags; })
];
};
packages."x86_64-linux"."prime-tx" =
{ gcc, cflags }:
let
@ -236,7 +254,7 @@
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
avr = pkgs.pkgsCross.avr.buildPackages;
in packages."x86_64-linux"."lantern" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; };
in packages."x86_64-linux"."lantern" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; };
packages."x86_64-linux"."lantern" =
{ gcc, cflags, avr }:
let
@ -284,6 +302,81 @@
];
};
packages."x86_64-linux"."display-i2c_" =
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 attiny85; avr = true; };
packages."x86_64-linux"."display-i2c" =
{ gcc, cflags, avr }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in mkProgram {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "display-i2c";
psrc = ./display-i2c;
inherit avr;
pbuildInputs = [
(packages."x86_64-linux"."dio" { inherit gcc cflags; })
(packages."x86_64-linux"."display" { inherit gcc cflags; })
(packages."x86_64-linux"."i2c" { inherit gcc cflags; })
(packages."x86_64-linux"."shift-register" { inherit gcc cflags; })
(packages."x86_64-linux"."spi" { inherit gcc cflags; })
];
};
packages."x86_64-linux"."lux-sensor_" =
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
avr = pkgs.pkgsCross.avr.buildPackages;
in packages."x86_64-linux"."lux-sensor" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; };
packages."x86_64-linux"."lux-sensor" =
{ gcc, cflags, avr }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in mkProgram {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "lux-sensor";
psrc = ./lux-sensor;
inherit avr;
pbuildInputs = [
(packages."x86_64-linux"."dio" { inherit gcc cflags; })
(packages."x86_64-linux"."display" { inherit gcc cflags; })
(packages."x86_64-linux"."i2c" { inherit gcc cflags; })
(packages."x86_64-linux"."shift-register" { inherit gcc cflags; })
(packages."x86_64-linux"."spi" { inherit gcc cflags; })
];
};
packages."x86_64-linux"."i2c_lights_" =
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
avr = pkgs.pkgsCross.avr.buildPackages;
in packages."x86_64-linux"."i2c_lights" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags attiny85; avr = true; };
packages."x86_64-linux"."i2c_lights" =
{ gcc, cflags, avr }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in mkProgram {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "i2c_lights";
psrc = ./i2c-lights;
inherit avr;
pbuildInputs = [
(packages."x86_64-linux"."i2c" { inherit gcc cflags; })
(packages."x86_64-linux"."dio" { inherit gcc cflags; })
];
};
devShell."x86_64-linux" =
let
pkgs = import nixpkgs { system = "x86_64-linux"; };

1
flash-tiny Executable file
View File

@ -0,0 +1 @@
avrdude -p $1 -c usbtiny -U flash:w:$2:i

73
i2c-lights/main.c Normal file
View File

@ -0,0 +1,73 @@
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <dio.h>
#include <i2c.h>
typedef enum registers {
POWER_OFF = 0x00,
SET_LIGHTS = 0x01,
} registers_e;
int main(void) {
i2c_device_t * i2c = i2c_device_init(
(dio_t){ .ddr = &DDRB, .port = &PORTB, .addr = 4 },
(dio_t){ .ddr = &DDRB, .port = &PORTB, .addr = 3 },
0x93,
3
);
dio_t blue = (dio_t){ .ddr = &DDRB, .port = &PORTB, .addr = 0 };
dio_t green = (dio_t){ .ddr = &DDRB, .port = &PORTB, .addr = 1 };
dio_t red = (dio_t){ .ddr = &DDRB, .port = &PORTB, .addr = 2 };
dio_set_direction(&blue, LINE_OUT);
dio_set_direction(&green, LINE_OUT);
dio_set_direction(&red, LINE_OUT);
set_sleep_mode(SLEEP_MODE_IDLE);
while (1) {
sei();
sleep_mode();
cli();
i2c_device_step(i2c_device_t *i2c);
// Now, it's necessary to check whether the register has been set
// /* SDA has changed. If it's low, let's listen for data */
// if (!dio_read(i2c.sda)) {
// /* data incoming. Read the address */
// uint8_t status = i2c_ok;
// uint8_t addr = i2c_client_read_byte(&i2c, &status);
// if (addr == 0x93) {
// /* Send the ack signal by pulling SDA low for one tick */
// i2c_client_write_bit(&i2c, 0, &status);
// uint8_t reg = i2c_client_read_byte(&i2c, &status);
// switch (reg) {
// case POWER_OFF:
// /* turn all of the lights off and put the device into lowest sleep mode */
// break;
// case SET_LIGHTS:
// uint8_t light_status = i2c_client_read_byte(&i2c, &status);
// dio_set(&red, light_status & _BV(2));
// dio_set(&green, light_status & _BV(1));
// dio_set(&blue, light_status & _BV(0));
// break;
// default:
// /* unrecognized register */
// i2c_client_write(&i2c, 1, &status);
// }
// } else {
// /* The message was not for us, so wait until the bus goes idle */
// bus_state_e state = ACTIVE;
// while (1) {
// sei();
// sleep_mode();
// cli();
// }
// }
// }
}
}

147
i2c/device.c Normal file
View File

@ -0,0 +1,147 @@
i2c_device_t * i2c_device_init(dio_t clock, dio_t data, uint8_t address, uint8_t register_count) {
i2c_device_t *self = malloc(sizeof(i2c_device_t) + register_count);
self->sda = data;
self->scl = clock;
self->addr = address;
self->bus_state_e = IDLE;
self->register_count = register_count;
self->target_register = -1;
for (int i = 0; i < register_count; i++) {
self->registers[i] = 0;
}
return self
}
/*
void i2c_device_write_bit(i2c_bus_t *bus, uint8_t value, i2c_error_e *error) {
if (*error != i2c_ok) return;
while (read_scl(bus) != 0) {
if (value) {
set_sda(bus);
} else {
clear_sda(bus);
}
}
}
void i2c_device_write_byte(i2c_bus_t *bus, uint8_t value, i2c_error_e *error) {
for (int i = 7; i >= 0; i--) {
i2c_write_bit(bus, value & _BV(i), error);
}
}
uint8_t i2c_device_read_bit(i2c_bus_t *bus, i2c_error_e *error) {
if (*error != i2c_ok) return 0;
while (read_scl(bus) == 0) {
i2c_delay();
}
return read_sda(bus);
}
uint8_t i2c_device_read_byte(i2c_bus_t *bus, i2c_error_e *error) {
if (*error != i2c_ok) return 0;
uint8_t value = 0;
for (int i = 0; i < 8; i++) {
value = (value << 1) | i2c_device_read_bit(bus, error);
}
return value;
}
void i2c_status_to_string(i2c_error_e status, char msg[20]) {
switch (status) {
case i2c_ok:
snprintf(msg, 20, "ok");
break;
case i2c_arbitration_lost:
snprintf(msg, 20, "arbitration lost");
break;
case i2c_timeout:
snprintf(msg, 20, "timeout");
break;
case i2c_nak:
snprintf(msg, 20, "nak");
break;
}
}
void i2c_wait_for_idle(i2c_device_t *bus, i2c_error_e *error) {
switch (bus_state) {
case ACTIVE:
if (dio_read(&i2c.scl) && !dio_read(&i2c.sda)) {
state = INACTIVE_1;
}
break;
case INACTIVE_1:
if (dio_read(&i2c.scl) && dio_read(&i2c.sda)) {
state = IDLE;
} else {
state = ACTIVE;
}
break;
case IDLE:
break;
}
}
bool i2c_is_idle(i2c_device_t *bus) {
return bus->state == IDLE,
}
*/
void i2c_device_step(i2c_device_t *self) {
uint8_t sda = dio_read(self.sda);
uint8_t scl = dio_read(self.scl);
i2c_error_e status = i2c_ok;
switch (self->state) {
case IDLE:
if (!sda) self->state = START_SDA;
break;
case START_SDA:
if (sda) {
self->state = IDLE;
break;
}
if (!scl) self->state = START;
break;
case START:
uint8_t header = i2c_device_read_byte(self, &status);
uint8_t addr = header >> 1;
uint8_t mode = header & 0x01;
if addr == self->addr {
self->state = ACTIVE_LISTEN;
if (mode) {
self->direction = DEVICE_TO_HOST;
} else {
self->direction = HOST_TO_DEVICE;
}
i2c_device_write_bit(self, 0, &status);
} else {
self->state = ACTIVE_IGNORE;
}
break;
case ACTIVE_LISTEN:
// Do all of the stuff to read data from the host
break;
case ACTIVE_IGNORE:
break;
case INACTIVE:
break;
}
}
/*
void i2c_device_release_clock(i2c_device_bus_t *bus) {
dio_set(bus->scl);
}
*/

211
i2c/i2c.c Normal file
View File

@ -0,0 +1,211 @@
/*
Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of Savanni's AVR library.
This AVR library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This AVR library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this AVR library. If not, see <https://www.gnu.org/licenses/>.
*/
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>
#include "i2c.h"
void i2c_delay(void) {
_delay_us(5);
}
inline void set_sda(i2c_bus_t *bus) {
bus->sda_state = true;
dio_set_direction(&bus->sda, LINE_IN);
}
inline void clear_sda(i2c_bus_t *bus) {
bus->sda_state = false;
dio_set_direction(&bus->sda, LINE_OUT);
}
inline uint8_t read_sda(i2c_bus_t *bus) {
return dio_read(&bus->sda);
}
inline void set_scl(i2c_bus_t *bus) {
bus->scl_state = true;
dio_set_direction(&bus->scl, LINE_IN);
}
inline void clear_scl(i2c_bus_t *bus) {
bus->scl_state = false;
dio_set_direction(&bus->scl, LINE_OUT);
}
inline uint8_t read_scl(i2c_bus_t *bus) {
return dio_read(&bus->scl);
}
i2c_bus_t *i2c_init(dio_t clock, dio_t data) {
i2c_bus_t *bus = malloc(sizeof(i2c_bus_t));
bus->sda = data;
bus->scl = clock;
dio_set(&bus->sda, 0);
dio_set(&bus->scl, 0);
set_sda(bus);
set_scl(bus);
return bus;
}
void i2c_start(i2c_bus_t *bus, i2c_error_e *error) {
if (*error != i2c_ok) return;
if (read_sda(bus) == 0) {
*error = i2c_arbitration_lost;
return;
}
clear_sda(bus);
i2c_delay();
clear_scl(bus);
}
void i2c_end(i2c_bus_t *bus, i2c_error_e *error) {
clear_sda(bus);
i2c_delay();
set_scl(bus);
int count = 0;
while (read_scl(bus) == 0) {
if (count == 5) {
*error = i2c_timeout;
return;
}
count++;
i2c_delay();
}
i2c_delay();
set_sda(bus);
i2c_delay();
if (read_sda(bus) == 0) {
*error = i2c_arbitration_lost;
}
}
void i2c_restart(i2c_bus_t *bus, i2c_error_e *error) {
if (*error != i2c_ok) return;
set_sda(bus);
i2c_delay();
set_scl(bus);
int count = 0;
while (read_scl(bus) == 0) {
if (count == 5) {
*error = i2c_timeout;
return;
}
count++;
i2c_delay();
}
i2c_start(bus, error);
}
void i2c_write_bit(i2c_bus_t *bus, uint8_t value, i2c_error_e *error) {
if (*error != i2c_ok) return;
if (value) {
set_sda(bus);
} else {
clear_sda(bus);
}
i2c_delay();
set_scl(bus);
i2c_delay();
int count = 0;
while (read_scl(bus) == 0) {
if (count == 5) {
*error = i2c_timeout;
return;
}
count++;
i2c_delay();
}
if (value && (read_sda(bus) == 0)) {
*error = i2c_arbitration_lost;
}
clear_scl(bus);
}
uint8_t i2c_read_bit(i2c_bus_t *bus, i2c_error_e *error) {
uint8_t bit;
if (*error != i2c_ok) return 0;
set_sda(bus);
i2c_delay();
set_scl(bus);
i2c_delay();
int count = 0;
while (read_scl(bus) == 0) {
if (count == 5) {
*error = i2c_timeout;
return 0;
}
count++;
i2c_delay();
}
bit = read_sda(bus);
clear_scl(bus);
return bit;
}
void i2c_write_byte(i2c_bus_t *bus, uint8_t value, i2c_error_e *error) {
if (*error != i2c_ok) return;
for (int i = 7; i >= 0; i--) {
i2c_write_bit(bus, value & _BV(i), error);
}
uint8_t nak = i2c_read_bit(bus, error);
if (*error != i2c_ok) return;
if (nak) {
*error = i2c_nak;
}
return;
}
uint8_t i2c_read_byte(i2c_bus_t *bus, bool final_nak, i2c_error_e *error) {
if (*error != i2c_ok) return 0;
uint8_t value = 0;
for (int i = 0; i < 8; i++) {
value = (value << 1) | i2c_read_bit(bus, error);
}
if (*error != i2c_ok || final_nak) {
i2c_error_e tmp_status;
i2c_write_bit(bus, 1, &tmp_status);
return value;
}
i2c_write_bit(bus, 0, error);
return value;
}

90
i2c/i2c.h Normal file
View File

@ -0,0 +1,90 @@
/*
Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of Savanni's AVR library.
This AVR library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This AVR library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this AVR library. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef __I2C_H__
#define __I2C_H__
#include <dio.h>
typedef enum i2c_bus_state {
IDLE,
START_SDA,
START,
ACTIVE_LISTEN,
ACTIVE_IGNORE,
INACTIVE,
} i2c_bus_state_e;
typedef enum message_direction {
HOST_TO_DEVICE,
DEVICE_TO_HOST,
} message_direction_e;
typedef enum {
i2c_ok,
i2c_arbitration_lost,
i2c_timeout,
i2c_nak,
} i2c_error_e;
typedef struct {
bool sda_state;
dio_t sda;
bool scl_state;
dio_t scl;
} i2c_bus_t;
typedef struct {
dio_t sda;
dio_t scl;
uint8_t addr;
i2c_bus_state_e state;
message_direction_e direction;
uint8_t register_count;
int8_t target_register;
uint8_t registers[];
} i2c_device_t;
i2c_bus_t * i2c_init(dio_t clock, dio_t data);
void i2c_start(i2c_bus_t *bus, i2c_error_e *error);
void i2c_end(i2c_bus_t *bus, i2c_error_e *error);
void i2c_restart(i2c_bus_t *bus, i2c_error_e *error);
void i2c_write_byte(i2c_bus_t *bus, uint8_t value, i2c_error_e *error);
uint8_t i2c_read_byte(i2c_bus_t *bus, bool final_nak, i2c_error_e *error);
// void i2c_host_write(i2c_host_t *, uint8_t, uint8_t, size_t, i2c_error_e);
void i2c_status_to_string(i2c_error_e status, char msg[20]);
i2c_device_t * i2c_device_init(dio_t clock, dio_t data, uint8_t address, uint8_t register_count);
void i2c_device_step(i2c_device_t *);
void i2c_device_release_bus(i2c_device_t *);
/*
void i2c_device_bus_state_update(i2c_device_bus_t *bus);
void i2c_set_register(i2c_registers_t *regs, i2c_error_e *error) {
}
void i2c_register_write(i2c_registers_t *regs, i2c_error_e *error) {
}
void i2c_register_read(i2c_registers_t *regs, i2c_error_e *error) {
}
*/
#endif

View File

@ -0,0 +1,18 @@
#include <dio.h>
#include <i2c.h>
typedef enum registers {
POWER_OFF = 0x00,
SET_LIGHTS = 0x01,
} registers_e;
int main(void) {
i2c_bus_t *bus = i2c_init(
(dio_t){ .ddr = &DDRB, .port = &PORTB, .addr = 4 },
(dio_t){ .ddr = &DDRB, .port = &PORTB, .addr = 3 },
);
while(1) {
}
}

103
lux-sensor-reader/main.c Normal file
View File

@ -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;
}

82
lux-sensor/main.c Normal file
View File

@ -0,0 +1,82 @@
#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++;
}
}