diff --git a/animation/bresenham.c b/animation/bresenham.c deleted file mode 100644 index b61148b..0000000 --- a/animation/bresenham.c +++ /dev/null @@ -1,32 +0,0 @@ -#include - -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; - } - } - } -} - diff --git a/animation/main.c b/animation/main.c deleted file mode 100644 index c85db9e..0000000 --- a/animation/main.c +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -#include - -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); - } - } -} diff --git a/base/base.h b/base/base.h index 7baab60..3ec4394 100644 --- a/base/base.h +++ b/base/base.h @@ -1,58 +1,9 @@ -/* -Copyright 2022, Savanni D'Gerinel -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 . -*/ - -#ifndef __BASE_H__ -#define __BASE_H__ - -#include +#include #include -#include -#define LINE_OUT 1 -#define LINE_IN 0 +#ifdef AVR -typedef struct DIO { - volatile uint8_t *ddr; - volatile uint8_t *port; - volatile uint8_t *pin; - uint8_t addr; -} dio_t; - -/* -#define DIO(addr) (dio_t){ .ddr = &DDRA, .port = &PORTA, .addr = addr } -#define DIO(addr) (dio_t){ .ddr = &DDRB, .port = &PORTB, .addr = addr } -#define DIO(addr) (dio_t){ .ddr = &DDRC, .port = &PORTC, .addr = addr } -#define DIO(addr) (dio_t){ .ddr = &DDRD, .port = &PORTD, .addr = addr } -*/ - -inline void dio_set_direction(dio_t *line, int direction) { - if (direction == LINE_OUT) { - *(line->ddr) |= _BV(line->addr); - } else { - *(line->ddr) &= ~(_BV(line->addr)); - } -} - -inline void dio_set(dio_t *line, bool value) { - if (value) { - *(line->port) |= _BV(line->addr); - } - else { - *(line->port) &= ~(_BV(line->addr)); - } -} - -inline int dio_read(dio_t *line) { - return (*(line->pin) & _BV(line->addr)) > 0; -} +#else #endif diff --git a/dio/dio.h b/dio/dio.h new file mode 100644 index 0000000..7baab60 --- /dev/null +++ b/dio/dio.h @@ -0,0 +1,58 @@ +/* +Copyright 2022, Savanni D'Gerinel + +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 . +*/ + +#ifndef __BASE_H__ +#define __BASE_H__ + +#include +#include +#include + +#define LINE_OUT 1 +#define LINE_IN 0 + +typedef struct DIO { + volatile uint8_t *ddr; + volatile uint8_t *port; + volatile uint8_t *pin; + uint8_t addr; +} dio_t; + +/* +#define DIO(addr) (dio_t){ .ddr = &DDRA, .port = &PORTA, .addr = addr } +#define DIO(addr) (dio_t){ .ddr = &DDRB, .port = &PORTB, .addr = addr } +#define DIO(addr) (dio_t){ .ddr = &DDRC, .port = &PORTC, .addr = addr } +#define DIO(addr) (dio_t){ .ddr = &DDRD, .port = &PORTD, .addr = addr } +*/ + +inline void dio_set_direction(dio_t *line, int direction) { + if (direction == LINE_OUT) { + *(line->ddr) |= _BV(line->addr); + } else { + *(line->ddr) &= ~(_BV(line->addr)); + } +} + +inline void dio_set(dio_t *line, bool value) { + if (value) { + *(line->port) |= _BV(line->addr); + } + else { + *(line->port) &= ~(_BV(line->addr)); + } +} + +inline int dio_read(dio_t *line) { + return (*(line->pin) & _BV(line->addr)) > 0; +} + +#endif diff --git a/display/display.c b/display/display.c index 54195c1..acd851a 100644 --- a/display/display.c +++ b/display/display.c @@ -10,7 +10,7 @@ Lumeto is distributed in the hope that it will be useful, but WITHOUT ANY WARRAN You should have received a copy of the GNU General Public License along with Lumeto. If not, see . */ -#include +#include #include #include #include diff --git a/flake.nix b/flake.nix index 523269d..6fe0f13 100644 --- a/flake.nix +++ b/flake.nix @@ -14,6 +14,38 @@ 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 = '' + set -x + for source in ${src}/*.c; do + if [ -e $source ]; then + ${gcc}/bin/gcc ${cflags} ${include_dirs} -c $source + fi + done + cp ${src}/*.h . + ''; + + installPhase = '' + mkdir -p $out/include $out/lib + cp *.h $out/include || true + cp *.o $out/lib || true + ''; + }; + + in rec { defaultPackage."x86_64-linux" = @@ -29,25 +61,64 @@ ]; }; - packages."x86_64-linux"."base-attiny85" = (packages."x86_64-linux"."base" attiny85); + packages."x86_64-linux"."dio-attiny85" = (packages."x86_64-linux"."dio" attiny85); - packages."x86_64-linux"."base" = + /* + packages."x86_64-linux"."dio" = + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + in mkLibrary { + mkDerivation = pkgs.stdenv.mkDerivation; + gcc = + cflags = + name = + */ + + + /* + packages."x86_64-linux"."dio" = { mcu, chip_select, f_cpu }: let pkgs = import nixpkgs { system = "x86_64-linux"; }; avr = pkgs.pkgsCross.avr.buildPackages; in pkgs.stdenv.mkDerivation rec { - name = "base"; + name = "dio"; src = ./.; CFLAGS = cflags { inherit mcu chip_select f_cpu; }; installPhase = '' mkdir $out - cp base/base.h $out/ + cp dio/dio.h $out/ ''; }; + */ + packages."x86_64-linux"."base" = + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + in mkLibrary { + pkgs = pkgs; + gcc = pkgs.gcc; + cflags = "-Werror -Wall"; + pname = "base"; + psrc = ./base; + pbuildInputs = [ ]; + }; + + packages."x86_64-linux"."rng" = + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + in mkLibrary { + pkgs = pkgs; + gcc = pkgs.gcc; + cflags = "-Werror -Wall"; + pname = "rng"; + psrc = ./rng; + pbuildInputs = [ packages."x86_64-linux"."base" ]; + }; + + /* packages."x86_64-linux"."rng" = { mcu, chip_select, f_cpu }: let @@ -58,7 +129,7 @@ src = ./.; buildInputs = [ - (packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; }) + (packages."x86_64-linux"."dio" { inherit mcu chip_select f_cpu; }) ]; CFLAGS = cflags { inherit mcu chip_select f_cpu; }; @@ -74,25 +145,17 @@ cp rng/rng.h rng.o $out/ ''; }; + */ packages."x86_64-linux"."animation" = - { gcc, mcu, chip_select, f_cpu }: 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/ - ''; + in mkLibrary { + pkgs = pkgs; + gcc = pkgs.gcc; + cflags = "-Werror -Wall"; + pname = "animation"; + psrc = ./animation; }; packages."x86_64-linux"."animation-test" = @@ -131,7 +194,7 @@ src = ./.; buildInputs = [ - (packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; }) + (packages."x86_64-linux"."dio" { inherit mcu chip_select f_cpu; }) ]; CFLAGS = cflags { inherit mcu chip_select f_cpu; }; @@ -157,7 +220,7 @@ src = ./.; buildInputs = [ - (packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; }) + (packages."x86_64-linux"."dio" { inherit mcu chip_select f_cpu; }) ]; CFLAGS = cflags { inherit mcu chip_select f_cpu; }; @@ -183,7 +246,7 @@ src = ./.; buildInputs = [ - (packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; }) + (packages."x86_64-linux"."dio" { inherit mcu chip_select f_cpu; }) ]; CFLAGS = cflags { inherit mcu chip_select f_cpu; }; @@ -209,7 +272,7 @@ src = ./.; buildInputs = [ - (packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; }) + (packages."x86_64-linux"."dio" { inherit mcu chip_select f_cpu; }) (packages."x86_64-linux"."spi" { inherit mcu chip_select f_cpu; }) ]; @@ -236,7 +299,7 @@ src = ./.; buildInputs = [ - (packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; }) + (packages."x86_64-linux"."dio" { inherit mcu chip_select f_cpu; }) (packages."x86_64-linux"."shift-register" { inherit mcu chip_select f_cpu; }) ]; @@ -264,7 +327,7 @@ src = ./.; buildInputs = [ - (packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; }) + (packages."x86_64-linux"."dio" { 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; @@ -297,7 +360,7 @@ src = ./.; buildInputs = [ - (packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; }) + (packages."x86_64-linux"."dio" { 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; }) @@ -332,7 +395,7 @@ src = ./.; buildInputs = [ - (packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; }) + (packages."x86_64-linux"."dio" { 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; }) @@ -367,7 +430,7 @@ src = ./.; buildInputs = [ - (packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; }) + (packages."x86_64-linux"."dio" { 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; }) ]; @@ -400,7 +463,7 @@ src = ./.; buildInputs = [ - (packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; }) + (packages."x86_64-linux"."dio" { inherit mcu chip_select f_cpu; }) ]; CFLAGS = cflags { inherit mcu chip_select f_cpu; }; diff --git a/flame/main.c b/flame/main.c index 6ca584c..d15d79d 100644 --- a/flame/main.c +++ b/flame/main.c @@ -1,7 +1,7 @@ #include #include -#include +#include #include #include #include diff --git a/interrupts/main.c b/interrupts/main.c index ea384e8..76ba7be 100644 --- a/interrupts/main.c +++ b/interrupts/main.c @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/morse/morse.c b/morse/morse.c index 30aa52c..8a85551 100644 --- a/morse/morse.c +++ b/morse/morse.c @@ -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); } } } diff --git a/morse/morse.h b/morse/morse.h index af437ec..6ced39d 100644 --- a/morse/morse.h +++ b/morse/morse.h @@ -16,7 +16,7 @@ You should have received a copy of the GNU General Public License along with Lum #include #include -#include +#include /* 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 diff --git a/power-management/main.c b/power-management/main.c index ec56e57..12a66b7 100644 --- a/power-management/main.c +++ b/power-management/main.c @@ -1,4 +1,4 @@ -#include +#include #define TIMEOUT 1 diff --git a/prime-tx/main.c b/prime-tx/main.c index 76d921c..6005bbe 100644 --- a/prime-tx/main.c +++ b/prime-tx/main.c @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/radio-rx/main.c b/radio-rx/main.c index 02db2fd..414b97e 100644 --- a/radio-rx/main.c +++ b/radio-rx/main.c @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/rfm69hcw/rfm.h b/rfm69hcw/rfm.h index d786627..2b99ee6 100644 --- a/rfm69hcw/rfm.h +++ b/rfm69hcw/rfm.h @@ -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 +#include #include #include diff --git a/rng/rng.h b/rng/rng.h index c98e374..7873205 100644 --- a/rng/rng.h +++ b/rng/rng.h @@ -1,5 +1,4 @@ #include -#include #ifndef __RNG_H__ #define __RNG_H__ diff --git a/shift_register/shift_register.c b/shift_register/shift_register.c index ce99177..508b886 100644 --- a/shift_register/shift_register.c +++ b/shift_register/shift_register.c @@ -11,7 +11,7 @@ You should have received a copy of the GNU General Public License along with Lum */ #include -#include +#include #include "shift_register.h" void sr_strobe_line(dio_t *line) { diff --git a/shift_register/shift_register.h b/shift_register/shift_register.h index e257d90..b7a6372 100644 --- a/shift_register/shift_register.h +++ b/shift_register/shift_register.h @@ -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 +#include typedef struct SHIFT_REGISTER { dio_t output; diff --git a/sk9822/sk9822.h b/sk9822/sk9822.h index 9313dd0..67e18c2 100644 --- a/sk9822/sk9822.h +++ b/sk9822/sk9822.h @@ -11,7 +11,7 @@ You should have received a copy of the GNU General Public License along with Lum */ #include -#include +#include #ifndef __SK9822_H__ #define __SK9822_H__ diff --git a/spi/spi.h b/spi/spi.h index ea49e7e..5574dfd 100644 --- a/spi/spi.h +++ b/spi/spi.h @@ -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 +#include #include typedef struct SPI {