2022-08-21 08:48:54 +00:00
# include "epd2in7.h"
# include "epdpaint.h"
// b/w e-Paper
# define COLORED 0
# define UNCOLORED 1
Epd epd ;
// style of the frame shown
2022-08-21 15:04:44 +00:00
sFONT TITLE_FONT = Font20 ;
2022-08-21 08:48:54 +00:00
char TITLE_TEXT [ ] = " Losung heute " ;
2022-08-21 15:04:44 +00:00
sFONT TEXT_FONT = Font12 ; // for the daily text
int TEXT_WIDTH = 168 ; // width of the box around the text; should be a multiple of the width of the font (eg. Font8 => multiple of 8)
int TEXT_PADDING_TOP = 40 ;
int TEXT_PADDING_BETWEEN_BLOCKS = 20 ; // pixels between the lines
int TEXT_PADDING_BETWEEN_LINES = 1 ; // pixels between the lines
sFONT SOURCE_FONT = Font8 ; // for the position in bible
String pad ( String text , int pad_length , char pad_letter ) {
if ( text . length ( ) > = pad_length ) return text ; // if theres nothing to fill, return the text unmodified
for ( int i = 0 ; text . length ( ) < pad_length ; i + + ) text + = pad_letter ; // fill with pad_letter until the text is as long as padlength specifies
return text ;
}
String pad ( String text , int pad_length ) { return pad ( text , pad_length , ' ' ) ; } // just a fallback for pad_letter
/* Tell how many rows of text will be needed at given font-width and box-width for the text given */
int getMaxRows ( String text , int width , int letter_width ) {
int lettersPerRow = width / letter_width ;
float maxRows = float ( text . length ( ) ) / float ( lettersPerRow ) ;
int maxRowsFloor = text . length ( ) / lettersPerRow ;
if ( maxRows > maxRowsFloor ) maxRows = maxRowsFloor + 1.0 ; // if maxRows is e.g. 5.2, write the next-bigger integer into maxRows
return maxRows ;
}
/* text_of_row_x tells which row shall be given, 0 for first row */
String getRow ( String text , int width , int letter_width , int text_of_row_x ) { // width should be a multiple of letter_width to fit perfect
// calculate needed vars
int lettersPerRow = width / letter_width ;
int maxRows = getMaxRows ( text , width , letter_width ) ;
text = pad ( text , maxRows * lettersPerRow , ' ' ) ;
// some tests
if ( text_of_row_x > maxRows ) return String ( ) ;
if ( text_of_row_x < 0 ) return String ( ) ;
return text . substring ( lettersPerRow * text_of_row_x , lettersPerRow * ( text_of_row_x + 1 ) ) ;
}
// remove special characters (ä,ö,ü,ß)
String removeSpecialChars ( String text ) {
text . replace ( " ä " , " ae " ) ;
text . replace ( " Ä " , " AE " ) ;
text . replace ( " ö " , " oe " ) ;
text . replace ( " Ö " , " OE " ) ;
text . replace ( " ü " , " ue " ) ;
text . replace ( " Ü " , " UE " ) ;
text . replace ( " ß " , " ss " ) ;
text . replace ( " ẞ " , " SS " ) ;
return text ;
}
// show the daily text on the epd
void showDailyText ( String losung , String lehrtext , String losungPosition , String lehrtextPosition ) {
// clean the strings for
losung = removeSpecialChars ( losung ) ;
lehrtext = removeSpecialChars ( lehrtext ) ;
Serial . println ( lehrtext ) ;
// position vars
int text_x = 0 ;
int text_y = 0 ;
2022-08-21 08:48:54 +00:00
/* This clears the SRAM of the e-paper display and displays it */
epd . ClearFrame ( ) ;
epd . DisplayFrame ( ) ;
// now the drawing init
unsigned char image [ 2048 ] ;
2022-08-21 15:04:44 +00:00
Paint paint ( image , 176 , TITLE_FONT . Height + 2 ) ; // width should be the multiple of 8
2022-08-21 08:48:54 +00:00
2022-08-21 15:04:44 +00:00
// display title
2022-08-21 08:48:54 +00:00
paint . Clear ( UNCOLORED ) ;
int startTitleAtX = round ( ( paint . GetWidth ( ) - strlen ( TITLE_TEXT ) * TITLE_FONT . Width ) / 2 ) ;
paint . DrawStringAt ( startTitleAtX , 2 , TITLE_TEXT , & TITLE_FONT , COLORED ) ;
paint . DrawHorizontalLine ( startTitleAtX + 15 , TITLE_FONT . Height + 1 , paint . GetWidth ( ) - ( startTitleAtX + 15 ) * 2 , COLORED ) ;
2022-08-21 15:04:44 +00:00
epd . TransmitPartialData ( paint . GetImage ( ) , text_x , text_y , paint . GetWidth ( ) , paint . GetHeight ( ) ) ;
text_y + = TITLE_FONT . Height + 2 ;
// Losung
paint . SetWidth ( TEXT_WIDTH ) ;
paint . SetHeight ( TEXT_FONT . Height ) ;
text_y + = TEXT_PADDING_TOP ;
for ( int row = 0 ; row < getMaxRows ( losung , TEXT_WIDTH , TEXT_FONT . Width ) ; row + + ) {
String text_of_row = getRow ( losung , TEXT_WIDTH , TEXT_FONT . Width , row ) ;
paint . Clear ( COLORED ) ;
paint . DrawStringAt ( 0 , 0 , text_of_row . c_str ( ) , & TEXT_FONT , UNCOLORED ) ;
epd . TransmitPartialData ( paint . GetImage ( ) , EPD_WIDTH - TEXT_WIDTH , text_y , paint . GetWidth ( ) , paint . GetHeight ( ) ) ;
text_y + = paint . GetHeight ( ) ;
text_y + = TEXT_PADDING_BETWEEN_LINES ;
}
// Losung bible position
paint . SetWidth ( losungPosition . length ( ) * SOURCE_FONT . Width ) ;
paint . SetHeight ( SOURCE_FONT . Height ) ;
paint . Clear ( UNCOLORED ) ;
paint . DrawStringAt ( 0 , 0 , losungPosition . c_str ( ) , & SOURCE_FONT , COLORED ) ;
epd . TransmitPartialData ( paint . GetImage ( ) , EPD_WIDTH - losungPosition . length ( ) * SOURCE_FONT . Width , text_y , paint . GetWidth ( ) , paint . GetHeight ( ) ) ;
// Lehrtext
paint . SetWidth ( TEXT_WIDTH ) ; // text block width
paint . SetHeight ( TEXT_FONT . Height ) ;
text_y + = TEXT_PADDING_BETWEEN_BLOCKS ; // some space between "losung" block
for ( int row = 0 ; row < getMaxRows ( lehrtext , TEXT_WIDTH , TEXT_FONT . Width ) ; row + + ) {
String text_of_row = getRow ( lehrtext , TEXT_WIDTH , TEXT_FONT . Width , row ) ;
paint . Clear ( UNCOLORED ) ;
paint . DrawStringAt ( 0 , 0 , text_of_row . c_str ( ) , & TEXT_FONT , COLORED ) ;
epd . TransmitPartialData ( paint . GetImage ( ) , 0 , text_y , paint . GetWidth ( ) , paint . GetHeight ( ) ) ;
text_y + = paint . GetHeight ( ) ;
text_y + = TEXT_PADDING_BETWEEN_LINES ;
}
// Lehrtext bible position
paint . SetWidth ( lehrtextPosition . length ( ) * SOURCE_FONT . Width ) ;
paint . SetHeight ( SOURCE_FONT . Height ) ;
paint . Clear ( COLORED ) ;
paint . DrawStringAt ( 0 , 0 , lehrtextPosition . c_str ( ) , & SOURCE_FONT , UNCOLORED ) ;
epd . TransmitPartialData ( paint . GetImage ( ) , 5 , text_y , paint . GetWidth ( ) , paint . GetHeight ( ) ) ;
// display the whole sent data
epd . DisplayFrame ( ) ;
2022-08-21 08:48:54 +00:00
}
2022-08-21 15:04:44 +00:00
2022-08-21 08:48:54 +00:00
void setup ( ) {
// put your setup code here, to run once:
Serial . begin ( 115200 ) ;
Serial . println ( " [INFO] Initializing e-Paper... " ) ;
if ( epd . Init ( ) ! = 0 ) {
Serial . println ( " [FATAL] Failed initializing e-Paper; Exiting... " ) ;
return ;
}
Serial . println ( " [INFO] Initialized e-Paper! " ) ;
2022-08-21 15:04:44 +00:00
String losung = " Gott spricht: Ich will für Israel wie der Tau sein, dass es blüht wie eine Lilie. " ;
String lehrtext = " Ich bin der Weinstock, ihr seid die Reben. Wer in mir bleibt und ich in ihm, der bringt viel Frucht; denn ohne mich könnt ihr nichts tun. " ;
showDailyText ( losung , lehrtext , " Hosea 14,6 " , " Johannes 15,5 " ) ;
2022-08-21 08:48:54 +00:00
}
void loop ( ) {
}