From 6577578051bfdc7feedc1a22951a1a8b291c1b4c Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Mon, 25 Apr 2016 01:16:30 +0200 Subject: [PATCH] Display: introduce display_writechar(). You see where the journey is going? This is the equivalent to the function to write a character to the serial line, so we can basically swap these two in other functions. --- display.c | 16 +++++++++++++++- display.h | 4 +++- display_ssd1306.c | 44 ++++++++++++++++++++------------------------ i2c_test.c | 2 +- 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/display.c b/display.c index e32ab19..0cca508 100644 --- a/display.c +++ b/display.c @@ -13,4 +13,18 @@ #include "display_ssd1306.c" #undef TEACUP_C_INCLUDE -/* No common code so far. */ + +#ifdef DISPLAY + +void display_writestr_P(PGM_P data_P) { + uint8_t r, i = 0; + + // Yes, this is *supposed* to be assignment rather than comparison, so we + // break when r is assigned zero. + while ((r = pgm_read_byte(&data_P[i]))) { + display_writechar(r); + i++; + } +} + +#endif /* DISPLAY */ diff --git a/display.h b/display.h index 7215c34..1eb63b1 100644 --- a/display.h +++ b/display.h @@ -36,6 +36,8 @@ void display_init(void); void display_clear(void); void display_set_cursor(uint8_t line, uint8_t column); -void display_text_P(PGM_P message_P); +void display_writechar(uint8_t data); + +void display_writestr_P(PGM_P data_P); #endif /* _DISPLAY_H */ diff --git a/display_ssd1306.c b/display_ssd1306.c index fd5b029..25d52e9 100644 --- a/display_ssd1306.c +++ b/display_ssd1306.c @@ -99,36 +99,32 @@ void display_set_cursor(uint8_t line, uint8_t column) { } /** - Prints the text at the current cursor position. + Prints a character at the current cursor position. - \param message Zero terminated string of the text to be displayed, stored - in program memory. + \param data The character to be displayed. */ -void display_text_P(PGM_P message) { - uint8_t i, index; +void display_writechar(uint8_t data) { + uint8_t i, index = data - 0x20; - // Render text to bitmap to display. + // Write pixels command. displaybus_write(0x40, 0); - while ((index = pgm_read_byte(message))) { - index -= 0x20; - // Send the character bitmap. - #ifdef FONT_IS_PROPORTIONAL - for (i = 0; i < pgm_read_byte(&font[index].columns); i++) { - #else - for (i = 0; i < FONT_COLUMNS; i++) { - #endif - displaybus_write(pgm_read_byte(&font[index].data[i]), 0); - } - // Send space between characters. - for (i = 0; i < FONT_SYMBOL_SPACE; i++) { - displaybus_write(0x00, 0); - } - - message++; + // Send the character bitmap. + #ifdef FONT_IS_PROPORTIONAL + for (i = 0; i < pgm_read_byte(&font[index].columns); i++) { + #else + for (i = 0; i < FONT_COLUMNS; i++) { + #endif + displaybus_write(pgm_read_byte(&font[index].data[i]), 0); + } + // Send space between characters. + for (i = 0; i < FONT_SYMBOL_SPACE; i++) { + // TODO: we finalise a I2C (or other) bus message after each character + // here because we have no idea on how many more are following. This + // is highly inefficient and makes the displaybus buffer almost + // pointless. + displaybus_write(0x00, (i == FONT_SYMBOL_SPACE - 1)); } - // Send another space for transmission end. - displaybus_write(0x00, 1); } #endif /* TEACUP_C_INCLUDE && DISPLAY_TYPE_SSD1306 */ diff --git a/i2c_test.c b/i2c_test.c index 7d74f75..abf196a 100644 --- a/i2c_test.c +++ b/i2c_test.c @@ -25,7 +25,7 @@ static void i2c_test(void) { 128 columns, so we offset by 32 columns to get it to the center. */ display_set_cursor(1, 32); - display_text_P(PSTR("Welcome to Teacup")); + display_writestr_P(PSTR("Welcome to Teacup")); } #endif /* I2C_TEST */