SSD1306Spi.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 SSD1306Spi_h
  28. #define SSD1306Spi_h
  29. #include "OLEDDisplay.h"
  30. #include <SPI.h>
  31. #if F_CPU == 160000000L
  32. #define BRZO_I2C_SPEED 1000
  33. #else
  34. #define BRZO_I2C_SPEED 800
  35. #endif
  36. class SSD1306Spi : public OLEDDisplay {
  37. private:
  38. uint8_t _rst;
  39. uint8_t _dc;
  40. uint8_t _cs;
  41. public:
  42. SSD1306Spi(uint8_t _rst, uint8_t _dc, uint8_t _cs) {
  43. this->_rst = _rst;
  44. this->_dc = _dc;
  45. this->_cs = _cs;
  46. }
  47. bool connect(){
  48. pinMode(_dc, OUTPUT);
  49. pinMode(_cs, OUTPUT);
  50. pinMode(_rst, OUTPUT);
  51. SPI.begin ();
  52. SPI.setClockDivider (SPI_CLOCK_DIV2);
  53. // Pulse Reset low for 10ms
  54. digitalWrite(_rst, HIGH);
  55. delay(1);
  56. digitalWrite(_rst, LOW);
  57. delay(10);
  58. digitalWrite(_rst, HIGH);
  59. return true;
  60. }
  61. void display(void) {
  62. #ifdef OLEDDISPLAY_DOUBLE_BUFFER
  63. uint8_t minBoundY = ~0;
  64. uint8_t maxBoundY = 0;
  65. uint8_t minBoundX = ~0;
  66. uint8_t maxBoundX = 0;
  67. uint8_t x, y;
  68. // Calculate the Y bounding box of changes
  69. // and copy buffer[pos] to buffer_back[pos];
  70. for (y = 0; y < (DISPLAY_HEIGHT / 8); y++) {
  71. for (x = 0; x < DISPLAY_WIDTH; x++) {
  72. uint16_t pos = x + y * DISPLAY_WIDTH;
  73. if (buffer[pos] != buffer_back[pos]) {
  74. minBoundY = _min(minBoundY, y);
  75. maxBoundY = _max(maxBoundY, y);
  76. minBoundX = _min(minBoundX, x);
  77. maxBoundX = _max(maxBoundX, x);
  78. }
  79. buffer_back[pos] = buffer[pos];
  80. }
  81. yield();
  82. }
  83. // If the minBoundY wasn't updated
  84. // we can savely assume that buffer_back[pos] == buffer[pos]
  85. // holdes true for all values of pos
  86. if (minBoundY == ~0) return;
  87. sendCommand(COLUMNADDR);
  88. sendCommand(minBoundX);
  89. sendCommand(maxBoundX);
  90. sendCommand(PAGEADDR);
  91. sendCommand(minBoundY);
  92. sendCommand(maxBoundY);
  93. digitalWrite(_cs, HIGH);
  94. digitalWrite(_dc, HIGH); // data mode
  95. digitalWrite(_cs, LOW);
  96. for (y = minBoundY; y <= maxBoundY; y++) {
  97. for (x = minBoundX; x <= maxBoundX; x++) {
  98. SPI.transfer(buffer[x + y * DISPLAY_WIDTH]);
  99. }
  100. yield();
  101. }
  102. digitalWrite(_cs, HIGH);
  103. #else
  104. // No double buffering
  105. sendCommand(COLUMNADDR);
  106. sendCommand(0x0);
  107. sendCommand(0x7F);
  108. sendCommand(PAGEADDR);
  109. sendCommand(0x0);
  110. sendCommand(0x7);
  111. digitalWrite(_cs, HIGH);
  112. digitalWrite(_dc, HIGH); // data mode
  113. digitalWrite(_cs, LOW);
  114. for (uint16_t i=0; i<DISPLAY_BUFFER_SIZE; i++) {
  115. SPI.transfer(buffer[i]);
  116. yield();
  117. }
  118. digitalWrite(_cs, HIGH);
  119. #endif
  120. }
  121. private:
  122. inline void sendCommand(uint8_t com) __attribute__((always_inline)){
  123. digitalWrite(_cs, HIGH);
  124. digitalWrite(_dc, LOW);
  125. digitalWrite(_cs, LOW);
  126. SPI.transfer(com);
  127. digitalWrite(_cs, HIGH);
  128. }
  129. };
  130. #endif