Compare commits

...

30 Commits
i2c ... main

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
Savanni D'Gerinel 4f778b79d3 Remove the dead code 2022-07-30 01:03:57 -04:00
Savanni D'Gerinel ef9b444b4d Rebuild prime-tx and radio-rx given the new flake structure 2022-07-30 01:01:53 -04:00
Savanni D'Gerinel 309475bcbc Remove base.h 2022-07-30 00:38:41 -04:00
Savanni D'Gerinel e287957168 Add mkLibrary to the flake 2022-07-30 00:33:46 -04:00
Savanni D'Gerinel c7b5541109 Adjust the LCD display settings 2022-07-26 21:12:11 -04:00
Savanni D'Gerinel ce2a2e98b5 Create a power management/battery management prototype 2022-07-20 19:19:45 -04:00
29 changed files with 1084 additions and 470 deletions

View File

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

View File

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

View File

@ -1,32 +0,0 @@
#include <stdlib.h>
void bresenham_time(int t1, int y1, int t2, int y2) {
int dt = t2 - t1;
int dy = y2 - y1;
int m = 2 * dy;
int err = 0;
if (m > 0) {
err = m - dt;
} else if (m < 0) {
err = m + dt;
}
for (int t = t1, y = y1; t <= t2; t++) {
printf("(%d, %d) [%d]\n", t, y, err);
err += m;
if (m > 0) {
if (err >= 0) {
y++;
err -= 2 * dt;
}
} else if (m < 0) {
if (err <= 0) {
y--;
err += 2 * dt;
}
}
}
}

View File

@ -1,36 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#include <animation.h>
int main (void) {
{
time_line_t line = time_line_new(0, 0, 15, 10);
time_line_print(&line);
for (int i = 0; i < 15; i++) {
time_line_next(&line);
time_line_print(&line);
}
}
printf("\n");
{
time_line_t line = time_line_new(0, 0, 15, 25);
time_line_print(&line);
for (int i = 0; i < 15; i++) {
time_line_next(&line);
time_line_print(&line);
}
}
printf("\n");
{
time_line_t line = time_line_new(0, 0, 10, 255);
time_line_print(&line);
for (int i = 0; i < 10; i++) {
time_line_next(&line);
time_line_print(&line);
}
}
}

View File

@ -3,11 +3,11 @@ Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of Savanni's AVR library.
Lumeto is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This AVR library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Lumeto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
This AVR library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License along with this AVR library. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef __BASE_H__
@ -51,7 +51,7 @@ inline void dio_set(dio_t *line, bool value) {
}
}
inline int dio_read(dio_t *line) {
inline uint8_t dio_read(dio_t *line) {
return (*(line->pin) & _BV(line->addr)) > 0;
}

View File

@ -3,14 +3,14 @@ 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 <base.h>
#include <dio.h>
#include <stdio.h>
#include <stdlib.h>
#include <util/delay.h>

View File

@ -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": {

507
flake.nix
View File

@ -9,382 +9,279 @@
let
version = builtins.string 0 8 self.lastModifiedDate;
supportedSystems = [ "x86_64-linux" "avr-none" ];
cflags = { mcu, chip_select, f_cpu }: ''-O -finline-functions -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Werror -Wstrict-prototypes -DF_CPU=${f_cpu} -std=gnu99 -D__${chip_select}__=1 -mmcu=${mcu}'';
mcu_cflags = { mcu, chip_select, f_cpu }: ''-O -finline-functions -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Werror -Wstrict-prototypes -DF_CPU=${f_cpu} -std=gnu99 -D__${chip_select}__=1 -mmcu=${mcu}'';
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 {
name = pname;
src = psrc;
buildInputs = pbuildInputs;
include_dirs = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}/include") buildInputs);
object_files = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs);
dontUnpack = true;
dontConfigure = true;
buildPhase = ''
for source in ${src}/*.c; do
if [ -e $source ]; then
${gcc} ${cflags} ${include_dirs} -c $source
fi
done
cp ${src}/*.h . || true
'';
installPhase = ''
mkdir -p $out/include $out/lib
cp *.h $out/include || true
cp *.o $out/lib || true
'';
};
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;
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}/*.c; do
if [ -e $source ]; then
${gcc} ${cflags} ${include_dirs} -c $source
fi
done
${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}.elf ${name}.hex $out/bin
'';
};
in rec
{
defaultPackage."x86_64-linux" =
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
avr = pkgs.pkgsCross.avr.buildPackages;
in pkgs.symlinkJoin {
name = "avr-monorepo";
paths = [
(packages."x86_64-linux"."flame" processor)
(packages."x86_64-linux"."prime-tx" processor)
(packages."x86_64-linux"."radio-rx" processor)
(packages."x86_64-linux"."interrupts" processor)
(packages."x86_64-linux"."prime-tx" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; })
(packages."x86_64-linux"."radio-rx" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; })
(packages."x86_64-linux"."lantern" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; })
(packages."x86_64-linux"."lantern-controller" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; })
];
};
packages."x86_64-linux"."base-attiny85" = (packages."x86_64-linux"."base" attiny85);
packages."x86_64-linux"."base" =
{ mcu, chip_select, f_cpu }:
packages."x86_64-linux"."dio" =
{ gcc, cflags }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
avr = pkgs.pkgsCross.avr.buildPackages;
in pkgs.stdenv.mkDerivation rec {
name = "base";
src = ./.;
CFLAGS = cflags { inherit mcu chip_select f_cpu; };
installPhase = ''
mkdir $out
cp base/base.h $out/
'';
};
packages."x86_64-linux"."rng" =
{ mcu, chip_select, f_cpu }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
avr = pkgs.pkgsCross.avr.buildPackages;
in pkgs.stdenv.mkDerivation rec {
name = "rng";
src = ./.;
buildInputs = [
(packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; })
];
CFLAGS = cflags { inherit mcu chip_select f_cpu; };
INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs);
OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs);
buildPhase = ''
${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o rng.o -c ${src}/rng/rng.c
'';
installPhase = ''
mkdir $out
cp rng/rng.h rng.o $out/
'';
in mkLibrary {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "dio";
psrc = ./dio;
};
packages."x86_64-linux"."animation" =
{ gcc, mcu, chip_select, f_cpu }:
{ gcc, cflags }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in pkgs.stdenv.mkDerivation rec {
name = "animation";
src = ./.;
# CFLAGS = cflags { inherit mcu chip_select f_cpu; };
buildPhase = ''
${gcc} -o animation.o -c ${src}/animation/animation.c
'';
installPhase = ''
mkdir $out
cp animation/animation.h animation.o $out/
'';
};
packages."x86_64-linux"."animation-test" =
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
lib = pkgs.lib;
in pkgs.stdenv.mkDerivation rec {
name = "animation-test";
src = ./.;
buildInputs = [
(packages."x86_64-linux"."animation" { inherit gcc; })
];
INCLUDE_DIRS = lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs);
OBJECT_FILES = lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs);
gcc = "${pkgs.gcc}/bin/gcc";
buildPhase = ''
${gcc} ${INCLUDE_DIRS} -o main.o -c ${src}/animation/main.c
${gcc} ${INCLUDE_DIRS} -o animation-test main.o ${OBJECT_FILES}
'';
installPhase = ''
mkdir $out
cp animation-test $out
'';
in mkLibrary {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "animation";
psrc = ./animation;
};
packages."x86_64-linux"."sk9822" =
{ mcu, chip_select, f_cpu }:
{ gcc, cflags }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
avr = pkgs.pkgsCross.avr.buildPackages;
in pkgs.stdenv.mkDerivation rec {
name = "sk9822";
src = ./.;
buildInputs = [
(packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; })
in mkLibrary {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "sk9822";
psrc = ./sk9822;
pbuildInputs = [
(packages."x86_64-linux"."dio" { inherit gcc cflags; })
];
CFLAGS = cflags { inherit mcu chip_select f_cpu; };
INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs);
OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs);
buildPhase = ''
${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o sk9822.o -c ${src}/sk9822/sk9822.c
'';
installPhase = ''
mkdir $out
cp sk9822/sk9822.h sk9822.o $out/
'';
};
packages."x86_64-linux"."shift-register" =
{ mcu, chip_select, f_cpu }:
{ gcc, cflags }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
avr = pkgs.pkgsCross.avr.buildPackages;
in pkgs.stdenv.mkDerivation rec {
name = "shift-register";
src = ./.;
buildInputs = [
(packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; })
in mkLibrary {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "shift-register";
psrc = ./shift-register;
pbuildInputs = [
(packages."x86_64-linux"."dio" { inherit gcc cflags; })
];
CFLAGS = cflags { inherit mcu chip_select f_cpu; };
INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs);
OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs);
buildPhase = ''
${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o shift_register.o -c ${src}/shift_register/shift_register.c
'';
installPhase = ''
mkdir $out
cp shift_register/shift_register.h shift_register.o $out/
'';
};
packages."x86_64-linux"."spi" =
{ mcu, chip_select, f_cpu }:
{ gcc, cflags }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
avr = pkgs.pkgsCross.avr.buildPackages;
in pkgs.stdenv.mkDerivation rec {
name = "spi";
src = ./.;
buildInputs = [
(packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; })
in mkLibrary {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "spi";
psrc = ./spi;
pbuildInputs = [
(packages."x86_64-linux"."dio" { inherit gcc cflags; })
];
CFLAGS = cflags { inherit mcu chip_select f_cpu; };
INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs);
OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs);
buildPhase = ''
${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o spi.o -c ${src}/spi/spi.c
'';
installPhase = ''
mkdir $out
cp spi/spi.h spi.o $out/
'';
};
packages."x86_64-linux"."rfm" =
{ mcu, chip_select, f_cpu }:
{ gcc, cflags }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
avr = pkgs.pkgsCross.avr.buildPackages;
in pkgs.stdenv.mkDerivation rec {
name = "rfm";
src = ./.;
buildInputs = [
(packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; })
(packages."x86_64-linux"."spi" { inherit mcu chip_select f_cpu; })
in mkLibrary {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "rfm";
psrc = ./rfm69hcw;
pbuildInputs = [
(packages."x86_64-linux"."dio" { inherit gcc cflags; })
(packages."x86_64-linux"."spi" { inherit gcc cflags; })
];
CFLAGS = cflags { inherit mcu chip_select f_cpu; };
INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs);
OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs);
buildPhase = ''
${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o rfm.o -c ${src}/rfm69hcw/rfm.c
'';
installPhase = ''
mkdir $out
cp rfm69hcw/rfm.h rfm.o $out/
'';
};
packages."x86_64-linux"."display" =
{ mcu, chip_select, f_cpu }:
{ gcc, cflags }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
avr = pkgs.pkgsCross.avr.buildPackages;
in pkgs.stdenv.mkDerivation rec {
name = "display";
src = ./.;
buildInputs = [
(packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; })
(packages."x86_64-linux"."shift-register" { inherit mcu chip_select f_cpu; })
in mkLibrary {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "display";
psrc = ./display;
pbuildInputs = [
(packages."x86_64-linux"."dio" { inherit gcc cflags; })
(packages."x86_64-linux"."shift-register" { inherit gcc cflags; })
];
CFLAGS = cflags { inherit mcu chip_select f_cpu; };
INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs);
OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs);
buildPhase = ''
${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o display.o -c ${src}/display/display.c
'';
installPhase = ''
mkdir $out
cp display/display.h display.o $out/
'';
};
packages."x86_64-linux"."flame" =
{ mcu, chip_select, f_cpu }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
lib = pkgs.lib;
avr = pkgs.pkgsCross.avr.buildPackages;
in pkgs.stdenv.mkDerivation rec {
name = "flame";
src = ./.;
buildInputs = [
(packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; })
(packages."x86_64-linux"."sk9822" { inherit mcu chip_select f_cpu; })
(packages."x86_64-linux"."rng" { inherit mcu chip_select f_cpu; })
(packages."x86_64-linux"."animation" { inherit mcu chip_select f_cpu;
gcc = "${avr.gcc}/bin/avr-gcc"; })
];
CFLAGS = cflags { inherit mcu chip_select f_cpu; };
INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs);
OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs);
buildPhase = ''
${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o main.o -c ${src}/flame/main.c
${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o flame.elf main.o ${OBJECT_FILES}
$OBJCOPY -O ihex flame.elf flame.hex
'';
installPhase = ''
mkdir $out
cp flame.elf flame.hex $out
'';
};
packages."x86_64-linux"."prime-tx" =
{ mcu, chip_select, f_cpu }:
{ gcc, cflags }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
lib = pkgs.lib;
avr = pkgs.pkgsCross.avr.buildPackages;
in pkgs.stdenv.mkDerivation rec {
name = "prime-tx";
src = ./.;
buildInputs = [
(packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; })
(packages."x86_64-linux"."spi" { inherit mcu chip_select f_cpu; })
(packages."x86_64-linux"."shift-register" { inherit mcu chip_select f_cpu; })
(packages."x86_64-linux"."rfm" { inherit mcu chip_select f_cpu; })
(packages."x86_64-linux"."display" { inherit mcu chip_select f_cpu; })
in mkProgram {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "prime-tx";
psrc = ./prime-tx;
pbuildInputs = [
(packages."x86_64-linux"."dio" { inherit gcc cflags; })
(packages."x86_64-linux"."spi" { inherit gcc cflags; })
(packages."x86_64-linux"."shift-register" { inherit gcc cflags; })
(packages."x86_64-linux"."rfm" { inherit gcc cflags; })
(packages."x86_64-linux"."display" { inherit gcc cflags; })
];
CFLAGS = cflags { inherit mcu chip_select f_cpu; };
INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs);
OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs);
buildPhase = ''
${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o main.o -c ${src}/prime-tx/main.c
${avr.gcc}/bin/avr-gcc ${CFLAGS} -o prime-tx.elf main.o ${OBJECT_FILES}
$OBJCOPY -O ihex prime-tx.elf prime-tx.hex
'';
installPhase = ''
mkdir $out
cp prime-tx.elf prime-tx.hex $out
'';
};
packages."x86_64-linux"."radio-rx" =
{ mcu, chip_select, f_cpu }:
{ gcc, cflags }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
lib = pkgs.lib;
avr = pkgs.pkgsCross.avr.buildPackages;
in pkgs.stdenv.mkDerivation rec {
name = "radio-rx";
src = ./.;
buildInputs = [
(packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; })
(packages."x86_64-linux"."spi" { inherit mcu chip_select f_cpu; })
(packages."x86_64-linux"."shift-register" { inherit mcu chip_select f_cpu; })
(packages."x86_64-linux"."rfm" { inherit mcu chip_select f_cpu; })
(packages."x86_64-linux"."display" { inherit mcu chip_select f_cpu; })
in mkProgram {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "radio-rx";
psrc = ./radio-rx;
pbuildInputs = [
(packages."x86_64-linux"."dio" { inherit gcc cflags; })
(packages."x86_64-linux"."spi" { inherit gcc cflags; })
(packages."x86_64-linux"."shift-register" { inherit gcc cflags; })
(packages."x86_64-linux"."rfm" { inherit gcc cflags; })
(packages."x86_64-linux"."display" { inherit gcc cflags; })
];
CFLAGS = cflags { inherit mcu chip_select f_cpu; };
INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs);
OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs);
buildPhase = ''
${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o main.o -c ${src}/radio-rx/main.c
${avr.gcc}/bin/avr-gcc ${CFLAGS} -o radio-rx.elf main.o ${OBJECT_FILES}
$OBJCOPY -O ihex radio-rx.elf radio-rx.hex
'';
installPhase = ''
mkdir $out
cp radio-rx.elf radio-rx.hex $out
'';
};
packages."x86_64-linux"."interrupts-standalone" = (packages."x86_64-linux"."interrupts" atmega32u4);
packages."x86_64-linux"."interrupts" =
{ mcu, chip_select, f_cpu }:
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"; };
lib = pkgs.lib;
avr = pkgs.pkgsCross.avr.buildPackages;
in pkgs.stdenv.mkDerivation rec {
name = "interrupts";
src = ./.;
in mkProgram {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "lantern";
psrc = ./lantern;
inherit avr;
buildInputs = [
(packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; })
(packages."x86_64-linux"."shift-register" { inherit mcu chip_select f_cpu; })
(packages."x86_64-linux"."display" { inherit mcu chip_select f_cpu; })
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; })
];
};
CFLAGS = cflags { inherit mcu chip_select f_cpu; };
INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs);
OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs);
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;
buildPhase = ''
${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o main.o -c ${src}/interrupts/main.c
${avr.gcc}/bin/avr-gcc ${CFLAGS} -o interrupts.elf main.o ${OBJECT_FILES}
$OBJCOPY -O ihex interrupts.elf interrupts.hex
'';
installPhase = ''
mkdir $out
cp interrupts.elf interrupts.hex $out
'';
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" =

View File

@ -1,7 +1,7 @@
#include <avr/io.h>
#include <util/delay.h>
#include <base.h>
#include <dio.h>
#include <sk9822.h>
#include <rng.h>
#include <animation.h>
@ -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);
}
}

View File

@ -1,4 +1,4 @@
#include <base.h>
#include <dio.h>
#include <display.h>
#include <stdbool.h>
#include <stdio.h>

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

55
lantern/lantern.h Normal file
View File

@ -0,0 +1,55 @@
/*
Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of Savanni's AVR library.
This AVR library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This AVR library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this AVR library. If not, see <https://www.gnu.org/licenses/>.
*/
#include <animation.h>
#include <display.h>
#include <sk9822.h>
#ifndef __LANTERN_H__
#define __LANTERN_H__
#define LIGHT_COUNT 9
typedef struct {
rgb_t color;
time_line_t red_line;
time_line_t green_line;
time_line_t blue_line;
uint8_t frame;
uint8_t duration;
} animation_t;
typedef enum {
cse_off,
cse_normal,
cse_spooky,
cse_eerie,
cse_flash
} color_scheme_e;
typedef struct {
sk9822_t lights;
color_scheme_e color_scheme;
animation_t animations[LIGHT_COUNT];
} lantern_t;
lantern_t lantern_new(sk9822_t);
void lantern_set_mode(lantern_t *, color_scheme_e);
void lantern_step(lantern_t *, rgb_t *);
void lantern_show(lantern_t *, display_t *);
#endif

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

@ -72,29 +72,29 @@ const struct morse_codepoint * morse_lookup(char val) {
return &MORSE[idx];
}
void send_codepoint(gpio_t *gpio, const struct morse_codepoint *codepoint) {
const uint8_t one = 1 << gpio->addr;
void send_codepoint(dio_t *dio, const struct morse_codepoint *codepoint) {
const uint8_t one = 1 << dio->addr;
for (size_t i = 0; i < codepoint->length; i++) {
*gpio->port = *(gpio->port) | one;
*dio->port = *(dio->port) | one;
if (codepoint->values[i] == DOT) {
_delay_ms(DOT * SCALE);
} else {
_delay_ms(DASH * SCALE);
}
*gpio->port = *(gpio->port) & ~one;
*dio->port = *(dio->port) & ~one;
_delay_ms(DIVIDER * SCALE);
}
_delay_ms(INTERCHAR * SCALE);
}
void send_morse(gpio_t *gpio, char *message) {
void send_morse(dio_t *dio, char *message) {
for (size_t i = 0; i < strlen(message); i++) {
if (message[i] == ' ') {
_delay_ms(INTERWORD * SCALE);
}
const struct morse_codepoint *codepoint = morse_lookup(message[i]);
if (codepoint != NULL) {
send_codepoint(gpio, codepoint);
send_codepoint(dio, codepoint);
}
}
}

View File

@ -16,7 +16,7 @@ You should have received a copy of the GNU General Public License along with Lum
#include <stdint.h>
#include <stdlib.h>
#include <base.h>
#include <dio.h>
/* TODO: make this parameterizable */
#define DOT 1
@ -26,6 +26,6 @@ You should have received a copy of the GNU General Public License along with Lum
#define INTERWORD 4
#define SCALE 100
void send_morse(gpio_t *gpio, char *message);
void send_morse(dio_t *dio, char *message);
#endif

20
power-management/main.c Normal file
View File

@ -0,0 +1,20 @@
#include <dio.h>
#define TIMEOUT 1
int main(void) {
dio_t power = { .ddr = &DDRB, .pin = &PINB, .port = &PORTB, .addr = 3 };
dio_set_direction(&power, LINE_OUT);
int timeout_seconds = TIMEOUT;
while (1) {
if (timeout_seconds <= 0) {
dio_set(&power, 1);
_delay_ms(50);
dio_set(&power, 0);
timeout_seconds = TIMEOUT;
}
timeout_seconds--;
_delay_ms(1000);
}
}

View File

@ -1,5 +1,5 @@
#include <avr/io.h>
#include <base.h>
#include <dio.h>
#include <display.h>
#include <rfm.h>
#include <spi.h>

View File

@ -1,5 +1,5 @@
#include <avr/io.h>
#include <base.h>
#include <dio.h>
#include <display.h>
#include <rfm.h>
#include <spi.h>
@ -50,9 +50,9 @@ void status(display_t *display, rfm_t *radio, int clock) {
int main(void) {
display_t display = {
.reg = {
.output = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 7 },
.shift_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 6 },
.latch_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 5 },
.output = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 7 },
.shift_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 6 },
.latch_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 5 },
},
};
@ -132,8 +132,13 @@ int main(void) {
char msg[16];
snprintf(msg, 15, "[%d]: %d", length, packet[0]);
char msg_2[16];
snprintf(msg_2, 15, "RSSI: %d", rfm_rssi(&radio));
display_clear(&display);
display_write_message(&display, msg);
display_set_location(&display, 1, 0);
display_write_message(&display, msg_2);
_delay_ms(1000);
}

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_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,14 +327,16 @@ interrupt_flags_t rfm_interrupts(rfm_t *rfm) {
}
uint8_t rfm_rssi(rfm_t *rfm) {
uint8_t rssi_reg;
// 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);

View File

@ -13,7 +13,7 @@ You should have received a copy of the GNU General Public License along with Lum
#ifndef __RFM_H__
#define __RFM_H__
#include <base.h>
#include <dio.h>
#include <spi.h>
#include <stdbool.h>
@ -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);

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 <base.h>
#include <stdlib.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

@ -11,7 +11,7 @@ You should have received a copy of the GNU General Public License along with Lum
*/
#include <util/delay.h>
#include <base.h>
#include <dio.h>
#include "shift_register.h"
void sr_strobe_line(dio_t *line) {

View File

@ -14,7 +14,7 @@ You should have received a copy of the GNU General Public License along with Lum
#ifndef __REG_H__
#define __REG_H__
#include <base.h>
#include <dio.h>
typedef struct SHIFT_REGISTER {
dio_t output;

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

View File

@ -3,15 +3,15 @@ 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>
#include <base.h>
#include <dio.h>
#ifndef __SK9822_H__
#define __SK9822_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

View File

@ -14,7 +14,7 @@ You should have received a copy of the GNU General Public License along with Lum
#ifndef __MORSE_H__
#define __MORSE_H__
#include <base.h>
#include <dio.h>
#include <stdlib.h>
typedef struct SPI {

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