SH1106Spi.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 SH1106Spi_h
  28. #define SH1106Spi_h
  29. #include "OLEDDisplay.h"
  30. #include <SPI.h>
  31. class SH1106Spi : public OLEDDisplay {
  32. private:
  33. uint8_t _rst;
  34. uint8_t _dc;
  35. public:
  36. SH1106Spi(uint8_t _rst, uint8_t _dc) {
  37. this->_rst = _rst;
  38. this->_dc = _dc;
  39. }
  40. bool connect(){
  41. pinMode(_dc, OUTPUT);
  42. pinMode(_rst, OUTPUT);
  43. SPI.begin ();
  44. SPI.setClockDivider (SPI_CLOCK_DIV2);
  45. // Pulse Reset low for 10ms
  46. digitalWrite(_rst, HIGH);
  47. delay(1);
  48. digitalWrite(_rst, LOW);
  49. delay(10);
  50. digitalWrite(_rst, HIGH);
  51. return true;
  52. }
  53. void display(void) {
  54. #ifdef OLEDDISPLAY_DOUBLE_BUFFER
  55. uint8_t minBoundY = ~0;
  56. uint8_t maxBoundY = 0;
  57. uint8_t minBoundX = ~0;
  58. uint8_t maxBoundX = 0;
  59. uint8_t x, y;
  60. // Calculate the Y bounding box of changes
  61. // and copy buffer[pos] to buffer_back[pos];
  62. for (y = 0; y < (DISPLAY_HEIGHT / 8); y++) {
  63. for (x = 0; x < DISPLAY_WIDTH; x++) {
  64. uint16_t pos = x + y * DISPLAY_WIDTH;
  65. if (buffer[pos] != buffer_back[pos]) {
  66. minBoundY = _min(minBoundY, y);
  67. maxBoundY = _max(maxBoundY, y);
  68. minBoundX = _min(minBoundX, x);
  69. maxBoundX = _max(maxBoundX, x);
  70. }
  71. buffer_back[pos] = buffer[pos];
  72. }
  73. yield();
  74. }
  75. // If the minBoundY wasn't updated
  76. // we can savely assume that buffer_back[pos] == buffer[pos]
  77. // holdes true for all values of pos
  78. if (minBoundY == ~0) return;
  79. // Calculate the colum offset
  80. uint8_t minBoundXp2H = (minBoundX + 2) & 0x0F;
  81. uint8_t minBoundXp2L = 0x10 | ((minBoundX + 2) >> 4 );
  82. for (y = minBoundY; y <= maxBoundY; y++) {
  83. sendCommand(0xB0 + y);
  84. sendCommand(minBoundXp2H);
  85. sendCommand(minBoundXp2L);
  86. digitalWrite(_dc, HIGH); // data mode
  87. for (x = minBoundX; x <= maxBoundX; x++) {
  88. SPI.transfer(buffer[x + y * DISPLAY_WIDTH]);
  89. }
  90. yield();
  91. }
  92. #else
  93. for (uint8_t y=0; y<DISPLAY_HEIGHT/8; y++) {
  94. sendCommand(0xB0 + y);
  95. sendCommand(0x02);
  96. sendCommand(0x10);
  97. digitalWrite(_dc, HIGH); // data mode
  98. for( uint8_t x=0; x < DISPLAY_WIDTH; x++) {
  99. SPI.transfer(buffer[x + y * DISPLAY_WIDTH]);
  100. }
  101. yield();
  102. }
  103. #endif
  104. }
  105. private:
  106. inline void sendCommand(uint8_t com) __attribute__((always_inline)){
  107. digitalWrite(_dc, LOW);
  108. SPI.transfer(com);
  109. }
  110. };
  111. #endif