OLEDDisplay.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. * Credits for parts of this code go to Mike Rankin. Thank you so much for sharing!
  26. */
  27. #ifndef OLEDDISPLAY_h
  28. #define OLEDDISPLAY_h
  29. #include <Arduino.h>
  30. #include "OLEDDisplayFonts.h"
  31. //#define DEBUG_OLEDDISPLAY(...) Serial.printf( __VA_ARGS__ )
  32. #ifndef DEBUG_OLEDDISPLAY
  33. #define DEBUG_OLEDDISPLAY(...)
  34. #endif
  35. // Use DOUBLE BUFFERING by default
  36. #ifndef OLEDDISPLAY_REDUCE_MEMORY
  37. #define OLEDDISPLAY_DOUBLE_BUFFER
  38. #endif
  39. // Display settings
  40. #define DISPLAY_WIDTH 128
  41. #define DISPLAY_HEIGHT 64
  42. #define DISPLAY_BUFFER_SIZE 1024
  43. // Header Values
  44. #define JUMPTABLE_BYTES 4
  45. #define JUMPTABLE_LSB 1
  46. #define JUMPTABLE_SIZE 2
  47. #define JUMPTABLE_WIDTH 3
  48. #define JUMPTABLE_START 4
  49. #define WIDTH_POS 0
  50. #define HEIGHT_POS 1
  51. #define FIRST_CHAR_POS 2
  52. #define CHAR_NUM_POS 3
  53. // Display commands
  54. #define CHARGEPUMP 0x8D
  55. #define COLUMNADDR 0x21
  56. #define COMSCANDEC 0xC8
  57. #define COMSCANINC 0xC0
  58. #define DISPLAYALLON 0xA5
  59. #define DISPLAYALLON_RESUME 0xA4
  60. #define DISPLAYOFF 0xAE
  61. #define DISPLAYON 0xAF
  62. #define EXTERNALVCC 0x1
  63. #define INVERTDISPLAY 0xA7
  64. #define MEMORYMODE 0x20
  65. #define NORMALDISPLAY 0xA6
  66. #define PAGEADDR 0x22
  67. #define SEGREMAP 0xA0
  68. #define SETCOMPINS 0xDA
  69. #define SETCONTRAST 0x81
  70. #define SETDISPLAYCLOCKDIV 0xD5
  71. #define SETDISPLAYOFFSET 0xD3
  72. #define SETHIGHCOLUMN 0x10
  73. #define SETLOWCOLUMN 0x00
  74. #define SETMULTIPLEX 0xA8
  75. #define SETPRECHARGE 0xD9
  76. #define SETSEGMENTREMAP 0xA1
  77. #define SETSTARTLINE 0x40
  78. #define SETVCOMDETECT 0xDB
  79. #define SWITCHCAPVCC 0x2
  80. #ifndef _swap_int16_t
  81. #define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; }
  82. #endif
  83. enum OLEDDISPLAY_COLOR {
  84. BLACK = 0,
  85. WHITE = 1,
  86. INVERSE = 2
  87. };
  88. enum OLEDDISPLAY_TEXT_ALIGNMENT {
  89. TEXT_ALIGN_LEFT = 0,
  90. TEXT_ALIGN_RIGHT = 1,
  91. TEXT_ALIGN_CENTER = 2,
  92. TEXT_ALIGN_CENTER_BOTH = 3
  93. };
  94. class OLEDDisplay : public Print {
  95. public:
  96. // Initialize the display
  97. bool init();
  98. // Free the memory used by the display
  99. void end();
  100. // Cycle through the initialization
  101. void resetDisplay(void);
  102. /* Drawing functions */
  103. // Sets the color of all pixel operations
  104. void setColor(OLEDDISPLAY_COLOR color);
  105. // Draw a pixel at given position
  106. void setPixel(int16_t x, int16_t y);
  107. // Draw a line from position 0 to position 1
  108. void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1);
  109. // Draw the border of a rectangle at the given location
  110. void drawRect(int16_t x, int16_t y, int16_t width, int16_t height);
  111. // Fill the rectangle
  112. void fillRect(int16_t x, int16_t y, int16_t width, int16_t height);
  113. // Draw the border of a circle
  114. void drawCircle(int16_t x, int16_t y, int16_t radius);
  115. // Draw all Quadrants specified in the quads bit mask
  116. void drawCircleQuads(int16_t x0, int16_t y0, int16_t radius, uint8_t quads);
  117. // Fill circle
  118. void fillCircle(int16_t x, int16_t y, int16_t radius);
  119. // Draw a line horizontally
  120. void drawHorizontalLine(int16_t x, int16_t y, int16_t length);
  121. // Draw a lin vertically
  122. void drawVerticalLine(int16_t x, int16_t y, int16_t length);
  123. // Draws a rounded progress bar with the outer dimensions given by width and height. Progress is
  124. // a unsigned byte value between 0 and 100
  125. void drawProgressBar(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t progress);
  126. // Draw a bitmap in the internal image format
  127. void drawFastImage(int16_t x, int16_t y, int16_t width, int16_t height, const char *image);
  128. // Draw a XBM
  129. void drawXbm(int16_t x, int16_t y, int16_t width, int16_t height, const char *xbm);
  130. /* Text functions */
  131. // Draws a string at the given location
  132. void drawString(int16_t x, int16_t y, String text);
  133. // Draws a String with a maximum width at the given location.
  134. // If the given String is wider than the specified width
  135. // The text will be wrapped to the next line at a space or dash
  136. void drawStringMaxWidth(int16_t x, int16_t y, uint16_t maxLineWidth, String text);
  137. // Returns the width of the const char* with the current
  138. // font settings
  139. uint16_t getStringWidth(const char* text, uint16_t length);
  140. // Convencience method for the const char version
  141. uint16_t getStringWidth(String text);
  142. // Specifies relative to which anchor point
  143. // the text is rendered. Available constants:
  144. // TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER_BOTH
  145. void setTextAlignment(OLEDDISPLAY_TEXT_ALIGNMENT textAlignment);
  146. // Sets the current font. Available default fonts
  147. // ArialMT_Plain_10, ArialMT_Plain_16, ArialMT_Plain_24
  148. void setFont(const char *fontData);
  149. /* Display functions */
  150. // Turn the display on
  151. void displayOn(void);
  152. // Turn the display offs
  153. void displayOff(void);
  154. // Inverted display mode
  155. void invertDisplay(void);
  156. // Normal display mode
  157. void normalDisplay(void);
  158. // Set display contrast
  159. void setContrast(char contrast);
  160. // Turn the display upside down
  161. void flipScreenVertically();
  162. // Write the buffer to the display memory
  163. virtual void display(void) = 0;
  164. // Clear the local pixel buffer
  165. void clear(void);
  166. // Log buffer implementation
  167. // This will define the lines and characters you can
  168. // print to the screen. When you exeed the buffer size (lines * chars)
  169. // the output may be truncated due to the size constraint.
  170. bool setLogBuffer(uint16_t lines, uint16_t chars);
  171. // Draw the log buffer at position (x, y)
  172. void drawLogBuffer(uint16_t x, uint16_t y);
  173. // Implementent needed function to be compatible with Print class
  174. size_t write(uint8_t c);
  175. size_t write(const char* s);
  176. uint8_t *buffer;
  177. #ifdef OLEDDISPLAY_DOUBLE_BUFFER
  178. uint8_t *buffer_back;
  179. #endif
  180. protected:
  181. OLEDDISPLAY_TEXT_ALIGNMENT textAlignment = TEXT_ALIGN_LEFT;
  182. OLEDDISPLAY_COLOR color = WHITE;
  183. const char *fontData = ArialMT_Plain_10;
  184. // State values for logBuffer
  185. uint16_t logBufferSize = 0;
  186. uint16_t logBufferFilled = 0;
  187. uint16_t logBufferLine = 0;
  188. uint16_t logBufferMaxLines = 0;
  189. char *logBuffer = NULL;
  190. // Send a command to the display (low level function)
  191. virtual void sendCommand(uint8_t com) {};
  192. // Connect to the display
  193. virtual bool connect() {};
  194. // Send all the init commands
  195. void sendInitCommands();
  196. // converts utf8 characters to extended ascii
  197. static char* utf8ascii(String s);
  198. static byte utf8ascii(byte ascii);
  199. void inline drawInternal(int16_t xMove, int16_t yMove, int16_t width, int16_t height, const char *data, uint16_t offset, uint16_t bytesInData) __attribute__((always_inline));
  200. void drawStringInternal(int16_t xMove, int16_t yMove, char* text, uint16_t textLength, uint16_t textWidth);
  201. };
  202. #endif