aaio

AVR All-In-One helper C library for Arduino boards
git clone git://git.luxferre.top/aaio.git
Log | Files | Refs

commit ac46e22deb687fc38289c0a9a31bd026d9a367a6
parent bb7c827087aa2edd1f9c9da77a1103577ad6f468
Author: Luxferre <lux@ferre>
Date:   Tue, 26 Nov 2024 10:49:17 +0200

some i/o fixes

Diffstat:
Maaio.h | 66++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 52 insertions(+), 14 deletions(-)

diff --git a/aaio.h b/aaio.h @@ -152,7 +152,7 @@ void aaio_led_toggle() { } /* set the internal LED */ -void aaio_led_set(uint8_t v) { +void aaio_led_set(register uint8_t v) { if(v == 0) { AAIO_LED_PORT &= ~(1 << AAIO_LED_PIN); } else { @@ -182,7 +182,7 @@ void aaio_uart_init(void) { } /* put a character into the bus */ -int aaio_uart_putchar(char c, FILE *stream) { +int aaio_uart_putchar(register char c, FILE *stream) { if(c == '\n') aaio_uart_putchar('\r', stream); loop_until_bit_is_set(UCSR0A, UDRE0); UDR0 = c; @@ -195,10 +195,28 @@ int aaio_uart_getchar(FILE *stream) { return (UDR0); } +/* put a null-terminated string into the bus */ +void aaio_uart_puts(char *buf) { + while(*buf) + aaio_uart_putchar(*(buf++), NULL); +} + +/* print a number into the bus */ +void aaio_uart_printnum(register unsigned int value) { + register uint8_t d; + register uint32_t threshold = 1000000000; + for(;threshold > 0;threshold /= 10) { + if(value > threshold) { + d = (value / threshold) % 10; + aaio_uart_putchar('0' + d, NULL); + } + } +} + /* get a string of length len from the bus, optionally echoing it back */ -int aaio_uart_gets(char *buf, unsigned int len, char echo) { - unsigned int i; - char c; +int aaio_uart_gets(char *buf, register unsigned int len, register char echo) { + register unsigned int i; + register char c; do { for(i = 0; i < len - 1; i++) { c = (char) aaio_uart_getchar(NULL); @@ -235,7 +253,7 @@ void aaio_uart_redirect_stdout() { /* scan the keyboard and return the associated value */ /* the definition must be stored in PROGMEM */ uint8_t aaio_scankey(const uint8_t keypad_def[4][4]) { - uint8_t row, col, dirmask; + register uint8_t row, col, dirmask; for(col = 0; col < 4; col++) { AAIO_KEYPAD_DDR = dirmask = 1 << (col + 4); AAIO_KEYPAD_PORT = 0xf & ~dirmask; @@ -273,7 +291,7 @@ static struct { }; /* send data to the LCD (in data or command mode) */ -static void _aaio_lcd_write(uint8_t byte, uint8_t is_data) { +static void _aaio_lcd_write(register uint8_t byte, register uint8_t is_data) { register uint8_t i; AAIO_LCD_PORT &= ~(1 << AAIO_LCD_SCE); /* enable the controller */ if(is_data) @@ -293,11 +311,11 @@ static void _aaio_lcd_write(uint8_t byte, uint8_t is_data) { AAIO_LCD_PORT |= (1 << AAIO_LCD_SCE); /* disable the controller */ } -static void _aaio_lcd_write_cmd(uint8_t cmd) { +static void _aaio_lcd_write_cmd(register uint8_t cmd) { _aaio_lcd_write(cmd, 0); } -static void _aaio_lcd_write_data(uint8_t data) { +static void _aaio_lcd_write_data(register uint8_t data) { _aaio_lcd_write(data, 1); } @@ -351,19 +369,19 @@ void aaio_lcd_clear(void) { } /* power the LCD on/off */ -void aaio_lcd_power(uint8_t on) { +void aaio_lcd_power(register uint8_t on) { _aaio_lcd_write_cmd(on ? 0x20 : 0x24); } /* set a pixel */ -void aaio_lcd_set_pixel(uint8_t x, uint8_t y, uint8_t value) { +void aaio_lcd_set_pixel(register uint8_t x, register uint8_t y, register uint8_t value) { uint8_t *byte = &_aaio_lcd.screen[(y >> 3) * 84 + x]; if(value) *byte |= (1 << (y & 7)); else *byte &= ~(1 << (y & 7)); } /* write an ASCII character */ -void aaio_lcd_write_char(char code, uint8_t scale) { +void aaio_lcd_write_char(register char code, register uint8_t scale) { register uint8_t x, y; for(x = 0; x < 5*scale; x++) for(y = 0; y < 7*scale; y++) @@ -382,11 +400,11 @@ void aaio_lcd_write_char(char code, uint8_t scale) { } } -void aaio_lcd_write_string(const char *str, uint8_t scale) { +void aaio_lcd_write_string(const char *str, register uint8_t scale) { while(*str) aaio_lcd_write_char(*str++, scale); } -void aaio_lcd_set_cursor(uint8_t x, uint8_t y) { +void aaio_lcd_set_cursor(register uint8_t x, register uint8_t y) { _aaio_lcd.cursor_x = x; _aaio_lcd.cursor_y = y; } @@ -402,4 +420,24 @@ void aaio_lcd_render(void) { #endif +/* PRNG feature */ + +#ifdef AAIO_FEATURE_RNG + +static uint32_t _aaio_rng_state; + +void aaio_srand() { + _aaio_rng_state = 0xdeadbeef; +} + +uint32_t aaio_rand() { + register uint32_t x = _aaio_rng_state; + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + return _aaio_rng_state = x; +} + +#endif + #endif