Skip to content

Commit 499b9a5

Browse files
committed
Extract ui state into struct and pass it to the frames and overlays
1 parent e3a5b65 commit 499b9a5

File tree

2 files changed

+68
-65
lines changed

2 files changed

+68
-65
lines changed

SSD1306Ui.cpp

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ void SSD1306Ui::setTargetFPS(byte fps){
2222
// -/------ Automatic controll ------\-
2323

2424
void SSD1306Ui::enableAutoTransition(){
25-
autoTransition = true;
25+
this->autoTransition = true;
2626
}
2727
void SSD1306Ui::disableAutoTransition(){
28-
autoTransition = false;
28+
this->autoTransition = false;
2929
}
3030
void SSD1306Ui::setAutoTransitionForwards(){
3131
this->frameTransitionDirection = 1;
@@ -54,100 +54,100 @@ void SSD1306Ui::setActiveSymbole(const char* symbole) {
5454
this->dirty = true;
5555
}
5656
void SSD1306Ui::setInactiveSymbole(const char* symbole) {
57-
this->inactiveSymbole = symbole;
57+
this->inactiveSymbole = symbole;
5858
this->dirty = true;
5959
}
6060

6161

6262
// -/----- Frame settings -----\-
63-
void SSD1306Ui::setFrameAnimation(AnimationDirection dir) {
63+
void SSD1306Ui::setFrameAnimation(AnimationDirection dir) {
6464
this->frameAnimationDirection = dir;
6565
}
66-
void SSD1306Ui::setFrames(bool (*frameFunctions[])(SSD1306 *display, int x, int y), int frameCount) {
66+
void SSD1306Ui::setFrames(bool (*frameFunctions[])(SSD1306 *display, SSD1306UiState* state, int x, int y), int frameCount) {
6767
this->frameCount = frameCount;
6868
this->frameFunctions = frameFunctions;
6969
}
7070

7171
// -/----- Overlays ------\-
72-
void SSD1306Ui::setOverlays(bool (*overlayFunctions[])(SSD1306 *display), int overlayCount){
72+
void SSD1306Ui::setOverlays(bool (*overlayFunctions[])(SSD1306 *display, SSD1306UiState* state), int overlayCount){
7373
this->overlayCount = overlayCount;
7474
this->overlayFunctions = overlayFunctions;
7575
}
7676

7777

7878
// -/----- Manuel control -----\-
7979
void SSD1306Ui::nextFrame() {
80-
this->frameState = IN_TRANSITION;
81-
this->ticksSinceLastStateSwitch = 0;
80+
this->state.frameState = IN_TRANSITION;
81+
this->state.ticksSinceLastStateSwitch = 0;
8282
this->frameTransitionDirection = 1;
8383
}
8484
void SSD1306Ui::previousFrame() {
85-
this->frameState = IN_TRANSITION;
86-
this->ticksSinceLastStateSwitch = 0;
85+
this->state.frameState = IN_TRANSITION;
86+
this->state.ticksSinceLastStateSwitch = 0;
8787
this->frameTransitionDirection = -1;
8888
}
8989

9090

9191
// -/----- State information -----\-
9292
FrameState SSD1306Ui::getFrameState(){
93-
return this->frameState;
93+
return this->state.frameState;
9494
}
95+
9596
int SSD1306Ui::getCurrentFrame(){
96-
return this->currentFrame;
97+
return this->state.currentFrame;
9798
}
9899

99100

100101
int SSD1306Ui::update(){
101-
int timeBudget = this->updateInterval - (millis() - this->lastUpdate);
102+
int timeBudget = this->updateInterval - (millis() - this->state.lastUpdate);
102103
if ( timeBudget <= 0) {
103-
104104
// Implement frame skipping to ensure time budget is keept
105-
if (this->autoTransition && this->lastUpdate != 0) this->ticksSinceLastStateSwitch += abs(timeBudget) / this->updateInterval;
106-
107-
this->lastUpdate = millis();
105+
if (this->autoTransition && this->state.lastUpdate != 0) this->state.ticksSinceLastStateSwitch += ceil(-timeBudget / this->updateInterval);
106+
107+
this->state.lastUpdate = millis();
108108
this->tick();
109-
}
109+
}
110110
return timeBudget;
111111
}
112112

113113

114114
void SSD1306Ui::tick() {
115-
this->ticksSinceLastStateSwitch++;
115+
this->state.ticksSinceLastStateSwitch++;
116116

117-
switch (this->frameState) {
117+
switch (this->state.frameState) {
118118
case IN_TRANSITION:
119119
this->dirty = true;
120-
if (this->ticksSinceLastStateSwitch >= this->ticksPerTransition){
121-
this->frameState = FIXED;
122-
this->currentFrame = getNextFrameNumber();
123-
this->ticksSinceLastStateSwitch = 0;
120+
if (this->state.ticksSinceLastStateSwitch >= this->ticksPerTransition){
121+
this->state.frameState = FIXED;
122+
this->state.currentFrame = getNextFrameNumber();
123+
this->state.ticksSinceLastStateSwitch = 0;
124124
}
125125
break;
126126
case FIXED:
127-
if (this->ticksSinceLastStateSwitch >= this->ticksPerFrame){
127+
if (this->state.ticksSinceLastStateSwitch >= this->ticksPerFrame){
128128
if (this->autoTransition){
129-
this->frameState = IN_TRANSITION;
129+
this->state.frameState = IN_TRANSITION;
130130
this->dirty = true;
131131
}
132-
this->ticksSinceLastStateSwitch = 0;
132+
this->state.ticksSinceLastStateSwitch = 0;
133133
}
134134
break;
135135
}
136-
136+
137137
if (this->dirty) {
138138
this->dirty = false;
139139
this->display->clear();
140-
this->drawIndicator();
140+
this->drawIndicator();
141141
this->drawFrame();
142142
this->drawOverlays();
143143
this->display->display();
144144
}
145145
}
146146

147147
void SSD1306Ui::drawFrame(){
148-
switch (this->frameState){
148+
switch (this->state.frameState){
149149
case IN_TRANSITION: {
150-
float progress = (float) this->ticksSinceLastStateSwitch / (float) this->ticksPerTransition;
150+
float progress = (float) this->state.ticksSinceLastStateSwitch / (float) this->ticksPerTransition;
151151
int x, y, x1, y1;
152152
switch(this->frameAnimationDirection){
153153
case SLIDE_LEFT:
@@ -180,36 +180,36 @@ void SSD1306Ui::drawFrame(){
180180
int dir = frameTransitionDirection >= 0 ? 1 : -1;
181181
x *= dir; y *= dir; x1 *= dir; y1 *= dir;
182182

183-
this->dirty |= (*this->frameFunctions[this->currentFrame])(this->display, x, y);
184-
this->dirty |= (*this->frameFunctions[this->getNextFrameNumber()])(this->display, x1, y1);
183+
this->dirty |= (*this->frameFunctions[this->state.currentFrame])(this->display, &this->state, x, y);
184+
this->dirty |= (*this->frameFunctions[this->getNextFrameNumber()])(this->display, &this->state, x1, y1);
185185
break;
186186
}
187187
case FIXED:
188-
this->dirty |= (*this->frameFunctions[this->currentFrame])(this->display, 0, 0);
188+
this->dirty |= (*this->frameFunctions[this->state.currentFrame])(this->display, &this->state, 0, 0);
189189
break;
190190
}
191191
}
192192

193193
void SSD1306Ui::drawIndicator() {
194-
byte posOfCurrentFrame;
195-
194+
byte posOfCurrentFrame;
195+
196196
switch (this->indicatorDirection){
197197
case LEFT_RIGHT:
198-
posOfCurrentFrame = this->currentFrame;
198+
posOfCurrentFrame = this->state.currentFrame;
199199
break;
200200
case RIGHT_LEFT:
201-
posOfCurrentFrame = (this->frameCount - 1) - this->currentFrame;
201+
posOfCurrentFrame = (this->frameCount - 1) - this->state.currentFrame;
202202
break;
203203
}
204-
204+
205205
for (byte i = 0; i < this->frameCount; i++) {
206-
206+
207207
const char *xbm;
208-
208+
209209
if (posOfCurrentFrame == i) {
210210
xbm = this->activeSymbole;
211211
} else {
212-
xbm = this->inactiveSymbole;
212+
xbm = this->inactiveSymbole;
213213
}
214214

215215
int x,y;
@@ -231,24 +231,23 @@ void SSD1306Ui::drawIndicator() {
231231
y = 32 - (12 * frameCount / 2) + 12 * i;
232232
break;
233233
}
234-
234+
235235
this->display->drawXbm(x, y, 8, 8, xbm);
236-
}
236+
}
237237
}
238238

239239
void SSD1306Ui::drawOverlays() {
240240
for (int i=0;i<this->overlayCount;i++){
241-
this->dirty |= (*this->overlayFunctions[i])(this->display);
241+
this->dirty |= (*this->overlayFunctions[i])(this->display, &this->state);
242242
}
243243
}
244244

245245
int SSD1306Ui::getNextFrameNumber(){
246-
int nextFrame = (this->currentFrame + this->frameTransitionDirection) % this->frameCount;
246+
int nextFrame = (this->state.currentFrame + this->frameTransitionDirection) % this->frameCount;
247247
if (nextFrame < 0){
248248
nextFrame = this->frameCount + nextFrame;
249249
}
250-
return nextFrame;
250+
return nextFrame;
251251
}
252252

253253

254-

SSD1306Ui.h

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ const char ANIMATION_inactiveSymbole[] PROGMEM = {
6060
0x00, 0x0, 0x0, 0x18, 0x18, 0x0, 0x0, 0x00
6161
};
6262

63+
64+
// Structure of the UiState
65+
struct SSD1306UiState {
66+
int lastUpdate = 0;
67+
int ticksSinceLastStateSwitch = 0;
68+
69+
FrameState frameState = FIXED;
70+
int currentFrame = 0;
71+
};
72+
6373
class SSD1306Ui {
6474
private:
6575
SSD1306 *display;
@@ -76,29 +86,26 @@ class SSD1306Ui {
7686

7787
// Values for the Frames
7888
AnimationDirection frameAnimationDirection = SLIDE_RIGHT;
79-
FrameState frameState = FIXED;
8089

8190
int frameTransitionDirection = 1;
8291

83-
int ticksPerFrame = 313; // ~ 5000ms at 60 FPS
84-
int ticksPerTransition = 32; // ~ 500ms at 60 FPS
85-
int ticksSinceLastStateSwitch = 0;
86-
int currentFrame = 0;
92+
int ticksPerFrame = 151; // ~ 5000ms at 30 FPS
93+
int ticksPerTransition = 15; // ~ 500ms at 30 FPS
8794

8895
bool autoTransition = true;
8996

90-
bool (**frameFunctions)(SSD1306 *display, int x, int y);
97+
bool (**frameFunctions)(SSD1306 *display, SSD1306UiState* state, int x, int y);
9198
int frameCount = 0;
9299

93100
// Values for Overlays
94-
bool (**overlayFunctions)(SSD1306 *display);
101+
bool (**overlayFunctions)(SSD1306 *display, SSD1306UiState* state);
95102
int overlayCount = 0;
96103

104+
// UI State
105+
SSD1306UiState state;
97106

98107
// Bookeeping for update
99-
int updateInterval = 16;
100-
unsigned long lastUpdate = 0;
101-
108+
int updateInterval = 33;
102109

103110
int getNextFrameNumber();
104111
void drawIndicator();
@@ -147,7 +154,7 @@ class SSD1306Ui {
147154
*/
148155
void setTimePerTransition(int time);
149156

150-
// Customize Indicator Position and style
157+
// Customize indicator position and style
151158
/**
152159
* Set the position of the indicator bar.
153160
*/
@@ -178,25 +185,22 @@ class SSD1306Ui {
178185
/**
179186
* Add frame drawing functions
180187
*/
181-
void setFrames(bool (*frameFunctions[])(SSD1306 *display, int x, int y), int frameCount);
188+
void setFrames(bool (*frameFunctions[])(SSD1306 *display, SSD1306UiState* state, int x, int y), int frameCount);
182189

183190
// Overlay
184191

185192
/**
186193
* Add overlays drawing functions that are draw independent of the Frames
187194
*/
188-
void setOverlays(bool (*overlayFunctions[])(SSD1306 *display), int overlayCount);
195+
void setOverlays(bool (*overlayFunctions[])(SSD1306 *display, SSD1306UiState* state), int overlayCount);
189196

190197
// Manuell Controll
191198
void nextFrame();
192199
void previousFrame();
193200

194201
// State Info
195-
FrameState getFrameState();
196-
int getCurrentFrame();
197-
202+
FrameState getUiState();
198203

199204
int update();
200205
};
201206

202-

0 commit comments

Comments
 (0)