86 lines
2.4 KiB
C++
86 lines
2.4 KiB
C++
#include "Board.h"
|
|
|
|
#include <Arduino_GFX_Library.h>
|
|
#include <SD.h>
|
|
#include <SPI.h>
|
|
|
|
#include "Config.h"
|
|
|
|
namespace {
|
|
Arduino_DataBus* bus = new Arduino_ESP32SPI(
|
|
config::kTftDc, config::kTftCs, config::kSpiSck, config::kSpiMosi,
|
|
config::kSpiMiso);
|
|
// The panel is natively 222x480. Rotation 1 gives the reader a 480x222 canvas.
|
|
Arduino_GFX* makeDisplay() {
|
|
return new Arduino_ST7796(bus, config::kTftReset, 1, true, 222, 480, 49, 0);
|
|
}
|
|
} // namespace
|
|
|
|
bool Board::begin() {
|
|
display_ = makeDisplay();
|
|
if (!display_->begin()) {
|
|
return false;
|
|
}
|
|
display_->fillScreen(BLACK);
|
|
pinMode(config::kBacklight, OUTPUT);
|
|
setBrightness(config::kNormalBrightness);
|
|
return true;
|
|
}
|
|
|
|
bool Board::beginSd() {
|
|
SPI.begin(config::kSpiSck, config::kSpiMiso, config::kSpiMosi);
|
|
return SD.begin(config::kSdCs, SPI);
|
|
}
|
|
|
|
void Board::drawWord(const String& word) {
|
|
if (display_ == nullptr) return;
|
|
|
|
display_->fillScreen(BLACK);
|
|
const size_t glyphs = word.length() == 0 ? 1 : word.length();
|
|
uint16_t calculatedScale = 480 / (glyphs * 6); // MVP font supports printable ASCII text.
|
|
if (calculatedScale < 2) calculatedScale = 2;
|
|
if (calculatedScale > 12) calculatedScale = 12;
|
|
const uint8_t scale = calculatedScale;
|
|
const int16_t width = glyphs * 6 * scale;
|
|
const int16_t height = 8 * scale;
|
|
int16_t x = (display_->width() - width) / 2;
|
|
if (x < 0) x = 0;
|
|
display_->setTextColor(WHITE);
|
|
display_->setTextSize(scale);
|
|
display_->setCursor(x, (display_->height() - height) / 2);
|
|
display_->print(word);
|
|
}
|
|
|
|
void Board::setBrightness(uint8_t level) {
|
|
level = constrain(level, 0, 16);
|
|
#ifdef TDS3P_V1_0
|
|
ledcSetup(1, 1000, 8);
|
|
ledcAttachPin(config::kBacklight, 1);
|
|
ledcWrite(1, map(level, 0, 16, 0, 255));
|
|
#else
|
|
// v1.1 uses a 16-step constant-current driver, not PWM.
|
|
if (level == 0) {
|
|
digitalWrite(config::kBacklight, LOW);
|
|
delay(3);
|
|
brightness_ = 0;
|
|
return;
|
|
}
|
|
if (brightness_ == 0) {
|
|
digitalWrite(config::kBacklight, HIGH);
|
|
brightness_ = 16;
|
|
delayMicroseconds(30);
|
|
}
|
|
const uint8_t from = 16 - brightness_;
|
|
const uint8_t to = 16 - level;
|
|
const uint8_t pulses = (16 + to - from) % 16;
|
|
for (uint8_t i = 0; i < pulses; ++i) {
|
|
digitalWrite(config::kBacklight, LOW);
|
|
digitalWrite(config::kBacklight, HIGH);
|
|
}
|
|
brightness_ = level;
|
|
#endif
|
|
}
|
|
|
|
void Board::displayOff() { setBrightness(0); }
|
|
void Board::displayOn() { setBrightness(config::kNormalBrightness); }
|