I have been playing around with the fantastic lib GxEPD2_PP build by @ZinggJM
Want to build a display that shows some citations. I had it working at some point and it looked like this:
But then I messed it all up..
Don't know what I did wrong.. But I have now moved it all out of web IDE and using the vscode particle workbench instead - so I can use git and be able to revert back (next time I mess up).
I would be very thankful if anybody can point out to me what I'm doing wrong.
I haven't refactored the code to be clean, yet - but it looks like this now:
/*
- Project eCitations
- Description:
- Author: Lasse Norfeldt
- Date: 2019
*/#include <Adafruit_GFX_RK.h>
#include <GxEPD2_PP.h>// Supporting Arduino Forum Topics:
// Waveshare e-paper displays with SPI: Waveshare e-paper displays with SPI - Displays - Arduino Forum
// Good Dispay ePaper for Arduino: Good Display ePaper for Arduino - Displays - Arduino Forum// 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.// base class GxEPD2_GFX can be used to pass references or pointers to the display instance as parameter, uses ~1.2k more code
// enable or disable GxEPD2_GFX base class
#define ENABLE_GxEPD2_GFX 0#include <Adafruit_GFX.h>
#include <Arduino.h>
#include <FreeSerifBoldItalic18pt7b.h>
#include <FreeSerifItalic18pt7b.h>#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));void setup() {
// Debugging Serial
Serial.begin(115200);
Serial.println();
Serial.println("Starting eCitations");display.init(115200); delay(3000); showCitation("**Jeg** elsker Josefine;Jeg **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 1;
}
}void showCitation(String citation) {
// Initial settings
display.setFullWindow();
display.fillScreen(GxEPD_WHITE);
display.setRotation(0);// Start sending the print display.firstPage(); do { // Get display width and height int dw = display.width(); int dh = display.height(); // 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); } // Figure out how many lines there are int nLines = countLines(citation); Serial.println(); Serial.print("nLines: "); Serial.println(nLines); // Loop through each line if (citation != "") { do { // Slice out a sentence to print int i_delimiter = citation.indexOf(";"); String sentence = citation.substring(0, i_delimiter); citation = citation.replace(i_delimiter > 0 ? sentence + ";" : sentence, ""); Serial.println(); Serial.print("sentence: "); Serial.println(sentence); Serial.print("citation: "); Serial.println(citation); // Figuring out where to start placing the cursor String rawStr = String(sentence); String pureStr = String(sentence.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 - 35 * nLines) / 2 + ((nLines - 1) * 40) + 20); display.setCursor(x, y); // Serial debugger Serial.print("x: "); Serial.println(x); Serial.print("y: "); Serial.println(y); // Fragment the sentence into blocks int strong; int space; if (rawStr.indexOf("**") == 0) { strong = 1; rawStr = rawStr.substring(2, rawStr.length()); // Dirty hacks x -= 5; } else { strong = 0; } rawStr = rawStr + "**"; 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); // 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 } while (citation.indexOf(";") > 0); // End of sentence } // End of IF } while (display.nextPage()); // End of printing Serial.println("Citation printed");
}