Add mkLibrary to the flake

This commit is contained in:
Savanni D'Gerinel 2022-07-30 00:33:46 -04:00
parent c7b5541109
commit e287957168
19 changed files with 173 additions and 170 deletions

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

@ -1,58 +1,9 @@
/*
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/>.
*/
#ifndef __BASE_H__
#define __BASE_H__
#include <avr/io.h>
#include <stdint.h>
#include <stdbool.h>
#include <util/delay.h>
#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

58
dio/dio.h Normal file
View File

@ -0,0 +1,58 @@
/*
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/>.
*/
#ifndef __BASE_H__
#define __BASE_H__
#include <avr/io.h>
#include <stdbool.h>
#include <util/delay.h>
#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

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <base.h>
#include <dio.h>
#include <stdio.h>
#include <stdlib.h>
#include <util/delay.h>

123
flake.nix
View File

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

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>

View File

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

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

View File

@ -1,4 +1,4 @@
#include <base.h>
#include <dio.h>
#define TIMEOUT 1

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>

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>

View File

@ -1,5 +1,4 @@
#include <base.h>
#include <stdlib.h>
#ifndef __RNG_H__
#define __RNG_H__

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

@ -11,7 +11,7 @@ You should have received a copy of the GNU General Public License along with Lum
*/
#include <avr/io.h>
#include <base.h>
#include <dio.h>
#ifndef __SK9822_H__
#define __SK9822_H__

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 {