KrisKasprzak
Well-known member
I'm trying to expand my flicker free print function to a class where I can pass in an unknown display object. I've used templates in the past and feel it may work
I'm stuck trying to figure out how to implement a template for an arguement in a class definition--assuming it can even be done
.ino code implementation
#include <FlickerFreePrint.h>
#include <ILI9341_t3.h>
// create the display object based on the ILI9341 library
ILI9341_t3 Display(PIN1, PIN2);
//Create my cute little print utility that will draw on the Display object (that uses the ILI9341 driver)
FlickerFreePrint Volts(&Display, COLOR_BLACK);
// print the volts to the display
Volts.print(23);
my class definition.
I'm stuck trying to figure out how to implement a template for an arguement in a class definition--assuming it can even be done
.ino code implementation
#include <FlickerFreePrint.h>
#include <ILI9341_t3.h>
// create the display object based on the ILI9341 library
ILI9341_t3 Display(PIN1, PIN2);
//Create my cute little print utility that will draw on the Display object (that uses the ILI9341 driver)
FlickerFreePrint Volts(&Display, COLOR_BLACK);
// print the volts to the display
Volts.print(23);
my class definition.
Code:
#ifndef FLICKER_FREE_PRINT
#define FLICKER_FREE_PRINT
#if ARDUINO >= 100
#include "Arduino.h"
#include "Print.h"
#else
#include "WProgram.h"
#endif
#ifdef __cplusplus
#include "Arduino.h"
#endif
#include "ILI9341_t3.h"
class FlickerFreePrint {
public:
template <typename T1> FlickerFreePrint(T1 *disp, uint16_t BackColor = 0)
{
T1 = disp;
bc = BackColor;
}
void print(int Data) {
dtostrf(Data, 0, 0, buf);
len = strlen(buf);
tHi = d->strPixelLen(cc);
for (i = 0; i < len; i++) {
if (buf[i] != obuf[i]) {
d->fillRect (d->getCursorX() + bl, d->getCursorY() + bt, oc - d->getCursorX() + br, tHi + bb , bc);
obuf[i] = buf[i];
}
d->setCursor(d->getCursorX(), d->getCursorY());
d->print(buf[i]);
c = d->getCursorX();
if (i == len - 1) {
oc = c;
}
}
}
void print(const char *buf) {
len = strlen(buf);
tHi = d->strPixelLen(cc);
for (i = 0; i < len; i++) {
if (buf[i] != obuf[i]) {
d->fillRect (d->getCursorX() + bl, d->getCursorY() + bt, oc - d->getCursorX() + br, tHi + bb , bc);
obuf[i] = buf[i];
}
d->setCursor(d->getCursorX(), d->getCursorY());
d->print(buf[i]);
c = d->getCursorX();
if (i == len - 1) {
oc = c;
}
}
}
void print(float Data, int Dec = 2) {
dtostrf(Data, 0, Dec, buf);
len = strlen(buf);
tHi = d->strPixelLen(cc);
for (i = 0; i < len; i++) {
if (buf[i] != obuf[i]) {
d->fillRect (d->getCursorX() + bl, d->getCursorY() + bt, oc - d->getCursorX() + br, tHi + bb , bc);
obuf[i] = buf[i];
}
d->setCursor(d->getCursorX(), d->getCursorY());
d->print(buf[i]);
c = d->getCursorX();
if (i == len - 1) {
oc = c;
}
}
}
void SetPaintMargins(int MarginTop = 0, int MarginLeft = 0, int MarginBottom = 0, int MarginRight = 0) {
bt = MarginTop;
bl = MarginLeft;
bb = MarginBottom;
br = MarginRight;
}
void SetBackColor(uint16_t BackColor = 0) {
bc = BackColor;
}
private:
T1 *d;
char obuf[38];
int oc;
int c;
uint16_t bc;
uint16_t i;
char buf[38];
int tHi;
byte di;
int len;
char cc[2] = "D";
int bt = 0, bl = 0, bb = 0, br = 0;
};