Skip to content

Commit

Permalink
Move to more efficient method of printing to display
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1ol committed Oct 31, 2024
1 parent 73ec01d commit ee9ee73
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 56 deletions.
29 changes: 21 additions & 8 deletions src/DisplayLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <LiquidCrystal.h>

#include "Tools.h"

void DisplayLine::concat(char* dst, const char* src) {
int srcLen = strlen(src);
int shift = strlen(dst);
Expand Down Expand Up @@ -31,6 +33,8 @@ bool DisplayLine::tryPrint(const char* src, bool blink, uint8_t alignSize, const
if (alignSize + dstlen > DISPLAY_COLS)
return false;

m_needRepaint = true;

memset(m_fwInfo + dstlen, ' ', alignSize - srclen);
m_fwInfo[dstlen + alignSize - srclen] = 0;

Expand All @@ -47,6 +51,7 @@ bool DisplayLine::tryPrint(const char* src, bool blink, uint8_t alignSize, const
}

void DisplayLine::reset() {
m_needRepaint = true;
m_fwInfo[0] = 0;
m_bwInfo[0] = 0;
m_blinkLength = 0;
Expand All @@ -59,11 +64,18 @@ void DisplayLine::resetBlink(bool state) {
}

void DisplayLine::tick() {
if (!m_needRepaint && !m_blinkLength)
return;
m_needRepaint = false;

char printBuf[DISPLAY_COLS + 1];

auto fwLen = strlen(m_fwInfo);
memset(m_fwInfo + fwLen, ' ', DISPLAY_COLS - fwLen);
memcpy(printBuf, m_fwInfo, fwLen);
memset(printBuf + fwLen, ' ', DISPLAY_COLS - fwLen);

auto bwLen = strlen(m_bwInfo);
memcpy(m_fwInfo + DISPLAY_COLS - bwLen, m_bwInfo, bwLen);
memcpy(printBuf + DISPLAY_COLS - bwLen, m_bwInfo, bwLen);

if (m_blinkLength) {
if (millis() - m_blinkTimer > 500) {
Expand All @@ -72,38 +84,39 @@ void DisplayLine::tick() {
}

if (m_blinkState) {
memset(m_fwInfo + m_blinkPos, ' ', m_blinkLength);
memset(printBuf + m_blinkPos, ' ', m_blinkLength);

if (m_mark) {
uint8_t marklen = strlen(m_mark);
memcpy(m_fwInfo + m_blinkPos + m_blinkLength - marklen, m_mark, marklen);
memcpy(printBuf + m_blinkPos + m_blinkLength - marklen, m_mark, marklen);
}
}
}

m_lcd.setCursor(0, m_line);

m_lcd.print(m_fwInfo);

reset();
m_lcd.print(printBuf);
}

DisplayLine& DisplayLine::operator<<(const char* src) {
m_needRepaint = true;
concat(m_fwInfo, src);
return *this;
}

DisplayLine& DisplayLine::operator<<(int value) {
m_needRepaint = true;
concatInt(m_fwInfo, value);
return *this;
}

DisplayLine& DisplayLine::operator>>(const char* src) {
m_needRepaint = true;
concat(m_bwInfo, src);
return *this;
}

DisplayLine& DisplayLine::operator>>(int value) {
m_needRepaint = true;
concatInt(m_bwInfo, value);
return *this;
}
1 change: 1 addition & 0 deletions src/DisplayLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class DisplayLine {
static void concat(char* dst, const char* src);
static void concatInt(char* dst, int value);

bool m_needRepaint = false;
int m_line;
LiquidCrystal& m_lcd;

Expand Down
2 changes: 1 addition & 1 deletion src/ModeProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "Tools.h"

void ModeProcessor::printTimeHelper(Time (*timeGetter)(const void* ctx, uint8_t id, bool& current, const char*& mark),
const void* ctx) const {
const void* ctx) const {
uint8_t id = 0;

for (uint8_t row = 0; row != DISPLAY_ROWS; ++row) {
Expand Down
4 changes: 3 additions & 1 deletion src/ModeProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class ModeProcessor {

virtual const char* preview() const = 0;

virtual void repaint() const = 0;

protected:
void printTimeHelper(Time (*timeGetter)(const void* ctx, uint8_t id, bool& current, const char*& mark),
const void* ctx) const;
const void* ctx) const;
};
53 changes: 37 additions & 16 deletions src/Modes/FStopTestMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ FStopTestMode::FStopTestMode(bool splitGrade) : kSplit(splitGrade) {
m_FStopPartId = 5;
m_step = kSplit ? Step::baseTime : Step::initTime;
m_currentRun = kSplit ? 0 : 1;

repaint();
}

void FStopTestMode::switchMode() {
Expand All @@ -21,35 +23,30 @@ void FStopTestMode::switchMode() {

m_currentRun = kSplit ? 0 : 1;
gTimer.reset();

repaint();
}

void FStopTestMode::process() {
switch (m_step) {
case Step::baseTime:
gDisplay[0] << preview();

getTime(m_baseTime);
gDisplay[1] << "Base t:" << m_baseTime;
if (getTime(m_baseTime))
repaint();
return;
case Step::initTime:
gDisplay[0] << preview();

getTime(m_initTime);
gDisplay[1] << "Init t:" << m_initTime;
if (getTime(m_initTime))
repaint();
return;
case Step::fstopSet: {
gDisplay[0] << preview();

getInt(m_FStopPartId, 0, sizeof(kFStopPartVarinatns) - 1);
gDisplay[1] << "F stop: 1/" << kFStopPartVarinatns[m_FStopPartId];
}
case Step::fstopSet:
if (getInt(m_FStopPartId, 0, sizeof(kFStopPartVarinatns) - 1))
repaint();
return;
case Step::run:
break;
}

gDisplay[0] << "Run ";
printTimes();
if (gTimer.state() == Timer::RUNNING)
repaint();

if (gTimer.state() == Timer::STOPPED && gStartBtn.click() && getStepTotalTime(m_currentRun) != kBadTime)
gTimer.start(getPrintTime());
Expand All @@ -60,6 +57,30 @@ void FStopTestMode::process() {
gBeeper.alarm("Change filter");
}
++m_currentRun;
repaint();
}
}

void FStopTestMode::repaint() const {
gDisplay.reset();

switch (m_step) {
case Step::baseTime:
gDisplay[0] << preview();
gDisplay[1] << "Base t:" << m_baseTime;
return;
case Step::initTime:
gDisplay[0] << preview();
gDisplay[1] << "Init t:" << m_initTime;
return;
case Step::fstopSet:
gDisplay[0] << preview();
gDisplay[1] << "F stop: 1/" << kFStopPartVarinatns[m_FStopPartId];
return;
case Step::run:
gDisplay[0] << "Run ";
printTimes();
return;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Modes/FStopTestMode.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class FStopTestMode final : public ModeProcessor {

const char* preview() const override;

void repaint() const override;

private:
void printTimes() const;
Time getPrintTime() const;
Expand Down
47 changes: 36 additions & 11 deletions src/Modes/LinearTestMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ LinearTestMode::LinearTestMode(bool splitGrade) : kSplit(splitGrade) {
m_stepTime = 2_s;
m_step = kSplit ? Step::baseTime : Step::initTime;
m_currentRun = kSplit ? 0 : 1;

repaint();
}

void LinearTestMode::switchMode() {
Expand All @@ -17,31 +19,30 @@ void LinearTestMode::switchMode() {

m_currentRun = kSplit ? 0 : 1;
gTimer.reset();

repaint();
}

void LinearTestMode::process() {
switch (m_step) {
case Step::baseTime:
gDisplay[0] << preview();
getTime(m_baseTime);
gDisplay[1] << "Base t:" << m_baseTime;
if (getTime(m_baseTime))
repaint();
return;
case Step::initTime:
gDisplay[0] << preview();
getTime(m_initTime);
gDisplay[1] << "Init t:" << m_initTime;
if (getTime(m_initTime))
repaint();
return;
case Step::stepTime:
gDisplay[0] << preview();
getTime(m_stepTime);
gDisplay[1] << "Step t:" << m_stepTime;
if (getTime(m_stepTime))
repaint();
return;
case Step::run:
break;
}

gDisplay[0] << "Run ";
printTimes();
if (gTimer.state() == Timer::RUNNING)
repaint();

if (gTimer.state() == Timer::STOPPED && gStartBtn.click() && getTotalTime(m_currentRun) != kBadTime)
gTimer.start(getPrintTime());
Expand All @@ -52,6 +53,30 @@ void LinearTestMode::process() {
gBeeper.alarm("Change filter");
}
++m_currentRun;
repaint();
}
}

void LinearTestMode::repaint() const {
gDisplay.reset();

switch (m_step) {
case Step::baseTime:
gDisplay[0] << preview();
gDisplay[1] << "Base t:" << m_baseTime;
return;
case Step::initTime:
gDisplay[0] << preview();
gDisplay[1] << "Init t:" << m_initTime;
return;
case Step::stepTime:
gDisplay[0] << preview();
gDisplay[1] << "Step t:" << m_stepTime;
return;
case Step::run:
gDisplay[0] << "Run ";
printTimes();
break;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Modes/LinearTestMode.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class LinearTestMode final : public ModeProcessor {

const char* preview() const override;

void repaint() const override;

private:
void printTimes() const;
Time getPrintTime() const;
Expand Down
Loading

0 comments on commit ee9ee73

Please sign in to comment.