Skip to content

Commit 35071c7

Browse files
committed
perf: increase performance by using nonblocking timers
1 parent edb8dd8 commit 35071c7

10 files changed

Lines changed: 51 additions & 22 deletions

File tree

include/plugins/CirclePlugin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#pragma once
22

33
#include "PluginManager.h"
4+
#include "timing.h"
45

56
class CirclePlugin : public Plugin
67
{
78
private:
89
uint8_t circleStep = 0;
10+
NonBlockingDelay timer;
911
std::vector<std::vector<int>> frames = {
1012
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1113
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

include/plugins/LinesPlugin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#pragma once
22

33
#include "PluginManager.h"
4+
#include "timing.h"
45

56
class LinesPlugin : public Plugin
67
{
78
private:
89
uint8_t count = 0;
10+
NonBlockingDelay timer;
911
std::vector<std::vector<int>> frames = {{0xcc, 0xcc}, {0x66, 0x66}, {0x33, 0x33}, {0x99, 0x99}};
1012

1113
public:

include/plugins/RainPlugin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#pragma once
22

33
#include "PluginManager.h"
4+
#include "timing.h"
45

56
class RainPlugin : public Plugin
67
{
78
private:
9+
NonBlockingDelay timer;
810
static constexpr uint8_t NUM_DROPS = 10;
911
static constexpr uint8_t X_MAX = 16;
1012
static constexpr uint8_t Y_MAX = 16;

include/scheduler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class PluginScheduler
1717
unsigned long lastSwitch = 0;
1818
size_t currentIndex = 0;
1919

20+
bool needsPersist = false;
21+
unsigned long lastPersistRequest = 0;
22+
static constexpr unsigned long PERSIST_DELAY_MS = 2000;
23+
2024
public:
2125
static PluginScheduler &getInstance();
2226

@@ -36,6 +40,8 @@ class PluginScheduler
3640

3741
private:
3842
void switchToCurrentPlugin();
43+
void requestPersist();
44+
void checkAndPersist();
3945
};
4046

4147
extern PluginScheduler &Scheduler;

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ void screenDrawingTask(void *parameter)
199199
for (;;)
200200
{
201201
pluginManager.runActivePlugin();
202-
vTaskDelay(10);
202+
vTaskDelay(1);
203203
}
204204
}
205205

src/messages.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ void Messages_::remove(int id)
5555

5656
void Messages_::scroll()
5757
{
58-
Screen.persist();
59-
6058
for (auto it = activeMessages.begin(); it != activeMessages.end();)
6159
{
6260
Message *msg = *it;
@@ -84,11 +82,6 @@ void Messages_::scroll()
8482
++it;
8583
}
8684
}
87-
88-
if (currentStatus == NONE)
89-
{
90-
Screen.loadFromStorage();
91-
}
9285
}
9386

9487
void Messages_::scrollMessageEveryMinute()
@@ -107,8 +100,12 @@ void Messages_::scrollMessageEveryMinute()
107100
{
108101
if (!activeMessages.empty())
109102
{
110-
indicatorPixel = timeinfo.tm_sec & 0b00000001;
111-
Screen.setPixel(0, 0, indicatorPixel);
103+
uint8_t newIndicatorPixel = timeinfo.tm_sec & 0b00000001;
104+
if (newIndicatorPixel != indicatorPixel)
105+
{
106+
indicatorPixel = newIndicatorPixel;
107+
Screen.setPixel(0, 0, indicatorPixel);
108+
}
112109
}
113110
else if (indicatorPixel > 0)
114111
{

src/plugins/CirclePlugin.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ void CirclePlugin::setup()
77

88
void CirclePlugin::loop()
99
{
10+
if (!timer.isReady(200))
11+
return;
12+
1013
std::vector<int> bits = Screen.readBytes(this->frames[this->circleStep]);
1114

1215
for (int i = 0; i < bits.size(); i++)
@@ -19,7 +22,6 @@ void CirclePlugin::loop()
1922
{
2023
this->circleStep = 7;
2124
}
22-
delay(200);
2325
}
2426

2527
const char *CirclePlugin::getName() const

src/plugins/LinesPlugin.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ void LinesPlugin::setup()
77

88
void LinesPlugin::loop()
99
{
10+
if (!timer.isReady(200))
11+
return;
12+
1013
std::vector<int> bits = Screen.readBytes(this->frames[this->count]);
1114
for (int row = 0; row < ROWS; row++)
1215
{
@@ -21,7 +24,6 @@ void LinesPlugin::loop()
2124
{
2225
this->count = 0;
2326
}
24-
delay(200);
2527
}
2628

2729
const char *LinesPlugin::getName() const

src/plugins/RainPlugin.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ void RainPlugin::setup()
1414

1515
void RainPlugin::loop()
1616
{
17+
if (!timer.isReady(96))
18+
return;
19+
1720
// dim the trail
1821
for (uint8_t x = 0; x < RainPlugin::X_MAX; x++)
1922
{
@@ -59,8 +62,6 @@ void RainPlugin::loop()
5962
Screen.setPixel(this->drops[i].x, this->drops[i].y, RainPlugin::LED_TYPE_ON, 255);
6063
}
6164
}
62-
63-
delay(96);
6465
}
6566

6667
const char *RainPlugin::getName() const

src/scheduler.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,42 @@ void PluginScheduler::start()
4444
currentIndex = 0;
4545
lastSwitch = millis();
4646
isActive = true;
47-
#ifdef ENABLE_STORAGE
48-
storage.begin("led-wall", false);
49-
storage.putInt("scheduleactive", 1);
50-
storage.end();
51-
#endif
47+
requestPersist();
5248
switchToCurrentPlugin();
5349
}
5450
}
5551

5652
void PluginScheduler::stop()
5753
{
5854
isActive = false;
55+
requestPersist();
56+
}
57+
58+
void PluginScheduler::requestPersist()
59+
{
60+
needsPersist = true;
61+
lastPersistRequest = millis();
62+
}
63+
64+
void PluginScheduler::checkAndPersist()
65+
{
5966
#ifdef ENABLE_STORAGE
60-
storage.begin("led-wall", false);
61-
storage.putInt("scheduleactive", 0);
62-
storage.end();
67+
if (needsPersist && (millis() - lastPersistRequest >= PERSIST_DELAY_MS))
68+
{
69+
storage.begin("led-wall", false);
70+
storage.putInt("scheduleactive", isActive ? 1 : 0);
71+
storage.end();
72+
needsPersist = false;
73+
}
74+
#else
75+
needsPersist = false;
6376
#endif
6477
}
6578

6679
void PluginScheduler::update()
6780
{
81+
checkAndPersist();
82+
6883
if (!isActive || schedule.empty())
6984
return;
7085

0 commit comments

Comments
 (0)