forked from ThingPulse/esp8266-oled-ssd1306
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSD1306Ui.h
209 lines (157 loc) · 5.5 KB
/
SSD1306Ui.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**The MIT License (MIT)
Copyright (c) 2015 by Fabrice Weinberg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Credits for parts of this code go to Daniel Eichhorn
*/
#pragma once
#include <Arduino.h>
#include "SSD1306.h"
enum AnimationDirection {
SLIDE_UP,
SLIDE_DOWN,
SLIDE_LEFT,
SLIDE_RIGHT
};
enum IndicatorPosition {
TOP,
RIGHT,
BOTTOM,
LEFT
};
enum IndicatorDirection {
LEFT_RIGHT,
RIGHT_LEFT
};
enum FrameState {
IN_TRANSITION,
FIXED
};
const char ANIMATION_activeSymbole[] PROGMEM = {
0x00, 0x18, 0x3c, 0x7e, 0x7e, 0x3c, 0x18, 0x00
};
const char ANIMATION_inactiveSymbole[] PROGMEM = {
0x00, 0x0, 0x0, 0x18, 0x18, 0x0, 0x0, 0x00
};
// Structure of the UiState
struct SSD1306UiState {
int lastUpdate = 0;
int ticksSinceLastStateSwitch = 0;
FrameState frameState = FIXED;
int currentFrame = 0;
};
typedef bool (*FrameCallback)(SSD1306 *display, SSD1306UiState* state, int x, int y);
typedef bool (*OverlayCallback)(SSD1306 *display, SSD1306UiState* state);
class SSD1306Ui {
private:
SSD1306 *display;
// Global dirty flag to indicate that the display needs to be redraw.
bool dirty = true;
// Symboles for the Indicator
IndicatorPosition indicatorPosition = BOTTOM;
IndicatorDirection indicatorDirection = LEFT_RIGHT;
const char* activeSymbole = ANIMATION_activeSymbole;
const char* inactiveSymbole = ANIMATION_inactiveSymbole;
// Values for the Frames
AnimationDirection frameAnimationDirection = SLIDE_RIGHT;
int frameTransitionDirection = 1;
int ticksPerFrame = 151; // ~ 5000ms at 30 FPS
int ticksPerTransition = 15; // ~ 500ms at 30 FPS
bool autoTransition = true;
FrameCallback* frameFunctions;
int frameCount = 0;
// Values for Overlays
OverlayCallback* overlayFunctions;
int overlayCount = 0;
// UI State
SSD1306UiState state;
// Bookeeping for update
int updateInterval = 33;
int getNextFrameNumber();
void drawIndicator();
void drawFrame();
void drawOverlays();
void tick();
public:
SSD1306Ui(SSD1306 *display);
/**
* Initialise the display
*/
void init();
/**
* Configure the internal used target FPS
*/
void setTargetFPS(byte fps);
// Automatic Controll
/**
* Enable automatic transition to next frame after the some time can be configured with `setTimePerFrame` and `setTimePerTransition`.
*/
void enableAutoTransition();
/**
* Disable automatic transition to next frame.
*/
void disableAutoTransition();
/**
* Set the direction if the automatic transitioning
*/
void setAutoTransitionForwards();
void setAutoTransitionBackwards();
/**
* Set the approx. time a frame is displayed
*/
void setTimePerFrame(int time);
/**
* Set the approx. time a transition will take
*/
void setTimePerTransition(int time);
// Customize indicator position and style
/**
* Set the position of the indicator bar.
*/
void setIndicatorPosition(IndicatorPosition pos);
/**
* Set the direction of the indicator bar. Defining the order of frames ASCENDING / DESCENDING
*/
void setIndicatorDirection(IndicatorDirection dir);
/**
* Set the symbole to indicate an active frame in the indicator bar.
*/
void setActiveSymbole(const char* symbole);
/**
* Set the symbole to indicate an inactive frame in the indicator bar.
*/
void setInactiveSymbole(const char* symbole);
// Frame settings
/**
* Configure what animation is used to transition from one frame to another
*/
void setFrameAnimation(AnimationDirection dir);
/**
* Add frame drawing functions
*/
void setFrames(FrameCallback* frameFunctions, int frameCount);
// Overlay
/**
* Add overlays drawing functions that are draw independent of the Frames
*/
void setOverlays(OverlayCallback* overlayFunctions, int overlayCount);
// Manuell Controll
void nextFrame();
void previousFrame();
// State Info
SSD1306UiState getUiState();
int update();
};