diff --git a/devel/.gitignore b/devel/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/devel/lib/README.md b/devel/lib/README.md index 7ac9922..876f46e 100644 --- a/devel/lib/README.md +++ b/devel/lib/README.md @@ -6,3 +6,5 @@ This is my ever-evolving library (not only) for AVR programming. When I'm done with a project, I copy the current library to the project, so it doesn't break when I do further improvements. Each library file contains a large comment block explaining it's function. + +You may need to add "c" files to your makefile for some of the library files. diff --git a/devel/lib/adc.c b/devel/lib/adc.c new file mode 100644 index 0000000..66a4482 --- /dev/null +++ b/devel/lib/adc.c @@ -0,0 +1,46 @@ +#include +#include + +#include "calc.h" +#include "adc.h" + +/** Initialize the ADC */ +void adc_init() +{ + ADCSRA |= _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0); // 128 prescaler -> 125 kHz + ADMUX |= _BV(REFS0); // Voltage reference + sbi(ADCSRA, ADEN); // Enable ADC +} + + +/** Disable AD */ +void adc_disable() +{ + cbi(ADCSRA, ADEN); +} + + +/** Sample analog pin with 8-bit precision */ +uint8_t adc_read_byte(uint8_t channel) +{ + write_low_nibble(ADMUX, channel); // Select channel to sample + sbi(ADMUX, ADLAR); // Align result to left + sbi(ADCSRA, ADSC); // Start conversion + + while(bit_is_high(ADCSRA, ADSC)); // Wait for it... + + return ADCH; // The upper 8 bits of ADC result +} + + +/** Sample analog pin with 10-bit precision */ +uint16_t adc_read_word(uint8_t channel) +{ + write_low_nibble(ADMUX, channel); // Select channel to sample + cbi(ADMUX, ADLAR); // Align result to right + sbi(ADCSRA, ADSC); // Start conversion + + while(get_bit(ADCSRA, ADSC)); // Wait for it... + + return ADCW; // The whole ADC word (10 bits) +} diff --git a/devel/lib/adc.h b/devel/lib/adc.h index 41e855c..64244f5 100644 --- a/devel/lib/adc.h +++ b/devel/lib/adc.h @@ -1,39 +1,19 @@ #pragma once +/* + Utilities for build-in A/D converter +*/ + #include -#include -#include "calc.h" /** Initialize the ADC */ -void adc_init() -{ - ADCSRA |= _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0); // 128 prescaler -> 125 kHz - ADMUX |= _BV(REFS0); // Voltage reference - sbi(ADCSRA, ADEN); // Enable ADC -} +void adc_init(); +/** Disable AD (for power saving?) */ +void adc_disable(); /** Sample analog pin with 8-bit precision */ -uint8_t adc_read_byte(uint8_t channel) -{ - write_low_nibble(ADMUX, channel); // Select channel to sample - sbi(ADMUX, ADLAR); // Align result to left - sbi(ADCSRA, ADSC); // Start conversion - - while(bit_is_high(ADCSRA, ADSC)); // Wait for it... - - return ADCH; // The upper 8 bits of ADC result -} - +uint8_t adc_read_byte(uint8_t channel); /** Sample analog pin with 10-bit precision */ -uint16_t adc_read_word(uint8_t channel) -{ - write_low_nibble(ADMUX, channel); // Select channel to sample - cbi(ADMUX, ADLAR); // Align result to right - sbi(ADCSRA, ADSC); // Start conversion - - while(get_bit(ADCSRA, ADSC)); // Wait for it... - - return ADCW; // The whole ADC word (10 bits) -} +uint16_t adc_read_word(uint8_t channel); diff --git a/devel/lib/colors.h b/devel/lib/colors.h index 9b45110..b6544de 100644 --- a/devel/lib/colors.h +++ b/devel/lib/colors.h @@ -11,6 +11,7 @@ XX_r (_g, _b) ... extract component from the color, and convert it to 0..255 */ + typedef struct { uint8_t r; uint8_t g; @@ -79,5 +80,5 @@ typedef uint8_t rgb6_t; #define rgb6_rgb24(c) rgb24(rgb6_r(c), rgb6_g(c), rgb6_b(c)) #define rgb6_rgb24c(c) rgb24c(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/devel/lib/debounce.c b/devel/lib/debounce.c new file mode 100644 index 0000000..409e80f --- /dev/null +++ b/devel/lib/debounce.c @@ -0,0 +1,45 @@ +#include +#include + +#include "debounce.h" +#include "calc.h" +#include "pins.h" +#include "debo_config.h" + +/** Debounce data array */ +uint8_t debo_next_slot = 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 + } + } +} diff --git a/devel/lib/debounce.h b/devel/lib/debounce.h index 00c0587..62af351 100644 --- a/devel/lib/debounce.h +++ b/devel/lib/debounce.h @@ -3,30 +3,34 @@ /** An implementation of button debouncer. - First, the system must be initialized - even before including: + ---- - #define DEBO_CHANNELS 2 - #define DEBO_TICKS 5 + You must provide a config file debo_config.h (next to your main.c) - #inclue "lib/debounce.h" + Example: + #pragma once + #define DEBO_CHANNELS 2 + #define DDEBO_TICKS 5 + + ---- A pin is registered like this: - #define BTN1 B,0 - #define BTN2 B,1 + #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 + 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(); + 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 + debo_get_pin(0); // state of input #0 (registered first) + debo_get_pin(1); // state of input #1 (registered second) */ #include @@ -34,18 +38,7 @@ #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 - +#include "debo_config.h" /* Internal deboucer entry */ typedef struct { @@ -54,50 +47,17 @@ typedef struct { uint8_t count; // number of ticks this was in the new state } 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!) */ +/** Add a pin for debouncing */ #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++; -} - +/** Add a pin for debouncing (low level function) */ +uint8_t debo_register(PORT_P pin_reg_pointer, uint8_t bit, bool invert); /** 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 - } - } -} - +void debo_tick(); /** 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)) diff --git a/devel/lib/hsl.c b/devel/lib/hsl.c new file mode 100644 index 0000000..8e0bc8b --- /dev/null +++ b/devel/lib/hsl.c @@ -0,0 +1,76 @@ +#include +#include +#include "colors.h" +#include "hsl.h" + +// based on: https://github.com/lewisd32/avr-hsl2rgb +xrgb_t hsl2xrgb(const hsl_t cc) +{ + // 0 .. 256*3 + const uint16_t hh = (uint16_t) cc.h * 3; + const uint8_t hue_mod = hh % 256; + + uint8_t r_temp, g_temp, b_temp; + if (hh < 256) { + r_temp = hue_mod ^ 255; + g_temp = hue_mod; + b_temp = 0; + } else if (hh < 512) { + r_temp = 0; + g_temp = hue_mod ^ 255; + b_temp = hue_mod; + } else if (hh < 768) { + r_temp = hue_mod; + g_temp = 0; + b_temp = hue_mod ^ 255; + } else { + r_temp = 0; + g_temp = 0; + b_temp = 0; + } + + const uint8_t inverse_sat = (cc.s ^ 255); + + xrgb_t rgb; + + uint8_t t8; + uint16_t t16; + +#ifdef HSL_LINEAR + const uint8_t bri = FADE_128[cc.l>>1]; +#else + const uint8_t bri = cc.l; +#endif + + t8 = r_temp; + t16 = t8 * cc.s + t8; + t16 = t16 + t8; + t8 = t16 >> 8; + t8 = t8 + inverse_sat; + t16 = t8 * bri; + t16 = t16 + t8; + t8 = t16 >> 8; + rgb.r = t8; + + t8 = g_temp; + t16 = t8 * cc.s; + t16 = t16 + t8; + t8 = t16 >> 8; + t8 = t8 + inverse_sat; + t16 = t8 * bri; + t16 = t16 + t8; + t8 = t16 >> 8; + rgb.g = t8; + + t8 = b_temp; + t16 = t8 * cc.s; + t16 = t16 + t8; + t8 = t16 >> 8; + t8 = t8 + inverse_sat; + t16 = t8 * bri; + t16 = t16 + t8; + t8 = t16 >> 8; + rgb.b = t8; + + return rgb; +} diff --git a/devel/lib/hsl.h b/devel/lib/hsl.h new file mode 100644 index 0000000..b2bd5a1 --- /dev/null +++ b/devel/lib/hsl.h @@ -0,0 +1,22 @@ +#pragma once + +/* + HSL support (addition to colors.h) +*/ + +#include "colors.h" + +// Define HSL_LINEAR to get more linear brightness in hsl->rgb conversion +#ifdef HSL_LINEAR +# include "linear_fade.h" +#endif + +// HSL data structure +typedef struct { + uint8_t h; + uint8_t s; + uint8_t l; +} hsl_t; + +/* Convert HSL to XRGB */ +xrgb_t hsl2xrgb(const hsl_t color); diff --git a/devel/lib/lcd.c b/devel/lib/lcd.c new file mode 100644 index 0000000..7138562 --- /dev/null +++ b/devel/lib/lcd.c @@ -0,0 +1,284 @@ +#include +#include +#include +#include +#include + +#include "calc.h" +#include "pins.h" +#include "nsdelay.h" +#include "lcd.h" +#include "lcd_config.h" + +// Start address of rows +const uint8_t LCD_ROW_ADDR[] = {0x00, 0x40, 0x14, 0x54}; + +// Internal prototypes +void _lcd_mode_r(); +void _lcd_mode_w(); +void _lcd_clk(); +void _lcd_wait_bf(); +void _lcd_write_byte(uint8_t bb); +uint8_t _lcd_read_byte(); + + +// Write utilities +#define _lcd_write_low(bb) _lcd_write_nibble((bb) & 0x0F) +#define _lcd_write_high(bb) _lcd_write_nibble(((bb) & 0xF0) >> 4) +#define _lcd_write_nibble(nib) do { \ + write_pin(LCD_D7, get_bit((nib), 3)); \ + write_pin(LCD_D6, get_bit((nib), 2)); \ + write_pin(LCD_D5, get_bit((nib), 1)); \ + write_pin(LCD_D4, get_bit((nib), 0)); \ +} while(0) + + +// 0 W, 1 R +bool _lcd_mode; + + +/** Initialize the display */ +void lcd_init() +{ + // configure pins as output + as_output(LCD_E); + as_output(LCD_RW); + as_output(LCD_RS); + _lcd_mode = 1; // force data pins to output + _lcd_mode_w(); + + // Magic sequence to invoke Cthulhu (or enter 4-bit mode) + _delay_ms(16); + _lcd_write_nibble(0b0011); + _lcd_clk(); + _delay_ms(5); + _lcd_clk(); + _delay_ms(5); + _lcd_clk(); + _delay_ms(5); + _lcd_write_nibble(0b0010); + _lcd_clk(); + _delay_us(100); + + // Configure the display + lcd_write_command(LCD_IFACE_4BIT_2LINE); + lcd_write_command(LCD_DISABLE); + lcd_write_command(LCD_CLEAR); + lcd_write_command(LCD_MODE_INC); + + // mark as enabled + lcd_enable(); +} + + +/** Send a pulse on the ENABLE line */ +void _lcd_clk() +{ + pin_up(LCD_E); + delay_ns(420); + pin_down(LCD_E); +} + + +/** Enter READ mode */ +void _lcd_mode_r() +{ + if (_lcd_mode == 1) return; // already in R mode + + pin_up(LCD_RW); + + as_input_pu(LCD_D7); + as_input_pu(LCD_D6); + as_input_pu(LCD_D5); + as_input_pu(LCD_D4); + + _lcd_mode = 1; +} + + +/** Enter WRITE mode */ +void _lcd_mode_w() +{ + if (_lcd_mode == 0) return; // already in W mode + + pin_down(LCD_RW); + + as_output(LCD_D7); + as_output(LCD_D6); + as_output(LCD_D5); + as_output(LCD_D4); + + _lcd_mode = 0; +} + + +/** Read a byte */ +uint8_t _lcd_read_byte() +{ + _lcd_mode_r(); + + uint8_t res = 0; + + _lcd_clk(); + res = (read_pin(LCD_D7) << 7) | (read_pin(LCD_D6) << 6) | (read_pin(LCD_D5) << 5) | (read_pin(LCD_D4) << 4); + + _lcd_clk(); + res |= (read_pin(LCD_D7) << 3) | (read_pin(LCD_D6) << 2) | (read_pin(LCD_D5) << 1) | (read_pin(LCD_D4) << 0); + + return res; +} + + +/** Write an instruction byte */ +void lcd_write_command(uint8_t bb) +{ + _lcd_wait_bf(); + pin_down(LCD_RS); // select instruction register + _lcd_write_byte(bb); // send instruction byte +} + + +/** Write a data byte */ +void lcd_write_data(uint8_t bb) +{ + _lcd_wait_bf(); + pin_up(LCD_RS); // select data register + _lcd_write_byte(bb); // send data byte +} + + +/** Read BF & Address */ +uint8_t lcd_read_bf_addr() +{ + pin_down(LCD_RS); + return _lcd_read_byte(); +} + + +/** Read CGRAM or DDRAM */ +uint8_t lcd_read_ram() +{ + pin_up(LCD_RS); + return _lcd_read_byte(); +} + + +/** Write a byte using the 8-bit interface */ +void _lcd_write_byte(uint8_t bb) +{ + _lcd_mode_w(); // enter W mode + + _lcd_write_high(bb); + _lcd_clk(); + + _lcd_write_low(bb); + _lcd_clk(); +} + + + +/** Wait until the device is ready */ +void _lcd_wait_bf() +{ + uint8_t d = 0; + while(d++ < 120 && lcd_read_bf_addr() & _BV(7)) + _delay_us(1); +} + + +/** Send a string to LCD */ +void lcd_str(char* str_p) +{ + while (*str_p) + lcd_char(*str_p++); +} + + +/** Sedn a char to LCD */ +void lcd_char(const char c) +{ + lcd_write_data(c); +} + + +/** Set cursor position */ +void lcd_xy(const uint8_t x, const uint8_t y) +{ + lcd_set_addr(LCD_ROW_ADDR[y] + (x)); +} + + +uint8_t _lcd_old_cursor = CURSOR_NONE; +bool _lcd_enabled = false; + +/** Set LCD cursor. If not enabled, only remember it. */ +void lcd_cursor(uint8_t type) +{ + _lcd_old_cursor = (type & CURSOR_BOTH); + + if (_lcd_enabled) lcd_write_command(LCD_CURSOR_NONE | _lcd_old_cursor); +} + + +/** Display display (preserving cursor) */ +void lcd_disable() +{ + lcd_write_command(LCD_DISABLE); + _lcd_enabled = false; +} + + +/** Enable display (restoring cursor) */ +void lcd_enable() +{ + _lcd_enabled = true; + lcd_cursor(_lcd_old_cursor); +} + + +/** Go home */ +void lcd_home() +{ + lcd_write_command(LCD_HOME); +} + + +/** Clear the screen */ +void lcd_clear() +{ + lcd_write_command(LCD_CLEAR); +} + + +/** Define a glyph */ +void lcd_define_glyph(const uint8_t index, const uint8_t* array) +{ + lcd_set_addr_cgram(index * 8); + for (uint8_t i = 0; i < 8; ++i) { + lcd_write_data(array[i]); + } +} + + +/** Define a glyph */ +void lcd_define_glyph_pgm(const uint8_t index, const uint8_t* array) +{ + lcd_set_addr_cgram(index * 8); + for (uint8_t i = 0; i < 8; ++i) { + lcd_write_data(pgm_read_byte(&array[i])); + } +} + + +/** Set address in CGRAM */ +void lcd_set_addr_cgram(const uint8_t acg) +{ + lcd_write_command(0b01000000 | ((acg) & 0b00111111)); +} + + +/** Set address in DDRAM */ +void lcd_set_addr(const uint8_t add) +{ + lcd_write_command(0b10000000 | ((add) & 0b01111111)); +} diff --git a/devel/lib/lcd.h b/devel/lib/lcd.h index b1a98d9..d2f3f95 100644 --- a/devel/lib/lcd.h +++ b/devel/lib/lcd.h @@ -1,32 +1,29 @@ #pragma once -#include -#include -#include -#include -#include - -#include "calc.h" -#include "pins.h" -#include "nsdelay.h" - /* HD44780 LCD display driver - 4-bit mode - Required macros - pin settings (eg. B,3 or D,0) + LCD pins are configured using a file lcd_config.h, which you + have to add next to your main.c file. + + Content can be something like this: - LCD_PIN_RS - LCD_PIN_RW - LCD_PIN_E - LCD_PIN_D7 - LCD_PIN_D6 - LCD_PIN_D5 - LCD_PIN_D4 + #pragma once + #include "lib/arduino_pins.h" + #define LCD_RS D10 + #define LCD_RW D11 + #define LCD_E D12 + #define LCD_D4 D13 + #define LCD_D5 D14 + #define LCD_D6 D15 + #define LCD_D7 D16 - Define those before including the header file. */ -// Commands for user +#include +#include "lcd_config.h" + +// Commands // Clear screen (reset) #define LCD_CLEAR 0b00000001 @@ -71,323 +68,63 @@ #define LCD_IFACE_8BIT_2LINE 0b00111000 -// Start address of rows -const uint8_t LCD_ROW_ADDR[] = {0x00, 0x40, 0x14, 0x54}; - -// prototypes - -// --- PUBLIC API --- - -/** Init the display */ -void lcd_init(); -/** Write a command */ -void lcd_write_command(const uint8_t bb); -/** Write data byte */ -void lcd_write_data(const uint8_t bb); -/** Read busy flag & address */ -uint8_t lcd_read_bf_addr(); -/** Read byte from ram */ -uint8_t lcd_read_ram(); -/** Show string */ -void lcd_str(char* str); -/** Show string at X, Y */ -#define lcd_str_xy(x, y, str_p) do { lcd_xy((x), (y)); lcd_str((str_p)); } while(0) -/** Show char */ -void lcd_char(const char c); -/** Show char at X, Y */ -#define lcd_char_xy(x, y, c) do { lcd_xy((x), (y)); lcd_char((c)); } while(0) -/** Move cursor to X, Y */ -void lcd_xy(const uint8_t x, const uint8_t y); -/** Set address in CGRAM */ -void lcd_set_addr_cgram(const uint8_t acg); -/** Set address in DDRAM */ -void lcd_set_addr(const uint8_t add); -/** Go home */ -void lcd_home(); -/** Clear the screen */ -void lcd_clear(); - -/** Set cursor */ -#define CURSOR_NONE 0b00 -#define CURSOR_BAR 0b10 -#define CURSOR_BLINK 0b01 -#define CURSOR_BOTH 0b11 -void lcd_cursor(uint8_t type); - -/** Disable / enable, preserving cursor */ -void lcd_disable(); -void lcd_enable(); - -/** Define a custom glyph */ -void lcd_define_glyph(const uint8_t index, const uint8_t* array); - - -// Internals -void _lcd_mode_r(); -void _lcd_mode_w(); -void _lcd_clk(); -void _lcd_wait_bf(); -void _lcd_write_byte(uint8_t bb); -uint8_t _lcd_read_byte(); - - -// Write utilities -#define _lcd_write_low(bb) _lcd_write_nibble((bb) & 0x0F) -#define _lcd_write_high(bb) _lcd_write_nibble(((bb) & 0xF0) >> 4) -#define _lcd_write_nibble(nib) do { \ - write_pin(LCD_PIN_D7, get_bit((nib), 3)); \ - write_pin(LCD_PIN_D6, get_bit((nib), 2)); \ - write_pin(LCD_PIN_D5, get_bit((nib), 1)); \ - write_pin(LCD_PIN_D4, get_bit((nib), 0)); \ -} while(0) - - - -// 0 W, 1 R -bool _lcd_mode; - /** Initialize the display */ -void lcd_init() -{ - // configure pins as output - as_output(LCD_PIN_E); - as_output(LCD_PIN_RW); - as_output(LCD_PIN_RS); - _lcd_mode = 1; // force data pins to output - _lcd_mode_w(); - - // Magic sequence to enter 4-bit mode - _delay_ms(16); - _lcd_write_nibble(0b0011); - _lcd_clk(); - _delay_ms(5); - _lcd_clk(); - _delay_ms(5); - _lcd_clk(); - _delay_ms(5); - _lcd_write_nibble(0b0010); - _lcd_clk(); - _delay_us(100); - - // Configure the display - lcd_write_command(LCD_IFACE_4BIT_2LINE); - lcd_write_command(LCD_DISABLE); - lcd_write_command(LCD_CLEAR); - lcd_write_command(LCD_MODE_INC); - - lcd_enable(); -} - - - -/** Send a pulse on the ENABLE line */ -void _lcd_clk() -{ - pin_up(LCD_PIN_E); - delay_ns(420); - pin_down(LCD_PIN_E); -} - - -/** Enter READ mode */ -void _lcd_mode_r() -{ - if (_lcd_mode == 1) return; // already in R mode - - pin_up(LCD_PIN_RW); - - as_input_pu(LCD_PIN_D7); - as_input_pu(LCD_PIN_D6); - as_input_pu(LCD_PIN_D5); - as_input_pu(LCD_PIN_D4); - - _lcd_mode = 1; -} - - -/** Enter WRITE mode */ -void _lcd_mode_w() -{ - if (_lcd_mode == 0) return; // already in W mode - - pin_down(LCD_PIN_RW); - - as_output(LCD_PIN_D7); - as_output(LCD_PIN_D6); - as_output(LCD_PIN_D5); - as_output(LCD_PIN_D4); - - _lcd_mode = 0; -} - - -/** Read a byte */ -uint8_t _lcd_read_byte() -{ - _lcd_mode_r(); - - uint8_t res = 0; - - _lcd_clk(); - res = (read_pin(LCD_PIN_D7) << 7) | (read_pin(LCD_PIN_D6) << 6) | (read_pin(LCD_PIN_D5) << 5) | (read_pin(LCD_PIN_D4) << 4); - - _lcd_clk(); - res |= (read_pin(LCD_PIN_D7) << 3) | (read_pin(LCD_PIN_D6) << 2) | (read_pin(LCD_PIN_D5) << 1) | (read_pin(LCD_PIN_D4) << 0); - - return res; -} - +void lcd_init(); /** Write an instruction byte */ -void lcd_write_command(uint8_t bb) -{ - _lcd_wait_bf(); - pin_down(LCD_PIN_RS); // select instruction register - _lcd_write_byte(bb); // send instruction byte -} - +void lcd_write_command(uint8_t bb); /** Write a data byte */ -void lcd_write_data(uint8_t bb) -{ - _lcd_wait_bf(); - pin_up(LCD_PIN_RS); // select data register - _lcd_write_byte(bb); // send data byte -} - +void lcd_write_data(uint8_t bb); /** Read BF & Address */ -uint8_t lcd_read_bf_addr() -{ - pin_down(LCD_PIN_RS); - return _lcd_read_byte(); -} - +uint8_t lcd_read_bf_addr(); /** Read CGRAM or DDRAM */ -uint8_t lcd_read_ram() -{ - pin_up(LCD_PIN_RS); - return _lcd_read_byte(); -} - - -/** Write a byte using the 8-bit interface */ -void _lcd_write_byte(uint8_t bb) -{ - _lcd_mode_w(); // enter W mode - - _lcd_write_high(bb); - _lcd_clk(); - - _lcd_write_low(bb); - _lcd_clk(); -} - - - -/** Wait until the device is ready */ -void _lcd_wait_bf() -{ - uint8_t d = 0; - while(d++ < 120 && lcd_read_bf_addr() & _BV(7)) - _delay_us(1); -} - +uint8_t lcd_read_ram(); /** Send a string to LCD */ -void lcd_str(char* str_p) -{ - while (*str_p) - lcd_char(*str_p++); -} - +void lcd_str(char* str_p); /** Sedn a char to LCD */ -void lcd_char(const char c) -{ - lcd_write_data(c); -} - +void lcd_char(const char c); -/** Set cursor position */ -void lcd_xy(const uint8_t x, const uint8_t y) -{ - lcd_set_addr(LCD_ROW_ADDR[y] + (x)); -} +/** Show string at X, Y */ +#define lcd_str_xy(x, y, str_p) do { lcd_xy((x), (y)); lcd_str((str_p)); } while(0) +/** Show char at X, Y */ +#define lcd_char_xy(x, y, c) do { lcd_xy((x), (y)); lcd_char((c)); } while(0) -uint8_t _lcd_old_cursor = CURSOR_NONE; -bool _lcd_enabled = false; +/** Set cursor position */ +void lcd_xy(const uint8_t x, const uint8_t y); /** Set LCD cursor. If not enabled, only remember it. */ -void lcd_cursor(uint8_t type) -{ - _lcd_old_cursor = (type & CURSOR_BOTH); - - if (_lcd_enabled) lcd_write_command(LCD_CURSOR_NONE | _lcd_old_cursor); -} - +#define CURSOR_NONE 0b00 +#define CURSOR_BAR 0b10 +#define CURSOR_BLINK 0b01 +#define CURSOR_BOTH 0b11 +void lcd_cursor(uint8_t type); /** Display display (preserving cursor) */ -void lcd_disable() -{ - lcd_write_command(LCD_DISABLE); - _lcd_enabled = false; -} - +void lcd_disable(); /** Enable display (restoring cursor) */ -void lcd_enable() -{ - _lcd_enabled = true; - lcd_cursor(_lcd_old_cursor); -} - +void lcd_enable(); /** Go home */ -void lcd_home() -{ - lcd_write_command(LCD_HOME); -} - +void lcd_home(); /** Clear the screen */ -void lcd_clear() -{ - lcd_write_command(LCD_CLEAR); -} - +void lcd_clear(); /** Define a glyph */ -void lcd_define_glyph(const uint8_t index, const uint8_t* array) -{ - lcd_set_addr_cgram(index * 8); - for (uint8_t i = 0; i < 8; ++i) { - lcd_write_data(array[i]); - } -} - +void lcd_define_glyph(const uint8_t index, const uint8_t* array); /** Define a glyph */ -void lcd_define_glyph_pgm(const uint8_t index, const uint8_t* array) -{ - lcd_set_addr_cgram(index * 8); - for (uint8_t i = 0; i < 8; ++i) { - lcd_write_data(pgm_read_byte(&array[i])); - } -} - +void lcd_define_glyph_pgm(const uint8_t index, const uint8_t* array); /** Set address in CGRAM */ -void lcd_set_addr_cgram(const uint8_t acg) -{ - lcd_write_command(0b01000000 | ((acg) & 0b00111111)); -} - +void lcd_set_addr_cgram(const uint8_t acg); /** Set address in DDRAM */ -void lcd_set_addr(const uint8_t add) -{ - lcd_write_command(0b10000000 | ((add) & 0b01111111)); -} +void lcd_set_addr(const uint8_t add); diff --git a/devel/lib/linear_fade.h b/devel/lib/linear_fade.h new file mode 100644 index 0000000..6f1a3f8 --- /dev/null +++ b/devel/lib/linear_fade.h @@ -0,0 +1,13 @@ +#pragma once +#include + +const uint8_t FADE_128[] = { + 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, + 5, 5, 6, 6, 6, 7, 7, 8, 8, 8, 9, 10, 10, 10, 11, 12, 13, 14, + 14, 15, 16, 17, 18, 20, 21, 22, 24, 26, 27, 28, 30, 31, 32, 34, 35, 36, + 38, 39, 40, 41, 42, 44, 45, 46, 48, 49, 50, 52, 54, 56, 58, 59, 61, 63, + 65, 67, 68, 69, 71, 72, 74, 76, 78, 80, 82, 85, 88, 90, 92, 95, 98, 100, + 103, 106, 109, 112, 116, 119, 122, 125, 129, 134, 138, 142, 147, 151, + 153, 156, 160, 163, 165, 170, 175, 180, 185, 190, 195, 200, 207, 214, 218, + 221, 225, 228, 232, 234, 241, 248, 254, 255 +}; \ No newline at end of file diff --git a/devel/lib/yeolde.h b/devel/lib/yeolde.h deleted file mode 100644 index cbdb863..0000000 --- a/devel/lib/yeolde.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -/** - Ye Olde Control Structures -*/ - -#include "loops.h" - -#define whilst(what) while((what)) -#define when(what) if((what)) -#define otherwise else -#define commence { -#define then { -#define cease } -#define choose(what) switch((what)) -#define option case -#define shatter break -#define replay continue -#define equals == -#define is == -#define be = -#define over > -#define above > -#define under < -#define below < -#define let /**/ -#define raise(what) (what)++ - -#define number int - -#warning "This is a joke. Do not use YeOlde.h in serious code!" diff --git a/devel/rgb_hsl/Makefile b/devel/rgb_hsl/Makefile deleted file mode 100644 index e08743e..0000000 --- a/devel/rgb_hsl/Makefile +++ /dev/null @@ -1,166 +0,0 @@ - -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 = 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/devel/rgb_hsl/lib b/devel/rgb_hsl/lib deleted file mode 120000 index 62f3b3d..0000000 --- a/devel/rgb_hsl/lib +++ /dev/null @@ -1 +0,0 @@ -/home/ondra/devel/avr/avr-projects/devel/lib \ No newline at end of file diff --git a/devel/rgb_hsl/main.c b/devel/rgb_hsl/main.c deleted file mode 100644 index db88d02..0000000 --- a/devel/rgb_hsl/main.c +++ /dev/null @@ -1,139 +0,0 @@ -#include -#include -#include -#include -#include - -#include "lib/meta.h" -#include "lib/arduino_pins.h" -#include "lib/calc.h" -#include "lib/colors.h" -#include "lib/adc.h" - -#define WS_T_1H 800 -#define WS_T_1L 400 -#define WS_T_0H 120 -#define WS_T_0L 900 - -#include "lib/ws_rgb.h" - -#define WS1 D2 - - -typedef struct { - uint8_t h; - uint8_t s; - uint8_t l; -} hsl_t; - - -// based on: https://github.com/lewisd32/avr-hsl2rgb -xrgb_t hsl2rgb(const hsl_t cc) -{ - // 0 .. 256*3 - const uint16_t hh = (uint16_t) cc.h * 3; - const uint8_t hue_mod = hh % 256; - - uint8_t r_temp, g_temp, b_temp; - if (hh < 256) { - r_temp = hue_mod ^ 255; - g_temp = hue_mod; - b_temp = 0; - } else if (hh < 512) { - r_temp = 0; - g_temp = hue_mod ^ 255; - b_temp = hue_mod; - } else if (hh < 768) { - r_temp = hue_mod; - g_temp = 0; - b_temp = hue_mod ^ 255; - } else { - r_temp = 0; - g_temp = 0; - b_temp = 0; - } - - const uint8_t inverse_sat = (cc.s ^ 255); - - xrgb_t rgb; - - uint8_t t8; - uint16_t t16; - - t8 = r_temp; - t16 = t8 * cc.s + t8; - t16 = t16 + t8; - t8 = t16 >> 8; - t8 = t8 + inverse_sat; - t16 = t8 * cc.l; - t16 = t16 + t8; - t8 = t16 >> 8; - rgb.r = t8; - - t8 = g_temp; - t16 = t8 * cc.s; - t16 = t16 + t8; - t8 = t16 >> 8; - t8 = t8 + inverse_sat; - t16 = t8 * cc.l; - t16 = t16 + t8; - t8 = t16 >> 8; - rgb.g = t8; - - t8 = b_temp; - t16 = t8 * cc.s; - t16 = t16 + t8; - t8 = t16 >> 8; - t8 = t8 + inverse_sat; - t16 = t8 * cc.l; - t16 = t16 + t8; - t8 = t16 >> 8; - rgb.b = t8; - - return rgb; -} - - -void SECTION(".init8") init() -{ - adc_init(); - srand(adc_read_word(0)); - - as_output(WS1); -} - - -void main() -{ - #define SIZE 7 - hsl_t board[SIZE]; - xrgb_t screen[SIZE]; - - for (uint8_t i = 0; i < SIZE; i++) { - board[i] = (hsl_t) {.h=0, .s=255, .l=0}; - screen[i] = (xrgb_t) {.r=0, .g=0, .b=0}; - } - - while(1) { - for(uint8_t i = 0; i < SIZE; i++) { - if (board[i].l > 0) { - board[i].l--; - } - - screen[i] = hsl2rgb(board[i]); - } - - if (rand() % 200 == 0) { - uint8_t i = rand() % SIZE; - - if (board[i].l == 0) { - board[i].h = rand() % 256; - board[i].s = 200 + rand() % 56; - board[i].l = 255; - } - } - - ws_send_xrgb_array(WS1, screen, SIZE); - _delay_ms(10); - } -} diff --git a/devel/rgbs_test/Makefile b/devel/rgbs_test/Makefile deleted file mode 100644 index e08743e..0000000 --- a/devel/rgbs_test/Makefile +++ /dev/null @@ -1,166 +0,0 @@ - -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 = 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/devel/rgbs_test/lib b/devel/rgbs_test/lib deleted file mode 120000 index 62f3b3d..0000000 --- a/devel/rgbs_test/lib +++ /dev/null @@ -1 +0,0 @@ -/home/ondra/devel/avr/avr-projects/devel/lib \ No newline at end of file diff --git a/devel/rgbs_test/main.c b/devel/rgbs_test/main.c deleted file mode 100644 index f2fd5be..0000000 --- a/devel/rgbs_test/main.c +++ /dev/null @@ -1,86 +0,0 @@ -#include -#include -#include -// #include -#include -#include - -#include "lib/meta.h" -#include "lib/arduino_pins.h" -#include "lib/calc.h" -#include "lib/colors.h" -#include "lib/adc.h" - -#define WS_T_1H 800 -#define WS_T_1L 400 -#define WS_T_0H 120 -#define WS_T_0L 900 - -#include "lib/ws_rgb.h" - -#define WS1 D2 - - -void SECTION(".init8") init() -{ - as_output(WS1); -} - -void main() -{ - const uint8_t anim_step = 50; - const uint8_t anim_max = 250; - const uint8_t pixel_count = 7; - - xrgb_t color = xrgb(anim_max, 0, 0); - uint8_t step = 0; - - xrgb_t color2 = xrgb(anim_max, 0, 0); - uint8_t step2 = 0; - - while (1) { - - color = color2; - step = step2; - - for (uint8_t i = 0; i < pixel_count; i++) { - ws_send_xrgb(WS1, color); - - if (i == 1) { - color2 = color; - step2 = step; - } - - switch (step) { - case 0: - color.g += anim_step; - if (color.g >= anim_max) step++; - break; - case 1: - color.r -= anim_step; - if (color.r == 0) step++; - break; - case 2: - color.b += anim_step; - if (color.b >= anim_max) step++; - break; - case 3: - color.g -= anim_step; - if (color.g == 0) step++; - break; - case 4: - color.r += anim_step; - if (color.r >= anim_max) step++; - break; - default: - color.b -= anim_step; - if (color.b == 0) step = 0; - break; - } - } - - ws_show(); - - _delay_ms(100); - } -} diff --git a/devel/snake/Makefile b/devel/snake/Makefile deleted file mode 100644 index 75b611d..0000000 --- a/devel/snake/Makefile +++ /dev/null @@ -1,166 +0,0 @@ - -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 -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/devel/snake/lcd_default.asm b/devel/snake/lcd_default.asm deleted file mode 100644 index c092f27..0000000 --- a/devel/snake/lcd_default.asm +++ /dev/null @@ -1,351 +0,0 @@ -; Zapojeni (Připojen DMC-50399 v 4-bitovem modu): -; +------u------+ -; Vcc -> reset --+ /RST Vcc +-- napajeni +5V -; --+ PD0 PB7 +-- RS (0=instr W, BF+addr R; 1=data W/R) -; --+ PD1 PB6 +-- R/W (1=read,0=write) -; --+ PA1 PB5 +-- E (clock, active falling edge) -; --+ PA0 PB4 +-- -; --+ PD2 PB3 +-- DATA 7 -; --+ PD3 PB2 +-- DATA 6 -; --+ PD4 PB1 +-- DATA 5 -; --+ PD5 PB0 +-- DATA 4 -; GND --+ GND PD6 +-- -; +-------------+ - -;DMC-50399: -; 1 - GND -; 2 - +5V -; 3 - 0V (lcd driver) -; 4 - RS -; 5 - R/W -; 6 - E -; 7 - DATA 0 -; 8 - DATA 1 -;... -;14 - DATA 7 - -.device attiny2313 -;běží na 4MHz, ckdiv8=1 (vypnuto) -;250x16=1ms=1000us - -;LFUSE: 0xE2 -U lfuse:w:0xE2:m -;HFUSE: 0xDF -U hfuse:w:0xDF:m - - -;K O N S T A N T Y + P R E Z D I V K Y P O R T U A P I N U -.equ LCDPORT = PORTB -.equ LCDPIN = PINB -.equ LCDDDR = DDRB - -.equ RS = 7 -.equ RW = 6 -.equ E = 5 - -.equ LCD_CLEAR = 0b00000001 -.equ LCD_HOME = 0b00000010 - -.equ LCD_MODE_INC_NOSHIFT = 0b00000110 -.equ LCD_MODE_INC_SHIFT = 0b00000111 -.equ LCD_MODE_DEC_NOSHIFT = 0b00000100 -.equ LCD_MODE_DEC_SHIFT = 0b00000101 - -.equ LCD_DISPLAY_DISABLED = 0b00001000 -.equ LCD_DISPLAY_NOCURSOR = 0b00001100 -.equ LCD_DISPLAY_CURSOR = 0b00001110 -.equ LCD_DISPLAY_ALTER = 0b00001101 -.equ LCD_DISPLAY_CURSOR_ALTER = 0b00001111 - -.equ LCD_CURSOR_LEFT = 0b00010000 -.equ LCD_CURSOR_RIGHT = 0b00010100 -.equ LCD_SHIFT_LEFT = 0b00011000 -.equ LCD_SHIFT_RIGHT = 0b00011100 - - -;5x7 font, 1-line -.equ LCD_MODE_4BIT_1LINE = 0b00100000 -;.equ LCD_MODE_8BIT_1LINE = 0b00110000 -;5x7 font, 2-line -.equ LCD_MODE_4BIT_2LINE = 0b00101000 -;.equ LCD_MODE_8BIT_2LINE = 0b00111000 - -.equ ROW1_ADDR = 0x00 -.equ ROW2_ADDR = 0x40 -.equ ROW3_ADDR = 0x14 -.equ ROW4_ADDR = 0x54 - - - -;aliases -.def ZH = r31 -.def ZL = r30 -.def YH = r29 -.def YL = r28 -.def XH = r27 -.def XL = r26 - -; Z A C A T E K P R O G R A M U -;vektory preruseni -.org 0x0000 ;RESET - rjmp RESET ;skok na start po resetu - -.org 0x0013 - -;nastaveni po resetu -.DB "HD44780 INTERFACE" ;(nazev programu) -RESET: - ldi r16,low(RAMEND) ;nastavi stack pointer - out SPL,r16 - cli ;zakazat vsechna preruseni - -; Nastaveni portu - ;PORTB = LCDPORT - ldi r16,0b11111111 ;smer portu B - out LCDDDR,r16 - ldi r16,0b00000000 ;vypnout B - out LCDPORT,r16 - - sei ;Global Interrupt Enable - -; == display init == - rcall LCD_INIT - - ldi r17,LCD_MODE_INC_NOSHIFT - rcall TX_INSTR - - ldi r17,LCD_DISPLAY_NOCURSOR - rcall TX_INSTR - -; == load user-defined characters to CGRAM == (default, array label named MYCHARS, end-mark=0xFE) - ldi r17,0 - rcall CGRAM_SET_ADDR - - ldi ZH,high(MYCHARS*2) - ldi ZL,low(MYCHARS*2) -CGRAM_loop: - lpm r17,Z+ - cpi r17,0xFE - breq CGRAM_loop_end - rcall TX_DATA - rjmp CGRAM_loop -CGRAM_loop_end: - - -; == pgm body == - -;load text to DDRAM - ldi r17,ROW1_ADDR - rcall DDRAM_SET_ADDR - - ldi ZH,high(MYTEXT1*2) - ldi ZL,low(MYTEXT1*2) -DDRAM_loop: - lpm r17,Z+ - cpi r17,0xFE - breq DDRAM_loop_end - rcall TX_DATA - rjmp DDRAM_loop -DDRAM_loop_end: - -;load text to DDRAM - ldi r17,ROW2_ADDR - rcall DDRAM_SET_ADDR - - ldi ZH,high(MYTEXT2*2) - ldi ZL,low(MYTEXT2*2) -DDRAM2_loop: - lpm r17,Z+ - cpi r17,0xFE - breq DDRAM2_loop_end - rcall TX_DATA - rjmp DDRAM2_loop -DDRAM2_loop_end: - -;direct write to X,Y - example - ldi r16,3 ;Y, zacina 0 a roste smerem dolu - ldi r17,5 ;X, zacina nulou a roste smerem doprava - rcall LCD_CURSOR_XY - ldi r17,"%" - rcall TX_DATA - ldi r17,"%" - rcall TX_DATA - ldi r17,"%" - rcall TX_DATA -;infinite loop -loop: rjmp loop - - -MYTEXT1: -.DB 0,0,0," POKUSNY TEXT ",0,0,0,0xFE -MYTEXT2: -.DB "Opravdu pekny text!",0xFE - - - -; == USER-DEFINED CHARS == -MYCHARS: -; 5x8, first 3 bits are not used -;end of mychars -.DB 0xe,0x1f,0x15,0x1f,0x1f,0x1f,0x15 ;smajlik - -;konec -.DB 0xFE - - - - - -;r16=Y -;r17=X -LCD_CURSOR_XY: - cpi r16,0 - brne test1 -fail: ldi r16,ROW1_ADDR - rjmp addrdone -test1: - cpi r16,1 - brne test2 - ldi r16,ROW2_ADDR - rjmp addrdone -test2: - cpi r16,2 - brne test3 - ldi r16,ROW3_ADDR - rjmp addrdone -test3: - cpi r16,3 - brne fail - ldi r16,ROW4_ADDR -addrdone: - add r17,r16 - rcall DDRAM_SET_ADDR - ret - -;r16=počet ms (cca) -delay: - push r17 ;2 - push r18 ;2 -d1: - ldi r17,250 ;1 -d2: - ldi r18,14 ;1 -d3: - dec r18 ;1 - nop - brne d3 ;2 (1 - dec r17 ; +1) - brne d2 ;2 (1 - dec r16 ; +1) - brne d1 ;2 (1) - - pop r18 ;2 - pop r17 ;2 - ret - - -LCD_INIT: - ldi r16,16 - rcall delay - ldi r16,0b00000010 ;4bit - out PORTB,r16 - rcall LCD_CLK - ldi r16,5 - rcall delay - ldi r17,LCD_MODE_4BIT_2LINE ;set 4-bit mode - rcall TX_INSTR - ret - -;r17 -TX_INSTR: - swap r17 ;send high nibble - mov r16,r17 - andi r16,0b00001111 - out LCDPORT,r16 - rcall LCD_CLK - - swap r17 ;send low nibble - mov r16,r17 - andi r16,0b00001111 - out LCDPORT,r16 - rcall LCD_CLK - - ret - -;r17 -TX_DATA: - swap r17 ;send high nibble - mov r16,r17 - andi r16,0b00001111 - sbr r16,(1< -#include -#include -#include -#include -#include -#include - -#include "lib/meta.h" -#include "lib/arduino_pins.h" -#include "lib/calc.h" -#include "lib/adc.h" - -#define LCD_PIN_RS D10 -#define LCD_PIN_RW D11 -#define LCD_PIN_E D12 -#define LCD_PIN_D4 D13 -#define LCD_PIN_D5 D14 -#define LCD_PIN_D6 D15 -#define LCD_PIN_D7 D16 -// D17 = A3 = source of entropy for random. - -#include "lib/lcd.h" - - -// Buttons (to ground) -#define BTN_LEFT D2 -#define BTN_RIGHT D3 -#define BTN_UP D4 -#define BTN_DOWN D5 -#define BTN_SELECT D6 -#define BTN_RESTART D7 - -// Debouncer channels for buttons -// (Must be added in this order to debouncer) -#define D_LEFT 0 -#define D_RIGHT 1 -#define D_UP 2 -#define D_DOWN 3 -#define D_SELECT 4 -#define D_RESTART 5 - -#define DEBO_CHANNELS 6 -#define DEBO_TICKS 1 // in 0.01s - -#include "lib/debounce.h" - -// Board size (!!! rows = 2x number of display lines, max 2*4 = 8 !!!) -#define ROWS 4 -#define COLS 20 - -// Delay between snake steps, in 10 ms -#define STEP_DELAY 24 - -// proto -void update(); -void init_cgram(); -void init_gameboard(); - -void SECTION(".init8") init() -{ - // Randomize RNG - adc_init(); - srand(adc_read_word(3)); - - // Init LCD - lcd_init(); - init_cgram(); // load default glyphs - - // Init game board. - init_gameboard(); - - // gamepad buttons - as_input_pu(BTN_LEFT); - as_input_pu(BTN_RIGHT); - as_input_pu(BTN_UP); - as_input_pu(BTN_DOWN); - as_input_pu(BTN_SELECT); - as_input_pu(BTN_RESTART); - - // add buttons to debouncer - debo_add_rev(BTN_LEFT); - debo_add_rev(BTN_RIGHT); - debo_add_rev(BTN_UP); - debo_add_rev(BTN_DOWN); - debo_add_rev(BTN_SELECT); - debo_add_rev(BTN_RESTART); - - // setup timer - TCCR0A = _BV(WGM01); // CTC - TCCR0B = _BV(CS02) | _BV(CS00); // prescaler 1024 - OCR0A = 156; // interrupt every 10 ms - sbi(TIMSK0, OCIE0A); - sei(); -} - - -/** timer 0 interrupt vector */ -ISR(TIMER0_COMPA_vect) -{ - debo_tick(); // poll debouncer - update(); // update and display -} - - - -// sub-glyphs -#define _HEAD_ 15, 21, 21, 30 -#define _BODY_ 15, 31, 31, 30 -#define _FOOD_ 10, 21, 17, 14 -//14, 17, 17, 14 -#define _NONE_ 0, 0, 0, 0 - -// complete glyphs for loading into memory - -// Only one food & one head glyph have to be loaded at a time. - -// Body - Body -const uint8_t SYMBOL_BB[] PROGMEM = {_BODY_, _BODY_}; - -// Body - None -const uint8_t SYMBOL_BX[] PROGMEM = {_BODY_, _NONE_}; -const uint8_t SYMBOL_XB[] PROGMEM = {_NONE_, _BODY_}; - -// Head - None -const uint8_t SYMBOL_HX[] PROGMEM = {_HEAD_, _NONE_}; -const uint8_t SYMBOL_XH[] PROGMEM = {_NONE_, _HEAD_}; - -// Body - Head -const uint8_t SYMBOL_BH[] PROGMEM = {_BODY_, _HEAD_}; -const uint8_t SYMBOL_HB[] PROGMEM = {_HEAD_, _BODY_}; - -// Head - Food -const uint8_t SYMBOL_HF[] PROGMEM = {_HEAD_, _FOOD_}; -const uint8_t SYMBOL_FH[] PROGMEM = {_FOOD_, _HEAD_}; - -// Food - None -const uint8_t SYMBOL_FX[] PROGMEM = {_FOOD_, _NONE_}; -const uint8_t SYMBOL_XF[] PROGMEM = {_NONE_, _FOOD_}; - -// Body - Food -const uint8_t SYMBOL_BF[] PROGMEM = {_BODY_, _FOOD_}; -const uint8_t SYMBOL_FB[] PROGMEM = {_FOOD_, _BODY_}; - - -// board block (snake, food...) -typedef enum { - bEMPTY = 0x00, - bHEAD = 0x01, - bFOOD = 0x02, - bBODY_LEFT = 0x80, - bBODY_RIGHT = 0x81, - bBODY_UP = 0x82, - bBODY_DOWN = 0x83, -} block_t; - -// Snake direction -typedef enum { - dLEFT = 0x00, - dRIGHT = 0x01, - dUP = 0x02, - dDOWN = 0x03, -} dir_t; - -// Coordinate on board -typedef struct { - int8_t x; - int8_t y; -} coord_t; - -#define is_body(blk) (((blk) & 0x80) != 0) -#define mk_body_dir(dir) (0x80 + (dir)) - -// compare two coords -#define coord_eq(a, b) (((a).x == (b).x) && ((a).y == (b).y)) - - -bool crashed; -uint8_t snake_len; - -// y, x indexing -block_t board[ROWS][COLS]; - -coord_t head_pos; -coord_t tail_pos; -dir_t head_dir; - -const uint8_t CODE_BB = 0; -const uint8_t CODE_BX = 1; -const uint8_t CODE_XB = 2; -const uint8_t CODE_H = 3; // glyph with head, dynamic -const uint8_t CODE_F = 4; // glyph with food, dynamic -const uint8_t CODE_XX = 32; // space - - -// Set a block in board -#define set_block_xy(x, y, block) do { board[y][x] = (block); } while(0) -#define get_block_xy(x, y) board[y][x] -#define get_block(pos) get_block_xy((pos).x, (pos).y) -#define set_block(pos, block) set_block_xy((pos).x, (pos).y, (block)) - - -void init_cgram() -{ - // those will be always the same - lcd_define_glyph_pgm(CODE_BB, SYMBOL_BB); - lcd_define_glyph_pgm(CODE_BX, SYMBOL_BX); - lcd_define_glyph_pgm(CODE_XB, SYMBOL_XB); - lcd_define_glyph_pgm(5, SYMBOL_XF); -} - - -void place_food() -{ - while(1) { - const uint8_t xx = rand() % COLS; - const uint8_t yy = rand() % ROWS; - - if (get_block_xy(xx, yy) == bEMPTY) { - set_block_xy(xx, yy, bFOOD); - break; - } - } -} - - -void init_gameboard() -{ - //erase the board - for (uint8_t x = 0; x < COLS; x++) { - for (uint8_t y = 0; y < ROWS; y++) { - set_block_xy(x, y, bEMPTY); - } - } - - lcd_clear(); - - tail_pos = (coord_t) {.x = 0, .y = 0}; - - set_block_xy(0, 0, bBODY_RIGHT); - set_block_xy(1, 0, bBODY_RIGHT); - set_block_xy(2, 0, bBODY_RIGHT); - set_block_xy(3, 0, bHEAD); - - head_pos = (coord_t) {.x = 3, .y = 0}; - - snake_len = 4; // includes head - - head_dir = dRIGHT; - crashed = false; - - place_food(); -} - - -uint8_t presc = 0; - -bool restart_held; -void update() -{ - if (debo_get_pin(D_RESTART)) { - - if (!restart_held) { - // restart - init_gameboard(); - presc = 0; - restart_held = true; - } - - } else { - restart_held = false; - } - - if(!crashed) { - - // resolve movement direction - if (debo_get_pin(D_LEFT)) - head_dir = dLEFT; - else if (debo_get_pin(D_RIGHT)) - head_dir = dRIGHT; - else if (debo_get_pin(D_UP)) - head_dir = dUP; - else if (debo_get_pin(D_DOWN)) - head_dir = dDOWN; - - // time's up for a move - if (presc++ == STEP_DELAY) { - presc = 0; - - // move snake - const coord_t oldpos = head_pos; - - switch (head_dir) { - case dLEFT: head_pos.x--; break; - case dRIGHT: head_pos.x++; break; - case dUP: head_pos.y--; break; - case dDOWN: head_pos.y++; break; - } - - bool do_move = false; - bool do_grow = false; - - if (head_pos.x < 0 || head_pos.x >= COLS || head_pos.y < 0 || head_pos.y >= ROWS) { - // ouch, a wall! - crashed = true; - } else { - // check if tile occupied or not - if (coord_eq(head_pos, tail_pos)) { - // head moved in previous tail, that's OK. - do_move = true; - } else { - // moved to other tile than tail - switch (get_block(head_pos)) { - - case bFOOD: - do_grow = true; // fall through - case bEMPTY: - do_move = true; - break; - - default: // includes all BODY_xxx - crashed = true; // snake crashed into some block - } - } - } - - if (do_move) { - // Move tail - if (do_grow) { - // let tail as is - snake_len++; // grow the counter - } else { - // tail dir - dir_t td = get_block(tail_pos) & 0xF; - - // clean tail - set_block(tail_pos, bEMPTY); - - // move tail based on old direction of tail block - switch (td) { - case dLEFT: tail_pos.x--; break; - case dRIGHT: tail_pos.x++; break; - case dUP: tail_pos.y--; break; - case dDOWN: tail_pos.y++; break; - } - } - - // Move head - set_block(head_pos, bHEAD); // place head in new pos - set_block(oldpos, mk_body_dir(head_dir)); // directional body in old head pos - - if (do_grow) { - // food eaten, place new - place_food(); - } - } - } - } // end !crashed - - - // Render the board - for (uint8_t r = 0; r < ROWS / 2; r++) { - lcd_xy(0, r); - for (uint8_t c = 0; c < COLS; c++) { - const block_t t1 = get_block_xy(c, r * 2); - const block_t t2 = get_block_xy(c, (r * 2) + 1); - - uint8_t code = '!'; // ! marks fail - - if ((t1 == bEMPTY) && (t2 == bEMPTY)) { - code = CODE_XX; - if (crashed) code = '*'; - } else if (is_body(t1) && is_body(t2)) - code = CODE_BB; - else if (is_body(t1) && (t2 == bEMPTY)) - code = CODE_BX; - else if (t1 == bEMPTY && is_body(t2)) - code = CODE_XB; - else if ((t1 == bFOOD) || (t2 == bFOOD)) { - // one is food - - code = CODE_F; - - if (t1 == bFOOD) { - if (t2 == bEMPTY) - lcd_define_glyph_pgm(code, SYMBOL_FX); - else if (t2 == bHEAD) - lcd_define_glyph_pgm(code, SYMBOL_FH); - else if (is_body(t2)) - lcd_define_glyph_pgm(code, SYMBOL_FB); - } else { // t2 is food - if (t1 == bEMPTY) - lcd_define_glyph_pgm(code, SYMBOL_XF); - else if (t1 == bHEAD) - lcd_define_glyph_pgm(code, SYMBOL_HF); - else if (is_body(t1)) - lcd_define_glyph_pgm(code, SYMBOL_BF); - } - lcd_xy(c,r); - - } else if ((t1 == bHEAD )|| (t2 == bHEAD)) { - // one is head - - code = CODE_H; - - if (t1 == bHEAD) { - if (t2 == bEMPTY) - lcd_define_glyph_pgm(code, SYMBOL_HX); - else if (is_body(t2)) - lcd_define_glyph_pgm(code, SYMBOL_HB); - } else { // t2 is head - if (t1 == bEMPTY) - lcd_define_glyph_pgm(code, SYMBOL_XH); - else if (is_body(t1)) - lcd_define_glyph_pgm(code, SYMBOL_BH); - } - - lcd_xy(c,r); - } - - lcd_char(code); - } - } -} - -void main() { while(1); } // timer does everything diff --git a/devel/tmp/.gitignore b/devel/tmp/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/devel/tmp/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore