SH1106Wire.h 4.5 KB

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