/*
長篇文之展示
ILI9341 規格:240 × 320 (TFT)
寬:240/8=30字,高:320/8=40字
中文字佔2BYTES
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "U8g2_for_Adafruit_GFX.h"
#define TFT_DC 21
#define TFT_CS 22
Adafruit_ILI9341 display = Adafruit_ILI9341(TFT_CS, TFT_DC);
U8G2_FOR_ADAFRUIT_GFX u8g2;
#define FONT u8g2_font_wqy16_t_gb2312b
const char c_str[] =
"《桃花庵歌》 唐寅\n\n"
"桃花塢里桃花庵,\n"
"桃花庵下桃花仙。\n"
"桃花仙人種桃樹,\n"
"又摘桃花換酒錢。\n"
"酒醒只在花前坐,\n"
"酒醉還來花下眠。\n"
"半醉半醒日復日,\n"
"花落花開年復年。\n"
"但愿老死花酒間,\n"
"不愿鞠躬車馬前。\n"
"車塵馬足顯者事,\n"
"酒盞花枝隱士緣。\n"
"若將顯者比隱士,\n"
"一在平地一在天。\n"
"若將花酒比車馬,\n"
"彼何碌碌我何閑。\n"
"世人笑我太瘋癲,\n"
"我笑他人看不穿。\n"
"不見五陵豪杰墓,\n"
"無花無酒鋤作田。\n";
char buf[48]; //每行最多8個字,每字2BYTE
uint8_t total_lines; //總行數
uint8_t line_cnt; //行計數器
uint8_t start_line=0; //開始的行數
uint8_t total_page; //總共要顯示的頁數
uint16_t height=display.height(); // height of the monitor
uint16_t width=display.width();
//計算唐詩的總列數,依每列最後的\n統計
uint8_t u8x8_GetStringLineCnt(const char *str)
{
char e;
uint8_t line_cnt = 1;
if ( str == NULL )
return 0;
for(;;)
{
e = *str;
if ( e == '\0' )
break;
str++;
if ( e == '\n' )
line_cnt++;
}
return line_cnt;
}
/* copy until first '\n' or '\0' in str */
/* Important: There is no string overflow check, ensure */
/* that the destination buffer is large enough */
void u8x8_CopyStringLine(char *dest, uint8_t line_idx, const char *str)
{
if ( dest == NULL )
return;
str = u8x8_GetStringLineStart( line_idx, str );
if ( str != NULL )
{
for(;;)
{
if ( *str == '\n' || *str == '\0' )
break;
*dest = *str;
dest++;
str++;
}
}
*dest = '\0';
}
void setup() {
display.begin();
display.setRotation(0);
display.fillScreen(ILI9341_BLACK);
u8g2.begin(display); // connect u8g2 procedures to Adafruit GFX
u8g2.setFont(FONT);
u8g2.setForegroundColor(ILI9341_YELLOW); // apply Adafruit GFX color
//計算長文的總行數
total_lines = u8x8_GetStringLineCnt(c_str);
total_page=total_lines/40; //每頁最多顯示40行
}
void loop() {
for(int p=0; p<total_page ;p++){
display.fillRect(0, 0, 319, 239, ILI9341_BLACK);
u8g2.setForegroundColor(ILI9341_WHITE);
line_cnt=p*40;
for( int i = line_cnt; i <line_cnt+ 40; i++ )
{
/* copy a line of the text to the local buffer */
u8x8_CopyStringLine(buf, line_cnt, c_str);
u8g2.drawUTF8(0, i%40, buf);
}
delay(2000);
}
}
const char *u8x8_GetStringLineStart(uint8_t line_idx, const char *str )
{
char e;
uint8_t line_cnt = 1;
if ( line_idx == 0 )
return str;
for(;;)
{
e = *str;
if ( e == '\0' )
break;
str++;
if ( e == '\n' )
{
if ( line_cnt == line_idx )
return str;
line_cnt++;
}
}
return NULL; /* line not found */
}