diff --git a/flake.nix b/flake.nix index 12ab68c..015bec0 100644 --- a/flake.nix +++ b/flake.nix @@ -90,6 +90,7 @@ (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"."tron-bag" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags attiny85; avr = true; }) ]; }; @@ -284,6 +285,28 @@ ]; }; + packages."x86_64-linux"."tron-bag_" = + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + avr = pkgs.pkgsCross.avr.buildPackages; + in packages."x86_64-linux"."tron-bag" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags attiny85; avr = true; }; + packages."x86_64-linux"."tron-bag" = + { gcc, cflags, avr }: + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + in mkProgram { + pkgs = pkgs; + gcc = gcc; + cflags = cflags; + pname = "tron-bag"; + psrc = ./tron-bag; + inherit avr; + + pbuildInputs = [ + (packages."x86_64-linux"."dio" { inherit gcc cflags; }) + ]; + }; + devShell."x86_64-linux" = let pkgs = import nixpkgs { system = "x86_64-linux"; }; diff --git a/tron-bag/main.c b/tron-bag/main.c new file mode 100644 index 0000000..14acafc --- /dev/null +++ b/tron-bag/main.c @@ -0,0 +1,62 @@ +#include +#include +#include + +/* +const struct avr_mmcu_vcd_trace_t _mytrace[] _MMCU_ = { + { AVR_MCU_VCD_SYMBOL("PORTB"), .what = (void *)&PORTB, }, +}; +*/ + +#define FPS 100 +#define FPS_MS 1000 / FPS +#define MULT 5 + +typedef struct { + uint8_t value; + int8_t inc; +} counter_t; + +void step_counter(counter_t *counter) { + int16_t value = counter->value; + value += counter->inc; + + if (value >= 255) { + counter->value = 255; + counter->inc = -1; + } else if (value <= 0) { + counter->value = 0; + counter->inc = 1; + } else { + counter->value = value; + } +} + +int main(void) { + dio_t dio0 = (dio_t){ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 0 }; + dio_t dio1 = (dio_t){ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 1 }; + dio_set_direction(&dio0, LINE_OUT); + dio_set_direction(&dio1, LINE_OUT); + + // GTCCR = _BV(TSM) | _BV(PSR0); + TCCR0A = _BV(COM0A1) | _BV(COM0A0) | _BV(COM0B1) | _BV(COM0B0) | _BV(WGM01) | _BV(WGM00); + TCCR0B = _BV(WGM02) | _BV(CS00); + + counter_t red = (counter_t){ .value = 128, .inc = -1 }; + counter_t blue = (counter_t){ .value = 128, .inc = -1 }; + + // GTCCR &= ~_BV(TSM); + + while(1) { + step_counter(&red); + step_counter(&blue); + + OCR0A = red.value; + OCR0B = blue.value; + + _delay_ms(FPS_MS); + } + + return 0; +} +