ThingPulse OLED SSD1306

Dependents:   Turtle_RadioShuttle

Committer:
Helmut Tschemernjak
Date:
Thu Jun 06 09:29:02 2019 +0200
Revision:
3:99a409809366
Parent:
1:9270c15c6aea
Updated with latest version for github

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Helmut64 0:56dd5df33ab4 1 /**
Helmut64 0:56dd5df33ab4 2 * The MIT License (MIT)
Helmut64 0:56dd5df33ab4 3 *
Helmut64 0:56dd5df33ab4 4 * Copyright (c) 2018 by ThingPulse, Daniel Eichhorn
Helmut64 0:56dd5df33ab4 5 * Copyright (c) 2018 by Fabrice Weinberg
Helmut64 0:56dd5df33ab4 6 * Copyright (c) 2019 by Helmut Tschemernjak - www.radioshuttle.de
Helmut64 0:56dd5df33ab4 7 *
Helmut64 0:56dd5df33ab4 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
Helmut64 0:56dd5df33ab4 9 * of this software and associated documentation files (the "Software"), to deal
Helmut64 0:56dd5df33ab4 10 * in the Software without restriction, including without limitation the rights
Helmut64 0:56dd5df33ab4 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Helmut64 0:56dd5df33ab4 12 * copies of the Software, and to permit persons to whom the Software is
Helmut64 0:56dd5df33ab4 13 * furnished to do so, subject to the following conditions:
Helmut64 0:56dd5df33ab4 14 *
Helmut64 0:56dd5df33ab4 15 * The above copyright notice and this permission notice shall be included in all
Helmut64 0:56dd5df33ab4 16 * copies or substantial portions of the Software.
Helmut64 0:56dd5df33ab4 17 *
Helmut64 0:56dd5df33ab4 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Helmut64 0:56dd5df33ab4 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Helmut64 0:56dd5df33ab4 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Helmut64 0:56dd5df33ab4 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Helmut64 0:56dd5df33ab4 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Helmut64 0:56dd5df33ab4 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Helmut64 0:56dd5df33ab4 24 * SOFTWARE.
Helmut64 0:56dd5df33ab4 25 *
Helmut64 0:56dd5df33ab4 26 * ThingPulse invests considerable time and money to develop these open source libraries.
Helmut64 0:56dd5df33ab4 27 * Please support us by buying our products (and not the clones) from
Helmut64 0:56dd5df33ab4 28 * https://thingpulse.com
Helmut64 0:56dd5df33ab4 29 *
Helmut64 0:56dd5df33ab4 30 */
Helmut64 0:56dd5df33ab4 31
Helmut64 0:56dd5df33ab4 32 #ifndef OLEDDISPLAYUI_h
Helmut64 0:56dd5df33ab4 33 #define OLEDDISPLAYUI_h
Helmut64 0:56dd5df33ab4 34
Helmut64 0:56dd5df33ab4 35 #ifdef ARDUINO
Helmut64 0:56dd5df33ab4 36 #include <Arduino.h>
Helmut64 0:56dd5df33ab4 37 #elif __MBED__
Helmut64 0:56dd5df33ab4 38 #include <mbed.h>
Helmut64 0:56dd5df33ab4 39 #else
Helmut64 0:56dd5df33ab4 40 #error "Unkown operating system"
Helmut64 0:56dd5df33ab4 41 #endif
Helmut64 0:56dd5df33ab4 42
Helmut64 0:56dd5df33ab4 43 #include "OLEDDisplay.h"
Helmut64 0:56dd5df33ab4 44
Helmut64 0:56dd5df33ab4 45 //#define DEBUG_OLEDDISPLAYUI(...) Serial.printf( __VA_ARGS__ )
Helmut64 0:56dd5df33ab4 46
Helmut64 0:56dd5df33ab4 47 #ifndef DEBUG_OLEDDISPLAYUI
Helmut64 0:56dd5df33ab4 48 #define DEBUG_OLEDDISPLAYUI(...)
Helmut64 0:56dd5df33ab4 49 #endif
Helmut64 0:56dd5df33ab4 50
Helmut64 0:56dd5df33ab4 51 enum AnimationDirection {
Helmut64 0:56dd5df33ab4 52 SLIDE_UP,
Helmut64 0:56dd5df33ab4 53 SLIDE_DOWN,
Helmut64 0:56dd5df33ab4 54 SLIDE_LEFT,
Helmut64 0:56dd5df33ab4 55 SLIDE_RIGHT
Helmut64 0:56dd5df33ab4 56 };
Helmut64 0:56dd5df33ab4 57
Helmut64 0:56dd5df33ab4 58 enum IndicatorPosition {
Helmut64 0:56dd5df33ab4 59 TOP,
Helmut64 0:56dd5df33ab4 60 RIGHT,
Helmut64 0:56dd5df33ab4 61 BOTTOM,
Helmut64 0:56dd5df33ab4 62 LEFT
Helmut64 0:56dd5df33ab4 63 };
Helmut64 0:56dd5df33ab4 64
Helmut64 0:56dd5df33ab4 65 enum IndicatorDirection {
Helmut64 0:56dd5df33ab4 66 LEFT_RIGHT,
Helmut64 0:56dd5df33ab4 67 RIGHT_LEFT
Helmut64 0:56dd5df33ab4 68 };
Helmut64 0:56dd5df33ab4 69
Helmut64 0:56dd5df33ab4 70 enum FrameState {
Helmut64 0:56dd5df33ab4 71 IN_TRANSITION,
Helmut64 0:56dd5df33ab4 72 FIXED
Helmut64 0:56dd5df33ab4 73 };
Helmut64 0:56dd5df33ab4 74
Helmut64 0:56dd5df33ab4 75
Helmut64 0:56dd5df33ab4 76 const uint8_t ANIMATION_activeSymbol[] PROGMEM = {
Helmut64 0:56dd5df33ab4 77 0x00, 0x18, 0x3c, 0x7e, 0x7e, 0x3c, 0x18, 0x00
Helmut64 0:56dd5df33ab4 78 };
Helmut64 0:56dd5df33ab4 79
Helmut64 0:56dd5df33ab4 80 const uint8_t ANIMATION_inactiveSymbol[] PROGMEM = {
Helmut64 0:56dd5df33ab4 81 0x00, 0x0, 0x0, 0x18, 0x18, 0x0, 0x0, 0x00
Helmut64 0:56dd5df33ab4 82 };
Helmut64 0:56dd5df33ab4 83
Helmut64 0:56dd5df33ab4 84
Helmut64 0:56dd5df33ab4 85 // Structure of the UiState
Helmut64 0:56dd5df33ab4 86 struct OLEDDisplayUiState {
Helmut64 0:56dd5df33ab4 87 uint64_t lastUpdate;
Helmut64 0:56dd5df33ab4 88 uint16_t ticksSinceLastStateSwitch;
Helmut64 0:56dd5df33ab4 89
Helmut64 0:56dd5df33ab4 90 FrameState frameState;
Helmut64 0:56dd5df33ab4 91 uint8_t currentFrame;
Helmut64 0:56dd5df33ab4 92
Helmut64 0:56dd5df33ab4 93 bool isIndicatorDrawen;
Helmut64 0:56dd5df33ab4 94
Helmut64 0:56dd5df33ab4 95 // Normal = 1, Inverse = -1;
Helmut64 0:56dd5df33ab4 96 int8_t frameTransitionDirection;
Helmut64 0:56dd5df33ab4 97
Helmut64 0:56dd5df33ab4 98 bool manuelControll;
Helmut64 0:56dd5df33ab4 99
Helmut64 0:56dd5df33ab4 100 // Custom data that can be used by the user
Helmut64 0:56dd5df33ab4 101 void* userData;
Helmut64 0:56dd5df33ab4 102 };
Helmut64 0:56dd5df33ab4 103
Helmut64 0:56dd5df33ab4 104 struct LoadingStage {
Helmut64 0:56dd5df33ab4 105 const char* process;
Helmut64 0:56dd5df33ab4 106 void (*callback)();
Helmut64 0:56dd5df33ab4 107 };
Helmut64 0:56dd5df33ab4 108
Helmut64 0:56dd5df33ab4 109 typedef void (*FrameCallback)(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
Helmut64 0:56dd5df33ab4 110 typedef void (*OverlayCallback)(OLEDDisplay *display, OLEDDisplayUiState* state);
Helmut64 0:56dd5df33ab4 111 typedef void (*LoadingDrawFunction)(OLEDDisplay *display, LoadingStage* stage, uint8_t progress);
Helmut64 0:56dd5df33ab4 112
Helmut64 0:56dd5df33ab4 113 class OLEDDisplayUi {
Helmut64 0:56dd5df33ab4 114 private:
Helmut64 0:56dd5df33ab4 115 OLEDDisplay *display;
Helmut64 0:56dd5df33ab4 116
Helmut64 0:56dd5df33ab4 117 // Symbols for the Indicator
Helmut64 0:56dd5df33ab4 118 IndicatorPosition indicatorPosition;
Helmut64 0:56dd5df33ab4 119 IndicatorDirection indicatorDirection;
Helmut64 0:56dd5df33ab4 120
Helmut64 0:56dd5df33ab4 121 const uint8_t* activeSymbol;
Helmut64 0:56dd5df33ab4 122 const uint8_t* inactiveSymbol;
Helmut64 0:56dd5df33ab4 123
Helmut64 0:56dd5df33ab4 124 bool shouldDrawIndicators;
Helmut64 0:56dd5df33ab4 125
Helmut64 0:56dd5df33ab4 126 // Values for the Frames
Helmut64 0:56dd5df33ab4 127 AnimationDirection frameAnimationDirection;
Helmut64 0:56dd5df33ab4 128
Helmut64 0:56dd5df33ab4 129 int8_t lastTransitionDirection;
Helmut64 0:56dd5df33ab4 130
Helmut64 0:56dd5df33ab4 131 uint16_t ticksPerFrame; // ~ 5000ms at 30 FPS
Helmut64 0:56dd5df33ab4 132 uint16_t ticksPerTransition; // ~ 500ms at 30 FPS
Helmut64 0:56dd5df33ab4 133
Helmut64 0:56dd5df33ab4 134 bool autoTransition;
Helmut64 0:56dd5df33ab4 135
Helmut64 0:56dd5df33ab4 136 FrameCallback* frameFunctions;
Helmut64 0:56dd5df33ab4 137 uint8_t frameCount;
Helmut64 0:56dd5df33ab4 138
Helmut64 0:56dd5df33ab4 139 // Internally used to transition to a specific frame
Helmut64 0:56dd5df33ab4 140 int8_t nextFrameNumber;
Helmut64 0:56dd5df33ab4 141
Helmut64 0:56dd5df33ab4 142 // Values for Overlays
Helmut64 0:56dd5df33ab4 143 OverlayCallback* overlayFunctions;
Helmut64 0:56dd5df33ab4 144 uint8_t overlayCount;
Helmut64 0:56dd5df33ab4 145
Helmut64 0:56dd5df33ab4 146 // Will the Indicator be drawen
Helmut64 0:56dd5df33ab4 147 // 3 Not drawn in both frames
Helmut64 0:56dd5df33ab4 148 // 2 Drawn this frame but not next
Helmut64 0:56dd5df33ab4 149 // 1 Not drown this frame but next
Helmut64 0:56dd5df33ab4 150 // 0 Not known yet
Helmut64 0:56dd5df33ab4 151 uint8_t indicatorDrawState;
Helmut64 0:56dd5df33ab4 152
Helmut64 0:56dd5df33ab4 153 // Loading screen
Helmut64 0:56dd5df33ab4 154 LoadingDrawFunction loadingDrawFunction;
Helmut64 0:56dd5df33ab4 155
Helmut64 0:56dd5df33ab4 156 // UI State
Helmut64 0:56dd5df33ab4 157 OLEDDisplayUiState state;
Helmut64 0:56dd5df33ab4 158
Helmut64 0:56dd5df33ab4 159 // Bookeeping for update
Helmut64 0:56dd5df33ab4 160 uint8_t updateInterval;
Helmut64 0:56dd5df33ab4 161
Helmut64 0:56dd5df33ab4 162 uint8_t getNextFrameNumber();
Helmut64 0:56dd5df33ab4 163 void drawIndicator();
Helmut64 0:56dd5df33ab4 164 void drawFrame();
Helmut64 0:56dd5df33ab4 165 void drawOverlays();
Helmut64 0:56dd5df33ab4 166 void tick();
Helmut64 0:56dd5df33ab4 167 void resetState();
Helmut64 0:56dd5df33ab4 168
Helmut64 0:56dd5df33ab4 169 public:
Helmut64 0:56dd5df33ab4 170
Helmut64 0:56dd5df33ab4 171 OLEDDisplayUi(OLEDDisplay *display);
Helmut64 0:56dd5df33ab4 172
Helmut64 0:56dd5df33ab4 173 /**
Helmut64 0:56dd5df33ab4 174 * Initialise the display
Helmut64 0:56dd5df33ab4 175 */
Helmut64 0:56dd5df33ab4 176 void init();
Helmut64 0:56dd5df33ab4 177
Helmut64 0:56dd5df33ab4 178 /**
Helmut64 0:56dd5df33ab4 179 * Configure the internal used target FPS
Helmut64 0:56dd5df33ab4 180 */
Helmut64 0:56dd5df33ab4 181 void setTargetFPS(uint8_t fps);
Helmut64 0:56dd5df33ab4 182
Helmut64 0:56dd5df33ab4 183 // Automatic Controll
Helmut64 0:56dd5df33ab4 184 /**
Helmut64 0:56dd5df33ab4 185 * Enable automatic transition to next frame after the some time can be configured with `setTimePerFrame` and `setTimePerTransition`.
Helmut64 0:56dd5df33ab4 186 */
Helmut64 0:56dd5df33ab4 187 void enableAutoTransition();
Helmut64 0:56dd5df33ab4 188
Helmut64 0:56dd5df33ab4 189 /**
Helmut64 0:56dd5df33ab4 190 * Disable automatic transition to next frame.
Helmut64 0:56dd5df33ab4 191 */
Helmut64 0:56dd5df33ab4 192 void disableAutoTransition();
Helmut64 0:56dd5df33ab4 193
Helmut64 0:56dd5df33ab4 194 /**
Helmut64 0:56dd5df33ab4 195 * Set the direction if the automatic transitioning
Helmut64 0:56dd5df33ab4 196 */
Helmut64 0:56dd5df33ab4 197 void setAutoTransitionForwards();
Helmut64 0:56dd5df33ab4 198 void setAutoTransitionBackwards();
Helmut64 0:56dd5df33ab4 199
Helmut64 0:56dd5df33ab4 200 /**
Helmut64 0:56dd5df33ab4 201 * Set the approx. time a frame is displayed
Helmut64 0:56dd5df33ab4 202 */
Helmut64 0:56dd5df33ab4 203 void setTimePerFrame(uint16_t time);
Helmut64 0:56dd5df33ab4 204
Helmut64 0:56dd5df33ab4 205 /**
Helmut64 0:56dd5df33ab4 206 * Set the approx. time a transition will take
Helmut64 0:56dd5df33ab4 207 */
Helmut64 0:56dd5df33ab4 208 void setTimePerTransition(uint16_t time);
Helmut64 0:56dd5df33ab4 209
Helmut64 0:56dd5df33ab4 210 // Customize indicator position and style
Helmut64 0:56dd5df33ab4 211
Helmut64 0:56dd5df33ab4 212 /**
Helmut64 0:56dd5df33ab4 213 * Draw the indicator.
Helmut64 0:56dd5df33ab4 214 * This is the defaut state for all frames if
Helmut64 0:56dd5df33ab4 215 * the indicator was hidden on the previous frame
Helmut64 0:56dd5df33ab4 216 * it will be slided in.
Helmut64 0:56dd5df33ab4 217 */
Helmut64 0:56dd5df33ab4 218 void enableIndicator();
Helmut64 0:56dd5df33ab4 219
Helmut64 0:56dd5df33ab4 220 /**
Helmut64 0:56dd5df33ab4 221 * Don't draw the indicator.
Helmut64 0:56dd5df33ab4 222 * This will slide out the indicator
Helmut64 0:56dd5df33ab4 223 * when transitioning to the next frame.
Helmut64 0:56dd5df33ab4 224 */
Helmut64 0:56dd5df33ab4 225 void disableIndicator();
Helmut64 0:56dd5df33ab4 226
Helmut64 0:56dd5df33ab4 227 /**
Helmut64 0:56dd5df33ab4 228 * Enable drawing of indicators
Helmut64 0:56dd5df33ab4 229 */
Helmut64 0:56dd5df33ab4 230 void enableAllIndicators();
Helmut64 0:56dd5df33ab4 231
Helmut64 0:56dd5df33ab4 232 /**
Helmut64 0:56dd5df33ab4 233 * Disable draw of indicators.
Helmut64 0:56dd5df33ab4 234 */
Helmut64 0:56dd5df33ab4 235 void disableAllIndicators();
Helmut64 0:56dd5df33ab4 236
Helmut64 0:56dd5df33ab4 237 /**
Helmut64 0:56dd5df33ab4 238 * Set the position of the indicator bar.
Helmut64 0:56dd5df33ab4 239 */
Helmut64 0:56dd5df33ab4 240 void setIndicatorPosition(IndicatorPosition pos);
Helmut64 0:56dd5df33ab4 241
Helmut64 0:56dd5df33ab4 242 /**
Helmut64 0:56dd5df33ab4 243 * Set the direction of the indicator bar. Defining the order of frames ASCENDING / DESCENDING
Helmut64 0:56dd5df33ab4 244 */
Helmut64 0:56dd5df33ab4 245 void setIndicatorDirection(IndicatorDirection dir);
Helmut64 0:56dd5df33ab4 246
Helmut64 0:56dd5df33ab4 247 /**
Helmut64 0:56dd5df33ab4 248 * Set the symbol to indicate an active frame in the indicator bar.
Helmut64 0:56dd5df33ab4 249 */
Helmut64 0:56dd5df33ab4 250 void setActiveSymbol(const uint8_t* symbol);
Helmut64 0:56dd5df33ab4 251
Helmut64 0:56dd5df33ab4 252 /**
Helmut64 0:56dd5df33ab4 253 * Set the symbol to indicate an inactive frame in the indicator bar.
Helmut64 0:56dd5df33ab4 254 */
Helmut64 0:56dd5df33ab4 255 void setInactiveSymbol(const uint8_t* symbol);
Helmut64 0:56dd5df33ab4 256
Helmut64 0:56dd5df33ab4 257
Helmut64 0:56dd5df33ab4 258 // Frame settings
Helmut64 0:56dd5df33ab4 259
Helmut64 0:56dd5df33ab4 260 /**
Helmut64 0:56dd5df33ab4 261 * Configure what animation is used to transition from one frame to another
Helmut64 0:56dd5df33ab4 262 */
Helmut64 0:56dd5df33ab4 263 void setFrameAnimation(AnimationDirection dir);
Helmut64 0:56dd5df33ab4 264
Helmut64 0:56dd5df33ab4 265 /**
Helmut64 0:56dd5df33ab4 266 * Add frame drawing functions
Helmut64 0:56dd5df33ab4 267 */
Helmut64 0:56dd5df33ab4 268 void setFrames(FrameCallback* frameFunctions, uint8_t frameCount);
Helmut64 0:56dd5df33ab4 269
Helmut64 0:56dd5df33ab4 270 // Overlay
Helmut64 0:56dd5df33ab4 271
Helmut64 0:56dd5df33ab4 272 /**
Helmut64 0:56dd5df33ab4 273 * Add overlays drawing functions that are draw independent of the Frames
Helmut64 0:56dd5df33ab4 274 */
Helmut64 0:56dd5df33ab4 275 void setOverlays(OverlayCallback* overlayFunctions, uint8_t overlayCount);
Helmut64 0:56dd5df33ab4 276
Helmut64 0:56dd5df33ab4 277
Helmut64 0:56dd5df33ab4 278 // Loading animation
Helmut64 0:56dd5df33ab4 279 /**
Helmut64 0:56dd5df33ab4 280 * Set the function that will draw each step
Helmut64 0:56dd5df33ab4 281 * in the loading animation
Helmut64 0:56dd5df33ab4 282 */
Helmut64 0:56dd5df33ab4 283 void setLoadingDrawFunction(LoadingDrawFunction loadingFunction);
Helmut64 0:56dd5df33ab4 284
Helmut64 0:56dd5df33ab4 285
Helmut64 0:56dd5df33ab4 286 /**
Helmut64 0:56dd5df33ab4 287 * Run the loading process
Helmut64 0:56dd5df33ab4 288 */
Helmut64 0:56dd5df33ab4 289 void runLoadingProcess(LoadingStage* stages, uint8_t stagesCount);
Helmut64 0:56dd5df33ab4 290
Helmut64 0:56dd5df33ab4 291
Helmut64 0:56dd5df33ab4 292 // Manual Control
Helmut64 0:56dd5df33ab4 293 void nextFrame();
Helmut64 0:56dd5df33ab4 294 void previousFrame();
Helmut64 0:56dd5df33ab4 295
Helmut64 0:56dd5df33ab4 296 /**
Helmut64 0:56dd5df33ab4 297 * Switch without transition to frame `frame`.
Helmut64 0:56dd5df33ab4 298 */
Helmut64 0:56dd5df33ab4 299 void switchToFrame(uint8_t frame);
Helmut64 0:56dd5df33ab4 300
Helmut64 0:56dd5df33ab4 301 /**
Helmut64 0:56dd5df33ab4 302 * Transition to frame `frame`, when the `frame` number is bigger than the current
Helmut64 0:56dd5df33ab4 303 * frame the forward animation will be used, otherwise the backwards animation is used.
Helmut64 0:56dd5df33ab4 304 */
Helmut64 0:56dd5df33ab4 305 void transitionToFrame(uint8_t frame);
Helmut64 0:56dd5df33ab4 306
Helmut64 0:56dd5df33ab4 307 // State Info
Helmut64 0:56dd5df33ab4 308 OLEDDisplayUiState* getUiState();
Helmut64 0:56dd5df33ab4 309
Helmut64 0:56dd5df33ab4 310 int8_t update();
Helmut64 0:56dd5df33ab4 311 };
Helmut64 0:56dd5df33ab4 312 #endif