diff options
Diffstat (limited to 'screen')
-rw-r--r-- | screen/console.c | 132 | ||||
-rw-r--r-- | screen/console.h | 16 |
2 files changed, 125 insertions, 23 deletions
diff --git a/screen/console.c b/screen/console.c index 57f9d6b..00e9ebb 100644 --- a/screen/console.c +++ b/screen/console.c @@ -30,6 +30,15 @@ void console_init() console_pointer = 0; } + +/********************************************************* + * Update cursor position to x,y co-ord for next char + ********************************************************/ +void console_set_cursor_xy(uint8_t x,uint8_t y) +{ + console_pointer = (160*y) + (2*x); +} + /********************************************************* * Update flashing cursor to match console_position * Note: Does _NOT_ dictate the pos for next printed byte @@ -47,18 +56,18 @@ void console_update_cursor() ********************************************************/ void console_clear() { - int console_pointer; - for (console_pointer = 0; console_pointer < 4000; console_pointer++) + for (console_pointer = 0; console_pointer < 4000; console_pointer+=2) { - console_buffer[console_pointer] = console_color; - console_buffer[console_pointer++] = 0; + console_buffer[console_pointer] = 0; + console_buffer[console_pointer+1] = console_color; } - //console_update_cursor(); + console_pointer = 0; + console_update_cursor(); } /********************************************************* - * Set the console foreground and background colours + * Set the console foreground AND background colours ********************************************************/ void console_set_colors(uint8_t fg, uint8_t bg) { @@ -67,7 +76,7 @@ void console_set_colors(uint8_t fg, uint8_t bg) /********************************************************* - * Set the console foreground colour + * Set ONLY the console foreground colour ********************************************************/ void console_set_color(uint8_t fg) { @@ -75,26 +84,113 @@ void console_set_color(uint8_t fg) console_color |= fg; // Apply foreground color } + +/********************************************************* + * Print a single character + ********************************************************/ +void console_print_char(const char c) +{ + // Scroll the screen up a line if the pointer is out of bounds + if (console_pointer >= 4000) + console_scroll(); + switch (c) + { + case 8: + console_buffer[console_pointer-=2] = 0; + break; + + case 10: + console_pointer += 160-(console_pointer%160); + break; + + default: + console_buffer[console_pointer++] = c; + console_buffer[console_pointer++] = console_color; + } +} + + +/********************************************************* + * Print an integer of a certain base + ********************************************************/ +void console_print_num(const uint32_t num, const uint8_t base) +{ + char buffer[32]; + uint32_t i = 0; + if (num == 0) + { + console_print_char('0'); + } else { + itoa(num,buffer,base); + // Find the first digit which isn't a trailing digit + while(buffer[i++] == '0'); + console_print_string(buffer+i-1); + } +} + /********************************************************* - * Print a null-terminated string to the text screen + * Print a single null-terminated string ********************************************************/ -void console_print(char *string, ...) +void console_print_string(const char *string) { + uint16_t i = 0; + while(string[i] != 0) + console_print_char(string[i++]); + console_update_cursor(); +} + +/********************************************************* + * Print a single null-terminated string + ********************************************************/ +void console_scroll(/*uint8_t lines*/) +{ + //memcpy(console_buffer, console_buffer+160, 3840); + memcpy(console_buffer, console_buffer+160, 3840); + memset(console_buffer+3840, 0, 160); + console_pointer = 3840; +} + + +/********************************************************* + * Print a formatted string + ********************************************************/ +void console_print(const char* format, ...) +{ + va_list strings; + va_start(strings,format); uint16_t i; - for (i = 0; i < strlen(string); i++) + uint64_t length = strlen(format); + + for (i = 0; i < length; i++) { - // If special char, handle appropriately - if (string[i] < 32) + if (format[i] == '%') { - switch (string[i]) - case 10: - console_pointer = 160 + (console_pointer / 160) * 160; + switch (format[++i]) + { + case 's': + console_print_string(va_arg(strings, char*)); + break; + case 'c': + console_print_char(va_arg(strings, uint64_t)); + break; + case 'd': + console_print_num(va_arg(strings, uint64_t),10); + break; + case 'x': + console_print_num(va_arg(strings, uint64_t),16); + break; + case 'o': + console_print_num(va_arg(strings, uint64_t),8); + break; + case 'b': + console_print_num(va_arg(strings, uint64_t),2); + break; + } } else { - console_buffer[console_pointer++] = string[i]; - console_buffer[console_pointer++] = console_color; + console_print_char(format[i]); } } - console_update_cursor(); + va_end(strings); } #endif
\ No newline at end of file diff --git a/screen/console.h b/screen/console.h index 66a867a..f037592 100644 --- a/screen/console.h +++ b/screen/console.h @@ -19,6 +19,8 @@ #ifndef __CONSOLE_H #define __CONSOLE_H +#include <stdarg.h> + #define console_width 80 #define console_height 25 #define console_x_pos (console_pointer%(2*console_width)) @@ -32,28 +34,32 @@ #define COLOR_PURPLE 0x5 #define COLOR_GOLD 0x6 #define COLOR_BRIGHT_GREY 0x7 -#define COLOR_BRIGHT_GRAY 0x7 // Alias for grey vs gray #define COLOR_GRAY 0x8 -#define COLOR_GREY 0x8 // Alias for grey vs gray #define COLOR_BRIGHT_BLUE 0x9 #define COLOR_BRIGHT_GREEN 0xA -#define COLOR_LIME 0xA // Alias for lime vs bright green #define COLOR_AQUA 0xB #define COLOR_RED 0xC #define COLOR_PINK 0xD #define COLOR_YELLOW 0xE #define COLOR_WHITE 0xF +#define COLOR_BRIGHT_GRAY COLOR_BRIGHT_GREY // Alias for grey vs gray +#define COLOR_GREY COLOR_GRAY // Alias for grey vs gray +#define COLOR_LIME COLOR_BRIGHT_GREEN // Alias for lime vs bright green char* console_buffer; uint16_t console_pointer; uint8_t console_color; void console_init(); +void console_set_cursor_xy(uint8_t x,uint8_t y); void console_update_cursor(); void console_clear(); void console_set_colors(uint8_t fg, uint8_t bg); void console_set_color(uint8_t fg); -void console_print(char *string, ...); - +void console_print_char(const char c); +void console_print_string(const char *string); +void console_print(const char* format,...); +void console_print_num(uint32_t num, uint8_t base); +void console_scroll(); #endif
\ No newline at end of file |