avr/i2c-lights/main.c

74 lines
2.5 KiB
C

#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();
// }
// }
// }
}
}