Playing more around with the awesome lib created by @ZinggJM and noticed some strange behavior when trying to print the same text at different positions.
showCitation("**Jeg** elsker Josefine;Jeg **elsker** Isabella;Jeg **elsker** Isabella;Jeg elsker **Tyson**;");
showCitation("**Jeg** elsker Josefine;Jeg **elsker** Isabella;Vi **elsker** Isabella;Jeg elsker **Tyson**;");
This is the entire code:
/*
* Project eCitations
* Description:
* Author: Lasse Norfeldt
* Date: 2019
*/
// mapping suggestion from Waveshare SPI e-Paper to Particle Photon
// A5 MOSI
// A4 MISO
// A3 SCK
// A2 SS
// BUSY -> D4, RST -> A0, DC -> A1, CS -> A2, CLK -> A3, DIN -> A5, GND ->
// GND, 3.3V -> 3.3V NOTE: it looks like MISO can't be used as general input pin
// for BUSY.
#include <GxEPD2_PP.h>
#define ENABLE_GxEPD2_GFX 0
#include <GxEPD2_3C.h>
#include <GxEPD2_BW.h>
GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT / 4> display(
GxEPD2_750c(/*CS=A2*/ SS, /*DC=*/A1, /*RST=*/A0, /*BUSY=*/D4));
#include <Adafruit_GFX.h>
#include <Adafruit_GFX_RK.h>
#include <Arduino.h>
#include <FreeSerifBoldItalic18pt7b.h>
#include <FreeSerifItalic18pt7b.h>
void setup() {
{ // Start debugging serial
Serial.begin(115200);
Serial.println();
Serial.println("Starting eCitations");
}
// Connect to the display
display.init(115200);
delay(3000);
// Print some "markdown" to the display
showCitation("**Jeg** elsker Josefine;Jeg **elsker** Isabella;Vi **elsker** Isabella;Jeg elsker **Tyson**;");
}
void loop() {}
int countLines(String str) {
int i = str.indexOf(";");
if (i > 0) {
return countLines(str.substring(i + 1, str.length())) + 1;
} else {
return 0;
}
}
void printMarkDown(String rawStr, int x, int y) {
{ // Serial debugger
Serial.print("rawStr: ");
Serial.println(rawStr);
}
// Setup strong and space
int strong;
int space;
if (rawStr.indexOf("**") == 0) { // Sentence can start with "strong"
strong = 1;
rawStr = rawStr.substring(2, rawStr.length()); // Dirty hacks
x -= 5;
} else {
strong = 0;
}
rawStr = rawStr + "**";
// Fragment the sentence into blocks
do {
// Set font style and color
if (strong == 1) {
display.setTextColor(GxEPD_RED);
display.setFont(&FreeSerifBoldItalic18pt7b);
strong--;
space = 2;
} else {
display.setTextColor(GxEPD_BLACK);
display.setFont(&FreeSerifItalic18pt7b);
strong++;
space = 5;
};
// Find "**"
int i_stars = rawStr.indexOf("**");
// Print until "**"
String prtStr = rawStr.substring(0, i_stars);
display.print(prtStr);
{ // Serial debugger
Serial.print("prtStr: ");
Serial.println(prtStr);
}
// Move cursor
int16_t t2bx, t2by;
uint16_t t2bw, t2bh;
display.getTextBounds(prtStr, 0, 0, &t2bx, &t2by, &t2bw, &t2bh);
x = x + t2bw + space;
display.setCursor(x, y);
// Remove until "**"
rawStr = rawStr.substring(i_stars + 2, rawStr.length());
} while (rawStr.indexOf("**") > 0); // End of sentence block/fragments
}
void showCitation(String citation) {
// Initial display settings
display.setFullWindow();
display.fillScreen(GxEPD_WHITE);
display.setRotation(0);
int dw = display.width();
int dh = display.height();
{ // Serial debugger
Serial.print("dw: ");
Serial.println(dw);
Serial.print("dh: ");
Serial.println(dh);
}
// Start sending the print
display.firstPage();
do {
// Draw a red border
int padding = 5;
int stroke = 4;
for (int p = padding; p < (padding + stroke + 1); p++) {
display.drawRect(p, p, dw - 2 * p, dh - 2 * p, GxEPD_RED);
}
// Loop through each line
String citationCopy = String(citation); // Make a copy because it will be shorten and needs to be restored at the end
if (citation != "") {
do {
// Figure out how many lines there are
int nLine = countLines(citation);
{ // Serial debugger
Serial.println();
Serial.print("nLine: ");
Serial.println(nLine);
}
// Slice out the string to print
int i_delimiter = citation.indexOf(";");
String rawStr = citation.substring(0, i_delimiter);
citation = citation.replace(i_delimiter > 0 ? rawStr + ";" : rawStr, "");
{ // Serial debugger
Serial.println();
Serial.print("rawStr: ");
Serial.println(rawStr);
Serial.print("citation: ");
Serial.println(citation);
}
// Figuring out where to start placing the cursor
String pureStr = String(rawStr).replace("*", "");
int16_t t1bx, t1by;
uint16_t t1bw, t1bh;
display.getTextBounds(
pureStr, 0, 0, &t1bx, &t1by,
&t1bw, &t1bh); // Don't take into account the different font width
uint16_t x = (int)((dw - t1bw) / 2);
uint16_t y = (int)((dh / 2) - ((nLine - 1) * 38) + countLines(citationCopy) / 2 * 38);
display.setCursor(x, y);
// Print out the rawStr
printMarkDown(rawStr, x, y);
{ // Serial debugger
Serial.print("pureStr: ");
Serial.println(pureStr);
Serial.print("x: ");
Serial.println(x);
Serial.print("t1bw: ");
Serial.println(t1bw);
Serial.print("y: ");
Serial.println(y);
Serial.print("t1bh: ");
Serial.println(t1bh);
}
} while (citation.indexOf(";") > 0); // End of sentence loop
} // End of IF (citation != "")
citation = citationCopy;
} while (display.nextPage()); // End of page printing
Serial.println("Citation printed");
}