From 32c1be7e547baf849657cda876b7146ed4e7e862 Mon Sep 17 00:00:00 2001 From: MightyPork Date: Sun, 10 May 2015 01:06:41 +0200 Subject: [PATCH] new pins system, updated lib files for it --- lib/arduino_pins.h | 42 ------- lib/calc.h | 28 ++--- lib/debounce.c | 2 +- lib/debounce.h | 10 +- lib/iopins.c | 276 +++++++++++++++++++++++++++++++++++++++++++++ lib/iopins.h | 213 ++++++++++++++++++++++++++++++++++ lib/lcd.c | 28 ++--- lib/loops.h | 22 ---- lib/meta.h | 6 - lib/pins.h | 129 --------------------- lib/sonar.c | 58 ++++------ lib/sonar.h | 8 +- lib/stream.c | 6 +- lib/uart.c | 22 +--- lib/ws_rgb.h | 46 ++++---- 15 files changed, 578 insertions(+), 318 deletions(-) delete mode 100644 lib/arduino_pins.h create mode 100644 lib/iopins.c create mode 100644 lib/iopins.h delete mode 100644 lib/loops.h delete mode 100644 lib/meta.h delete mode 100644 lib/pins.h diff --git a/lib/arduino_pins.h b/lib/arduino_pins.h deleted file mode 100644 index 68169d4..0000000 --- a/lib/arduino_pins.h +++ /dev/null @@ -1,42 +0,0 @@ -#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/lib/calc.h b/lib/calc.h index 619d2e2..58f27bf 100644 --- a/lib/calc.h +++ b/lib/calc.h @@ -30,16 +30,14 @@ #define cbi(reg, bit) do { (reg) &= ~(1 << (uint8_t)(bit)); } while(0) // Get n-th bit -#define read_bit(reg, bit) (((reg) >> (uint8_t)(bit)) & 0x1) -#define get_bit(reg, bit) read_bit(reg, bit) +#define get_bit(reg, bit) (((reg) >> (uint8_t)(bit)) & 0x1) // Test n-th bit (Can't use bit_is_set, as it's redefined in sfr_def.h) -#define bit_is_high(reg, bit) read_bit(reg, bit) -#define bit_is_low(reg, bit) (!read_bit(reg, bit)) +#define bit_is_high(reg, bit) get_bit(reg, bit) +#define bit_is_low(reg, bit) (!get_bit(reg, bit)) // Write value to n-th 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 set_bit(reg, bit, value) do { (reg) = ((reg) & ~(1 << (uint8_t)(bit))) | (((uint8_t)(value) & 0x1) << (uint8_t)(bit)); } while(0) // Invert n-th bit #define toggle_bit(reg, bit) do { (reg) ^= (1 << (uint8_t)(bit)); } while(0) @@ -53,27 +51,25 @@ #define cbi_p(reg_p, bit) do { (*(reg_p)) &= ~(1 << (uint8_t)(bit)); } while(0) // Get n-th bit in pointee -#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 get_bit_p(reg_p, bit) ((*(reg_p) >> (uint8_t)(bit)) & 0x1) // Test n-th bit in pointee (Can't use bit_is_set, as it's redefined in sfr_def.h) -#define bit_is_high_p(reg_p, bit) read_bit_p(reg_p, bit) -#define bit_is_low_p(reg_p, bit) (!read_bit_p(reg_p, bit)) +#define bit_is_high_p(reg_p, bit) get_bit_p(reg_p, bit) +#define bit_is_low_p(reg_p, bit) (!get_bit_p(reg_p, bit)) // Write value to a bit in pointee -#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_p, bit, value) write_bit_p(reg_p, bit, value) +#define set_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 toggle_bit_p(reg_p, bit) do { *(reg_p) ^= (1 << (uint8_t)(bit)); } while(0) // --- Nibble manipulation --- // Replace nibble in a byte -#define write_low_nibble(reg, value) do { (reg) = ((reg) & 0xF0) | ((uint8_t)(value) & 0xF); } while(0) -#define write_high_nibble(reg, value) do { (reg) = ((reg) & 0x0F) | (((uint8_t)(value) & 0xF) << 4); } while(0) +#define set_low_nibble(reg, value) do { (reg) = ((reg) & 0xF0) | ((uint8_t)(value) & 0xF); } while(0) +#define set_high_nibble(reg, value) do { (reg) = ((reg) & 0x0F) | (((uint8_t)(value) & 0xF) << 4); } while(0) -#define write_low_nibble_p(reg_p, value) do { *(reg_p) = (*(reg_p) & 0xF0) | ((uint8_t)(value) & 0xF); } while(0) -#define write_high_nibble_p(reg_p, value) do { *(reg_p) = (*(reg_p) & 0x0F) | (((uint8_t)(value) & 0xF) << 4); } while(0) +#define set_low_nibble_p(reg_p, value) do { *(reg_p) = (*(reg_p) & 0xF0) | ((uint8_t)(value) & 0xF); } while(0) +#define set_high_nibble_p(reg_p, value) do { *(reg_p) = (*(reg_p) & 0x0F) | (((uint8_t)(value) & 0xF) << 4); } while(0) #define low_nibble(x) ((uint8_t)(x) & 0xF) #define high_nibble(x) (((uint8_t)(x) & 0xF0) >> 4) diff --git a/lib/debounce.c b/lib/debounce.c index 409e80f..0e6e803 100644 --- a/lib/debounce.c +++ b/lib/debounce.c @@ -3,7 +3,7 @@ #include "debounce.h" #include "calc.h" -#include "pins.h" +#include "iopins.h" #include "debo_config.h" /** Debounce data array */ diff --git a/lib/debounce.h b/lib/debounce.h index 3909c32..89ac266 100644 --- a/lib/debounce.h +++ b/lib/debounce.h @@ -16,8 +16,8 @@ // // A pin is registered like this: // -// #define BTN1 B,0 -// #define BTN2 B,1 +// #define BTN1 12 // pin D12 +// #define BTN2 13 // // debo_add(BTN0); // The function returns number assigned to the pin (0, 1, ...) // debo_add_rev(BTN1); // active low @@ -50,9 +50,9 @@ typedef struct { debo_slot_t debo_slots[DEBO_CHANNELS]; -/** 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) +/** Add a pin for debouncing (must be used with constant args) */ +#define debo_add_rev(pin) debo_register(&_pin(pin), _pn(pin), 1) +#define debo_add(pin) debo_register(&_pin(pin), _pn(pin), 0) /** Add a pin for debouncing (low level function) */ uint8_t debo_register(PORT_P pin_reg_pointer, uint8_t bit, bool invert); diff --git a/lib/iopins.c b/lib/iopins.c new file mode 100644 index 0000000..f775e6f --- /dev/null +++ b/lib/iopins.c @@ -0,0 +1,276 @@ +#include +#include +#include + +#include "calc.h" +#include "iopins.h" + + +void set_dir_n(const uint8_t pin, const uint8_t d) +{ + switch(pin) { + case 0: set_dir(0, d); return; + case 1: set_dir(1, d); return; + case 2: set_dir(2, d); return; + case 3: set_dir(3, d); return; + case 4: set_dir(4, d); return; + case 5: set_dir(5, d); return; + case 6: set_dir(6, d); return; + case 7: set_dir(7, d); return; + case 8: set_dir(8, d); return; + case 9: set_dir(9, d); return; + case 10: set_dir(10, d); return; + case 11: set_dir(11, d); return; + case 12: set_dir(12, d); return; + case 13: set_dir(13, d); return; + case 14: set_dir(14, d); return; + case 15: set_dir(15, d); return; + case 16: set_dir(16, d); return; + case 17: set_dir(17, d); return; + case 18: set_dir(18, d); return; + case 19: set_dir(19, d); return; + case 20: set_dir(20, d); return; + case 21: set_dir(21, d); return; + } +} + +void as_input_n(const uint8_t pin) +{ + switch(pin) { + case 0: as_input(0); return; + case 1: as_input(1); return; + case 2: as_input(2); return; + case 3: as_input(3); return; + case 4: as_input(4); return; + case 5: as_input(5); return; + case 6: as_input(6); return; + case 7: as_input(7); return; + case 8: as_input(8); return; + case 9: as_input(9); return; + case 10: as_input(10); return; + case 11: as_input(11); return; + case 12: as_input(12); return; + case 13: as_input(13); return; + case 14: as_input(14); return; + case 15: as_input(15); return; + case 16: as_input(16); return; + case 17: as_input(17); return; + case 18: as_input(18); return; + case 19: as_input(19); return; + case 20: as_input(20); return; + case 21: as_input(21); return; + } +} + + +void as_input_pu_n(const uint8_t pin) +{ + switch(pin) { + case 0: as_input_pu(0); return; + case 1: as_input_pu(1); return; + case 2: as_input_pu(2); return; + case 3: as_input_pu(3); return; + case 4: as_input_pu(4); return; + case 5: as_input_pu(5); return; + case 6: as_input_pu(6); return; + case 7: as_input_pu(7); return; + case 8: as_input_pu(8); return; + case 9: as_input_pu(9); return; + case 10: as_input_pu(10); return; + case 11: as_input_pu(11); return; + case 12: as_input_pu(12); return; + case 13: as_input_pu(13); return; + case 14: as_input_pu(14); return; + case 15: as_input_pu(15); return; + case 16: as_input_pu(16); return; + case 17: as_input_pu(17); return; + case 18: as_input_pu(18); return; + case 19: as_input_pu(19); return; + case 20: as_input_pu(20); return; + case 21: as_input_pu(21); return; + } +} + + +void as_output_n(const uint8_t pin) +{ + switch(pin) { + case 0: as_output(0); return; + case 1: as_output(1); return; + case 2: as_output(2); return; + case 3: as_output(3); return; + case 4: as_output(4); return; + case 5: as_output(5); return; + case 6: as_output(6); return; + case 7: as_output(7); return; + case 8: as_output(8); return; + case 9: as_output(9); return; + case 10: as_output(10); return; + case 11: as_output(11); return; + case 12: as_output(12); return; + case 13: as_output(13); return; + case 14: as_output(14); return; + case 15: as_output(15); return; + case 16: as_output(16); return; + case 17: as_output(17); return; + case 18: as_output(18); return; + case 19: as_output(19); return; + case 20: as_output(20); return; + case 21: as_output(21); return; + } +} + +void set_pin_n(const uint8_t pin, const uint8_t v) +{ + switch(pin) { + case 0: set_pin(0, v); return; + case 1: set_pin(1, v); return; + case 2: set_pin(2, v); return; + case 3: set_pin(3, v); return; + case 4: set_pin(4, v); return; + case 5: set_pin(5, v); return; + case 6: set_pin(6, v); return; + case 7: set_pin(7, v); return; + case 8: set_pin(8, v); return; + case 9: set_pin(9, v); return; + case 10: set_pin(10, v); return; + case 11: set_pin(11, v); return; + case 12: set_pin(12, v); return; + case 13: set_pin(13, v); return; + case 14: set_pin(14, v); return; + case 15: set_pin(15, v); return; + case 16: set_pin(16, v); return; + case 17: set_pin(17, v); return; + case 18: set_pin(18, v); return; + case 19: set_pin(19, v); return; + case 20: set_pin(20, v); return; + case 21: set_pin(21, v); return; + } +} + +void set_low_n(const uint8_t pin) +{ + switch(pin) { + case 0: set_low(0); return; + case 1: set_low(1); return; + case 2: set_low(2); return; + case 3: set_low(3); return; + case 4: set_low(4); return; + case 5: set_low(5); return; + case 6: set_low(6); return; + case 7: set_low(7); return; + case 8: set_low(8); return; + case 9: set_low(9); return; + case 10: set_low(10); return; + case 11: set_low(11); return; + case 12: set_low(12); return; + case 13: set_low(13); return; + case 14: set_low(14); return; + case 15: set_low(15); return; + case 16: set_low(16); return; + case 17: set_low(17); return; + case 18: set_low(18); return; + case 19: set_low(19); return; + case 20: set_low(20); return; + case 21: set_low(21); return; + } +} + +void set_high_n(const uint8_t pin) +{ + switch(pin) { + case 0: set_high(0); return; + case 1: set_high(1); return; + case 2: set_high(2); return; + case 3: set_high(3); return; + case 4: set_high(4); return; + case 5: set_high(5); return; + case 6: set_high(6); return; + case 7: set_high(7); return; + case 8: set_high(8); return; + case 9: set_high(9); return; + case 10: set_high(10); return; + case 11: set_high(11); return; + case 12: set_high(12); return; + case 13: set_high(13); return; + case 14: set_high(14); return; + case 15: set_high(15); return; + case 16: set_high(16); return; + case 17: set_high(17); return; + case 18: set_high(18); return; + case 19: set_high(19); return; + case 20: set_high(20); return; + case 21: set_high(21); return; + } +} + + +void toggle_pin_n(const uint8_t pin) +{ + switch(pin) { + case 0: toggle_pin(0); return; + case 1: toggle_pin(1); return; + case 2: toggle_pin(2); return; + case 3: toggle_pin(3); return; + case 4: toggle_pin(4); return; + case 5: toggle_pin(5); return; + case 6: toggle_pin(6); return; + case 7: toggle_pin(7); return; + case 8: toggle_pin(8); return; + case 9: toggle_pin(9); return; + case 10: toggle_pin(10); return; + case 11: toggle_pin(11); return; + case 12: toggle_pin(12); return; + case 13: toggle_pin(13); return; + case 14: toggle_pin(14); return; + case 15: toggle_pin(15); return; + case 16: toggle_pin(16); return; + case 17: toggle_pin(17); return; + case 18: toggle_pin(18); return; + case 19: toggle_pin(19); return; + case 20: toggle_pin(20); return; + case 21: toggle_pin(21); return; + } +} + + +bool get_pin_n(const uint8_t pin) +{ + switch(pin) { + case 0: return get_pin(0); + case 1: return get_pin(1); + case 2: return get_pin(2); + case 3: return get_pin(3); + case 4: return get_pin(4); + case 5: return get_pin(5); + case 6: return get_pin(6); + case 7: return get_pin(7); + case 8: return get_pin(8); + case 9: return get_pin(9); + case 10: return get_pin(10); + case 11: return get_pin(11); + case 12: return get_pin(12); + case 13: return get_pin(13); + case 14: return get_pin(14); + case 15: return get_pin(15); + case 16: return get_pin(16); + case 17: return get_pin(17); + case 18: return get_pin(18); + case 19: return get_pin(19); + case 20: return get_pin(20); + case 21: return get_pin(21); + } + return false; +} + + +bool is_low_n(const uint8_t pin) +{ + return !get_pin_n(pin); +} + + +bool is_high_n(const uint8_t pin) +{ + return get_pin_n(pin); +} diff --git a/lib/iopins.h b/lib/iopins.h new file mode 100644 index 0000000..acd57ae --- /dev/null +++ b/lib/iopins.h @@ -0,0 +1,213 @@ +#pragma once + +// +// * Utilities for pin aliasing / numbering. * +// +// Designed for Arduino. +// +// If you know the pin number beforehand, you can use the macros. +// +// If you need to use a variable for pin number, use the `_n` functions. +// They are much slower, so always check if you really need them +// - and they aren't fit for things where precise timing is required. +// + +#include +#include +#include + +#include "calc.h" + + +// type: pointer to port +typedef volatile uint8_t* PORT_P; + + +/** Pin numbering reference */ +#define D0 0 +#define D1 1 +#define D2 2 +#define D3 3 +#define D4 4 +#define D5 5 +#define D6 6 +#define D7 7 +#define D8 8 +#define D9 9 +#define D10 10 +#define D11 11 +#define D12 12 +#define D13 13 +#define D14 14 +#define D15 15 +#define D16 16 +#define D17 17 +#define D18 18 +#define D19 19 +#define D20 20 +#define D21 21 +#define A0 14 +#define A1 15 +#define A2 16 +#define A3 17 +#define A4 18 +#define A5 19 +#define A6 20 +#define A7 21 + + +#define _ddr(pin) _DDR_##pin +#define _pin(pin) _PIN_##pin +#define _pn(pin) _PN_##pin +#define _port(pin) _PORT_##pin + + +/** Set pin direction */ +#define set_dir(pin, d) set_bit( _ddr(pin), _pn(pin), d ) +void set_dir_n(const uint8_t pin, const uint8_t d); + + +/** Configure pin as input */ +#define as_input(pin) cbi( _ddr(pin), _pn(pin) ) +void as_input_n(const uint8_t pin); + + +/** Configure pin as input, with pull-up enabled */ +#define as_input_pu(pin) { as_input(pin); set_high(pin); } +void as_input_pu_n(const uint8_t pin); + + +/** Configure pin as output */ +#define as_output(pin) sbi( _ddr(pin), _pn(pin) ) +void as_output_n(const uint8_t pin); + + +/** Write value to a pin */ +#define set_pin(pin, v) set_bit( _port(pin), _pn(pin), v ) +void set_pin_n(const uint8_t pin, const uint8_t v); + + +/** Write 0 to a pin */ +#define set_low(pin) cbi( _port(pin), _pn(pin) ) +void set_low_n(const uint8_t pin); + + +/** Write 1 to a pin */ +#define set_high(pin) sbi( _port(pin), _pn(pin) ) +void set_high_n(const uint8_t pin); + + +/** Toggle a pin state */ +#define toggle_pin(pin) sbi( _pin(pin), _pn(pin) ) +void toggle_pin_n(const uint8_t pin); + + +/** Read a pin value */ +#define get_pin(pin) get_bit( _pin(pin), _pn(pin) ) +bool get_pin_n(const uint8_t pin); + + +/** CHeck if pin is low */ +#define is_low(pin) (get_pin(pin) == 0) +bool is_low_n(const uint8_t pin); + + +/** CHeck if pin is high */ +#define is_high(pin) (get_pin(pin) != 0) +bool is_high_n(const uint8_t pin); + + + +// Helper macros + +#define _PORT_0 PORTD +#define _PORT_1 PORTD +#define _PORT_2 PORTD +#define _PORT_3 PORTD +#define _PORT_4 PORTD +#define _PORT_5 PORTD +#define _PORT_6 PORTD +#define _PORT_7 PORTD +#define _PORT_8 PORTB +#define _PORT_9 PORTB +#define _PORT_10 PORTB +#define _PORT_11 PORTB +#define _PORT_12 PORTB +#define _PORT_13 PORTB +#define _PORT_14 PORTC +#define _PORT_15 PORTC +#define _PORT_16 PORTC +#define _PORT_17 PORTC +#define _PORT_18 PORTC +#define _PORT_19 PORTC +#define _PORT_20 PORTC +#define _PORT_21 PORTC + +#define _PIN_0 PIND +#define _PIN_1 PIND +#define _PIN_2 PIND +#define _PIN_3 PIND +#define _PIN_4 PIND +#define _PIN_5 PIND +#define _PIN_6 PIND +#define _PIN_7 PIND +#define _PIN_8 PINB +#define _PIN_9 PINB +#define _PIN_10 PINB +#define _PIN_11 PINB +#define _PIN_12 PINB +#define _PIN_13 PINB +#define _PIN_14 PINC +#define _PIN_15 PINC +#define _PIN_16 PINC +#define _PIN_17 PINC +#define _PIN_18 PINC +#define _PIN_19 PINC +#define _PIN_20 PINC +#define _PIN_21 PINC + +#define _DDR_0 DDRD +#define _DDR_1 DDRD +#define _DDR_2 DDRD +#define _DDR_3 DDRD +#define _DDR_4 DDRD +#define _DDR_5 DDRD +#define _DDR_6 DDRD +#define _DDR_7 DDRD +#define _DDR_8 DDRB +#define _DDR_9 DDRB +#define _DDR_10 DDRB +#define _DDR_11 DDRB +#define _DDR_12 DDRB +#define _DDR_13 DDRB +#define _DDR_14 DDRC +#define _DDR_15 DDRC +#define _DDR_16 DDRC +#define _DDR_17 DDRC +#define _DDR_18 DDRC +#define _DDR_19 DDRC +#define _DDR_20 DDRC +#define _DDR_21 DDRC + +#define _PN_0 0 +#define _PN_1 1 +#define _PN_2 2 +#define _PN_3 3 +#define _PN_4 4 +#define _PN_5 5 +#define _PN_6 6 +#define _PN_7 7 +#define _PN_8 0 +#define _PN_9 1 +#define _PN_10 2 +#define _PN_11 3 +#define _PN_12 4 +#define _PN_13 5 +#define _PN_14 0 +#define _PN_15 1 +#define _PN_16 2 +#define _PN_17 3 +#define _PN_18 4 +#define _PN_19 5 +#define _PN_20 6 +#define _PN_21 7 diff --git a/lib/lcd.c b/lib/lcd.c index 20ef4a0..cf5fb1e 100644 --- a/lib/lcd.c +++ b/lib/lcd.c @@ -32,10 +32,10 @@ uint8_t _lcd_read_byte(); #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)); \ + set_pin(LCD_D7, get_bit((nib), 3)); \ + set_pin(LCD_D6, get_bit((nib), 2)); \ + set_pin(LCD_D5, get_bit((nib), 1)); \ + set_pin(LCD_D4, get_bit((nib), 0)); \ } while(0) @@ -100,9 +100,9 @@ void lcd_init() /** Send a pulse on the ENABLE line */ void _lcd_clk() { - pin_up(LCD_E); + set_high(LCD_E); delay_ns(450); - pin_down(LCD_E); + set_low(LCD_E); } @@ -111,7 +111,7 @@ void _lcd_mode_r() { if (_lcd_mode == 1) return; // already in R mode - pin_up(LCD_RW); + set_high(LCD_RW); as_input_pu(LCD_D7); as_input_pu(LCD_D6); @@ -127,7 +127,7 @@ void _lcd_mode_w() { if (_lcd_mode == 0) return; // already in W mode - pin_down(LCD_RW); + set_low(LCD_RW); as_output(LCD_D7); as_output(LCD_D6); @@ -146,10 +146,10 @@ uint8_t _lcd_read_byte() 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); + res = (get_pin(LCD_D7) << 7) | (get_pin(LCD_D6) << 6) | (get_pin(LCD_D5) << 5) | (get_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); + res |= (get_pin(LCD_D7) << 3) | (get_pin(LCD_D6) << 2) | (get_pin(LCD_D5) << 1) | (get_pin(LCD_D4) << 0); return res; } @@ -159,7 +159,7 @@ uint8_t _lcd_read_byte() void lcd_command(uint8_t bb) { _lcd_wait_bf(); - pin_down(LCD_RS); // select instruction register + set_low(LCD_RS); // select instruction register _lcd_write_byte(bb); // send instruction byte } @@ -186,7 +186,7 @@ void lcd_write(uint8_t bb) } _lcd_wait_bf(); - pin_up(LCD_RS); // select data register + set_high(LCD_RS); // select data register _lcd_write_byte(bb); // send data byte } @@ -194,7 +194,7 @@ void lcd_write(uint8_t bb) /** Read BF & Address */ uint8_t lcd_read_bf_addr() { - pin_down(LCD_RS); + set_low(LCD_RS); return _lcd_read_byte(); } @@ -204,7 +204,7 @@ uint8_t lcd_read() { if (_addrtype == TEXT) _pos.x++; - pin_up(LCD_RS); + set_high(LCD_RS); return _lcd_read_byte(); } diff --git a/lib/loops.h b/lib/loops.h deleted file mode 100644 index d36d077..0000000 --- a/lib/loops.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -// -// Custom loops -// - -// Repeat code n times (uint8_t counter) -#define repeat(count) repeat_aux(count, _repeat_##__COUNTER__) -#define repeat_aux(count, cntvar) for (uint8_t cntvar = 0; cntvar < (count); cntvar++) - -// Repeat code n times (uint16_t counter) -#define repeatx(count) repeatx_aux(count, _repeatx_##__COUNTER__) -#define repeatx_aux(count, cntvar) for (uint16_t cntvar = 0; cntvar < (count); cntvar++) - -// Repeat with custom counter name (uint8_t) -#define loop(var, count) repeat_aux(count, var) -// ..., uint16_t -#define loopx(var, count) repeatx_aux(count, var) - -// Do until condition is met -#define until(what) while(!(what)) - diff --git a/lib/meta.h b/lib/meta.h deleted file mode 100644 index cb7c2b7..0000000 --- a/lib/meta.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -// Weird constructs for the compiler - -// general macros -#define SECTION(pos) __attribute__((naked, used, section(pos))) diff --git a/lib/pins.h b/lib/pins.h deleted file mode 100644 index df9ad22..0000000 --- a/lib/pins.h +++ /dev/null @@ -1,129 +0,0 @@ -#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 underscored and _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" - -// Helpers -// 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 _io2port_aux(reg, bit) _reg_port(reg) -#define _io2pin_aux(reg, bit) _reg_pin(reg) -#define _io2n_aux(reg, bit) bit - - -// === Convert A,1 to corresponding register and pin number === - -#define io2ddr(io) _io2ddr_aux(io) -#define io2port(io) _io2port_aux(io) -#define io2pin(io) _io2pin_aux(io) -#define io2n(io) _io2n_aux(io) - -// === covert "A", "1" to "A,1" for passing on to another macro === -#define io_pack(port, bit) port, bit - - -// === Useful types for ports and pins === - -// pointer to port -typedef volatile uint8_t* PORT_P; -// number of bit in port -typedef uint8_t BIT_N; - - -// === pin manipulation === - -// Helpers -#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)) - - -// Set pin to HIGH -#define pin_up(io) _set_pin_aux(io) -#define pin_high(io) _set_pin_aux(io) - -// Set pin to LOW -#define pin_down(io) _clear_pin_aux(io) -#define pin_low(io) _clear_pin_aux(io) - -// Get input pin value -#define get_pin(io) _read_pin_aux(io) -#define read_pin(io) _read_pin_aux(io) - -// Check if pin is low or high -#define pin_is_low(io) !_read_pin_aux(io) -#define pin_is_high(io) _read_pin_aux(io) - -// Write a value to pin -#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 === - -// Helpers -#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)) - - -// Pin as input (_pu ... with pull-up) -#define as_input(io) _as_input_aux(io) -#define as_input_pu(io) do { _as_input_aux(io); _pullup_enable_aux(io); } while(0) - -// Pin as output -#define as_output(io) _as_output_aux(io) - -// Set direction (1 ... output) -#define set_dir(io, dir) _set_dir_aux(io, (dir)) - - -// === Setting pullup === - -// Helpers -#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)) - -// Enable pullup -#define pullup_enable(io) _pullup_enable_aux(io) -#define pullup_on(io) _pullup_enable_aux(io) - -// Disable pullup -#define pullup_disable(io) _pullup_disable_aux(io) -#define pullup_off(io) _pullup_disable_aux(io) - -// Set pullup to value (1 ... pullup enabled) -#define set_pullup(io, on) _set_pullup_aux(io, on) diff --git a/lib/sonar.c b/lib/sonar.c index 7a5f80c..d392d04 100644 --- a/lib/sonar.c +++ b/lib/sonar.c @@ -3,11 +3,11 @@ #include #include -#include "pins.h" +#include "iopins.h" #include "sonar.h" // Currently measured sonar -sonar_t* _sonar_active_so; +static sonar_t* _so; // Flag that measurement is in progress volatile bool sonar_busy; @@ -24,15 +24,9 @@ void _sonar_init_do(sonar_t* so, PORT_P port, uint8_t ntx, PORT_P pin, uint8_t n so->nrx = nrx; switch((const uint16_t) pin) { - case (const uint16_t)&PINB: - so->bank = 0; - break; - case (const uint16_t)&PINC: - so->bank = 1; - break; - case (const uint16_t)&PIND: - so->bank = 2; - break; + case ((const uint16_t) &PINB): so->bank = 0; break; + case ((const uint16_t) &PINC): so->bank = 1; break; + case ((const uint16_t) &PIND): so->bank = 2; break; } } @@ -47,7 +41,7 @@ bool sonar_start(sonar_t* so) { if (sonar_busy) return false; - _sonar_active_so = so; + _so = so; sonar_busy = true; @@ -56,37 +50,31 @@ bool sonar_start(sonar_t* so) // Timer overflow interrupt enable // We'll stop measuring on overflow - TIMSK1 |= (1 << TOIE1); + sbi(TIMSK1, TOIE1); // Clear the timer value TCNT1 = 0; // Set up pin change interrupt mask for the RX pin switch(so->bank) { - case 0: - PCMSK0 |= (1 << (so->nrx)); - break; - case 1: - PCMSK1 |= (1 << (so->nrx)); - break; - case 2: - PCMSK2 |= (1 << (so->nrx)); - break; + case 0: sbi(PCMSK0, so->nrx); break; + case 1: sbi(PCMSK1, so->nrx); break; + case 2: sbi(PCMSK2, so->nrx); break; } // send positive pulse - *(so->port) |= (1 << so->ntx); + sbi_p(so->port, so->ntx); _delay_us(_SNR_TRIG_TIME); - *(so->port) &= ~(1 << so->ntx); + cbi_p(so->port, so->ntx); // Wait for start of response - while ( (*(so->pin) & (1 << so->nrx)) == 0 ); + while (bit_is_low_p(so->pin, so->nrx)); // Set timer clock source: F_CPU / 8 (0.5 us resolution) TCCR1B = (0b010 << CS10); // Enable pin change interrupt - PCICR |= (1 << (so->bank)); + sbi(PCICR, so->bank); return true; } @@ -99,20 +87,14 @@ void _sonar_stop() TCCR1B = 0; // Disable RX pin interrupt mask - switch(_sonar_active_so->bank) { - case 0: - PCMSK0 &= ~(1 << (_sonar_active_so->nrx)); - break; - case 1: - PCMSK1 &= ~(1 << (_sonar_active_so->nrx)); - break; - case 2: - PCMSK2 &= ~(1 << (_sonar_active_so->nrx)); - break; + switch(_so->bank) { + case 0: PCMSK0 &= ~(1 << (_so->nrx)); break; + case 1: PCMSK1 &= ~(1 << (_so->nrx)); break; + case 2: PCMSK2 &= ~(1 << (_so->nrx)); break; } // Disable timer1 overflow interrupt - TIMSK1 &= ~(1 << TOIE1); + cbi(TIMSK1, TOIE1); sonar_busy = false; } @@ -137,7 +119,7 @@ inline bool sonar_handle_pci() return false; // nothing } - if (*(_sonar_active_so->pin) & (1 << _sonar_active_so->nrx)) { + if (bit_is_high_p(_so->pin, _so->nrx)) { // rx is high, not our pin change event return false; } diff --git a/lib/sonar.h b/lib/sonar.h index 63c48d2..0a92764 100644 --- a/lib/sonar.h +++ b/lib/sonar.h @@ -11,7 +11,7 @@ #include #include -#include "lib/pins.h" +#include "iopins.h" // Calib constant for the module // CM = uS / _DIV_CONST @@ -41,9 +41,9 @@ extern volatile int16_t sonar_result; // Create a Sonar port // Args: sonar_t* so, Trig pin, Echo pin #define sonar_init(so, trig, echo) do { \ - as_output(io_pack(trig)); \ - as_input_pu(io_pack(echo)); \ - _sonar_init_do(so, &io2port(io_pack(trig)), io2n(io_pack(trig)), &io2pin(io_pack(echo)), io2n(io_pack(echo))); \ + as_output(trig); \ + as_input_pu(echo); \ + _sonar_init_do(so, &_port(trig), _pn(trig), &_pin(echo), _pn(echo)); \ } while(0) // private, in header because of the macro. diff --git a/lib/stream.c b/lib/stream.c index 8e357ee..9397b73 100644 --- a/lib/stream.c +++ b/lib/stream.c @@ -12,16 +12,18 @@ static char tmpstr[20]; // buffer for number rendering void put_str(const STREAM *p, char* str) { char c; - while ((c = *str++)) + while ((c = *str++)) { p->tx(c); + } } void put_str_P(const STREAM *p, const char* str) { char c; - while ((c = pgm_read_byte(str++))) + while ((c = pgm_read_byte(str++))) { p->tx(c); + } } diff --git a/lib/uart.c b/lib/uart.c index c1d3b9f..566517d 100644 --- a/lib/uart.c +++ b/lib/uart.c @@ -5,6 +5,7 @@ #include #include +#include "calc.h" #include "uart.h" #include "stream.h" @@ -34,33 +35,21 @@ void _uart_init_do(uint16_t ubrr) { /** Enable or disable RX ISR */ void uart_isr_rx(bool yes) { - if(yes) { - UCSR0B |= (1 << RXCIE0); - } else { - UCSR0B &= ~(1 << RXCIE0); - } + set_bit(UCSR0B, RXCIE0, yes); } /** Enable or disable TX ISR (1 byte is sent) */ void uart_isr_tx(bool yes) { - if(yes) { - UCSR0B |= (1 << TXCIE0); - } else { - UCSR0B &= ~(1 << TXCIE0); - } + set_bit(UCSR0B, TXCIE0, yes); } /** Enable or disable DRE ISR (all is sent) */ void uart_isr_dre(bool yes) { - if(yes) { - UCSR0B |= (1 << UDRIE0); - } else { - UCSR0B &= ~(1 << UDRIE0); - } + set_bit(UCSR0B, UDRIE0, yes); } @@ -107,6 +96,7 @@ void uart_puts_P(const char* str) void uart_flush() { uint8_t dummy; - while (UCSR0A & (1 << RXC0)) + while (bit_is_high(UCSR0A, RXC0)) { dummy = UDR0; + } } diff --git a/lib/ws_rgb.h b/lib/ws_rgb.h index cb99ef9..b58a28f 100644 --- a/lib/ws_rgb.h +++ b/lib/ws_rgb.h @@ -18,7 +18,7 @@ #include -#include "pins.h" +#include "iopins.h" #include "nsdelay.h" #include "colors.h" @@ -55,11 +55,11 @@ #define ws_send_byte(io, bb) do { \ for (volatile int8_t __ws_tmp = 7; __ws_tmp >= 0; --__ws_tmp) { \ if ((bb) & (1 << __ws_tmp)) { \ - pin_high(io_pack(io)); delay_ns_c(WS_T_1H, -2); \ - pin_low(io_pack(io)); delay_ns_c(WS_T_1L, -10); \ + set_high(io); delay_ns_c(WS_T_1H, -2); \ + set_low(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); \ + set_high(io); delay_ns_c(WS_T_0H, -2); \ + set_low(io); delay_ns_c(WS_T_0L, -10); \ } \ } \ } while(0) @@ -67,32 +67,32 @@ /** 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); \ + ws_send_byte(io, g); \ + ws_send_byte(io, r); \ + ws_send_byte(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) +#define ws_send_xrgb(io, xrgb) ws_send_rgb(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)) +#define ws_send_rgb24(io, rgb) ws_send_rgb(io, rgb24_r(rgb), rgb24_g(rgb), rgb24_b(rgb)) +#define ws_send_rgb15(io, rgb) ws_send_rgb(io, rgb15_r(rgb), rgb15_g(rgb), rgb15_b(rgb)) +#define ws_send_rgb12(io, rgb) ws_send_rgb(io, rgb12_r(rgb), rgb12_g(rgb), rgb12_b(rgb)) +#define ws_send_rgb6(io, rgb) ws_send_rgb(io, rgb6_r(rgb), rgb6_g(rgb), rgb6_b(rgb)) /** Send array of colors */ -#define ws_send_xrgb_array(io, rgbs, length) __ws_send_array_proto(io_pack(io), (rgbs), (length), xrgb) -#define ws_send_rgb24_array(io, rgbs, length) __ws_send_array_proto(io_pack(io), (rgbs), (length), rgb24) -#define ws_send_rgb15_array(io, rgbs, length) __ws_send_array_proto(io_pack(io), (rgbs), (length), rgb15) -#define ws_send_rgb12_array(io, rgbs, length) __ws_send_array_proto(io_pack(io), (rgbs), (length), rgb12) -#define ws_send_rgb6_array(io, rgbs, length) __ws_send_array_proto(io_pack(io), (rgbs), (length), rgb6) +#define ws_send_xrgb_array(io, rgbs, length) __ws_send_array_proto(io, (rgbs), (length), xrgb) +#define ws_send_rgb24_array(io, rgbs, length) __ws_send_array_proto(io, (rgbs), (length), rgb24) +#define ws_send_rgb15_array(io, rgbs, length) __ws_send_array_proto(io, (rgbs), (length), rgb15) +#define ws_send_rgb12_array(io, rgbs, length) __ws_send_array_proto(io, (rgbs), (length), rgb12) +#define ws_send_rgb6_array(io, rgbs, length) __ws_send_array_proto(io, (rgbs), (length), rgb6) // prototype for sending array. it's ugly, sorry. #define __ws_send_array_proto(io, rgbs, length, style) do { \ for (uint8_t __ws_sap_i = 0; __ws_sap_i < length; __ws_sap_i++) { \ style ## _t __ws_sap2 = (rgbs)[__ws_sap_i]; \ - ws_send_ ## style(io_pack(io), __ws_sap2); \ + ws_send_ ## style(io, __ws_sap2); \ } \ } while(0) @@ -101,11 +101,11 @@ int8_t __ws_sxaz_y, __ws_sxaz_x; \ for(__ws_sxaz_y = 0; __ws_sxaz_y < (height); __ws_sxaz_y ++) { \ for(__ws_sxaz_x = 0; __ws_sxaz_x < (width); __ws_sxaz_x++) { \ - ws_send_xrgb(io_pack(io), (rgbs)[__ws_sxaz_y][__ws_sxaz_x]); \ + ws_send_xrgb(io, (rgbs)[__ws_sxaz_y][__ws_sxaz_x]); \ } \ __ws_sxaz_y++; \ for(__ws_sxaz_x = (width) - 1; __ws_sxaz_x >= 0; __ws_sxaz_x--) { \ - ws_send_xrgb(io_pack(io), (rgbs)[__ws_sxaz_y][__ws_sxaz_x]); \ + ws_send_xrgb(io, (rgbs)[__ws_sxaz_y][__ws_sxaz_x]); \ } \ } \ } while(0) @@ -116,11 +116,11 @@ int8_t __ws_sxazl_x, __ws_sxazl_y; \ for(__ws_sxazl_y = 0; __ws_sxazl_y < (height); __ws_sxazl_y++) { \ for(__ws_sxazl_x = 0; __ws_sxazl_x < (width); __ws_sxazl_x++) { \ - ws_send_xrgb(io_pack(io), (rgbs)[__ws_sxazl_y * (width) + __ws_sxazl_x]); \ + ws_send_xrgb(io, (rgbs)[__ws_sxazl_y * (width) + __ws_sxazl_x]); \ } \ __ws_sxazl_y++; \ for(__ws_sxazl_x = width-1; __ws_sxazl_x >=0; __ws_sxazl_x--) { \ - ws_send_xrgb(io_pack(io), (rgbs)[__ws_sxazl_y * (width) + __ws_sxazl_x]); \ + ws_send_xrgb(io, (rgbs)[__ws_sxazl_y * (width) + __ws_sxazl_x]); \ } \ } \ } while(0)