本项目可以在受限的显示范围内通过滚动显示,显示出更多文本内容,以下示例配合CardKB使用。实现效果:该显示的行为就像一个终端,在底部添加新文本,并在每一行中向上滚动。每显示一行会自动换行,可以使用M5StickC的任何方向。
实现思路:文本缓冲区用于存储要显示的文本。根据是以纵向还是横向使用显示器,文本缓冲区有5行或10行。新字符总是写在缓冲区的最后一行,因此文本像经典终端一样向上滚动。每行可能的字符数在字符之间变化。纵向模式下,一行中可容纳8至23个字符。典型的文本需要9到11个字符。在横向方向上,一行中包含19到50个字符。典型的文本需要21到24个字符。当要显示新字符时,它将在显示屏上绘制该功能,并检查该字符后面的位置是否在显示屏之外。
初始化非常简单。您只需要使用所需方向作为参数调用init函数即可: void tb_display_init(int ScreenRotation);
可以将单个字符或整个字符串写入缓冲区。提供以下功能: void tb_display_print_char(byte data);void tb_display_print_String(const char * s,int chr_delay = 0);
print_string函数具有用于延迟的可选参数,因此显示效果看起来像打字机。 使用全局变量tb_display_word_wrap可以打开或关闭自动换行功能。如果激活此功能,则该行将在最后一个未完成的单词之前换行,并且该单词将显示在新行中。 [mw_shl_code=arduino,true]#include <Arduino.h>
// M5StickC Library:
// Install for PlatformIO:
// pio lib install "M5StickC"
#include <M5StickC.h>
#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);
}
}
}
}
[/mw_shl_code]
|