diff options
Diffstat (limited to 'screen/console.c')
-rw-r--r-- | screen/console.c | 132 |
1 files changed, 114 insertions, 18 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 |