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
|
||||
|
|
|
@ -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": {
|
||||
|
|
166
flake.nix
166
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 {
|
||||
|
@ -34,12 +33,7 @@
|
|||
${gcc} ${cflags} ${include_dirs} -c $source
|
||||
fi
|
||||
done
|
||||
for source in ${src}/src/*.c; do
|
||||
if [ -e $source ]; then
|
||||
${gcc} ${cflags} ${include_dirs} -c $source
|
||||
fi
|
||||
done
|
||||
cp ${src}/*.h ${src}/src/*.h . || true
|
||||
cp ${src}/*.h . || true
|
||||
'';
|
||||
|
||||
installPhase = ''
|
||||
|
@ -49,16 +43,17 @@
|
|||
'';
|
||||
};
|
||||
|
||||
mkProgram = { pkgs, gcc, cflags, pname, psrc, pbuildInputs ? [], pnativeBuildInputs ? [] }:
|
||||
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;
|
||||
|
||||
buildInputs = pbuildInputs;
|
||||
nativeBuildInputs = pnativeBuildInputs;
|
||||
|
||||
include_dirs = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}/include") pbuildInputs);
|
||||
object_files = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/lib/*.o") pbuildInputs);
|
||||
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;
|
||||
|
@ -70,42 +65,15 @@
|
|||
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
|
||||
'';
|
||||
};
|
||||
|
||||
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
|
||||
done
|
||||
|
||||
${gcc} -o ${name} ${cflags} ${object_files} *.o
|
||||
'';
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p $out/bin
|
||||
cp ${name} $out/bin
|
||||
cp ${name}.elf ${name}.hex $out/bin
|
||||
'';
|
||||
};
|
||||
|
||||
|
@ -120,8 +88,8 @@
|
|||
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"."packet-radio" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = "-Wall -Werror"; })
|
||||
(packages."x86_64-linux"."packet-radio-tests" { 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"."lantern-controller" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; })
|
||||
];
|
||||
};
|
||||
|
||||
|
@ -137,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
|
||||
|
@ -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" =
|
||||
{ gcc, cflags }:
|
||||
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" =
|
||||
let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
|
@ -327,7 +294,6 @@
|
|||
avrdude
|
||||
simavr
|
||||
gtkwave
|
||||
criterion
|
||||
];
|
||||
in
|
||||
pkgs.mkShell {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
*/
|
||||
|
|
@ -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/>.
|
||||
*/
|
||||
|
||||
#ifndef __PACKET_H__
|
||||
#define __PACKET_H__
|
||||
#include <animation.h>
|
||||
#include <display.h>
|
||||
#include <sk9822.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#ifndef __LANTERN_H__
|
||||
#define __LANTERN_H__
|
||||
|
||||
typedef enum {
|
||||
packet_type_data,
|
||||
packet_type_ack,
|
||||
packet_type_retry,
|
||||
} packet_type_e;
|
||||
#define LIGHT_COUNT 9
|
||||
|
||||
typedef struct {
|
||||
packet_type_e type;
|
||||
uint16_t count;
|
||||
uint8_t sender;
|
||||
uint8_t receiver;
|
||||
size_t length;
|
||||
char message[60];
|
||||
} packet_t;
|
||||
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;
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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
|
|
@ -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?
|
||||
*/
|
||||
}
|
||||
|
|
@ -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
|
|
@ -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));
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -327,8 +327,19 @@ interrupt_flags_t rfm_interrupts(rfm_t *rfm) {
|
|||
}
|
||||
|
||||
uint8_t rfm_rssi(rfm_t *rfm) {
|
||||
// uint8_t rssi_reg;
|
||||
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);
|
||||
|
||||
return rssi_value;
|
||||
}
|
||||
|
||||
|
|
|
@ -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