diff --git a/projects/alive-color-whell/Makefile b/projects/alive-color-whell/Makefile new file mode 100644 index 0000000..82a8c47 --- /dev/null +++ b/projects/alive-color-whell/Makefile @@ -0,0 +1,166 @@ + +MCU = atmega328p + +F_CPU = 16000000 + +LFUSE = 0xFF +HFUSE = 0xDE +EFUSE = 0x05 + +MAIN = main.c + +## If you've split your program into multiple files, +## include the additional .c source (in same directory) here +## (and include the .h files in your foo.c) +LOCAL_SOURCE = + +## Here you can link to one more directory (and multiple .c files) +# EXTRA_SOURCE_DIR = ../AVR-Programming-Library/ +EXTRA_SOURCE_DIR = +EXTRA_SOURCE_FILES = + + + +##########------------------------------------------------------########## +########## Programmer Defaults ########## +########## Set up once, then forget about it ########## +########## (Can override. See bottom of file.) ########## +##########------------------------------------------------------########## +#19200 +PROGRAMMER_TYPE = arduino +PROGRAMMER_ARGS = -b 57600 -P /dev/ttyUSB0 + + +##########------------------------------------------------------########## +########## Makefile Magic! ########## +########## Summary: ########## +########## We want a .hex file ########## +########## Compile source files into .elf ########## +########## Convert .elf file into .hex ########## +########## You shouldn't need to edit below. ########## +##########------------------------------------------------------########## + +## Defined programs / locations +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AVRSIZE = avr-size +AVRDUDE = sudo avrdude + +## Compilation options, type man avr-gcc if you're curious. +CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -I. -I$(EXTRA_SOURCE_DIR) +CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums +CFLAGS += -Wall -Wno-main -Wno-strict-prototypes -Wno-comment +CFLAGS += -g2 -Wextra -pedantic -Wfatal-errors +CFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--relax + +CFLAGS_BUILD = $(CFLAGS) -Os + +# CFLAGS += -lm +## CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## for floating-point printf +## CFLAGS += -Wl,-u,vfprintf -lprintf_min ## for smaller printf + +## Lump target and extra source files together +TARGET = $(strip $(basename $(MAIN))) +SRC1 = $(TARGET).c +SRC = $(SRC1) +EXTRA_SOURCE = $(addprefix $(EXTRA_SOURCE_DIR), $(EXTRA_SOURCE_FILES)) +SRC += $(EXTRA_SOURCE) +SRC += $(LOCAL_SOURCE) + +## List of all header files +HEADERS = $(SRC:.c=.h) + +## For every .c file, compile an .o object file +OBJ = $(SRC:.c=.o) + +## Generic Makefile targets. (Only .hex file is necessary) +all: $(TARGET).hex size +pre: $(TARGET).pre + +%.hex: %.elf + $(OBJCOPY) -R .eeprom -O ihex $< $@ + +%.elf: $(SRC) + $(CC) $(CFLAGS_BUILD) $(SRC) --output $@ + +%.pre: $(SRC1) + $(CC) $(CFLAGS) -E $(SRC1) --output $@ + +%.eeprom: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + +debug: + @echo + @echo "Source files:" $(SRC) + @echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD) + @echo + +# Optionally create listing file from .elf +# This creates approximate assembly-language equivalent of your code. +# Useful for debugging time-sensitive bits, +# or making sure the compiler does what you want. +disassemble: $(TARGET).lst + +dis: disassemble +lst: disassemble + +eeprom: $(TARGET).eeprom + +%.lst: %.elf + $(OBJDUMP) -S $< > $@ + +# Optionally show how big the resulting program is +size: $(TARGET).elf + $(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf + +clean: + rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \ + $(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \ + $(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \ + $(TARGET).eeprom + +squeaky_clean: + rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ + + +##########------------------------------------------------------########## +########## Programmer-specific details ########## +########## Flashing code to AVR using avrdude ########## +##########------------------------------------------------------########## + +flash: $(TARGET).hex + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$< + +flash_eeprom: $(TARGET).eeprom + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$< + +terminal: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt + + +flash_arduino: PROGRAMMER_TYPE = arduino +flash_arduino: PROGRAMMER_ARGS = +flash_arduino: flash + +flash_dragon_isp: PROGRAMMER_TYPE = dragon_isp +flash_dragon_isp: PROGRAMMER_ARGS = +flash_dragon_isp: flash + + +##########------------------------------------------------------########## +########## Fuse settings and suitable defaults ########## +##########------------------------------------------------------########## + +## Generic +FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m + +fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) \ + $(PROGRAMMER_ARGS) $(FUSE_STRING) +show_fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nv + +## Called with no extra definitions, sets to defaults +set_default_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m +set_default_fuses: fuses diff --git a/projects/alive-color-whell/README.md b/projects/alive-color-whell/README.md new file mode 100644 index 0000000..2f5c253 --- /dev/null +++ b/projects/alive-color-whell/README.md @@ -0,0 +1,7 @@ +Alive color wheel +================= + +This is an animation for WS2812B RGB strip with 72 LEDs, but can be adjusted to smaller. + +See video: [https://www.youtube.com/watch?v=pgHmZdYfbz8](https://www.youtube.com/watch?v=pgHmZdYfbz8) + diff --git a/projects/alive-color-whell/lib/arduino_pins.h b/projects/alive-color-whell/lib/arduino_pins.h new file mode 100644 index 0000000..ca4f4c6 --- /dev/null +++ b/projects/alive-color-whell/lib/arduino_pins.h @@ -0,0 +1,42 @@ +#pragma once + +/** + Pin definitions for Arduino (Pro Mini with ATmega328P) +*/ + +#include "pins.h" + +#define D0 D,0 +#define D1 D,1 +#define D2 D,2 +#define D3 D,3 +#define D4 D,4 +#define D5 D,5 +#define D6 D,6 +#define D7 D,7 +#define D8 B,0 +#define D9 B,1 +#define D10 B,2 + +// MOSI MISO SCK - not good for input +#define D11 B,3 +#define D12 B,4 +#define D13 B,5 + +#define D14 C,0 +#define D15 C,1 +#define D16 C,2 +#define D17 C,3 +#define D18 C,4 +#define D19 C,5 +#define D20 C,6 +#define D21 C,7 + +#define A0 C,0 +#define A1 C,1 +#define A2 C,2 +#define A3 C,3 +#define A4 C,4 +#define A5 C,5 +#define A6 C,6 +#define A7 C,7 diff --git a/projects/alive-color-whell/lib/calc.h b/projects/alive-color-whell/lib/calc.h new file mode 100644 index 0000000..df04dac --- /dev/null +++ b/projects/alive-color-whell/lib/calc.h @@ -0,0 +1,40 @@ +#pragma once + +/** + General purpose calculation and bit manipulation utilities. +*/ + +// if max, go to zero. Else increment. +#define inc_wrap(var, min, max) do { if ((var) >= (max)) { (var)=min; } else { (var)++; } } while(0) + +// If zero, go to max. Else decrement, +#define dec_wrap(var, min, max) do { if ((var) > min) { (var)--; } else { (var)=(max); } } while(0) + +// === general bit manipulation with register === +#define sbi(reg, bit) do { (reg) |= (1 << (uint8_t)(bit)); } while(0) +#define cbi(reg, bit) do { (reg) &= ~(1 << (uint8_t)(bit)); } while(0) + +#define read_bit(reg, bit) ((((uint8_t)(reg)) >> (uint8_t)(bit)) & 0x1) +#define get_bit(reg, bit) read_bit(reg, bit) + +#define write_bit(reg, bit, value) do { (reg) = ((reg) & ~(1 << (uint8_t)(bit))) | (((uint8_t)(value) & 0x1) << (uint8_t)(bit)); } while(0) +#define set_bit(reg, bit, value) write_bit(reg, bit, value) +#define toggle_bit(reg, bit) do { (reg) ^= (1 << (uint8_t)(bit)); } while(0) + +// general pin manipulation - with pointer to register +#define sbi_p(reg_p, bit) do { (*(reg_p)) |= (1 << (uint8_t)(bit)); } while(0) +#define cbi_p(reg_p, bit) do { (*(reg_p)) &= ~(1 << (uint8_t)(bit)); } while(0) + +#define read_bit_p(reg_p, bit) ((*(reg_p) >> (uint8_t)(bit)) & 0x1) +#define get_bit_p(reg_p, bit) read_bit_p(reg_p, bit) + +#define write_bit_p(reg_p, bit, value) do { *(reg_p) = (*(reg_p) & ~(1 << ((uint8_t)(bit) & 0x1))) | (((uint8_t)(value) & 0x1) << (uint8_t)(bit)); } while(0) +#define set_bit_p(reg, bit, value) write_bit_p(reg_p, bit, value) +#define toggle_bit_p(reg_p, bit) do { *(reg_p) ^= (1 << (uint8_t)(bit)); } while(0) + + +// Check if value is in range A..B or B..A +#define in_range(x, low, high) (((low) < (high)) && ((x) > (low) && (x) < (high))) || (((low) > (high)) && ((x) < (low) || (x) > (high))) + +// Check if value is in range A..B. If B < A, matches all outside B..A +#define in_range_wrap(x, low, high) (((low) < (high)) && ((x) > (low) && (x) < (high))) || (((low) > (high)) && ((x) > (low) || (x) < (high))) diff --git a/projects/alive-color-whell/lib/colors.h b/projects/alive-color-whell/lib/colors.h new file mode 100644 index 0000000..fe3a7ec --- /dev/null +++ b/projects/alive-color-whell/lib/colors.h @@ -0,0 +1,59 @@ +#pragma once + +/* + Some useful utilities for RGB color manipulation +*/ + +typedef struct { + uint8_t r; + uint8_t g; + uint8_t b; +} xrgb_t; + +typedef uint32_t rgb24_t; +typedef uint16_t rgb16_t; +typedef uint16_t rgb12_t; +typedef uint8_t rgb6_t; + + +#define xrgb(rr, gg, bb) ((xrgb_t) { .r = ((uint8_t)(rr)), .g = ((uint8_t)(gg)), .b = ((uint8_t)(bb)) }) +#define xrgb_r(c) ((uint8_t)(c.r)) +#define xrgb_g(c) ((uint8_t)(c.g)) +#define xrgb_b(c) ((uint8_t)(c.b)) +#define xrgb_rgb24(c) ((((rgb24_t)c.r) << 16) | (((rgb24_t)c.g) << 8) | (((rgb24_t)c.b))) +#define xrgb_rgb15(c) (((((rgb15_t)c.r) & 0xF8) << 7) | ((((rgb15_t)c.g) & 0xF8) << 2) | ((((rgb15_t)c.b) & 0xF8) >> 3)) +#define xrgb_rgb12(c) (((((rgb12_t)c.r) & 0xF0) << 4) | ((((rgb12_t)c.g) & 0xF0)) | ((((rgb12_t)c.b) & 0xF0) >> 4)) +#define xrgb_rgb6(c) (((((rgb6_t)c.r) & 0xC0) >> 2) | ((((rgb6_t)c.g) & 0xC0) >> 4) | ((((rgb6_t)c.b) & 0xC0) >> 6)) + + +#define rgb24(r,g,b) ((rgb24_t) (((((rgb24_t)r) & 0xFF) << 16) | ((((rgb24_t)g) & 0xFF) << 8) | (((rgb24_t)b) & 0xFF))) +#define rgb24_r(c) ((((rgb24_t) (c)) >> 16) & 0xFF) +#define rgb24_g(c) ((((rgb24_t) (c)) >> 8) & 0xFF) +#define rgb24_b(c) ((((rgb24_t) (c)) >> 0) & 0xFF) +#define rgb24_xrgb(c) xrgb(rgb24_r(c), rgb24_g(c), rgb24_b(c)) + + +#define rgb15(r,g,b) ((rgb16_t) (((r & 0x1F) << 10) | ((g & 0x1F) << 5) | (b & 0x1F))) +#define rgb15_r(c) ((((rgb15_t) (c)) & 0x7C00) >> 7) +#define rgb15_g(c) ((((rgb15_t) (c)) & 0x3E0) >> 2) +#define rgb15_b(c) ((((rgb15_t) (c)) & 0x1F) << 3) +#define rgb15_xrgb(c) xrgb(rgb15_r(c), rgb15_g(c), rgb15_b(c)) +#define rgb15_rgb24(c) rgb24(rgb15_r(c), rgb15_g(c), rgb15_b(c)) + + +#define rgb12(r,g,b) ((rgb12_t) (((r & 0xF) << 8) | ((g & 0xF) << 4) | (b & 0xF))) +#define rgb12_r(c) ((((rgb12_t) (c)) & 0xF00) >> 4) +#define rgb12_g(c) (((rgb12_t) (c)) & 0xF0) +#define rgb12_b(c) (((r(rgb12_t) (c)gb) & 0x0F) << 4) +#define rgb12_xrgb(c) xrgb(rgb12_r(c), rgb12_g(c), rgb12_b(c)) +#define rgb12_rgb24(c) rgb24(rgb12_r(c), rgb12_g(c), rgb12_b(c)) + + +#define rgb6(r,g,b) ((rgb6_t) (((r & 3) << 4) | ((g & 3) << 2) | (b & 3))) +#define rgb6_r(c) ((((rgb6_t) (c)) & 0x30) << 2) +#define rgb6_g(c) ((((rgb6_t) (c)) & 0xC) << 4) +#define rgb6_b(c) ((((rgb6_t) (c)) & 0x3) << 6) +#define rgb6_xrgb(c) xrgb(rgb6_r(c), rgb6_g(c), rgb6_b(c)) +#define rgb6_rgb24(c) rgb24(rgb6_r(c), rgb6_g(c), rgb6_b(c)) + +#define add_xrgb(x, y) ((xrgb_t) { (((y).r > (255 - (x).r)) ? 255 : ((x).r + (y).r)), (((y).g > (255 - (x).g)) ? 255 : ((x).g + (y).g)), (((y).b > 255 - (x).b) ? 255 : ((x).b + (y).b)) }) diff --git a/projects/alive-color-whell/lib/debounce.h b/projects/alive-color-whell/lib/debounce.h new file mode 100644 index 0000000..eaf233c --- /dev/null +++ b/projects/alive-color-whell/lib/debounce.h @@ -0,0 +1,104 @@ +#pragma once + +/** + An implementation of button debouncer. + + First, the system must be initialized - even before including: + + #define DEBO_CHANNELS 2 + #define DEBO_TICKS 5 + + #inclue "lib/debounce.h" + + A pin is registered like this: + + #define BTN1 B,0 + #define BTN2 B,1 + + debo_add(BTN0); // The function returns number assigned to the pin (0, 1, ...) + debo_add_rev(BTN1); // active low + debo_register(&PINB, PB2, 0); // direct access - register, pin & invert + + Then periodically call the tick function (perhaps in a timer interrupt): + + debo_tick(); + + To check if input is active, use + + debo_get_pin(0); // state of input registered as #0 + debo_get_pin(1); // state of input registered as #1 +*/ + +#include +#include + +#include "calc.h" +#include "pins.h" + + +// Number of pins to debounce +#ifndef DEBO_CHANNELS +# error "DEBO_CHANNELS not defined!" +#endif + +#ifndef DEBO_TICKS +# warning "DEBO_TICKS not defined, defaulting to 5!" +# define DEBO_TICKS 5 +#endif + + +/* Internal deboucer entry */ +typedef struct { + PORT_P reg; + uint8_t bit; + uint8_t count; +} debo_slot_t; + +/** Debounce data array */ +debo_slot_t debo_slots[DEBO_CHANNELS]; +uint8_t debo_next_slot = 0; + +/** Define a debounced pin (must be IO!) */ + +#define debo_add_rev(io) debo_register(&io2pin(io_pack(io)), io2n(io_pack(io)), 1) +#define debo_add(io) debo_register(&io2pin(io_pack(io)), io2n(io_pack(io)), 0) + +uint8_t debo_register(PORT_P reg, uint8_t bit, bool invert) +{ + debo_slots[debo_next_slot] = (debo_slot_t){ + .reg = reg, + .bit = bit | ((invert & 1) << 7) | (get_bit_p(reg, bit) << 6), // bit 7 = invert, bit 6 = state + .count = 0, + }; + + return debo_next_slot++; +} + + +/** Check debounced pins, should be called periodically. */ +void debo_tick() +{ + for (uint8_t i = 0; i < debo_next_slot; i++) { + // current pin value (right 3 bits, xored with inverse bit) + bool value = get_bit_p(debo_slots[i].reg, debo_slots[i].bit & 0x7); + + if (value != get_bit(debo_slots[i].bit, 6)) { + + // different pin state than last recorded state + if (debo_slots[i].count < DEBO_TICKS) { + debo_slots[i].count++; + } else { + // overflown -> latch value + set_bit(debo_slots[i].bit, 6, value); // set state bit + debo_slots[i].count = 0; + } + } else { + debo_slots[i].count = 0; // reset the counter + } + } +} + + +/** Get a value of debounced pin */ +#define debo_get_pin(i) (get_bit(debo_slots[i].bit, 6) ^ get_bit(debo_slots[i].bit, 7)) +//(get_bit(debo_slots[i].bit, 6) ^ get_bit(debo_slots[i].bit, 7)) diff --git a/projects/alive-color-whell/lib/meta.h b/projects/alive-color-whell/lib/meta.h new file mode 100644 index 0000000..ec16799 --- /dev/null +++ b/projects/alive-color-whell/lib/meta.h @@ -0,0 +1,6 @@ +#pragma once + +/** Weird constructs for the compiler */ + +// general macros +#define SECTION(pos) __attribute__((naked, used, section(pos))) diff --git a/projects/alive-color-whell/lib/nsdelay.h b/projects/alive-color-whell/lib/nsdelay.h new file mode 100644 index 0000000..0a1e843 --- /dev/null +++ b/projects/alive-color-whell/lib/nsdelay.h @@ -0,0 +1,18 @@ +#pragma once + +/** + Functions for precise delays (nanoseconds / cycles) +*/ + +#include +#include +#include + +/* Convert nanoseconds to cycle count */ +#define ns2cycles(ns) ( (ns) / (1000000000L / (signed long) F_CPU) ) + +/** Wait c cycles */ +#define delay_c(c) (((c) > 0) ? __builtin_avr_delay_cycles(c) : __builtin_avr_delay_cycles(0)) + +/** Wait n nanoseconds, plus c cycles */ +#define delay_ns_c(ns, c) delay_c(ns2cycles(ns) + (c)) diff --git a/projects/alive-color-whell/lib/pins.h b/projects/alive-color-whell/lib/pins.h new file mode 100644 index 0000000..93960b4 --- /dev/null +++ b/projects/alive-color-whell/lib/pins.h @@ -0,0 +1,107 @@ +#pragma once + +/** + This file provides macros for pin manipulation. + + You can define your application pins like so: + + // Led at PORTB, pin 1 + #define LED B,1 + + // Switch at PORTD, pin 7 + #define SW1 D,7 + + Now you can use macros from this file to wirh with the pins, eg: + + as_output(LED); + as_input(SW1); + pullup_on(SW1); + + toggle_pin(LED); + while (pin_is_low(SW1)); + + - The macros io2XXX() can be used to get literal name of register associated with the pin. + - io2n() provides pin number. + - The XXX_aux() macros are internal and should not be used elsewhere. + - The io_pack() macro is used to pass pin (io) to other macro without expanding it. +*/ + +#include +#include "calc.h" + + +// Get particular register associated with the name X (eg. D -> PORTD) +#define reg_ddr(X) DDR ## X +#define reg_port(X) PORT ## X +#define reg_pin(X) PIN ## X + +#define io2ddr_aux(reg, bit) reg_ddr(reg) +#define io2ddr(io) io2ddr_aux(io) +#define io2port_aux(reg, bit) reg_port(reg) +#define io2port(io) io2port_aux(io) +#define io2pin_aux(reg, bit) reg_pin(reg) +#define io2pin(io) io2pin_aux(io) +#define io2n_aux(reg, bit) bit +#define io2n(io) io2n_aux(io) + +#define io_pack(port, bit) port, bit + + +// pointer to port +typedef volatile uint8_t* PORT_P; +// number of bit in port +typedef uint8_t BIT_N; + + +// === pin manipulation === +#define set_pin_aux(port, bit) sbi(reg_port(port), (bit)) +#define clear_pin_aux(port, bit) cbi(reg_port(port), (bit)) +#define read_pin_aux(port, bit) get_bit(reg_pin(port), (bit)) +#define write_pin_aux(port, bit, value) set_bit(reg_port(port), (bit), (value)) +#define toggle_pin_aux(port, bit) sbi(reg_pin(port), (bit)) + + +#define pin_up(io) set_pin_aux(io) +#define pin_high(io) set_pin_aux(io) + +#define pin_down(io) clear_pin_aux(io) +#define pin_low(io) clear_pin_aux(io) + +#define get_pin(io) read_pin_aux(io) +#define read_pin(io) read_pin_aux(io) + +#define pin_is_low(io) !read_pin_aux(io) +#define pin_is_high(io) read_pin_aux(io) + +#define set_pin(io, value) write_pin_aux(io, (value)) +#define write_pin(io, value) write_pin_aux(io, (value)) +#define toggle_pin(io) toggle_pin_aux(io) + + + +// setting pin direction +#define as_input_aux(port, bit) cbi(reg_ddr(port), (bit)) +#define as_output_aux(port, bit) sbi(reg_ddr(port), (bit)) +#define set_dir_aux(port, bit, dir) write_bit(reg_ddr(port), (bit), (dir)) + + +#define as_input(io) as_input_aux(io) +#define as_input_pu(io) do { as_input_aux(io); pullup_enable_aux(io); } while(0) + +#define as_output(io) as_output_aux(io) +#define set_dir(io, dir) set_dir_aux(io, (dir)) + + +// setting pullup +#define pullup_enable_aux(port, bit) sbi(reg_port(port), (bit)) +#define pullup_disable_aux(port, bit) cbi(reg_port(port), (bit)) +#define set_pullup_aux(port, bit, on) write_bit(reg_port(port), (bit), (on)) + + +#define pullup_enable(io) pullup_enable_aux(io) +#define pullup_on(io) pullup_enable_aux(io) + +#define pullup_disable(io) pullup_disable_aux(io) +#define pullup_off(io) pullup_disable_aux(io) + +#define set_pullup(io, on) set_pullup_aux(io, on) diff --git a/projects/alive-color-whell/lib/ws2812.h b/projects/alive-color-whell/lib/ws2812.h new file mode 100644 index 0000000..815d250 --- /dev/null +++ b/projects/alive-color-whell/lib/ws2812.h @@ -0,0 +1,77 @@ +#pragma once + +/** + Utils for driving a WS2812 (WS2812B) RGB LED strips. + + It's implemented as macros to avoid overhead when passing values, and to + enable driving multiple strips at once. There is over 1us of free time between + the colors, which can be used for some processing or color computation. + + To avoid bloating your code, try to reduce the nuýmber of invocations - + compute color and then send it. +*/ + +#include + +#include "pins.h" +#include "nsdelay.h" +#include "colors.h" + +/* Driver code for WS2812B */ + +// --- timing constraints (NS) --- + +#ifndef WS_T_1H +# define WS_T_1H 700 +#endif + +#ifndef WS_T_1L +# define WS_T_1L 150 +#endif + +#ifndef WS_T_0H +# define WS_T_0H 150 +#endif + +#ifndef WS_T_0L +# define WS_T_0L 700 +#endif + +#ifndef WS_T_LATCH +# define WS_T_LATCH 7000 +#endif + + +/** Wait long enough for the colors to show */ +#define ws_show() do { delay_ns_c(WS_T_LATCH, 0); } while(0) + + +/** Send one byte to the RGB strip */ +#define ws_send_byte(io, bb) do { \ + for (volatile int8_t __wsba_i = 7; __wsba_i >= 0; --__wsba_i) { \ + if ((bb) & (1 << __wsba_i)) { \ + pin_high(io_pack(io)); delay_ns_c(WS_T_1H, -2); \ + pin_low(io_pack(io)); delay_ns_c(WS_T_1L, -10); \ + } else { \ + pin_high(io_pack(io)); delay_ns_c(WS_T_0H, -2); \ + pin_low(io_pack(io)); delay_ns_c(WS_T_0L, -10); \ + } \ + } \ +} while(0) + + +/** Send R,G,B color to the strip */ +#define ws_send_rgb(io, r, g, b) do { \ + ws_send_byte(io_pack(io), g); \ + ws_send_byte(io_pack(io), r); \ + ws_send_byte(io_pack(io), b); \ +} while(0) + +/** Send a RGB struct */ +#define ws_send_xrgb(io, xrgb) ws_send_rgb(io_pack(io), (xrgb).r, (xrgb).g, (xrgb).b) + +/** Send color hex */ +#define ws_send_rgb24(io, rgb) ws_send_rgb(io_pack(io), rgb24_r(rgb), rgb24_g(rgb), rgb24_b(rgb)) +#define ws_send_rgb15(io, rgb) ws_send_rgb(io_pack(io), rgb15_r(rgb), rgb15_g(rgb), rgb15_b(rgb)) +#define ws_send_rgb12(io, rgb) ws_send_rgb(io_pack(io), rgb12_r(rgb), rgb12_g(rgb), rgb12_b(rgb)) +#define ws_send_rgb6(io, rgb) ws_send_rgb(io_pack(io), rgb6_r(rgb), rgb6_g(rgb), rgb6_b(rgb)) diff --git a/projects/alive-color-whell/main.c b/projects/alive-color-whell/main.c new file mode 100644 index 0000000..ac3a90c --- /dev/null +++ b/projects/alive-color-whell/main.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include + +#include "lib/meta.h" +#include "lib/arduino_pins.h" +#include "lib/calc.h" +#include "lib/colors.h" +#include "lib/ws2812.h" + +#define WS1 D10 + +void render(); +void init_karts(); +void move_karts(); + + +void SECTION(".init8") init_io() +{ + // led strip data + as_output(WS1); + + // setup timer 10 ms + TCCR0A = _BV(WGM01); // CTC + TCCR0B = _BV(CS02) | _BV(CS00); // prescaler 512 + OCR0A = 100; // interrupt every 10 ms 156 + sbi(TIMSK0, OCIE0A); + sei(); +} + +/** timer 0 interrupt vector */ +ISR(TIMER0_COMPA_vect) +{ + move_karts(); + render(); +} + + +/** Unsigned int range struct */ +typedef struct { + uint8_t a; + uint8_t b; + int8_t dir; + uint8_t steptime; + uint8_t stepcnt; + xrgb_t color; +} kart_t; + +#define BLACK xrgb(0,0,0) + +#define karts_len 6 +#define screen_len 72 +kart_t karts[karts_len]; +xrgb_t screen[screen_len]; + + +void init_karts() +{ + karts[0] = (kart_t) { .a=0, .b=17, .dir=1, .steptime=2, .stepcnt=0, .color=xrgb(50, 0, 0) }; + karts[1] = (kart_t) { .a=10, .b=19, .dir=1, .steptime=3, .stepcnt=0, .color=xrgb(0, 50, 0) }; + karts[2] = (kart_t) { .a=20, .b=29, .dir=1, .steptime=7, .stepcnt=0, .color=xrgb(0, 0, 50) }; + karts[3] = (kart_t) { .a=40, .b=52, .dir=-1, .steptime=2, .stepcnt=0, .color=xrgb(50, 0, 50) }; + karts[4] = (kart_t) { .a=50, .b=69, .dir=-1, .steptime=11, .stepcnt=0, .color=xrgb(0, 20, 20) }; + karts[5] = (kart_t) { .a=25, .b=37, .dir=-1, .steptime=1, .stepcnt=0, .color=xrgb(60, 30, 0) }; + karts[5] = (kart_t) { .a=17, .b=25, .dir=1, .steptime=5, .stepcnt=0, .color=xrgb(25, 25, 0) }; +} + + +void move_karts() +{ + for (uint8_t j = 0; j < karts_len; j++) { + if (++karts[j].stepcnt >= karts[j].steptime) { + // move yo ass + + if (karts[j].dir > 0) { + inc_wrap(karts[j].a, 0, screen_len - 1); + inc_wrap(karts[j].b, 0, screen_len - 1); + } else { + dec_wrap(karts[j].a, 0, screen_len - 1); + dec_wrap(karts[j].b, 0, screen_len - 1); + } + + karts[j].stepcnt = 0; + } + } +} + + +void render() +{ + // build the screen + + // for each pixel + for (uint8_t i = 0; i < screen_len; i++) { + screen[i] = BLACK; + // for each kart + for (uint8_t j = 0; j < karts_len; j++) { + if (in_range_wrap(i, karts[j].a, karts[j].b)) { + screen[i] = add_xrgb(screen[i], karts[j].color); + } + } + } + + for (uint8_t i = 0; i < screen_len; i++) { + ws_send_xrgb(WS1, screen[i]); + } +} + + +void main() +{ + init_karts(); + while(1) {} // Timer interrupts do the rest +}