diff --git a/README.md b/README.md index c4a63a5..e42b4d5 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,13 @@ Any orientation of the M5StickC can be used! ## How it works: -A text buffer is used to store the text which is to be displayed. Depending on whether the display is used in portrait or landscape orientation, the text buffer has 5 lines or 10 lines. +A text buffer is used to store the text which is to be displayed. With text size 2 (the default), depending on whether the display is used in portrait or landscape orientation, the text buffer has 5 lines or 10 lines. New characters are always written in the last line of the buffer, so that the text scrolls up like a classic terminal. The number of possible characters per line varies between characters. In the portrait orientation, between 8 and 23 characters fit in one line. A typical text needs 9 to 11 characters. In the landscape orientation between 19 and 50 characters fit in one line. A typical text requires between 21 and 24 characters. When a new character is to be displayed, it draws the function on the display and checks if the position behind the character is outside the display. If so, the line is automatically wrapped and the character is displayed as the first character in the new line. +With larger text sizes, fewer characters will fit per line, and fewer lines will fit on the screen. + An example can be viewed here: [Example Video](https://youtu.be/PCo_sT5_lpc) @@ -26,6 +28,11 @@ The initialization is very simple. You only need to call the init function with ```c++ void tb_display_init(int ScreenRotation); ``` +Or, to specify an alternative text size: +```c++ +void tb_display_init(int ScreenRotation, int TextSize) +``` + Either single characters can be written into the buffer, or a whole string. The following functions are available: ```c++ void tb_display_print_char(byte data); @@ -37,8 +44,7 @@ With the global variable tb_display_word_wrap the word-wrapping function can be ## Environment: -The files work fine with PlatformIO. For use with the Arduino IDE only really minor changes are required: -Change main.cpp to tb_display.ino and place the .cpp .h and .ino into a directory. +The files work fine with PlatformIO. For use with the Arduino IDE, point your IDE to the tb_display dir in lib ## Changelog @@ -52,3 +58,6 @@ Change main.cpp to tb_display.ino and place the .cpp .h and .ino into a director * Add a word wrapping fuction inside the print_char function * v1.3 * Bugfix if the character that causes a word wrap is a space character +* v1.4 + * Support arbitrary text size. + * Code restructuring for platformio conventions and Arduino compatibility. diff --git a/tb_display.cpp b/lib/tb_display/tb_display.cpp similarity index 78% rename from tb_display.cpp rename to lib/tb_display/tb_display.cpp index d69c2cc..88ad78c 100644 --- a/tb_display.cpp +++ b/lib/tb_display/tb_display.cpp @@ -1,271 +1,289 @@ -/****************************************************************************** - * tb_display.cpp - * Library for a simple text buffer scrolling display on the M5StickC. - * Hague Nusseck @ electricidea - * v1.3 04.Feb.2020 - * https://github.com/electricidea/M5StickC-TB_Display - * - * This library makes it easy to display texts on the M5StickC. - * The display behaves like a terminal: New text is added at the bottom. - * The text scrolls up with every new line. The lines are automatically wrapped. - * The display can be used in any orientation. - * - * Changelog: - * v1.0 = - initial version - * v1.1 = - Added delay parameter to tb_display_print_String function - * - Added text demo in Example (Button B on M5StickC) - * v1.2 = - Supress of space characters as first character on a new row - * after a new line - * - Add a word wrapping fuction inside the print_char function - * v1.3 = - Bugfix if the character that causes a word wrap is a space character - * - * Distributed as-is; no warranty is given. - ******************************************************************************/ - -#include -#include -#include "tb_display.h" - - -// TextSize 1 is very small on the display = hard to read -// Textsize 2 is good readable without the need of an microscope. -// This code only runs with text size 2! -#define TEXT_SIZE 2 -#define TEXT_HEIGHT 16 // Height of text to be printed -// Display size of M5StickC = 160x80 -// With TEXT_HEIGHT=16, the screen can display: -// 5 rows of text in portrait mode -// 10 rows of text in landscape mode - -// screen buffer for 10 rows of 60 characters max. -#define TEXT_BUFFER_HEIGHT_MAX 10 -#define TEXT_BUFFER_LINE_LENGTH_MAX 60 -char text_buffer[TEXT_BUFFER_HEIGHT_MAX][TEXT_BUFFER_LINE_LENGTH_MAX]; - -int text_buffer_height; -int text_buffer_line_length; -int text_buffer_write_pointer_x; -int text_buffer_write_pointer_y; -int text_buffer_read_pointer; -// with M5.Lcd.setRotation(1) -// the position 0,0 is the upper left corner -// starting a bit more right... -#define SCREEN_XSTARTPOS 5 -int screen_xpos = SCREEN_XSTARTPOS; -// start writing at the last line -int screen_ypos; -// maximum width of the screen -int screen_max; - -// Enable or disable Waord Wrap -boolean tb_display_word_wrap = true; - -// ============================================================= -// Initialization of the Text Buffer and Screen -// ScreenRotation values: -// 1 = Button right -// 2 = Button above -// 3 = Button left -// 4 = Button below -// Display size of M5StickC = 160x80pixel -// With TEXT_HEIGHT=16, the screen can display: -// 5 rows of text in landscape mode -// 10 rows of text in portrait mode -// ============================================================= -void tb_display_init(int ScreenRotation){ - M5.Lcd.setRotation(ScreenRotation); - switch (ScreenRotation) { - case 1: case 3: { - // 5 rows of text in landscape mode - text_buffer_height = 5; - text_buffer_line_length = 60; - // width of the screen in landscape mode is 160 pixel - // A small margin on the right side prevent false print results - screen_max = 160-2; - break; - } - case 2: case 4: { - // 10 rows of text in portrait mode - text_buffer_height = 10; - text_buffer_line_length = 30; - // width of the screen in portrait mode is 80 pixel - // A small margin on the right side prevent false print results - screen_max = 80-2; - break; - } - default: { - break; - } - } - tb_display_clear(); - tb_display_show(); -} - -// ============================================================= -// clear the text buffer -// without refreshing the screen -// call tb_display_show(); to clear the screen -// ============================================================= -void tb_display_clear(){ - for(int line=0; line= text_buffer_height) - text_buffer_write_pointer_y = 0; - if(text_buffer_read_pointer >= text_buffer_height) - text_buffer_read_pointer = 0; - // clear the actual new line for writing (first character a null terminator) - text_buffer[text_buffer_write_pointer_y][text_buffer_write_pointer_x] = '\0'; - tb_display_show(); -} - -// ============================================================= -// print a single character -// the character is added to the text buffer and -// directly printed on the screen. -// The text is automatically wrapped if longer than the display -// example: -// tb_display_print_char('X'); -// ============================================================= -void tb_display_print_char(byte data){ - // check for LF for new line - if (data == '\n') { - // last character in the text_buffer line should be always a null terminator - text_buffer[text_buffer_write_pointer_y][text_buffer_write_pointer_x] = '\0'; - tb_display_new_line(); - } - // only 'printable' characters - if (data > 31 && data < 128) { - // print the character and get the new xpos - screen_xpos += M5.Lcd.drawChar(data,screen_xpos,screen_ypos,TEXT_SIZE); - // if maximum number of characters reached - if(text_buffer_write_pointer_x >= text_buffer_line_length-1){ - tb_display_new_line(); - // draw the character again because it was out of the screen last time - screen_xpos += M5.Lcd.drawChar(data,screen_xpos,screen_ypos,TEXT_SIZE); - } - // or if line wrap is reached - if(screen_xpos >= screen_max) { - // prepare for Word-Wrap stuff... - // the buffer for storing the last word content - char Char_buffer[TEXT_BUFFER_LINE_LENGTH_MAX]; - int n = 1; - Char_buffer[0] = data; - Char_buffer[n] = '\0'; - // if Word-Wrap, go backwards and get the last "word" by finding the - // last space character: - if(tb_display_word_wrap){ - int test_pos = text_buffer_write_pointer_x-1; - // get backwards and search a space character - while(test_pos > 0 && text_buffer[text_buffer_write_pointer_y][test_pos] != ' '){ - // store all the characters on the way back to the last space character - Char_buffer[n] = text_buffer[text_buffer_write_pointer_y][test_pos]; - test_pos--; - n++; - Char_buffer[n] = '\0'; - } - // if there was no space character in the row, Word-Wrap is not possible - if(test_pos == 0) { - // don't use the buffer but draw the character passed to the function - n = 1; - } else { - // otherwise use the buffer to print the last found characters of the word - // but only, if the charachter that causes a word wrap is not a space character - if(data != ' '){ - // place a \0 at the position of the found space so that the drawing fuction ends here - text_buffer[text_buffer_write_pointer_y][test_pos] = '\0'; - } - } - } - tb_display_new_line(); - // icharacter passed to the function is a space character, then don't display - // it as the first character of the new line - if(data == ' ') - // don't use the buffer at all - n = 0; - n--; - while(n >= 0){ - // draw the characters from the buffer back on the screen - screen_xpos += M5.Lcd.drawChar(Char_buffer[n],screen_xpos,screen_ypos,TEXT_SIZE); - // write the characters into the screen buffer of the new line - text_buffer[text_buffer_write_pointer_y][text_buffer_write_pointer_x] = Char_buffer[n]; - text_buffer_write_pointer_x++; - n--; - } - text_buffer[text_buffer_write_pointer_y][text_buffer_write_pointer_x] = '\0'; - } else { - // write the character into the screen buffer - text_buffer[text_buffer_write_pointer_y][text_buffer_write_pointer_x] = data; - text_buffer_write_pointer_x++; - // following character a null terminator to clear the old characters of the line - text_buffer[text_buffer_write_pointer_y][text_buffer_write_pointer_x] = '\0'; - } - } -} - -// ============================================================= -// print a string -// The string is added to the text buffer and directly printed -// on the screen. -// The otional parameter "chr_delay" allows a "character by character" -// processing of the String. Then, it looks like Teletype or Typewriter -// The delay is in milliseconds. -// The text is automatically wrapped if longer than the display -// example: -// tb_display_print_String("a new line\n"); -// tb_display_print_String("one\nand two lines\n"); -// -// char String_buffer[128]; -// snprintf(String_buffer, sizeof(String_buffer), "\nthe value: %i",value); -// tb_display_print_String(String_buffer); -// -// std::string msg; -// msg = ss.str(); -// const char * c_msg = msg.c_str(); -// tb_display_print_String(c_msg); -// ============================================================= -void tb_display_print_String(const char *s, int chr_delay){ - while(*s != 0){ - tb_display_print_char(*s++); - if(chr_delay > 0) - delay(chr_delay); - } -} - +/****************************************************************************** + * tb_display.cpp + * Library for a simple text buffer scrolling display on the M5StickC. + * Hague Nusseck @ electricidea + * v1.3 04.Feb.2020 + * https://github.com/electricidea/M5StickC-TB_Display + * + * This library makes it easy to display texts on the M5StickC. + * The display behaves like a terminal: New text is added at the bottom. + * The text scrolls up with every new line. The lines are automatically wrapped. + * The display can be used in any orientation. + * + * Changelog: + * v1.0 = - initial version + * v1.1 = - Added delay parameter to tb_display_print_String function + * - Added text demo in Example (Button B on M5StickC) + * v1.2 = - Supress of space characters as first character on a new row + * after a new line + * - Add a word wrapping fuction inside the print_char function + * v1.3 = - Bugfix if the character that causes a word wrap is a space character + * + * Distributed as-is; no warranty is given. + ******************************************************************************/ + +#include +#include +#include "tb_display.h" + + + +// TextSize 1 is very small on the display = hard to read +// Textsize 2 is good readable without the need of an microscope. +// Each size is 8 px taller. 8, 16, 24... +// Display size of M5StickC = 160x80 +// With TEXT_HEIGHT=16, the screen can display: +// 5 rows of text in portrait mode +// 10 rows of text in landscape mode + +// screen buffer for 10 rows of 60 characters max. +#define TEXT_BUFFER_HEIGHT_MAX 10 +#define TEXT_BUFFER_LINE_LENGTH_MAX 60 +char text_buffer[TEXT_BUFFER_HEIGHT_MAX][TEXT_BUFFER_LINE_LENGTH_MAX]; + +int text_size = 2; // 1, 2, 3, etc. Each corresponds to 8px taller: 8, 16, 24 +int text_buffer_height; +int text_buffer_line_length; +int text_buffer_write_pointer_x; +int text_buffer_write_pointer_y; +int text_buffer_read_pointer; +// with M5.Lcd.setRotation(1) +// the position 0,0 is the upper left corner +// starting a bit more right... +#define SCREEN_XSTARTPOS 5 +int screen_xpos = SCREEN_XSTARTPOS; +// start writing at the last line +int screen_ypos; +// maximum width of the screen +int screen_max; + +// Enable or disable Word Wrap +boolean tb_display_word_wrap = true; + +uint32_t tb_display_text_color = TFT_WHITE; +uint32_t tb_display_background_color = TFT_BLACK; + +// ============================================================= +// Initialization of the Text Buffer and Screen +// ScreenRotation values: +// 1 = Button right +// 2 = Button above +// 3 = Button left +// 4 = Button below +// +// TextSize values: 1, 2, 3... +// each value corresponds to 8px taller: 8, 16, 24... +// +// Display size of M5StickC = 160x80pixel +// With TEXT_HEIGHT=16, the screen can display: +// 5 rows of text in landscape mode +// 10 rows of text in portrait mode +// ============================================================= +void tb_display_init(int ScreenRotation, int TextSize){ + text_size = TextSize; + M5.Lcd.setRotation(ScreenRotation); + M5.Lcd.setTextSize(text_size); + float lcdHeight = M5.Lcd.height(); + float lcdWidth = M5.Lcd.width(); + text_buffer_height = ceil(lcdHeight / (text_size * 8)); + text_buffer_line_length = 60; + // width of the screen in landscape mode is 160 pixel + // A small margin on the right side prevent false print results + screen_max = lcdWidth-2; + tb_display_clear(); + tb_display_show(); +} + +// ============================================================= +// Initialization of the Text Buffer and Screen +// ScreenRotation values: +// 1 = Button right +// 2 = Button above +// 3 = Button left +// 4 = Button below +// Display size of M5StickC = 160x80pixel +// With TEXT_HEIGHT=16, the screen can display: +// 5 rows of text in landscape mode +// 10 rows of text in portrait mode +// ============================================================= +void tb_display_init(int ScreenRotation){ + tb_display_init(ScreenRotation, 2); +} + +// ============================================================= +// clear the text buffer +// without refreshing the screen +// call tb_display_show(); to clear the screen +// ============================================================= +void tb_display_clear(){ + for(int line=0; line= text_buffer_height) + text_buffer_write_pointer_y = 0; + if(text_buffer_read_pointer >= text_buffer_height) + text_buffer_read_pointer = 0; + // clear the actual new line for writing (first character a null terminator) + text_buffer[text_buffer_write_pointer_y][text_buffer_write_pointer_x] = '\0'; + tb_display_show(); +} + +// ============================================================= +// print a single character +// the character is added to the text buffer and +// directly printed on the screen. +// The text is automatically wrapped if longer than the display +// example: +// tb_display_print_char('X'); +// ============================================================= +void tb_display_print_char(byte data){ + // check for LF for new line + if (data == '\n') { + // last character in the text_buffer line should be always a null terminator + text_buffer[text_buffer_write_pointer_y][text_buffer_write_pointer_x] = '\0'; + tb_display_new_line(); + } + // only 'printable' characters + if (data > 31 && data < 128) { + // print the character and get the new xpos + screen_xpos += drawChar(data, screen_xpos, screen_ypos, text_size); + // if maximum number of characters reached + if(text_buffer_write_pointer_x >= text_buffer_line_length-1){ + tb_display_new_line(); + // draw the character again because it was out of the screen last time + screen_xpos += drawChar(data,screen_xpos,screen_ypos,text_size); + } + // or if line wrap is reached + if(screen_xpos >= screen_max) { + // prepare for Word-Wrap stuff... + // the buffer for storing the last word content + char Char_buffer[TEXT_BUFFER_LINE_LENGTH_MAX]; + int n = 1; + Char_buffer[0] = data; + Char_buffer[n] = '\0'; + // if Word-Wrap, go backwards and get the last "word" by finding the + // last space character: + if(tb_display_word_wrap){ + int test_pos = text_buffer_write_pointer_x-1; + // get backwards and search a space character + while(test_pos > 0 && text_buffer[text_buffer_write_pointer_y][test_pos] != ' '){ + // store all the characters on the way back to the last space character + Char_buffer[n] = text_buffer[text_buffer_write_pointer_y][test_pos]; + test_pos--; + n++; + Char_buffer[n] = '\0'; + } + // if there was no space character in the row, Word-Wrap is not possible + if(test_pos == 0) { + // don't use the buffer but draw the character passed to the function + n = 1; + } else { + // otherwise use the buffer to print the last found characters of the word + // but only, if the charachter that causes a word wrap is not a space character + if(data != ' '){ + // place a \0 at the position of the found space so that the drawing fuction ends here + text_buffer[text_buffer_write_pointer_y][test_pos] = '\0'; + } + } + } + tb_display_new_line(); + // if character passed to the function is a space character, then don't display + // it as the first character of the new line + if(data == ' ') + // don't use the buffer at all + n = 0; + n--; + while(n >= 0){ + // draw the characters from the buffer back on the screen + screen_xpos += drawChar(Char_buffer[n],screen_xpos,screen_ypos,text_size); + // write the characters into the screen buffer of the new line + text_buffer[text_buffer_write_pointer_y][text_buffer_write_pointer_x] = Char_buffer[n]; + text_buffer_write_pointer_x++; + n--; + } + text_buffer[text_buffer_write_pointer_y][text_buffer_write_pointer_x] = '\0'; + } else { + // write the character into the screen buffer + text_buffer[text_buffer_write_pointer_y][text_buffer_write_pointer_x] = data; + text_buffer_write_pointer_x++; + // following character a null terminator to clear the old characters of the line + text_buffer[text_buffer_write_pointer_y][text_buffer_write_pointer_x] = '\0'; + } + } +} + +// ============================================================= +// print a string +// The string is added to the text buffer and directly printed +// on the screen. +// The optional parameter "chr_delay" allows a "character by character" +// processing of the String. Then, it looks like Teletype or Typewriter +// The delay is in milliseconds. +// The text is automatically wrapped if longer than the display +// example: +// tb_display_print_String("a new line\n"); +// tb_display_print_String("one\nand two lines\n"); +// +// char String_buffer[128]; +// snprintf(String_buffer, sizeof(String_buffer), "\nthe value: %i",value); +// tb_display_print_String(String_buffer); +// +// std::string msg; +// msg = ss.str(); +// const char * c_msg = msg.c_str(); +// tb_display_print_String(c_msg); +// ============================================================= +void tb_display_print_String(const char *s, int chr_delay){ + while(*s != 0){ + tb_display_print_char(*s++); + if(chr_delay > 0) + delay(chr_delay); + } +} diff --git a/tb_display.h b/lib/tb_display/tb_display.h similarity index 84% rename from tb_display.h rename to lib/tb_display/tb_display.h index 5ef9a89..f13c487 100644 --- a/tb_display.h +++ b/lib/tb_display/tb_display.h @@ -1,97 +1,112 @@ - -/****************************************************************************** - * tb_display.h - * Library for a simple text buffer scrolling display on the M5StickC. - * Hague Nusseck @ electricidea - * v1.3 04.Feb.2020 - * https://github.com/electricidea/M5StickC-TB_Display - * - * This library makes it easy to display texts on the M5StickC. - * The display behaves like a terminal: New text is added at the bottom. - * The text scrolls up with every new line. The lines are automatically wrapped. - * The display can be used in any orientation. - * - * Changelog: - * v1.0 = - initial version - * v1.1 = - Added delay parameter to tb_display_print_String function - * - Added text demo in Example (Button B on M5StickC) - * v1.2 = - Supress of space characters as first character on a new row - * after a new line - * - Add a word wrapping fuction inside the print_char function - * v1.3 = - Bugfix if the character that causes a word wrap is a space character - * - * Distributed as-is; no warranty is given. - ******************************************************************************/ - -// Enable or disable Waord Wrap -extern boolean tb_display_word_wrap; - -// ============================================================= -// tb_display_init(int ScreenRotation); -// Initialization of the Text Buffer and Screen -// ScreenRotation values: -// 1 = Button right -// 2 = Button above -// 3 = Button left -// 4 = Button below -// Display size of M5StickC = 160x80pixel -// With TEXT_HEIGHT=16, the screen can display: -// 5 rows of text in landscape mode -// 10 rows of text in portrait mode -// ============================================================= -void tb_display_init(int ScreenRotation); - -// ============================================================= -// tb_display_show(); -// clear the screen and display the text buffer -// ============================================================= -void tb_display_show(); - -// ============================================================= -// tb_display_clear(); -// clear the text buffer -// without refreshing the screen -// call tb_display_show() afterwards to clear the screen -// ============================================================= -void tb_display_clear(); - -// ============================================================= -// tb_display_new_line(); -// creates a new line and scroll the display upwards -// ============================================================= -void tb_display_new_line(); - -// ============================================================= -// tb_display_print_String(const char *s, int chr_delay = 0); -// print a string -// The string is added to the text buffer and directly printed -// on the screen. -// The otional parameter "chr_delay" allows a "character by character" -// processing of the String. Then, it looks like Teletype or Typewriter -// The delay is in milliseconds. -// The text is automatically wrapped if longer than the display -// example: -// tb_display_print_String("a new line\n"); -// tb_display_print_String("one\nand two lines\n"); -// -// char String_buffer[128]; -// snprintf(String_buffer, sizeof(String_buffer), "\nthe value: %i",value); -// tb_display_print_String(String_buffer); -// -// std::string msg; -// msg = ss.str(); -// const char * c_msg = msg.c_str(); -// tb_display_print_String(c_msg); -// ============================================================= -void tb_display_print_String(const char *s, int chr_delay = 0); - -// ============================================================= -// tb_display_print_char(byte data); -// print a single character -// the character is added to the text buffer and -// directly printed on the screen. -// The text is automatically wrapped if longer than the display -// example: -// tb_display_print_char('X'); -// ============================================================= +/****************************************************************************** + * tb_display.h + * Library for a simple text buffer scrolling display on the M5StickC. + * Hague Nusseck @ electricidea + * v1.3 04.Feb.2020 + * https://github.com/electricidea/M5StickC-TB_Display + * + * This library makes it easy to display texts on the M5StickC. + * The display behaves like a terminal: New text is added at the bottom. + * The text scrolls up with every new line. The lines are automatically wrapped. + * The display can be used in any orientation. + * + * Changelog: + * v1.0 = - initial version + * v1.1 = - Added delay parameter to tb_display_print_String function + * - Added text demo in Example (Button B on M5StickC) + * v1.2 = - Supress of space characters as first character on a new row + * after a new line + * - Add a word wrapping fuction inside the print_char function + * v1.3 = - Bugfix if the character that causes a word wrap is a space character + * + * Distributed as-is; no warranty is given. + ******************************************************************************/ + +// Enable or disable Word Wrap +extern boolean tb_display_word_wrap; + +// ============================================================= +// tb_display_init(int ScreenRotation); +// Initialization of the Text Buffer and Screen +// ScreenRotation values: +// 1 = Button right +// 2 = Button above +// 3 = Button left +// 4 = Button below +// Display size of M5StickC = 160x80pixel +// With TextSize = 2, the screen can display: +// 5 rows of text in landscape mode +// 10 rows of text in portrait mode +// ============================================================= +void tb_display_init(int ScreenRotation); + +// ============================================================= +// tb_display_init(int ScreenRotation); +// Initialization of the Text Buffer and Screen +// ScreenRotation values: +// 1 = Button right +// 2 = Button above +// 3 = Button left +// 4 = Button below +// Display size of M5StickC = 160x80pixel +// TextSize 1, 2, 3... corresponds to character height 8, 16, 24... +// With TextSize = 2, the screen can display: +// 5 rows of text in landscape mode +// 10 rows of text in portrait mode +// ============================================================= +void tb_display_init(int ScreenRotation, int TextSize); + +// ============================================================= +// tb_display_show(); +// clear the screen and display the text buffer +// ============================================================= +void tb_display_show(); + +// ============================================================= +// tb_display_clear(); +// clear the text buffer +// without refreshing the screen +// call tb_display_show() afterwards to clear the screen +// ============================================================= +void tb_display_clear(); + +// ============================================================= +// tb_display_new_line(); +// creates a new line and scroll the display upwards +// ============================================================= +void tb_display_new_line(); + +// ============================================================= +// tb_display_print_String(const char *s, int chr_delay = 0); +// print a string +// The string is added to the text buffer and directly printed +// on the screen. +// The otional parameter "chr_delay" allows a "character by character" +// processing of the String. Then, it looks like Teletype or Typewriter +// The delay is in milliseconds. +// The text is automatically wrapped if longer than the display +// example: +// tb_display_print_String("a new line\n"); +// tb_display_print_String("one\nand two lines\n"); +// +// char String_buffer[128]; +// snprintf(String_buffer, sizeof(String_buffer), "\nthe value: %i",value); +// tb_display_print_String(String_buffer); +// +// std::string msg; +// msg = ss.str(); +// const char * c_msg = msg.c_str(); +// tb_display_print_String(c_msg); +// ============================================================= +void tb_display_print_String(const char *s, int chr_delay = 0); + +// ============================================================= +// tb_display_print_char(byte data); +// print a single character +// the character is added to the text buffer and +// directly printed on the screen. +// The text is automatically wrapped if longer than the display +// example: +// tb_display_print_char('X'); +// ============================================================= void tb_display_print_char(byte data); \ No newline at end of file diff --git a/main.cpp b/src/main.cpp similarity index 96% rename from main.cpp rename to src/main.cpp index 7cfdd26..b810117 100644 --- a/main.cpp +++ b/src/main.cpp @@ -1,139 +1,139 @@ -/****************************************************************************** - * Example for tb_display - * - * Library for a simple text buffer scrolling display on the M5StickC. - * Hague Nusseck @ electricidea - * v1.3 04.Feb.2020 - * https://github.com/electricidea/M5StickC-TB_Display - * - * This library makes it easy to display texts on the M5StickC. - * The display behaves like a terminal: New text is added at the bottom. - * The text scrolls up with every new line. The lines are automatically wrapped. - * The display can be used in any orientation. - * - * This example shows characters from the serial port on the M5StickC display. - * If a Keyboard-Hat is connected, also the characters from the Keyboard - * are shown on the display. - * - * Changelog: - * v1.0 = - initial version - * v1.1 = - Added delay parameter to tb_display_print_String function - * - Added text demo in Example (Button B on M5StickC) - * v1.2 = - Supress of space characters as first character on a new row - * after a new line - * - Add a word wrapping fuction inside the print_char function - * v1.3 = - Bugfix if the character that causes a word wrap is a space character - * - * Distributed as-is; no warranty is given. - ******************************************************************************/ -#include - -// M5StickC Library: -// Install for PlatformIO: -// pio lib install "M5StickC" -#include - -#include "tb_display.h" - -// I2C Adress of the Keyboard Hat -#define CARDKB_ADDR 0x5F - -// Display brightness level -// possible values: 7 - 15 -uint8_t screen_brightness = 10; - -// scren Rotation values: -// 1 = Button right -// 2 = Button above -// 3 = Button left -// 4 = Button below -int screen_orientation = 3; - - -void setup() { - // initialize the M5Stack object - m5.begin(); - // initialize I2C for the Keyboard Hat (not required) - Wire.begin(0, 26); - // set screen brightness - M5.Axp.ScreenBreath(screen_brightness); - - // print a welcome message over serial porta - Serial.println("==================="); - Serial.println(" M5StickC"); - Serial.println("Textbuffer Display"); - Serial.println(" 04.02.2020 v1.3"); - Serial.println("==================="); - - // init the text buffer display and print welcome text on the display - tb_display_init(screen_orientation); - tb_display_print_String(" M5StickC\n\n Textbuffer Display\n\n"); -} - - -void loop() { - M5.update(); - - // change the display orientation if Button A is pressed - if (M5.BtnA.wasPressed()){ - screen_orientation++; - if(screen_orientation > 4) - screen_orientation = 1; - // init the text buffer display with the new orientation - tb_display_init(screen_orientation); - // different text alignment for landscape or portrait mode - switch (screen_orientation) { - case 1: case 3: { - tb_display_print_String(" M5StickC\n\n Textbuffer Display\n\n"); - break; - } - case 2: case 4: { - tb_display_print_String(" M5StickC\n\nTextbuffer\n Display\n\n\n\n\n"); - break; - } - default: { - break; - } - } - } - - // Display a long Text if Button B is pressed - if (M5.BtnB.wasPressed()){ - // note: - // with 85ms Character delay, the display looks more - // like Teletype or a typewriter - tb_display_word_wrap = !tb_display_word_wrap; - if(tb_display_word_wrap) - tb_display_print_String("\n\nwwrap ON\n\n"); - else - tb_display_print_String("\n\nwwrap OFF\n\n"); - tb_display_print_String("The quick brown fox jumps over the lazy dog and was surprised that he used all letters of the alphabet.", 85); - } - - // check for serial input and print the received characters - while(Serial.available() > 0){ - char data = Serial.read(); - tb_display_print_char(data); - Serial.write(data); - } - - - // check for input from the Keyboard Hat and print the received characters - Wire.requestFrom(CARDKB_ADDR, 1); - while (Wire.available()) - { - char c = Wire.read(); // receive a byte as characterif - if (c != 0) - { - if (c == 13) { //0x0D = CR = '\r' - // Map CR to LF (0x0A) - tb_display_print_char('\n'); - Serial.write('\n'); - } else { - tb_display_print_char(c); - Serial.write(c); - } - } - } -} - +/****************************************************************************** + * Example for tb_display + * + * Library for a simple text buffer scrolling display on the M5StickC. + * Hague Nusseck @ electricidea + * v1.3 04.Feb.2020 + * https://github.com/electricidea/M5StickC-TB_Display + * + * This library makes it easy to display texts on the M5StickC. + * The display behaves like a terminal: New text is added at the bottom. + * The text scrolls up with every new line. The lines are automatically wrapped. + * The display can be used in any orientation. + * + * This example shows characters from the serial port on the M5StickC display. + * If a Keyboard-Hat is connected, also the characters from the Keyboard + * are shown on the display. + * + * Changelog: + * v1.0 = - initial version + * v1.1 = - Added delay parameter to tb_display_print_String function + * - Added text demo in Example (Button B on M5StickC) + * v1.2 = - Supress of space characters as first character on a new row + * after a new line + * - Add a word wrapping fuction inside the print_char function + * v1.3 = - Bugfix if the character that causes a word wrap is a space character + * + * Distributed as-is; no warranty is given. + ******************************************************************************/ +#include + +// M5StickC Library: +// Install for PlatformIO: +// pio lib install "M5StickC" +#include + +#include "tb_display.h" + +// I2C Adress of the Keyboard Hat +#define CARDKB_ADDR 0x5F + +// Display brightness level +// possible values: 7 - 15 +uint8_t screen_brightness = 10; + +// scren Rotation values: +// 1 = Button right +// 2 = Button above +// 3 = Button left +// 4 = Button below +int screen_orientation = 3; + + +void setup() { + // initialize the M5Stack object + m5.begin(); + // initialize I2C for the Keyboard Hat (not required) + Wire.begin(0, 26); + // set screen brightness + M5.Axp.ScreenBreath(screen_brightness); + + // print a welcome message over serial porta + Serial.println("==================="); + Serial.println(" M5StickC"); + Serial.println("Textbuffer Display"); + Serial.println(" 04.02.2020 v1.3"); + Serial.println("==================="); + + // init the text buffer display and print welcome text on the display + tb_display_init(screen_orientation); + tb_display_print_String(" M5StickC\n\n Textbuffer Display\n\n"); +} + + +void loop() { + M5.update(); + + // change the display orientation if Button A is pressed + if (M5.BtnA.wasPressed()){ + screen_orientation++; + if(screen_orientation > 4) + screen_orientation = 1; + // init the text buffer display with the new orientation + tb_display_init(screen_orientation); + // different text alignment for landscape or portrait mode + switch (screen_orientation) { + case 1: case 3: { + tb_display_print_String(" M5StickC\n\n Textbuffer Display\n\n"); + break; + } + case 2: case 4: { + tb_display_print_String(" M5StickC\n\nTextbuffer\n Display\n\n\n\n\n"); + break; + } + default: { + break; + } + } + } + + // Display a long Text if Button B is pressed + if (M5.BtnB.wasPressed()){ + // note: + // with 85ms Character delay, the display looks more + // like Teletype or a typewriter + tb_display_word_wrap = !tb_display_word_wrap; + if(tb_display_word_wrap) + tb_display_print_String("\n\nwwrap ON\n\n"); + else + tb_display_print_String("\n\nwwrap OFF\n\n"); + tb_display_print_String("The quick brown fox jumps over the lazy dog and was surprised that he used all letters of the alphabet.", 85); + } + + // check for serial input and print the received characters + while(Serial.available() > 0){ + char data = Serial.read(); + tb_display_print_char(data); + Serial.write(data); + } + + + // check for input from the Keyboard Hat and print the received characters + Wire.requestFrom(CARDKB_ADDR, 1); + while (Wire.available()) + { + char c = Wire.read(); // receive a byte as characterif + if (c != 0) + { + if (c == 13) { //0x0D = CR = '\r' + // Map CR to LF (0x0A) + tb_display_print_char('\n'); + Serial.write('\n'); + } else { + tb_display_print_char(c); + Serial.write(c); + } + } + } +} +