Compare commits

..

24 Commits

Author SHA1 Message Date
Savanni D'Gerinel b15c5edb4d Update the flake 2023-04-30 23:46:24 -04:00
Savanni D'Gerinel 8d412df7e1 Transmit an off signal 2022-10-30 12:19:26 -04:00
Savanni D'Gerinel a7af4accc0 Make the spooky and eerie animations more pronounced 2022-10-30 12:11:51 -04:00
Savanni D'Gerinel 196278a2fe Remove the strcpy usage 2022-10-29 19:12:54 -04:00
Savanni D'Gerinel 3e68db14bb Minor adjustments to the lantern program 2022-08-16 17:24:48 -04:00
Savanni D'Gerinel 966ef20eca Resolve a race condition around the radio interrupt 2022-08-09 22:43:45 -04:00
Savanni D'Gerinel 2acff8d3f5 Set up radio listener to control the lantern 2022-08-06 17:09:35 -04:00
Savanni D'Gerinel 7b4429db35 Create the lantern controller 2022-08-06 14:45:45 -04:00
Savanni D'Gerinel 78cd1908a7 Disable the buttons on the lantern 2022-08-06 14:18:14 -04:00
Savanni D'Gerinel 11436eeb28 Added all lights, all animations, and the trigger buttons 2022-08-06 11:10:04 -04:00
Savanni D'Gerinel bdbb13f67f Add the remaining six lights 2022-08-05 14:59:58 -04:00
Savanni D'Gerinel 7e16faefae Add flags to generate .hex files 2022-08-05 14:50:03 -04:00
Savanni D'Gerinel 301496cf0e Extract the lantern animation code from main 2022-08-05 10:15:09 -04:00
Savanni D'Gerinel dddf48651a Fix some errors with entering and exiting the sk9822 idle state 2022-08-05 09:52:50 -04:00
Savanni D'Gerinel 4b5d5632b9 Full prototype for the lantern 2022-08-05 09:52:50 -04:00
Savanni D'Gerinel e19bf772cb Save power by putting the module to sleep 2022-08-05 09:52:49 -04:00
Savanni D'Gerinel cf21388bd8 Allow different speed animations for each light in spooky and eerie 2022-08-05 09:52:07 -04:00
Savanni D'Gerinel aefb2184bf Set up different animation speeds per light 2022-08-05 09:52:07 -04:00
Savanni D'Gerinel ca0847c73d Add eerie and spooky animations 2022-08-05 09:52:07 -04:00
Savanni D'Gerinel 307617b1b2 Further refine the flame animation 2022-08-05 09:52:05 -04:00
Savanni D'Gerinel 74a1f9f945 Finish the basic lantern animation
Add some bounding boxes to keep the animation doing something like the right thing
2022-08-05 09:48:23 -04:00
Savanni D'Gerinel fde5db66f7 Fix some bugs and add the display 2022-08-05 09:47:04 -04:00
Savanni D'Gerinel 4c02e99566 Start building timing and animation code 2022-08-05 09:42:29 -04:00
Savanni D'Gerinel 94efdbb1c3 Improve the API for sk9822 2022-08-05 09:41:19 -04:00
24 changed files with 888 additions and 787 deletions

View File

@ -1,6 +1,17 @@
#include "animation.h" #include "animation.h"
#include <stdio.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) { time_line_t time_line_new(int t0, int y0, int t1, int y1) {
int dt = t1 - t0; int dt = t1 - t0;
int dy = y1 - y0; 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; 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) { int time_line_next(time_line_t *self) {
@ -28,11 +39,17 @@ int time_line_next(time_line_t *self) {
} }
} else { } else {
while (error < 0) { while (error < 0) {
self->y -= self->inc; self->y += self->inc;
error += 2 * self->dt; 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; self->error = error;
return self->y; return self->y;
} }

View File

@ -1,3 +1,6 @@
#include <stdint.h>
#ifndef __ANIMATION_H__ #ifndef __ANIMATION_H__
#define __ANIMATION_H__ #define __ANIMATION_H__
@ -12,6 +15,8 @@ typedef struct {
int inc; int inc;
int dt; int dt;
int dy; int dy;
int y0;
int y1;
} time_line_t; } time_line_t;
// Construct a new line object. x1 must be greater than x0, and dy must be less than dx. // 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 *); int time_line_next(time_line_t *);
// Diagnostic printout of the current state of 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 #endif

View File

@ -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; return (*(line->pin) & _BV(line->addr)) > 0;
} }

View File

@ -3,11 +3,11 @@ Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of Savanni's AVR library. 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> #include <dio.h>

View File

@ -2,11 +2,11 @@
"nodes": { "nodes": {
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1649944829, "lastModified": 1659446231,
"narHash": "sha256-wjOgLfjCdyoRamMOrVJceeDJk4LvJsQOxBoT3k16/7Q=", "narHash": "sha256-hekabNdTdgR/iLsgce5TGWmfIDZ86qjPhxDg/8TlzhE=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "2f06b87f64bc06229e05045853e0876666e1b023", "rev": "eabc38219184cc3e04a974fe31857d8e0eac098d",
"type": "github" "type": "github"
}, },
"original": { "original": {

164
flake.nix
View File

@ -13,7 +13,6 @@
attiny85 = { mcu = "attiny85"; chip_select = "AVR_ATtiny85"; f_cpu = "8000000"; }; attiny85 = { mcu = "attiny85"; chip_select = "AVR_ATtiny85"; f_cpu = "8000000"; };
atmega32u4 = { mcu = "atmega32u4"; chip_select = "AVR_ATmega32u4"; f_cpu = "8000000"; }; atmega32u4 = { mcu = "atmega32u4"; chip_select = "AVR_ATmega32u4"; f_cpu = "8000000"; };
processor = atmega32u4;
mkLibrary = { pkgs, gcc, cflags, pname, psrc, pbuildInputs ? [] }: mkLibrary = { pkgs, gcc, cflags, pname, psrc, pbuildInputs ? [] }:
pkgs.stdenv.mkDerivation rec { pkgs.stdenv.mkDerivation rec {
@ -34,12 +33,7 @@
${gcc} ${cflags} ${include_dirs} -c $source ${gcc} ${cflags} ${include_dirs} -c $source
fi fi
done done
for source in ${src}/src/*.c; do cp ${src}/*.h . || true
if [ -e $source ]; then
${gcc} ${cflags} ${include_dirs} -c $source
fi
done
cp ${src}/*.h ${src}/src/*.h . || true
''; '';
installPhase = '' installPhase = ''
@ -49,16 +43,17 @@
''; '';
}; };
mkProgram = { pkgs, gcc, cflags, pname, psrc, pbuildInputs ? [], pnativeBuildInputs ? [] }: mkProgram = { pkgs, gcc, cflags, pname, psrc, pbuildInputs ? [], avr ? false }:
pkgs.stdenv.mkDerivation rec { let
AVR = if avr == true then "1" else "";
in pkgs.stdenv.mkDerivation rec {
name = pname; name = pname;
src = psrc; src = psrc;
buildInputs = pbuildInputs; buildInputs = pbuildInputs;
nativeBuildInputs = pnativeBuildInputs;
include_dirs = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}/include") pbuildInputs); include_dirs = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}/include") buildInputs);
object_files = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/lib/*.o") pbuildInputs); object_files = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/lib/*.o") buildInputs);
dontUnpack = true; dontUnpack = true;
dontConfigure = true; dontConfigure = true;
@ -70,42 +65,15 @@
fi fi
done 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
installPhase = ''
mkdir -p $out/bin
cp ${name} $out/bin
'';
};
mkTests = { pkgs, gcc, cflags, pname, psrc, pbuildInputs ? [], pnativeBuildInputs ? [] }:
pkgs.stdenv.mkDerivation rec {
name = pname;
src = psrc;
buildInputs = pbuildInputs;
nativeBuildInputs = pnativeBuildInputs;
include_dirs = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}/include") buildInputs);
object_files = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/lib/*.o") buildInputs);
dontUnpack = true;
dontConfigure = true;
buildPhase = ''
for source in ${src}/tests/*.c; do
if [ -e $source ]; then
${gcc} ${cflags} ${include_dirs} -c $source
fi fi
done
${gcc} -o ${name} ${cflags} ${object_files} *.o
''; '';
installPhase = '' installPhase = ''
mkdir -p $out/bin mkdir -p $out/bin
cp ${name} $out/bin cp ${name}.elf ${name}.hex $out/bin
''; '';
}; };
@ -120,8 +88,8 @@
paths = [ paths = [
(packages."x86_64-linux"."prime-tx" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; }) (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"."radio-rx" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; })
(packages."x86_64-linux"."packet-radio" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = "-Wall -Werror"; }) (packages."x86_64-linux"."lantern" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; })
(packages."x86_64-linux"."packet-radio-tests" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = "-Wall -Werror"; }) (packages."x86_64-linux"."lantern-controller" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; })
]; ];
}; };
@ -137,18 +105,6 @@
psrc = ./dio; 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" = packages."x86_64-linux"."animation" =
{ gcc, cflags }: { gcc, cflags }:
let let
@ -238,47 +194,6 @@
]; ];
}; };
packages."x86_64-linux"."packet-radio_" =
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in packages."x86_64-linux"."packet-radio" { gcc = "${pkgs.gcc}/bin/gcc"; cflags = "-Wall -Werror"; };
packages."x86_64-linux"."packet-radio" =
{ gcc, cflags }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in mkLibrary {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "packet-radio";
psrc = ./packet-radio;
pbuildInputs = [ ];
};
packages."x86_64-linux"."packet-radio-tests_" =
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in packages."x86_64-linux"."packet-radio-tests" { gcc = "${pkgs.gcc}/bin/gcc"; cflags = "-Wall -Werror -lcriterion"; };
packages."x86_64-linux"."packet-radio-tests" =
{ gcc, cflags }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in mkTests {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "packet-radio-tests";
psrc = ./packet-radio;
pbuildInputs = [
(packages."x86_64-linux"."packet-radio" { inherit gcc cflags; })
];
pnativeBuildInputs = [
pkgs.criterion
];
};
packages."x86_64-linux"."prime-tx" = packages."x86_64-linux"."prime-tx" =
{ gcc, cflags }: { gcc, cflags }:
let let
@ -317,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" = devShell."x86_64-linux" =
let let
pkgs = import nixpkgs { system = "x86_64-linux"; }; pkgs = import nixpkgs { system = "x86_64-linux"; };
@ -327,7 +294,6 @@
avrdude avrdude
simavr simavr
gtkwave gtkwave
criterion
]; ];
in in
pkgs.mkShell { pkgs.mkShell {

View File

@ -88,14 +88,16 @@ int main (void) {
PORTB = 0; PORTB = 0;
_delay_ms(50); _delay_ms(50);
dio_t data_pin = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 0 }; sk9822_t lights = {
dio_t clock_pin = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 2 }; .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); rng_t rng = rng_new(0);
animation_t animation = animation_new(); animation_t animation = animation_new();
while (1) { while (1) {
animation_step(&animation, &rng); animation_step(&animation, &rng);
send_pixels(data_pin, clock_pin, &animation.lamp, 1); sk9822_send(&lights, &animation.lamp, 1);
_delay_ms(FRAME_DELAY_MS); _delay_ms(FRAME_DELAY_MS);
} }
} }

90
lantern-controller/main.c Normal file
View File

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

349
lantern/lantern.c Normal file
View File

@ -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);
}
*/

View File

@ -10,27 +10,46 @@ This AVR library is distributed in the hope that it will be useful, but WITHOUT
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/>. 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 __PACKET_H__ #include <animation.h>
#define __PACKET_H__ #include <display.h>
#include <sk9822.h>
#include <stdint.h> #ifndef __LANTERN_H__
#include <stdlib.h> #define __LANTERN_H__
#include <stdbool.h>
typedef enum { #define LIGHT_COUNT 9
packet_type_data,
packet_type_ack,
packet_type_retry,
} packet_type_e;
typedef struct { typedef struct {
packet_type_e type; rgb_t color;
uint16_t count;
uint8_t sender; time_line_t red_line;
uint8_t receiver; time_line_t green_line;
size_t length; time_line_t blue_line;
char message[60];
} packet_t; 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 #endif

212
lantern/main.c Normal file
View File

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

View File

@ -1,81 +0,0 @@
/*
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 "radio.h"
#include "packet_buffer.h"
// #include <stdlib.h>
// #include <string.h>
size_t circular_next(size_t current, size_t max);
packet_buffer_t *packet_buffer_new(void) {
packet_buffer_t *buffer = malloc(sizeof(packet_buffer_t));
packet_buffer_init(buffer);
return buffer;
}
void packet_buffer_init(packet_buffer_t *buffer) {
buffer->count = 0;
buffer->bottom = 0;
buffer->top = 0;
}
packet_t *packet_buffer_enqueue(packet_buffer_t *self, packet_buffer_status_e *status) {
if (!IS_OK(status)) return NULL;
if (packet_buffer_is_full(self)) {
*status = packet_buffer_status_full;
return NULL;
}
packet_t *ptr = &self->buffer[self->top];
self->top = circular_next(self->top, 5);
self->count++;
return ptr;
}
packet_t *packet_buffer_head(packet_buffer_t *self, packet_buffer_status_e *status) {
if (!IS_OK(status)) return NULL;
if (packet_buffer_is_empty(self)) {
*status = packet_buffer_status_empty;
return NULL;
}
return &self->buffer[self->bottom];
}
void packet_buffer_dequeue(packet_buffer_t *self, packet_buffer_status_e *status) {
if (!IS_OK(status)) return;
if (packet_buffer_is_empty(self)) {
*status = packet_buffer_status_empty;
return;
}
self->bottom = circular_next(self->bottom, 5);
self->count--;
}
bool packet_buffer_is_full(packet_buffer_t *self) {
return self->count == 5;
}
bool packet_buffer_is_empty(packet_buffer_t *self) {
return self->count == 0;
}
size_t circular_next(size_t current, size_t max) {
current++;
return (current < max) ? current : 0;
}

View File

@ -1,41 +0,0 @@
/*
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 __PACKET_BUFFER_H__
#define __PACKET_BUFFER_H__
#include "packet.h"
typedef enum {
packet_buffer_status_ok,
packet_buffer_status_empty,
packet_buffer_status_full,
} packet_buffer_status_e;
typedef struct {
size_t count;
size_t bottom;
size_t top;
packet_t buffer[5];
} packet_buffer_t;
packet_buffer_t *packet_buffer_new(void);
void packet_buffer_init(packet_buffer_t *);
packet_t * packet_buffer_enqueue(packet_buffer_t *, packet_buffer_status_e *);
packet_t * packet_buffer_head(packet_buffer_t *, packet_buffer_status_e *);
void packet_buffer_dequeue(packet_buffer_t *, packet_buffer_status_e *);
bool packet_buffer_is_full(packet_buffer_t *);
bool packet_buffer_is_empty(packet_buffer_t *);
#endif

View File

@ -1,175 +0,0 @@
/*
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 "radio.h"
#include "packet.h"
#include "packet_buffer.h"
#include <string.h>
struct conn_s {
uint16_t packet_counter;
uint8_t address;
packet_buffer_t outgoing;
packet_buffer_t incoming;
void (*set_mode)(radio_t *, radio_mode_e, radio_status_e *);
flags_t (*get_flags)(radio_t *, radio_status_e *);
void (*transmit)(radio_t *, packet_t *, radio_status_e *);
void (*receive)(radio_t *, uint8_t data[66], radio_status_e *);
radio_t *radio;
};
conn_t * conn_connect(radio_t *radio) {
conn_t *connection = malloc(sizeof(struct conn_s));
connection->packet_counter = 0;
connection->address = 0;
packet_buffer_init(&connection->outgoing);
packet_buffer_init(&connection->incoming);
connection->set_mode = radio->set_mode;
connection->get_flags = radio->get_flags;
connection->transmit = radio->transmit;
connection->receive = radio->receive;
connection->radio = radio;
return connection;
}
void conn_set_address(conn_t *self, uint8_t addr, radio_status_e *status) {
if (!IS_OK(status)) return;
self->address = addr;
}
void conn_set_mode(conn_t *self, radio_mode_e mode, radio_status_e *status) {
if (!IS_OK(status)) return;
self->set_mode(self->radio, mode, status);
}
flags_t conn_get_flags(conn_t *self, radio_status_e *status) {
if (!IS_OK(status)) return (flags_t){ .packet_sent = false, .packet_ready = false };
return self->get_flags(self->radio, status);
}
void conn_send(conn_t *self, uint8_t dest, msg_t *message, radio_status_e *status) {
if (!IS_OK(status)) return;
if (message->length > MAX_MESSAGE_LENGTH) {
*status = message_too_long;
return;
}
if (packet_buffer_is_full(&self->outgoing)) {
*status = outgoing_full;
return;
}
packet_buffer_status_e buffer_status = packet_buffer_status_ok;
packet_t *packet = packet_buffer_enqueue(&self->outgoing, &buffer_status);
if (!IS_OK(&buffer_status)) {
switch (buffer_status) {
case packet_buffer_status_full:
*status = outgoing_full;
break;
default:
*status = unhandled_error;
break;
}
return;
}
packet->type = packet_type_data;
packet->count = self->packet_counter;
packet->sender = self->address;
packet->receiver = dest;
packet->length = message->length;
memcpy(&packet->message, message->data, message->length);
self->transmit(self->radio, packet, status);
if (!IS_OK(status)) return;
return;
}
received_msg_t * conn_receive(conn_t *self, radio_status_e *status) {
if (!IS_OK(status)) return NULL;
if (packet_buffer_is_empty(&self->incoming)) {
*status = packet_buffer_status_empty;
return NULL;
}
packet_buffer_status_e buffer_status = packet_buffer_status_ok;
packet_t *packet = packet_buffer_head(&self->incoming, &buffer_status);
if (!IS_OK(&buffer_status)) {
// *status =
}
received_msg_t *msg = malloc(sizeof(received_msg_t) + packet->length);
msg->sender = packet->sender;
msg->message.length = packet->length;
memcpy(msg->message.data, packet->message, packet->length);
packet_buffer_dequeue(&self->incoming, &buffer_status);
if (!IS_OK(&buffer_status)) {
}
return msg;
}
void conn_handle_interrupt(conn_t *self) {
/*
radio_status_e *status = ok;
flags_t flags = self->get_flags(self->radio, status);
// if this is failing, we should raise a flag that indicates that the radio itself is in trouble
if (!IS_OK(status)) return;
if (flags.packet_ready) {
uint8_t data[66];
self->receive(self->radio, data, status);
// if this fails, increment an error counter
if (!IS_OK(status)) return;
packet_t *packet = (packet_t *)data;
if (packet->receiver != self->address) {
return;
}
switch (packet->type) {
case packet_type_data:
packet_buffer_status_e status;
packet_t *dest = packet_buffer_enqueue(self->incoming, &status);
memcpy(dest, packet, sizeof(packet_t) + packet->length);
break;
case packet_type_ack:
// uint16_t confirmed = packet->data[0] << 8 + packet->data[1];
// while (self->outgoing_buffer[self->outgoing_bottom]->count < confirmed && !is_empty(self->outgoing_buffer)
break;
case packet_type_retry:
break;
}
// process the packet type
// break;
}
// timeout processing
// if (time > receiving_timeout) { }
// if we were receiving data, a receiving_timeout must have been set. At this point, we're not receiving any more data, so it's time to send a confirmation.
// if (time > confirmation_timeout) { }
// if we were sending data, a confirmation timeout must have been set. But I don't recall what this is good for? Do we want to retransmit everything?
*/
}

View File

@ -1,75 +0,0 @@
/*
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 __RADIO_H__
#define __RADIO_H__
#include <stddef.h>
#include "packet.h"
#define MAX_MESSAGE_LENGTH 60
#define IS_OK(val) (val == NULL || !*val)
typedef struct {
size_t length;
char data[0];
} msg_t;
typedef struct {
uint16_t sender;
msg_t message;
} received_msg_t;
typedef enum {
ok,
not_found,
message_too_long,
outgoing_full,
unhandled_error,
} radio_status_e;
typedef struct conn_s conn_t;
typedef enum {
sleep,
standby,
tx,
rx,
listen
} radio_mode_e;
typedef struct {
bool packet_sent;
bool packet_ready;
} flags_t;
typedef struct radio_s radio_t;
struct radio_s {
void (*set_mode)(radio_t *, radio_mode_e, radio_status_e *);
flags_t (*get_flags)(radio_t *, radio_status_e *);
void (*transmit)(radio_t *, packet_t *, radio_status_e *);
void (*receive)(radio_t *, uint8_t data[66], radio_status_e *);
};
conn_t * conn_connect(radio_t *radio);
void conn_set_address(conn_t *, uint8_t, radio_status_e *);
void conn_set_mode(conn_t *, radio_mode_e, radio_status_e *);
flags_t conn_get_flags(conn_t *, radio_status_e *);
void conn_send(conn_t *, uint8_t, msg_t *, radio_status_e *);
received_msg_t * conn_receive(conn_t *, radio_status_e *);
// void radio_broadcast(conn_t *, uint16_t, msg_t *, radio_status_e *);
#endif

View File

@ -1,180 +0,0 @@
/*
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 <criterion/criterion.h>
#include <stdio.h>
#include "packet_buffer.h"
Test(packet_buffer, empty_buffer) {
packet_buffer_t *buffer = packet_buffer_new();
cr_assert(packet_buffer_is_empty(buffer));
cr_assert(!packet_buffer_is_full(buffer));
}
Test(packet_buffer, full_buffer) {
packet_buffer_status_e status = packet_buffer_status_ok;
packet_buffer_t *buffer = packet_buffer_new();
packet_t *packet;
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(packet != NULL);
cr_assert(status == packet_buffer_status_ok);
cr_assert(!packet_buffer_is_empty(buffer));
cr_assert(!packet_buffer_is_full(buffer));
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(packet != NULL);
cr_assert(status == packet_buffer_status_ok);
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(packet != NULL);
cr_assert(status == packet_buffer_status_ok);
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(packet != NULL);
cr_assert(status == packet_buffer_status_ok);
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(packet != NULL);
cr_assert(status == packet_buffer_status_ok);
cr_assert(packet_buffer_is_full(buffer));
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(packet == NULL);
cr_assert(status == packet_buffer_status_full);
cr_assert(packet_buffer_is_full(buffer));
}
Test(packet_buffer, basic_enqueue_and_dequeue) {
packet_buffer_status_e status = packet_buffer_status_ok;
packet_buffer_t *buffer = packet_buffer_new();
packet_t *packet;
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
packet->type = packet_type_data;
packet->count = 0;
packet->sender = 15;
packet->receiver = 16;
packet->length = 2;
memcpy(packet->message, (char [2]){ 25, 24 }, 2);
packet = packet_buffer_head(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
cr_assert(packet->type == packet_type_data);
cr_assert(packet->count == 0);
cr_assert(packet->sender == 15);
cr_assert(packet->receiver == 16);
cr_assert(packet->length == 2);
cr_assert(packet->message[0] == 25);
cr_assert(packet->message[1] == 24);
packet_buffer_dequeue(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
}
Test(packet_buffer, enqueue_to_full) {
packet_buffer_status_e status = packet_buffer_status_ok;
packet_buffer_t *buffer = packet_buffer_new();
packet_t *packet;
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
packet->type = packet_type_data;
packet->count = 0;
packet->sender = 15;
packet->receiver = 16;
packet->length = 2;
memcpy(packet->message, (char [2]){ 25, 24 }, 2);
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
packet = packet_buffer_head(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
cr_assert(packet->type == packet_type_data);
cr_assert(packet->count == 0);
cr_assert(packet->sender == 15);
cr_assert(packet->receiver == 16);
cr_assert(packet->length == 2);
cr_assert(packet->message[0] == 25);
cr_assert(packet->message[1] == 24);
packet_buffer_dequeue(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
}
Test(packet_buffer, circular_buffer) {
packet_buffer_status_e status = packet_buffer_status_ok;
packet_buffer_t *buffer = packet_buffer_new();
packet_t *packet;
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
packet->type = packet_type_data;
packet->count = 0;
packet->sender = 15;
packet->receiver = 16;
packet->length = 2;
memcpy(packet->message, (char [2]){ 25, 24 }, 2);
packet_buffer_dequeue(buffer, &status);
packet_buffer_dequeue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
packet = packet_buffer_enqueue(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
packet->type = packet_type_data;
packet->count = 1;
packet->sender = 15;
packet->receiver = 16;
packet->length = 2;
memcpy(packet->message, (char [2]){ 25, 24 }, 2);
packet = packet_buffer_head(buffer, &status);
printf("packet-type: %d\n", packet->type);
cr_assert(status == packet_buffer_status_ok);
cr_assert(packet->type == packet_type_data);
cr_assert(packet->count == 0);
cr_assert(packet->sender == 15);
cr_assert(packet->receiver == 16);
cr_assert(packet->length == 2);
cr_assert(packet->message[0] == 25);
cr_assert(packet->message[1] == 24);
packet_buffer_dequeue(buffer, &status);
packet_buffer_dequeue(buffer, &status);
packet_buffer_dequeue(buffer, &status);
packet_buffer_dequeue(buffer, &status);
packet = packet_buffer_head(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
cr_assert(packet->type == packet_type_data);
cr_assert(packet->count == 1);
cr_assert(packet->sender == 15);
cr_assert(packet->receiver == 16);
cr_assert(packet->length == 2);
cr_assert(packet->message[0] == 25);
cr_assert(packet->message[1] == 24);
packet_buffer_dequeue(buffer, &status);
cr_assert(status == packet_buffer_status_ok);
cr_assert(packet_buffer_is_empty(buffer));
}

View File

@ -1,46 +0,0 @@
/*
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 <criterion/criterion.h>
#include "radio.h"
/*
typedef struct {
void (*set_mode)(radio_mock_t *, radio_mode_e, radio_status_e *);
flags_t (*get_flags)(radio_mock_t *, radio_status_e *);
radio_mock_t (*transmit)(radio_mock_t *, packet_t *, radio_status_e *);
void (*receive)(radio_mock_t *, uint8_t data[66], radio_status_e *);
} radio_mock_t;
*/
void set_mode(radio_t *radio, radio_mode_e mode, radio_status_e *status) { }
flags_t get_flags(radio_t *radio, radio_status_e *status) {
flags_t flags = { .packet_sent = false, .packet_ready = false };
return flags;
}
void transmit(radio_t *radio, packet_t *packet, radio_status_e *status) { }
void receive(radio_t *radio, uint8_t data[66], radio_status_e *status) { }
Test(radio, connect) {
radio_t radio = (radio_t){ .set_mode = set_mode, .get_flags = get_flags, .transmit = transmit, .receive = receive };
conn_t *conn = conn_connect((radio_t *)&radio);
radio_status_e status = ok;
flags_t flags = conn_get_flags(conn, &status);
cr_assert(IS_OK(&status));
cr_assert(flags.packet_sent == false);
cr_assert(flags.packet_ready == false);
}

View File

@ -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_mode(rfm, (op_mode_t){ .listen_on = false, .mode = standby });
_rfm_set_low_power(rfm); _rfm_set_low_power(rfm);
_rfm_write(rfm, REG_FIFO, &length, 1); _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_write(rfm, REG_DIO_MAPPING1, (uint8_t [1]){ 0x00 }, 1);
rfm_set_mode(rfm, (op_mode_t){ .listen_on = false, .mode = tx }); 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 }); 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_write(rfm, REG_DIO_MAPPING1, (uint8_t [1]){ _BV(6) }, 1);
rfm_set_mode(rfm, (op_mode_t){ .listen_on = false, .mode = rx }); rfm_set_mode(rfm, (op_mode_t){ .listen_on = false, .mode = rx });
_rfm_read(rfm, REG_FIFO, length, 1); _rfm_read(rfm, REG_FIFO, length, 1);
if (*length > 0) { if (*length > 0) {
_rfm_read(rfm, 0x00, data, *length); _rfm_read(rfm, 0x00, (uint8_t *)data, *length);
} }
} }
@ -327,8 +327,19 @@ interrupt_flags_t rfm_interrupts(rfm_t *rfm) {
} }
uint8_t rfm_rssi(rfm_t *rfm) { uint8_t rfm_rssi(rfm_t *rfm) {
// uint8_t rssi_reg;
uint8_t rssi_value; uint8_t rssi_value;
/*
_rfm_write(rfm, REG_RSSI_CONFIG, (uint8_t [1]){ _BV(0) }, 1);
while(!(rssi_reg & _BV(1))) {
_rfm_read(rfm, REG_RSSI_CONFIG, &rssi_reg, 1);
}
*/
_rfm_read(rfm, REG_RSSI_VALUE, &rssi_value, 1); _rfm_read(rfm, REG_RSSI_VALUE, &rssi_value, 1);
return rssi_value; return rssi_value;
} }

View File

@ -76,7 +76,7 @@ void rfm_sleep(rfm_t *);
void rfm_standby(rfm_t *); void rfm_standby(rfm_t *);
void rfm_receive_mode(rfm_t *); void rfm_receive_mode(rfm_t *);
void rfm_transmit(rfm_t *rfm, uint8_t *data, uint8_t length); 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); op_mode_t rfm_mode(rfm_t *rfm);
void rfm_set_mode(rfm_t *rfm, op_mode_t mode); void rfm_set_mode(rfm_t *rfm, op_mode_t mode);

View File

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

View File

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

View File

@ -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) { 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); 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); 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) { void sk9822_init(sk9822_t *lights) {
send_start(data_pin, clock_pin); dio_set_direction(&lights->data_pin, LINE_OUT);
for (uint8_t i = 0; i < count; i++) { dio_set_direction(&lights->clock_pin, LINE_OUT);
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); void sk9822_send(sk9822_t *lights, rgb_t * pixels, uint8_t count) {
send_byte(data_pin, clock_pin, pixels[i].g); send_start(lights->data_pin, lights->clock_pin);
} for (uint8_t i = 0; i < count; i++) {
send_term(data_pin, clock_pin); 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);
} }

View File

@ -3,11 +3,11 @@ Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of Savanni's AVR library. 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> #include <avr/io.h>
@ -23,6 +23,13 @@ typedef struct RGB_s {
uint8_t b; uint8_t b;
} rgb_t; } 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 #endif

56
timer/timer.h Normal file
View File

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