Compare commits
9 Commits
main
...
display-i2
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | 34be227b97 | |
Savanni D'Gerinel | ba2e5b84dc | |
Savanni D'Gerinel | 9daaab9e9e | |
Savanni D'Gerinel | a32779774f | |
Savanni D'Gerinel | cbb537b27d | |
Savanni D'Gerinel | 4a97284b49 | |
Savanni D'Gerinel | 529cf6067d | |
Savanni D'Gerinel | 0d0f90e91d | |
Savanni D'Gerinel | 31a9bd76ba |
|
@ -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");
|
||||
}
|
|
@ -2,11 +2,11 @@
|
|||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1659446231,
|
||||
"narHash": "sha256-hekabNdTdgR/iLsgce5TGWmfIDZ86qjPhxDg/8TlzhE=",
|
||||
"lastModified": 1649944829,
|
||||
"narHash": "sha256-wjOgLfjCdyoRamMOrVJceeDJk4LvJsQOxBoT3k16/7Q=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "eabc38219184cc3e04a974fe31857d8e0eac098d",
|
||||
"rev": "2f06b87f64bc06229e05045853e0876666e1b023",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
|
95
flake.nix
95
flake.nix
|
@ -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"; };
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
avrdude -p $1 -c usbtiny -U flash:w:$2:i
|
|
@ -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();
|
||||
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
*/
|
|
@ -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;
|
||||
}
|
|
@ -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
|
|
@ -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) {
|
||||
}
|
||||
}
|
||||
|
|
@ -5,13 +5,6 @@
|
|||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef enum {
|
||||
lme_off,
|
||||
lme_normal,
|
||||
lme_spooky,
|
||||
lme_eerie,
|
||||
} lantern_msg_e;
|
||||
|
||||
dio_t buttons[4] = {
|
||||
{ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 5 },
|
||||
{ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 6 },
|
||||
|
@ -37,10 +30,6 @@ int main(void) {
|
|||
EIMSK = 1 << INT2;
|
||||
EICRA |= 1 << ISC21 | 1 << ISC20;
|
||||
|
||||
dio_t int2 = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 2 };
|
||||
dio_set_direction(&int2, LINE_IN);
|
||||
dio_set(&int2, false);
|
||||
|
||||
rfm_t radio = (rfm_t){
|
||||
.spi = {
|
||||
.clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 1 },
|
||||
|
@ -61,24 +50,22 @@ int main(void) {
|
|||
sleep_mode();
|
||||
cli();
|
||||
|
||||
uint8_t msg;
|
||||
char msg[60];
|
||||
if (status_flags.button_press[0]) {
|
||||
status_flags.button_press[0] = false;
|
||||
msg = lme_normal;
|
||||
strcpy(msg, "normal");
|
||||
} else if (status_flags.button_press[1]) {
|
||||
status_flags.button_press[1] = false;
|
||||
msg = lme_spooky;
|
||||
strcpy(msg, "spooky");
|
||||
} else if (status_flags.button_press[2]) {
|
||||
status_flags.button_press[2] = false;
|
||||
msg = lme_eerie;
|
||||
strcpy(msg, "eerie");
|
||||
} else if (status_flags.button_press[3]) {
|
||||
status_flags.button_press[3] = false;
|
||||
msg = lme_off;
|
||||
} else {
|
||||
continue;
|
||||
strcpy(msg, "flash");
|
||||
}
|
||||
|
||||
rfm_transmit(&radio, &msg, 1);
|
||||
rfm_transmit(&radio, (uint8_t *)msg, strlen(msg) + 1);
|
||||
|
||||
interrupt_flags_t flags = rfm_interrupts(&radio);
|
||||
while(!flags.packet_sent) {
|
||||
|
|
|
@ -33,7 +33,7 @@ uint8_t random_step(uint8_t value, uint8_t min, uint8_t max) {
|
|||
|
||||
animation_t animation_new(void) {
|
||||
return (animation_t) {
|
||||
.color = { .brightness = 16, .r = 0, .g = 0, .b = 0 },
|
||||
.color = { .brightness = 7, .r = 0, .g = 0, .b = 0 },
|
||||
.red_line = time_line_new(0, 0, 0, 0),
|
||||
.green_line = time_line_new(0, 0, 0, 0),
|
||||
.blue_line = time_line_new(0, 0, 0, 0),
|
||||
|
@ -71,7 +71,7 @@ lantern_t lantern_new(sk9822_t lights) {
|
|||
sk9822_init(&lights);
|
||||
return (lantern_t){
|
||||
.lights = lights,
|
||||
.color_scheme = cse_off,
|
||||
.color_scheme = normal,
|
||||
.animations = {
|
||||
animation_new(),
|
||||
animation_new(),
|
||||
|
@ -86,15 +86,6 @@ lantern_t lantern_new(sk9822_t lights) {
|
|||
};
|
||||
}
|
||||
|
||||
void lantern_start_off(lantern_t *self, int light_idx) {
|
||||
animation_begin(
|
||||
&self->animations[light_idx],
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
1);
|
||||
}
|
||||
|
||||
void lantern_start_normal(lantern_t *self, int light_idx) {
|
||||
switch (light_idx) {
|
||||
case 0:
|
||||
|
@ -166,19 +157,19 @@ void lantern_start_spooky(lantern_t *self, int light_idx) {
|
|||
animation_begin(
|
||||
&self->animations[0],
|
||||
0,
|
||||
random_step(self->animations[0].color.g, 40, 140),
|
||||
random_step(self->animations[0].color.g, 80, 120),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[3],
|
||||
0,
|
||||
random_step(self->animations[3].color.g, 40, 140),
|
||||
random_step(self->animations[3].color.g, 80, 120),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[6],
|
||||
0,
|
||||
random_step(self->animations[6].color.g, 40, 140),
|
||||
random_step(self->animations[6].color.g, 80, 120),
|
||||
0,
|
||||
5);
|
||||
break;
|
||||
|
@ -186,19 +177,19 @@ void lantern_start_spooky(lantern_t *self, int light_idx) {
|
|||
animation_begin(
|
||||
&self->animations[1],
|
||||
0,
|
||||
random_step(self->animations[1].color.g, 20, 80),
|
||||
random_step(self->animations[1].color.g, 60, 100),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[4],
|
||||
0,
|
||||
random_step(self->animations[4].color.g, 20, 80),
|
||||
random_step(self->animations[4].color.g, 60, 100),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[7],
|
||||
0,
|
||||
random_step(self->animations[7].color.g, 20, 80),
|
||||
random_step(self->animations[7].color.g, 60, 100),
|
||||
0,
|
||||
5);
|
||||
break;
|
||||
|
@ -206,19 +197,19 @@ void lantern_start_spooky(lantern_t *self, int light_idx) {
|
|||
animation_begin(
|
||||
&self->animations[2],
|
||||
0,
|
||||
random_step(self->animations[2].color.g, 10, 40),
|
||||
random_step(self->animations[2].color.g, 20, 40),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[5],
|
||||
0,
|
||||
random_step(self->animations[5].color.g, 10, 40),
|
||||
random_step(self->animations[5].color.g, 20, 40),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[8],
|
||||
0,
|
||||
random_step(self->animations[8].color.g, 10, 40),
|
||||
random_step(self->animations[8].color.g, 20, 40),
|
||||
0,
|
||||
5);
|
||||
break;
|
||||
|
@ -298,19 +289,16 @@ void lantern_step(lantern_t *self, rgb_t colors[LIGHT_COUNT]) {
|
|||
for (int i = 0; i < LIGHT_COUNT; i++) {
|
||||
if (!animation_running(&self->animations[i])) {
|
||||
switch (self->color_scheme) {
|
||||
case cse_off:
|
||||
lantern_start_off(self, i);
|
||||
break;
|
||||
case cse_normal:
|
||||
case normal:
|
||||
lantern_start_normal(self, i);
|
||||
break;
|
||||
case cse_spooky:
|
||||
case spooky:
|
||||
lantern_start_spooky(self, i);
|
||||
break;
|
||||
case cse_eerie:
|
||||
case eerie:
|
||||
lantern_start_eerie(self, i);
|
||||
break;
|
||||
case cse_flash:
|
||||
case flash:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,11 +31,10 @@ typedef struct {
|
|||
} animation_t;
|
||||
|
||||
typedef enum {
|
||||
cse_off,
|
||||
cse_normal,
|
||||
cse_spooky,
|
||||
cse_eerie,
|
||||
cse_flash
|
||||
normal,
|
||||
spooky,
|
||||
eerie,
|
||||
flash
|
||||
} color_scheme_e;
|
||||
|
||||
typedef struct {
|
||||
|
|
126
lantern/main.c
126
lantern/main.c
|
@ -25,33 +25,22 @@ You should have received a copy of the GNU General Public License along with thi
|
|||
#define FPS 15
|
||||
#define TICKS_PER_SECOND 7812
|
||||
#define FPS_TIMEOUT TICKS_PER_SECOND / FPS
|
||||
#define FPS_MS 1000 / FPS
|
||||
#define PULSE_OFF_COUNT FPS * 5
|
||||
#define PULSE_ON_COUNT 5
|
||||
|
||||
typedef enum {
|
||||
lme_off,
|
||||
lme_normal,
|
||||
lme_spooky,
|
||||
lme_eerie,
|
||||
} lantern_msg_e;
|
||||
|
||||
typedef struct {
|
||||
bool frame_timeout;
|
||||
} status_flags_t;
|
||||
|
||||
status_flags_t status_flags;
|
||||
|
||||
/*
|
||||
ISR(TIMER1_COMPA_vect) {
|
||||
status_flags.frame_timeout = true;
|
||||
}
|
||||
*/
|
||||
|
||||
ISR(INT6_vect) {
|
||||
}
|
||||
|
||||
/*
|
||||
void setup_fps_timer(void) {
|
||||
// WGM = 0100: CTC with OCR1nA top
|
||||
// CS = 101: clock / 1024
|
||||
|
@ -65,34 +54,6 @@ void setup_fps_timer(void) {
|
|||
// Enable compare on A match interrupt
|
||||
TIMSK1 |= _BV(1);
|
||||
}
|
||||
*/
|
||||
|
||||
void startup_animation(sk9822_t *lights, rgb_t *colors) {
|
||||
for (int i = 0; i < LIGHT_COUNT; i++) {
|
||||
colors[i].brightness = 1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < LIGHT_COUNT; i++) {
|
||||
colors[i].b = 0;
|
||||
colors[i].r = 128;
|
||||
sk9822_send(lights, colors, LIGHT_COUNT);
|
||||
_delay_ms(50);
|
||||
}
|
||||
|
||||
for (int i = 0; i < LIGHT_COUNT; i++) {
|
||||
colors[i].r = 0;
|
||||
colors[i].g = 128;
|
||||
sk9822_send(lights, colors, LIGHT_COUNT);
|
||||
_delay_ms(100);
|
||||
}
|
||||
|
||||
for (int i = 0; i < LIGHT_COUNT; i++) {
|
||||
colors[i].g = 0;
|
||||
colors[i].b = 128;
|
||||
sk9822_send(lights, colors, LIGHT_COUNT);
|
||||
_delay_ms(100);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
EIMSK = 1 << INT6 | 1 << INT2;
|
||||
|
@ -125,88 +86,81 @@ int main(void) {
|
|||
.data_pin = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 6 }
|
||||
};
|
||||
|
||||
rgb_t colors[LIGHT_COUNT];
|
||||
rgb_t off[LIGHT_COUNT] = {
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
};
|
||||
/*
|
||||
rgb_t error_indicator[LIGHT_COUNT] = {
|
||||
{ .brightness = 1, .r = 255, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 1, .r = 255, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 1, .r = 255, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
};
|
||||
*/
|
||||
|
||||
sk9822_send(&lights, off, LIGHT_COUNT);
|
||||
_delay_ms(500);
|
||||
|
||||
lantern_t lantern = lantern_new(lights);
|
||||
lantern_set_mode(&lantern, cse_off);
|
||||
lantern_set_mode(&lantern, normal);
|
||||
|
||||
// setup_fps_timer();
|
||||
lantern_step(&lantern, colors);
|
||||
sk9822_send(&lights, colors, LIGHT_COUNT);
|
||||
|
||||
// set_sleep_mode(SLEEP_MODE_IDLE);
|
||||
|
||||
rgb_t colors[LIGHT_COUNT] = {
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
|
||||
};
|
||||
_delay_ms(1000);
|
||||
startup_animation(&lights, (rgb_t *)&colors);
|
||||
setup_fps_timer();
|
||||
|
||||
set_sleep_mode(SLEEP_MODE_IDLE);
|
||||
while(1) {
|
||||
/*
|
||||
if (status_flags.frame_timeout) {
|
||||
status_flags.frame_timeout = false;
|
||||
lantern_step(&lantern, colors);
|
||||
sk9822_send(&lights, colors, LIGHT_COUNT);
|
||||
}
|
||||
*/
|
||||
|
||||
lantern_step(&lantern, colors);
|
||||
sk9822_send(&lights, colors, LIGHT_COUNT);
|
||||
|
||||
interrupt_flags_t flags = rfm_interrupts(&radio);
|
||||
while (flags.fifo_not_empty) {
|
||||
uint8_t msg[60];
|
||||
/*
|
||||
sk9822_send(&lights, off, LIGHT_COUNT);
|
||||
_delay_ms(100);
|
||||
*/
|
||||
char msg[60] = "";
|
||||
uint8_t length;
|
||||
|
||||
rfm_receive(&radio, (uint8_t *)msg, &length);
|
||||
if (length == 1) {
|
||||
switch (msg[0]) {
|
||||
case lme_off:
|
||||
lantern_set_mode(&lantern, cse_off);
|
||||
break;
|
||||
case lme_normal:
|
||||
lantern_set_mode(&lantern, cse_normal);
|
||||
break;
|
||||
case lme_spooky:
|
||||
lantern_set_mode(&lantern, cse_spooky);
|
||||
break;
|
||||
case lme_eerie:
|
||||
lantern_set_mode(&lantern, cse_eerie);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (length == strlen("normal") + 1 && !strncmp(msg, "normal", length)) {
|
||||
lantern_set_mode(&lantern, normal);
|
||||
} else if (length == strlen("spooky") + 1 && !strncmp(msg, "spooky", length)) {
|
||||
lantern_set_mode(&lantern, spooky);
|
||||
} else if (length == strlen("eerie") + 1 && !strncmp(msg, "eerie", length)) {
|
||||
lantern_set_mode(&lantern, eerie);
|
||||
/*
|
||||
} else {
|
||||
sk9822_send(&lights, error_indicator, LIGHT_COUNT);
|
||||
_delay_ms(500);
|
||||
*/
|
||||
}
|
||||
flags = rfm_interrupts(&radio);
|
||||
}
|
||||
|
||||
_delay_ms(FPS_MS);
|
||||
/*
|
||||
sei();
|
||||
sleep_mode();
|
||||
cli();
|
||||
*/
|
||||
}
|
||||
|
||||
sk9822_send(&lights, error_indicator, LIGHT_COUNT);
|
||||
_delay_ms(1000);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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++;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue