OLEDDisplayUi.h 8.0 KB

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