SSD1306Wire.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 SSD1306Wire_h
  28. #define SSD1306Wire_h
  29. #include "OLEDDisplay.h"
  30. #include <Wire.h>
  31. class SSD1306Wire : public OLEDDisplay {
  32. private:
  33. uint8_t _address;
  34. uint8_t _sda;
  35. uint8_t _scl;
  36. public:
  37. SSD1306Wire(uint8_t _address, uint8_t _sda, uint8_t _scl) {
  38. this->_address = _address;
  39. this->_sda = _sda;
  40. this->_scl = _scl;
  41. }
  42. bool connect() {
  43. Wire.begin(this->_sda, this->_scl);
  44. // Let's use ~700khz if ESP8266 is in 160Mhz mode
  45. // this will be limited to ~400khz if the ESP8266 in 80Mhz mode.
  46. Wire.setClock(700000);
  47. return true;
  48. }
  49. void display(void) {
  50. #ifdef OLEDDISPLAY_DOUBLE_BUFFER
  51. uint8_t minBoundY = ~0;
  52. uint8_t maxBoundY = 0;
  53. uint8_t minBoundX = ~0;
  54. uint8_t maxBoundX = 0;
  55. uint8_t x, y;
  56. // Calculate the Y bounding box of changes
  57. // and copy buffer[pos] to buffer_back[pos];
  58. for (y = 0; y < (DISPLAY_HEIGHT / 8); y++) {
  59. for (x = 0; x < DISPLAY_WIDTH; x++) {
  60. uint16_t pos = x + y * DISPLAY_WIDTH;
  61. if (buffer[pos] != buffer_back[pos]) {
  62. minBoundY = _min(minBoundY, y);
  63. maxBoundY = _max(maxBoundY, y);
  64. minBoundX = _min(minBoundX, x);
  65. maxBoundX = _max(maxBoundX, x);
  66. }
  67. buffer_back[pos] = buffer[pos];
  68. }
  69. yield();
  70. }
  71. // If the minBoundY wasn't updated
  72. // we can savely assume that buffer_back[pos] == buffer[pos]
  73. // holdes true for all values of pos
  74. if (minBoundY == ~0) return;
  75. sendCommand(COLUMNADDR);
  76. sendCommand(minBoundX);
  77. sendCommand(maxBoundX);
  78. sendCommand(PAGEADDR);
  79. sendCommand(minBoundY);
  80. sendCommand(maxBoundY);
  81. byte k = 0;
  82. for (y = minBoundY; y <= maxBoundY; y++) {
  83. for (x = minBoundX; x <= maxBoundX; x++) {
  84. if (k == 0) {
  85. Wire.beginTransmission(_address);
  86. Wire.write(0x40);
  87. }
  88. Wire.write(buffer[x + y * DISPLAY_WIDTH]);
  89. k++;
  90. if (k == 16) {
  91. Wire.endTransmission();
  92. k = 0;
  93. }
  94. }
  95. yield();
  96. }
  97. if (k != 0) {
  98. Wire.endTransmission();
  99. }
  100. #else
  101. sendCommand(COLUMNADDR);
  102. sendCommand(0x0);
  103. sendCommand(0x7F);
  104. sendCommand(PAGEADDR);
  105. sendCommand(0x0);
  106. sendCommand(0x7);
  107. for (uint16_t i=0; i < DISPLAY_BUFFER_SIZE; i++) {
  108. Wire.beginTransmission(this->_address);
  109. Wire.write(0x40);
  110. for (uint8_t x = 0; x < 16; x++) {
  111. Wire.write(buffer[i]);
  112. i++;
  113. }
  114. i--;
  115. Wire.endTransmission();
  116. }
  117. #endif
  118. }
  119. private:
  120. inline void sendCommand(uint8_t command) __attribute__((always_inline)){
  121. Wire.beginTransmission(_address);
  122. Wire.write(0x80);
  123. Wire.write(command);
  124. Wire.endTransmission();
  125. }
  126. };
  127. #endif