From d716190cc5a40ce6e8ffdece8b54da64784f0aa4 Mon Sep 17 00:00:00 2001 From: MightyPork Date: Fri, 24 Apr 2015 14:33:05 +0200 Subject: [PATCH] changed comments & added basic VT100 support --- lib/adc.h | 6 +- lib/arduino_pins.h | 6 +- lib/calc.h | 6 +- lib/colors.h | 20 +++--- lib/debounce.h | 63 ++++++++--------- lib/hsl.h | 6 +- lib/lcd.h | 36 +++++----- lib/loops.h | 6 +- lib/meta.h | 2 +- lib/nsdelay.h | 6 +- lib/pins.h | 50 +++++++------- lib/uart.h | 12 ++-- lib/uart_ansi.c | 166 +++++++++++++++++++++++++++++++++++++++++++++ lib/uart_ansi.h | 62 +++++++++++++++++ lib/ws_rgb.h | 31 ++++----- 15 files changed, 352 insertions(+), 126 deletions(-) create mode 100644 lib/uart_ansi.c create mode 100644 lib/uart_ansi.h diff --git a/lib/adc.h b/lib/adc.h index 64244f5..fdd08d2 100644 --- a/lib/adc.h +++ b/lib/adc.h @@ -1,8 +1,8 @@ #pragma once -/* - Utilities for build-in A/D converter -*/ +// +// Utilities for build-in A/D converter +// #include diff --git a/lib/arduino_pins.h b/lib/arduino_pins.h index ca4f4c6..68169d4 100644 --- a/lib/arduino_pins.h +++ b/lib/arduino_pins.h @@ -1,8 +1,8 @@ #pragma once -/** - Pin definitions for Arduino (Pro Mini with ATmega328P) -*/ +// +// Pin definitions for Arduino (Pro Mini with ATmega328P) +// #include "pins.h" diff --git a/lib/calc.h b/lib/calc.h index 750e173..6418437 100644 --- a/lib/calc.h +++ b/lib/calc.h @@ -1,8 +1,8 @@ #pragma once -/** - Bit and byte manipulation utilities -*/ +// +// Bit and byte manipulation utilities +// // --- Increment in range --- diff --git a/lib/colors.h b/lib/colors.h index fd31969..e3e40f0 100644 --- a/lib/colors.h +++ b/lib/colors.h @@ -1,15 +1,15 @@ #pragma once -/* - Some useful utilities for RGB color manipulation - - The XXXc macros don't use cast, so they can be used in array initializers. - - xrgb ... 3-byte true-color RGB (8 bits per component) - rgbXX ... XX-bit color value, with equal nr of bits per component - - XX_r (_g, _b) ... extract component from the color, and convert it to 0..255 -*/ +// +// Some useful utilities for RGB color manipulation +// +// The XXXc macros don't use cast, so they can be used in array initializers. +// +// xrgb ... 3-byte true-color RGB (8 bits per component) +// rgbXX ... XX-bit color value, with equal nr of bits per component +// +// XX_r (_g, _b) ... extract component from the color, and convert it to 0..255 +// typedef struct { diff --git a/lib/debounce.h b/lib/debounce.h index 62af351..3909c32 100644 --- a/lib/debounce.h +++ b/lib/debounce.h @@ -1,37 +1,38 @@ #pragma once -/** - An implementation of button debouncer. +// +// An implementation of button debouncer. +// +// ---- +// +// You must provide a config file debo_config.h (next to your main.c) +// +// 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 +// +// debo_add(BTN0); // The function returns number assigned to the pin (0, 1, ...) +// debo_add_rev(BTN1); // active low +// debo_register(&PINB, PB2, 0); // direct access - register, pin & invert +// +// Then periodically call the tick function (perhaps in a timer interrupt): +// +// debo_tick(); +// +// To check if input is active, use +// +// debo_get_pin(0); // state of input #0 (registered first) +// debo_get_pin(1); // state of input #1 (registered second) +// - ---- - - You must provide a config file debo_config.h (next to your main.c) - - 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 - - debo_add(BTN0); // The function returns number assigned to the pin (0, 1, ...) - debo_add_rev(BTN1); // active low - debo_register(&PINB, PB2, 0); // direct access - register, pin & invert - - Then periodically call the tick function (perhaps in a timer interrupt): - - debo_tick(); - - To check if input is active, use - - debo_get_pin(0); // state of input #0 (registered first) - debo_get_pin(1); // state of input #1 (registered second) -*/ #include #include diff --git a/lib/hsl.h b/lib/hsl.h index 528e784..b2d89cf 100644 --- a/lib/hsl.h +++ b/lib/hsl.h @@ -1,8 +1,8 @@ #pragma once -/* - HSL support (addition to colors.h) -*/ +// +// HSL support (addition to colors.h) +// #include "colors.h" diff --git a/lib/lcd.h b/lib/lcd.h index 132e67a..0754091 100644 --- a/lib/lcd.h +++ b/lib/lcd.h @@ -1,24 +1,22 @@ #pragma once -/* - HD44780 LCD display driver - 4-bit mode - - 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: - - #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 - -*/ +// HD44780 LCD display driver - 4-bit mode +// +// 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: +// +// #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 +// #include #include "lcd_config.h" diff --git a/lib/loops.h b/lib/loops.h index 4fa963e..d36d077 100644 --- a/lib/loops.h +++ b/lib/loops.h @@ -1,8 +1,8 @@ #pragma once -/** - Custom loops -*/ +// +// Custom loops +// // Repeat code n times (uint8_t counter) #define repeat(count) repeat_aux(count, _repeat_##__COUNTER__) diff --git a/lib/meta.h b/lib/meta.h index ec16799..cb7c2b7 100644 --- a/lib/meta.h +++ b/lib/meta.h @@ -1,6 +1,6 @@ #pragma once -/** Weird constructs for the compiler */ +// Weird constructs for the compiler // general macros #define SECTION(pos) __attribute__((naked, used, section(pos))) diff --git a/lib/nsdelay.h b/lib/nsdelay.h index 73fd667..dd93a83 100644 --- a/lib/nsdelay.h +++ b/lib/nsdelay.h @@ -1,8 +1,8 @@ #pragma once -/** - Functions for precise delays (nanoseconds / cycles) -*/ +// +// Functions for precise delays (nanoseconds / cycles) +// #include #include diff --git a/lib/pins.h b/lib/pins.h index 93960b4..1efe54e 100644 --- a/lib/pins.h +++ b/lib/pins.h @@ -1,30 +1,30 @@ #pragma once -/** - This file provides macros for pin manipulation. - - You can define your application pins like so: - - // Led at PORTB, pin 1 - #define LED B,1 - - // Switch at PORTD, pin 7 - #define SW1 D,7 - - Now you can use macros from this file to wirh with the pins, eg: - - as_output(LED); - as_input(SW1); - pullup_on(SW1); - - toggle_pin(LED); - while (pin_is_low(SW1)); - - - The macros io2XXX() can be used to get literal name of register associated with the pin. - - io2n() provides pin number. - - The XXX_aux() macros are internal and should not be used elsewhere. - - The io_pack() macro is used to pass pin (io) to other macro without expanding it. -*/ +// +// This file provides macros for pin manipulation. +// +// You can define your application pins like so: +// +// // Led at PORTB, pin 1 +// #define LED B,1 +// +// // Switch at PORTD, pin 7 +// #define SW1 D,7 +// +// Now you can use macros from this file to wirh with the pins, eg: +// +// as_output(LED); +// as_input(SW1); +// pullup_on(SW1); +// +// toggle_pin(LED); +// while (pin_is_low(SW1)); +// +// - The macros io2XXX() can be used to get literal name of register associated with the pin. +// - io2n() provides pin number. +// - The XXX_aux() macros are internal and should not be used elsewhere. +// - The io_pack() macro is used to pass pin (io) to other macro without expanding it. +// #include #include "calc.h" diff --git a/lib/uart.h b/lib/uart.h index a4182e5..0f9fd20 100644 --- a/lib/uart.h +++ b/lib/uart.h @@ -1,11 +1,11 @@ #pragma once -/* - Utilities for UART communication. - - First, init uart with desired baud rate using uart_init(). - Then, enable interrupts you want, and that's it. -*/ +// +// Utilities for UART communication. +// +// First, init uart with desired baud rate using uart_init(). +// Then, enable interrupts you want, and that's it. +// #include #include diff --git a/lib/uart_ansi.c b/lib/uart_ansi.c new file mode 100644 index 0000000..57662d2 --- /dev/null +++ b/lib/uart_ansi.c @@ -0,0 +1,166 @@ +#include +#include +#include + +#include "uart.h" +#include "uart_ansi.h" + + +void vt_goto(uint16_t x, uint16_t y) +{ + uart_putc(27); + uart_putc('['); + uart_putu(x); + uart_putc(';'); + uart_putu(y); + uart_putc('H'); +} + + +void vt_move(int16_t x, int16_t y) +{ + vt_move_x(x); + vt_move_y(y); +} + + +void vt_move_x(int16_t x) +{ + if (x < 0) { + vt_left(-x); + } else { + vt_right(x); + } +} + + +void vt_move_y(int16_t y) +{ + if (y < 0) { + vt_up(-y); + } else { + vt_down(y); + } +} + + +void vt_up(uint16_t y) +{ + if (y == 0) return; + uart_putc(27); + uart_putc('['); + uart_putu(y); + uart_putc('A'); +} + + +void vt_down(uint16_t y) +{ + if (y == 0) return; + uart_putc(27); + uart_putc('['); + uart_putu(y); + uart_putc('B'); +} + + +void vt_left(uint16_t x) +{ + if (x == 0) return; + uart_putc(27); + uart_putc('['); + uart_putu(x); + uart_putc('D'); +} + + +void vt_right(uint16_t x) +{ + if (x == 0) return; + uart_putc(27); + uart_putc('['); + uart_putu(x); + uart_putc('C'); +} + + +void vt_scroll(int16_t y) +{ + while (y < 0) { + uart_putc(27); + uart_putc('D'); // up + y++; + } + + while (y > 0) { + uart_putc(27); + uart_putc('M'); // down + y--; + } +} + + +void vt_save() +{ + uart_putc(27); + uart_putc(7); +} + + +void vt_restore() +{ + uart_putc(27); + uart_putc(8); +} + + +void vt_style(uint8_t flags) +{ + if (flags == VT_NORMAL) { + uart_puts("\x1B[m"); // reset + return; + } + + if (flags & VT_BOLD) { + uart_puts("\x1B[1m"); + } + + if (flags & VT_UNDERLINE) { + uart_puts("\x1B[4m"); + } + + if (flags & VT_BLINK) { + uart_puts("\x1B[5m"); + } + + if (flags & VT_REVERSE) { + uart_puts("\x1B[7m"); + } + + if (flags & VT_HIDDEN) { + uart_puts("\x1B[8m"); + } +} + + +void vt_color(uint8_t fg, uint8_t bg) +{ + uart_putc(27); + uart_putc('['); + uart_putu(fg); + uart_putc(';'); + uart_putu(bg); + uart_putc('m'); +} + + +void vt_clear() +{ + uart_puts("\x1B[2J"); +} + + +void vt_home() +{ + uart_puts("\x1B[H"); +} diff --git a/lib/uart_ansi.h b/lib/uart_ansi.h new file mode 100644 index 0000000..bab86e0 --- /dev/null +++ b/lib/uart_ansi.h @@ -0,0 +1,62 @@ +#pragma once + +// +// ANSI / VT100 utilities for UART +// + +#include +#include +#include +#include "uart.h" + +#define VT_NORMAL 0 +#define VT_BOLD 1 +#define VT_UNDERLINE 2 +#define VT_BLINK 4 +#define VT_REVERSE 8 +#define VT_HIDDEN 16 + +/** Jump to a location on the screen */ +void vt_goto(uint16_t x, uint16_t y); + +/** Move cursor relative to current location */ +void vt_move(int16_t x, int16_t y); + +/** Move cursor horizontally */ +void vt_move_x(int16_t x); + +/** Move cursor vertically */ +void vt_move_y(int16_t y); + +/** Move cursor up y cells */ +void vt_up(uint16_t y); + +/** Move cursor down y cells */ +void vt_down(uint16_t y); + +/** Move cursor left x cells */ +void vt_left(uint16_t x); + +/** Move cursor right x cells */ +void vt_right(uint16_t x); + +/** Scroll y lines down (like up/down, but moves window if needed) */ +void vt_scroll(int16_t down); + +/** Set font style */ +void vt_style(uint8_t flags); + +/** Set color */ +void vt_color(uint8_t fg, uint8_t bg); + +/** Save cursor position & text attributes */ +void vt_save(); + +/** Restore cursor to saved values */ +void vt_restore(); + +/** Clear the screen */ +void vt_clear(); + +/** Move cursor to top left corner */ +void vt_home(); diff --git a/lib/ws_rgb.h b/lib/ws_rgb.h index 9772c79..cb99ef9 100644 --- a/lib/ws_rgb.h +++ b/lib/ws_rgb.h @@ -1,21 +1,20 @@ #pragma once -/** - Utils for driving a WS28xx (tested on WS2812B) RGB LED strips. - - It's implemented as macros to avoid overhead when passing values, and to - enable driving multiple strips at once. - - To avoid bloating your code, try to reduce the number of invocations - - compute color and then send it. - - [IMPORTANT] - - Some seemingly random influences can ruin the communication. - If you have enough memory, consider preparing the colors in array, - and sending this array using one of the "ws_send_XXX_array" macros. - -*/ +// +// Utils for driving a WS28xx (tested on WS2812B) RGB LED strips. +// +// It's implemented as macros to avoid overhead when passing values, and to +// enable driving multiple strips at once. +// +// To avoid bloating your code, try to reduce the number of invocations - +// compute color and then send it. +// +// [IMPORTANT] +// +// Some seemingly random influences can ruin the communication. +// If you have enough memory, consider preparing the colors in array, +// and sending this array using one of the "ws_send_XXX_array" macros. +// #include