Compare commits
24 Commits
radio-abst
...
main
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | b15c5edb4d | |
Savanni D'Gerinel | 8d412df7e1 | |
Savanni D'Gerinel | a7af4accc0 | |
Savanni D'Gerinel | 196278a2fe | |
Savanni D'Gerinel | 3e68db14bb | |
Savanni D'Gerinel | 966ef20eca | |
Savanni D'Gerinel | 2acff8d3f5 | |
Savanni D'Gerinel | 7b4429db35 | |
Savanni D'Gerinel | 78cd1908a7 | |
Savanni D'Gerinel | 11436eeb28 | |
Savanni D'Gerinel | bdbb13f67f | |
Savanni D'Gerinel | 7e16faefae | |
Savanni D'Gerinel | 301496cf0e | |
Savanni D'Gerinel | dddf48651a | |
Savanni D'Gerinel | 4b5d5632b9 | |
Savanni D'Gerinel | e19bf772cb | |
Savanni D'Gerinel | cf21388bd8 | |
Savanni D'Gerinel | aefb2184bf | |
Savanni D'Gerinel | ca0847c73d | |
Savanni D'Gerinel | 307617b1b2 | |
Savanni D'Gerinel | 74a1f9f945 | |
Savanni D'Gerinel | fde5db66f7 | |
Savanni D'Gerinel | 4c02e99566 | |
Savanni D'Gerinel | 94efdbb1c3 |
|
@ -1,6 +1,17 @@
|
|||
#include "animation.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int bound(int value, uint8_t min, uint8_t max) {
|
||||
if (value > max) {
|
||||
return max;
|
||||
} else if (value < min) {
|
||||
return min;
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
time_line_t time_line_new(int t0, int y0, int t1, int y1) {
|
||||
int dt = t1 - t0;
|
||||
int dy = y1 - y0;
|
||||
|
@ -15,7 +26,7 @@ time_line_t time_line_new(int t0, int y0, int t1, int y1) {
|
|||
error = de + dt;
|
||||
}
|
||||
|
||||
return (time_line_t){ .y = y0, .dt = dt, .de = de, .error = error, .inc = inc, .dy = dy };
|
||||
return (time_line_t){ .y = y0, .dt = dt, .de = de, .error = error, .inc = inc, .dy = dy, .y0 = y0, .y1 = y1 };
|
||||
}
|
||||
|
||||
int time_line_next(time_line_t *self) {
|
||||
|
@ -28,11 +39,17 @@ int time_line_next(time_line_t *self) {
|
|||
}
|
||||
} else {
|
||||
while (error < 0) {
|
||||
self->y -= self->inc;
|
||||
self->y += self->inc;
|
||||
error += 2 * self->dt;
|
||||
}
|
||||
}
|
||||
|
||||
if (self->y0 < self->y1) {
|
||||
self->y = bound(self->y, self->y0, self->y1);
|
||||
} else {
|
||||
self->y = bound(self->y, self->y1, self->y0);
|
||||
}
|
||||
|
||||
self->error = error;
|
||||
return self->y;
|
||||
}
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef __ANIMATION_H__
|
||||
#define __ANIMATION_H__
|
||||
|
||||
|
@ -12,6 +15,8 @@ typedef struct {
|
|||
int inc;
|
||||
int dt;
|
||||
int dy;
|
||||
int y0;
|
||||
int y1;
|
||||
} time_line_t;
|
||||
|
||||
// Construct a new line object. x1 must be greater than x0, and dy must be less than dx.
|
||||
|
@ -21,6 +26,6 @@ time_line_t time_line_new(int, int, int, int);
|
|||
int time_line_next(time_line_t *);
|
||||
|
||||
// Diagnostic printout of the current state of line_t.
|
||||
// void line_print(line_t *line);
|
||||
void time_line_print(time_line_t *);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -3,11 +3,11 @@ Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
|||
|
||||
This file is part of Savanni's AVR library.
|
||||
|
||||
Lumeto 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 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.
|
||||
|
||||
Lumeto 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.
|
||||
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 Lumeto. If not, see <https://www.gnu.org/licenses/>.
|
||||
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 __BASE_H__
|
||||
|
@ -51,7 +51,7 @@ inline void dio_set(dio_t *line, bool value) {
|
|||
}
|
||||
}
|
||||
|
||||
inline int dio_read(dio_t *line) {
|
||||
inline uint8_t dio_read(dio_t *line) {
|
||||
return (*(line->pin) & _BV(line->addr)) > 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,11 +3,11 @@ Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
|||
|
||||
This file is part of Savanni's AVR library.
|
||||
|
||||
Lumeto 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 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.
|
||||
|
||||
Lumeto 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.
|
||||
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 Lumeto. If not, see <https://www.gnu.org/licenses/>.
|
||||
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 <dio.h>
|
||||
|
|
|
@ -2,11 +2,11 @@
|
|||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1649944829,
|
||||
"narHash": "sha256-wjOgLfjCdyoRamMOrVJceeDJk4LvJsQOxBoT3k16/7Q=",
|
||||
"lastModified": 1659446231,
|
||||
"narHash": "sha256-hekabNdTdgR/iLsgce5TGWmfIDZ86qjPhxDg/8TlzhE=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "2f06b87f64bc06229e05045853e0876666e1b023",
|
||||
"rev": "eabc38219184cc3e04a974fe31857d8e0eac098d",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
|
94
flake.nix
94
flake.nix
|
@ -13,7 +13,6 @@
|
|||
|
||||
attiny85 = { mcu = "attiny85"; chip_select = "AVR_ATtiny85"; f_cpu = "8000000"; };
|
||||
atmega32u4 = { mcu = "atmega32u4"; chip_select = "AVR_ATmega32u4"; f_cpu = "8000000"; };
|
||||
processor = atmega32u4;
|
||||
|
||||
mkLibrary = { pkgs, gcc, cflags, pname, psrc, pbuildInputs ? [] }:
|
||||
pkgs.stdenv.mkDerivation rec {
|
||||
|
@ -44,8 +43,10 @@
|
|||
'';
|
||||
};
|
||||
|
||||
mkProgram = { pkgs, gcc, cflags, pname, psrc, pbuildInputs ? [] }:
|
||||
pkgs.stdenv.mkDerivation rec {
|
||||
mkProgram = { pkgs, gcc, cflags, pname, psrc, pbuildInputs ? [], avr ? false }:
|
||||
let
|
||||
AVR = if avr == true then "1" else "";
|
||||
in pkgs.stdenv.mkDerivation rec {
|
||||
name = pname;
|
||||
src = psrc;
|
||||
|
||||
|
@ -64,16 +65,18 @@
|
|||
fi
|
||||
done
|
||||
|
||||
${gcc} -o ${name} ${cflags} ${object_files} *.o
|
||||
${gcc} -o ${name}.elf ${cflags} ${object_files} *.o
|
||||
if $AVR; then
|
||||
$OBJCOPY -O ihex ${name}.elf ${name}.hex
|
||||
fi
|
||||
'';
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p $out/bin
|
||||
cp ${name} $out/bin
|
||||
cp ${name}.elf ${name}.hex $out/bin
|
||||
'';
|
||||
};
|
||||
|
||||
|
||||
in rec
|
||||
{
|
||||
defaultPackage."x86_64-linux" =
|
||||
|
@ -85,22 +88,11 @@
|
|||
paths = [
|
||||
(packages."x86_64-linux"."prime-tx" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; })
|
||||
(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"."dio-attiny85" = (packages."x86_64-linux"."dio" attiny85); */
|
||||
|
||||
/*
|
||||
packages."x86_64-linux"."dio" =
|
||||
let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in mkLibrary {
|
||||
mkDerivation = pkgs.stdenv.mkDerivation;
|
||||
gcc =
|
||||
cflags =
|
||||
name =
|
||||
*/
|
||||
|
||||
packages."x86_64-linux"."dio" =
|
||||
{ gcc, cflags }:
|
||||
let
|
||||
|
@ -113,18 +105,6 @@
|
|||
psrc = ./dio;
|
||||
};
|
||||
|
||||
packages."x86_64-linux"."rng" =
|
||||
{ gcc, cflags }:
|
||||
let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in mkLibrary {
|
||||
pkgs = pkgs;
|
||||
gcc = gcc;
|
||||
cflags = cflags;
|
||||
pname = "rng";
|
||||
psrc = ./rng;
|
||||
};
|
||||
|
||||
packages."x86_64-linux"."animation" =
|
||||
{ gcc, cflags }:
|
||||
let
|
||||
|
@ -252,6 +232,58 @@
|
|||
];
|
||||
};
|
||||
|
||||
packages."x86_64-linux"."lantern_" =
|
||||
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; };
|
||||
packages."x86_64-linux"."lantern" =
|
||||
{ gcc, cflags, avr }:
|
||||
let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in mkProgram {
|
||||
pkgs = pkgs;
|
||||
gcc = gcc;
|
||||
cflags = cflags;
|
||||
pname = "lantern";
|
||||
psrc = ./lantern;
|
||||
inherit avr;
|
||||
|
||||
pbuildInputs = [
|
||||
(packages."x86_64-linux"."animation" { inherit gcc cflags; })
|
||||
(packages."x86_64-linux"."dio" { inherit gcc cflags; })
|
||||
(packages."x86_64-linux"."display" { inherit gcc cflags; })
|
||||
(packages."x86_64-linux"."rfm" { inherit gcc cflags; })
|
||||
(packages."x86_64-linux"."shift-register" { inherit gcc cflags; })
|
||||
(packages."x86_64-linux"."sk9822" { inherit gcc cflags; })
|
||||
(packages."x86_64-linux"."spi" { inherit gcc cflags; })
|
||||
];
|
||||
};
|
||||
|
||||
packages."x86_64-linux"."lantern-controller_" =
|
||||
let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
avr = pkgs.pkgsCross.avr.buildPackages;
|
||||
in packages."x86_64-linux"."lantern-controller" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; };
|
||||
packages."x86_64-linux"."lantern-controller" =
|
||||
{ gcc, cflags, avr }:
|
||||
let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in mkProgram {
|
||||
pkgs = pkgs;
|
||||
gcc = gcc;
|
||||
cflags = cflags;
|
||||
pname = "lantern-controller";
|
||||
psrc = ./lantern-controller;
|
||||
inherit avr;
|
||||
|
||||
pbuildInputs = [
|
||||
(packages."x86_64-linux"."dio" { inherit gcc cflags; })
|
||||
(packages."x86_64-linux"."spi" { inherit gcc cflags; })
|
||||
(packages."x86_64-linux"."rfm" { inherit gcc cflags; })
|
||||
];
|
||||
};
|
||||
|
||||
devShell."x86_64-linux" =
|
||||
let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
|
|
|
@ -88,14 +88,16 @@ int main (void) {
|
|||
PORTB = 0;
|
||||
_delay_ms(50);
|
||||
|
||||
dio_t data_pin = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 0 };
|
||||
dio_t clock_pin = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 2 };
|
||||
sk9822_t lights = {
|
||||
.data_pin = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 0 },
|
||||
.clock_pin = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 2 }
|
||||
};
|
||||
|
||||
rng_t rng = rng_new(0);
|
||||
animation_t animation = animation_new();
|
||||
while (1) {
|
||||
animation_step(&animation, &rng);
|
||||
send_pixels(data_pin, clock_pin, &animation.lamp, 1);
|
||||
sk9822_send(&lights, &animation.lamp, 1);
|
||||
_delay_ms(FRAME_DELAY_MS);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,90 @@
|
|||
#include <avr/interrupt.h>
|
||||
#include <avr/sleep.h>
|
||||
#include <dio.h>
|
||||
#include <rfm.h>
|
||||
#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 },
|
||||
{ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 7 },
|
||||
{ .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 6 },
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
bool button_press[4];
|
||||
} status_flags_t;
|
||||
|
||||
status_flags_t status_flags = {
|
||||
.button_press = { false, false, false, false }
|
||||
};
|
||||
|
||||
ISR(INT2_vect) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
status_flags.button_press[i] = dio_read(&buttons[i]);
|
||||
}
|
||||
}
|
||||
|
||||
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 },
|
||||
.data_out = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 2 },
|
||||
.data_in = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 3 },
|
||||
.chip_select = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 4 },
|
||||
},
|
||||
.irq = { .ddr = &DDRE, .port = &PORTE, .pin = &PINE, .addr = 6 },
|
||||
.reset = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 4 },
|
||||
};
|
||||
|
||||
rfm_error_e error;
|
||||
rfm_init(&radio, (uint8_t [4]){ 0xde, 0xca, 0xfb, 0xad }, 4, &error);
|
||||
|
||||
set_sleep_mode(SLEEP_MODE_IDLE);
|
||||
while(1) {
|
||||
sei();
|
||||
sleep_mode();
|
||||
cli();
|
||||
|
||||
uint8_t msg;
|
||||
if (status_flags.button_press[0]) {
|
||||
status_flags.button_press[0] = false;
|
||||
msg = lme_normal;
|
||||
} else if (status_flags.button_press[1]) {
|
||||
status_flags.button_press[1] = false;
|
||||
msg = lme_spooky;
|
||||
} else if (status_flags.button_press[2]) {
|
||||
status_flags.button_press[2] = false;
|
||||
msg = lme_eerie;
|
||||
} else if (status_flags.button_press[3]) {
|
||||
status_flags.button_press[3] = false;
|
||||
msg = lme_off;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
rfm_transmit(&radio, &msg, 1);
|
||||
|
||||
interrupt_flags_t flags = rfm_interrupts(&radio);
|
||||
while(!flags.packet_sent) {
|
||||
_delay_ms(1);
|
||||
flags = rfm_interrupts(&radio);
|
||||
}
|
||||
rfm_standby(&radio);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,349 @@
|
|||
/*
|
||||
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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "lantern.h"
|
||||
|
||||
uint8_t bound_uint8(int value, uint8_t min, uint8_t max) {
|
||||
if (value > max) {
|
||||
return max;
|
||||
} else if (value < min) {
|
||||
return min;
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t random_step(uint8_t value, uint8_t min, uint8_t max) {
|
||||
int8_t step = (rand() % 40) - 20;
|
||||
int new_value = value + step;
|
||||
|
||||
return bound_uint8(new_value, min, max);
|
||||
}
|
||||
|
||||
animation_t animation_new(void) {
|
||||
return (animation_t) {
|
||||
.color = { .brightness = 16, .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),
|
||||
|
||||
.frame = 0,
|
||||
.duration = 0,
|
||||
};
|
||||
}
|
||||
|
||||
void animation_begin(animation_t *self, uint8_t rdest, uint8_t gdest, uint8_t bdest, uint8_t duration) {
|
||||
self->red_line = time_line_new(0, self->color.r, duration, rdest);
|
||||
self->green_line = time_line_new(0, self->color.g, duration, gdest);
|
||||
self->blue_line = time_line_new(0, self->color.b, duration, bdest);
|
||||
self->frame = 0;
|
||||
self->duration = duration;
|
||||
}
|
||||
|
||||
void animation_step(animation_t *self) {
|
||||
if (self->frame == self->duration) {
|
||||
self->frame = 0;
|
||||
self->duration = 0;
|
||||
} else {
|
||||
self->frame++;
|
||||
self->color.r = bound_uint8(time_line_next(&self->red_line), 0, 255);
|
||||
self->color.g = bound_uint8(time_line_next(&self->green_line), 0, 255);
|
||||
self->color.b = bound_uint8(time_line_next(&self->blue_line), 0, 255);
|
||||
}
|
||||
}
|
||||
|
||||
bool animation_running(animation_t *self) {
|
||||
return self->frame != self->duration;
|
||||
}
|
||||
|
||||
lantern_t lantern_new(sk9822_t lights) {
|
||||
sk9822_init(&lights);
|
||||
return (lantern_t){
|
||||
.lights = lights,
|
||||
.color_scheme = cse_off,
|
||||
.animations = {
|
||||
animation_new(),
|
||||
animation_new(),
|
||||
animation_new(),
|
||||
animation_new(),
|
||||
animation_new(),
|
||||
animation_new(),
|
||||
animation_new(),
|
||||
animation_new(),
|
||||
animation_new(),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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:
|
||||
animation_begin(
|
||||
&self->animations[0],
|
||||
random_step(self->animations[0].color.r, 180, 255),
|
||||
random_step(self->animations[0].color.g, 20, 50),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[3],
|
||||
random_step(self->animations[3].color.r, 180, 255),
|
||||
random_step(self->animations[3].color.g, 20, 50),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[6],
|
||||
random_step(self->animations[3].color.r, 180, 255),
|
||||
random_step(self->animations[3].color.g, 20, 50),
|
||||
0,
|
||||
5);
|
||||
break;
|
||||
case 1:
|
||||
animation_begin(
|
||||
&self->animations[1],
|
||||
random_step(self->animations[1].color.r, 160, 230),
|
||||
random_step(self->animations[1].color.g, 10, 30),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[4],
|
||||
random_step(self->animations[4].color.r, 160, 230),
|
||||
random_step(self->animations[4].color.g, 10, 30),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[7],
|
||||
random_step(self->animations[7].color.r, 160, 230),
|
||||
random_step(self->animations[7].color.g, 10, 30),
|
||||
0,
|
||||
5);
|
||||
break;
|
||||
case 2:
|
||||
animation_begin(
|
||||
&self->animations[2],
|
||||
random_step(self->animations[2].color.r, 140, 170),
|
||||
random_step(self->animations[2].color.g, 0, 10),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[5],
|
||||
random_step(self->animations[5].color.r, 140, 170),
|
||||
random_step(self->animations[5].color.g, 0, 10),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[8],
|
||||
random_step(self->animations[8].color.r, 140, 170),
|
||||
random_step(self->animations[8].color.g, 0, 10),
|
||||
0,
|
||||
5);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void lantern_start_spooky(lantern_t *self, int light_idx) {
|
||||
switch (light_idx) {
|
||||
case 0:
|
||||
animation_begin(
|
||||
&self->animations[0],
|
||||
0,
|
||||
random_step(self->animations[0].color.g, 40, 140),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[3],
|
||||
0,
|
||||
random_step(self->animations[3].color.g, 40, 140),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[6],
|
||||
0,
|
||||
random_step(self->animations[6].color.g, 40, 140),
|
||||
0,
|
||||
5);
|
||||
break;
|
||||
case 1:
|
||||
animation_begin(
|
||||
&self->animations[1],
|
||||
0,
|
||||
random_step(self->animations[1].color.g, 20, 80),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[4],
|
||||
0,
|
||||
random_step(self->animations[4].color.g, 20, 80),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[7],
|
||||
0,
|
||||
random_step(self->animations[7].color.g, 20, 80),
|
||||
0,
|
||||
5);
|
||||
break;
|
||||
case 2:
|
||||
animation_begin(
|
||||
&self->animations[2],
|
||||
0,
|
||||
random_step(self->animations[2].color.g, 10, 40),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[5],
|
||||
0,
|
||||
random_step(self->animations[5].color.g, 10, 40),
|
||||
0,
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[8],
|
||||
0,
|
||||
random_step(self->animations[8].color.g, 10, 40),
|
||||
0,
|
||||
5);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void lantern_start_eerie(lantern_t *self, int light_idx) {
|
||||
switch (light_idx) {
|
||||
case 0:
|
||||
animation_begin(
|
||||
&self->animations[0],
|
||||
random_step(self->animations[0].color.r, 20, 40),
|
||||
random_step(self->animations[0].color.g, 20, 40),
|
||||
random_step(self->animations[0].color.b, 80, 120),
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[3],
|
||||
random_step(self->animations[3].color.r, 20, 40),
|
||||
random_step(self->animations[3].color.g, 20, 40),
|
||||
random_step(self->animations[3].color.b, 80, 120),
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[6],
|
||||
random_step(self->animations[6].color.r, 20, 40),
|
||||
random_step(self->animations[6].color.g, 20, 40),
|
||||
random_step(self->animations[6].color.b, 80, 120),
|
||||
5);
|
||||
break;
|
||||
case 1:
|
||||
animation_begin(
|
||||
&self->animations[1],
|
||||
random_step(self->animations[1].color.r, 0, 30),
|
||||
random_step(self->animations[1].color.g, 0, 30),
|
||||
random_step(self->animations[1].color.b, 60, 100),
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[4],
|
||||
random_step(self->animations[4].color.r, 0, 30),
|
||||
random_step(self->animations[4].color.g, 0, 30),
|
||||
random_step(self->animations[4].color.b, 60, 100),
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[7],
|
||||
random_step(self->animations[7].color.r, 0, 30),
|
||||
random_step(self->animations[7].color.g, 0, 30),
|
||||
random_step(self->animations[7].color.b, 60, 100),
|
||||
5);
|
||||
break;
|
||||
case 2:
|
||||
animation_begin(
|
||||
&self->animations[2],
|
||||
random_step(self->animations[2].color.r, 0, 10),
|
||||
random_step(self->animations[2].color.g, 0, 10),
|
||||
random_step(self->animations[2].color.b, 20, 40),
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[5],
|
||||
random_step(self->animations[5].color.r, 0, 10),
|
||||
random_step(self->animations[5].color.g, 0, 10),
|
||||
random_step(self->animations[5].color.b, 20, 40),
|
||||
5);
|
||||
animation_begin(
|
||||
&self->animations[8],
|
||||
random_step(self->animations[8].color.r, 0, 10),
|
||||
random_step(self->animations[8].color.g, 0, 10),
|
||||
random_step(self->animations[8].color.b, 20, 40),
|
||||
5);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void lantern_set_mode(lantern_t *self, color_scheme_e scheme) {
|
||||
self->color_scheme = scheme;
|
||||
}
|
||||
|
||||
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:
|
||||
lantern_start_normal(self, i);
|
||||
break;
|
||||
case cse_spooky:
|
||||
lantern_start_spooky(self, i);
|
||||
break;
|
||||
case cse_eerie:
|
||||
lantern_start_eerie(self, i);
|
||||
break;
|
||||
case cse_flash:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
animation_step(&self->animations[i]);
|
||||
colors[i] = self->animations[i].color;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
void lantern_show(lantern_t *lantern, display_t *display) {
|
||||
char msg1[20];
|
||||
char msg2[20];
|
||||
|
||||
snprintf(msg1, 20, "[%d,%d] %x,%x,%x",
|
||||
lantern->animations[0].frame,
|
||||
lantern->animations[0].duration,
|
||||
lantern->animations[0].color.r,
|
||||
lantern->animations[0].color.g,
|
||||
lantern->animations[0].color.b
|
||||
);
|
||||
snprintf(msg2, 20, "%x,%x,%x %x,%x,%x",
|
||||
lantern->animations[1].color.r,
|
||||
lantern->animations[1].color.g,
|
||||
lantern->animations[1].color.b,
|
||||
lantern->animations[2].color.r,
|
||||
lantern->animations[2].color.g,
|
||||
lantern->animations[2].color.b
|
||||
);
|
||||
display_clear(display);
|
||||
display_write_message(display, msg1);
|
||||
display_set_location(display, 1, 0);
|
||||
display_write_message(display, msg2);
|
||||
}
|
||||
*/
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
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 <animation.h>
|
||||
#include <display.h>
|
||||
#include <sk9822.h>
|
||||
|
||||
#ifndef __LANTERN_H__
|
||||
#define __LANTERN_H__
|
||||
|
||||
#define LIGHT_COUNT 9
|
||||
|
||||
typedef struct {
|
||||
rgb_t color;
|
||||
|
||||
time_line_t red_line;
|
||||
time_line_t green_line;
|
||||
time_line_t blue_line;
|
||||
|
||||
uint8_t frame;
|
||||
uint8_t duration;
|
||||
} animation_t;
|
||||
|
||||
typedef enum {
|
||||
cse_off,
|
||||
cse_normal,
|
||||
cse_spooky,
|
||||
cse_eerie,
|
||||
cse_flash
|
||||
} color_scheme_e;
|
||||
|
||||
typedef struct {
|
||||
sk9822_t lights;
|
||||
|
||||
color_scheme_e color_scheme;
|
||||
animation_t animations[LIGHT_COUNT];
|
||||
} lantern_t;
|
||||
|
||||
lantern_t lantern_new(sk9822_t);
|
||||
void lantern_set_mode(lantern_t *, color_scheme_e);
|
||||
void lantern_step(lantern_t *, rgb_t *);
|
||||
|
||||
void lantern_show(lantern_t *, display_t *);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,212 @@
|
|||
/*
|
||||
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 <avr/interrupt.h>
|
||||
#include <avr/sleep.h>
|
||||
#include <dio.h>
|
||||
#include <display.h>
|
||||
#include "lantern.h"
|
||||
#include <rfm.h>
|
||||
#include <sk9822.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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
|
||||
TCCR1A = 0;
|
||||
TCCR1B = _BV(3) | _BV(2) | _BV(0);
|
||||
|
||||
// Set the top for the counter
|
||||
OCR1AH = FPS_TIMEOUT >> 8;
|
||||
OCR1AL = FPS_TIMEOUT & 0xff;
|
||||
|
||||
// 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;
|
||||
EICRA |= 1 << ISC21 | 1 << ISC20;
|
||||
EICRB |= 1 << ISC61 | 1 << ISC60;
|
||||
|
||||
status_flags.frame_timeout = false;
|
||||
|
||||
// Disable unneeded modules
|
||||
PRR0 = _BV(7) | _BV(5) | _BV(2);
|
||||
PRR1 = _BV(7) | _BV(4) | _BV(3) | _BV(0);
|
||||
|
||||
rfm_t radio = (rfm_t){
|
||||
.spi = {
|
||||
.clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 1 },
|
||||
.data_out = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 2 },
|
||||
.data_in = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 3 },
|
||||
.chip_select = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 4 },
|
||||
},
|
||||
.irq = { .ddr = &DDRE, .port = &PORTE, .pin = &PINE, .addr = 6 },
|
||||
.reset = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 4 },
|
||||
};
|
||||
|
||||
rfm_error_e error;
|
||||
rfm_init(&radio, (uint8_t [4]){ 0xde, 0xca, 0xfb, 0xad }, 4, &error);
|
||||
rfm_listen(&radio);
|
||||
|
||||
sk9822_t lights = {
|
||||
.clock_pin = { .ddr = &DDRC, .port = &PORTC, .pin = &PINC, .addr = 7 },
|
||||
.data_pin = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 6 }
|
||||
};
|
||||
|
||||
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 },
|
||||
};
|
||||
|
||||
lantern_t lantern = lantern_new(lights);
|
||||
lantern_set_mode(&lantern, cse_off);
|
||||
|
||||
// setup_fps_timer();
|
||||
|
||||
// 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);
|
||||
|
||||
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];
|
||||
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;
|
||||
}
|
||||
}
|
||||
flags = rfm_interrupts(&radio);
|
||||
}
|
||||
|
||||
_delay_ms(FPS_MS);
|
||||
/*
|
||||
sei();
|
||||
sleep_mode();
|
||||
cli();
|
||||
*/
|
||||
}
|
||||
|
||||
sk9822_send(&lights, error_indicator, LIGHT_COUNT);
|
||||
_delay_ms(1000);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -196,7 +196,7 @@ void rfm_transmit(rfm_t *rfm, uint8_t *data, uint8_t length) {
|
|||
rfm_set_mode(rfm, (op_mode_t){ .listen_on = false, .mode = standby });
|
||||
_rfm_set_low_power(rfm);
|
||||
_rfm_write(rfm, REG_FIFO, &length, 1);
|
||||
_rfm_write(rfm, REG_FIFO, data, length);
|
||||
_rfm_write(rfm, REG_FIFO, (uint8_t *)data, length);
|
||||
_rfm_write(rfm, REG_DIO_MAPPING1, (uint8_t [1]){ 0x00 }, 1);
|
||||
rfm_set_mode(rfm, (op_mode_t){ .listen_on = false, .mode = tx });
|
||||
}
|
||||
|
@ -206,13 +206,13 @@ void rfm_receive_mode(rfm_t *rfm) {
|
|||
rfm_set_mode(rfm, (op_mode_t){ .listen_on = false, .mode = rx });
|
||||
}
|
||||
|
||||
void rfm_receive(rfm_t *rfm, uint8_t data[66], uint8_t *length) {
|
||||
void rfm_receive(rfm_t *rfm, uint8_t *data, uint8_t *length) {
|
||||
_rfm_write(rfm, REG_DIO_MAPPING1, (uint8_t [1]){ _BV(6) }, 1);
|
||||
rfm_set_mode(rfm, (op_mode_t){ .listen_on = false, .mode = rx });
|
||||
_rfm_read(rfm, REG_FIFO, length, 1);
|
||||
|
||||
if (*length > 0) {
|
||||
_rfm_read(rfm, 0x00, data, *length);
|
||||
_rfm_read(rfm, 0x00, (uint8_t *)data, *length);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -76,7 +76,7 @@ void rfm_sleep(rfm_t *);
|
|||
void rfm_standby(rfm_t *);
|
||||
void rfm_receive_mode(rfm_t *);
|
||||
void rfm_transmit(rfm_t *rfm, uint8_t *data, uint8_t length);
|
||||
void rfm_receive(rfm_t *rfm, uint8_t data[66], uint8_t *length);
|
||||
void rfm_receive(rfm_t *rfm, uint8_t *data, uint8_t *length);
|
||||
|
||||
op_mode_t rfm_mode(rfm_t *rfm);
|
||||
void rfm_set_mode(rfm_t *rfm, op_mode_t mode);
|
||||
|
|
24
rng/rng.c
24
rng/rng.c
|
@ -1,24 +0,0 @@
|
|||
/*
|
||||
Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
||||
|
||||
This file is part of Savanni's AVR library.
|
||||
|
||||
Lumeto 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.
|
||||
|
||||
Lumeto 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 Lumeto. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "rng.h"
|
||||
|
||||
rng_t rng_new(uint8_t seed) {
|
||||
rng_t rng = { .mod = 255, .a = 253, .c = 41, .seed = seed };
|
||||
return rng;
|
||||
}
|
||||
|
||||
uint8_t rng_sample(rng_t *state) {
|
||||
state->seed = (state->a * state->seed + state->c) % state->mod;
|
||||
return state->seed;
|
||||
}
|
||||
|
18
rng/rng.h
18
rng/rng.h
|
@ -1,18 +0,0 @@
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef __RNG_H__
|
||||
#define __RNG_H__
|
||||
|
||||
typedef struct RNG {
|
||||
uint8_t mod;
|
||||
int8_t a;
|
||||
int8_t c;
|
||||
uint8_t seed;
|
||||
} rng_t;
|
||||
|
||||
rng_t rng_new(uint8_t seed);
|
||||
|
||||
uint8_t rng_sample(rng_t *state);
|
||||
|
||||
#endif
|
|
@ -22,6 +22,7 @@ void send_byte(dio_t data_pin, dio_t clock_pin, uint8_t byte) {
|
|||
}
|
||||
|
||||
void send_start(dio_t data_pin, dio_t clock_pin) {
|
||||
dio_set(&clock_pin, 0);
|
||||
send_byte(data_pin, clock_pin, 0);
|
||||
send_byte(data_pin, clock_pin, 0);
|
||||
send_byte(data_pin, clock_pin, 0);
|
||||
|
@ -33,15 +34,21 @@ void send_term(dio_t data_pin, dio_t clock_pin) {
|
|||
send_byte(data_pin, clock_pin, 0xff);
|
||||
send_byte(data_pin, clock_pin, 0xff);
|
||||
send_byte(data_pin, clock_pin, 0xff);
|
||||
dio_set(&clock_pin, 1);
|
||||
}
|
||||
|
||||
void send_pixels(dio_t data_pin, dio_t clock_pin, rgb_t * pixels, uint8_t count) {
|
||||
send_start(data_pin, clock_pin);
|
||||
for (uint8_t i = 0; i < count; i++) {
|
||||
send_byte(data_pin, clock_pin, 0xe0 + pixels[i].brightness);
|
||||
send_byte(data_pin, clock_pin, pixels[i].r);
|
||||
send_byte(data_pin, clock_pin, pixels[i].b);
|
||||
send_byte(data_pin, clock_pin, pixels[i].g);
|
||||
}
|
||||
send_term(data_pin, clock_pin);
|
||||
void sk9822_init(sk9822_t *lights) {
|
||||
dio_set_direction(&lights->data_pin, LINE_OUT);
|
||||
dio_set_direction(&lights->clock_pin, LINE_OUT);
|
||||
}
|
||||
|
||||
void sk9822_send(sk9822_t *lights, rgb_t * pixels, uint8_t count) {
|
||||
send_start(lights->data_pin, lights->clock_pin);
|
||||
for (uint8_t i = 0; i < count; i++) {
|
||||
send_byte(lights->data_pin, lights->clock_pin, 0xe0 + pixels[i].brightness);
|
||||
send_byte(lights->data_pin, lights->clock_pin, pixels[i].r);
|
||||
send_byte(lights->data_pin, lights->clock_pin, pixels[i].b);
|
||||
send_byte(lights->data_pin, lights->clock_pin, pixels[i].g);
|
||||
}
|
||||
send_term(lights->data_pin, lights->clock_pin);
|
||||
}
|
||||
|
|
|
@ -3,11 +3,11 @@ Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
|||
|
||||
This file is part of Savanni's AVR library.
|
||||
|
||||
Lumeto 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 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.
|
||||
|
||||
Lumeto 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.
|
||||
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 Lumeto. If not, see <https://www.gnu.org/licenses/>.
|
||||
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 <avr/io.h>
|
||||
|
@ -23,6 +23,13 @@ typedef struct RGB_s {
|
|||
uint8_t b;
|
||||
} rgb_t;
|
||||
|
||||
void send_pixels(dio_t data_pin, dio_t clock_pin, rgb_t *pixels, uint8_t count);
|
||||
typedef struct SK9822 {
|
||||
dio_t data_pin;
|
||||
dio_t clock_pin;
|
||||
} sk9822_t;
|
||||
|
||||
void sk9822_init(sk9822_t *lights);
|
||||
|
||||
void sk9822_send(sk9822_t *lights, rgb_t *pixels, uint8_t count);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
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 Lumeto. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __TIMER_H__
|
||||
#define __TIMER_H__
|
||||
|
||||
#include <avr/io.h>
|
||||
|
||||
// ATmega16U4/32U4 Datasheet, table 14.4, page 133
|
||||
typedef enum {
|
||||
normal,
|
||||
pwm_phasecorrect_8bit,
|
||||
pwm_phasecorrect_9bit,
|
||||
pwm_phasecorrect_10bit,
|
||||
ctc,
|
||||
fast_pwm_8bit,
|
||||
fast_pwm_9bit,
|
||||
fast_pwm_10bit,
|
||||
pwm_phase_and_frequency_correct_icrtop,
|
||||
pwm_phase_and_frequency_correct_ocrtop,
|
||||
pwm_phase_correct_icrtop,
|
||||
pwm_phase_correct_ocrtop,
|
||||
ctc_icr,
|
||||
fast_pwm_icrtop,
|
||||
fast_pwm_ocrtop,
|
||||
} timer_mode_t;
|
||||
|
||||
// Clock prescaler selector, ATmega16U4/32U4 Datasheet, table 14.5, page 1354p
|
||||
typedef enum {
|
||||
none,
|
||||
clk_1,
|
||||
clk_8,
|
||||
clk_64,
|
||||
clk_256,
|
||||
clk_1024,
|
||||
external_falling_edge,
|
||||
external_rising_edge,
|
||||
} clock_select_t;
|
||||
|
||||
typedef struct {
|
||||
volatile uint8_t count_msb;
|
||||
volatile uint8_t count_lsb;
|
||||
volatile uint8_t top_msb;
|
||||
volatile uint8_t top_lsb;
|
||||
} timer_16bit_t;
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue