SSD1306Brzo.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 SSD1306Brzo_h
  28. #define SSD1306Brzo_h
  29. #include "OLEDDisplay.h"
  30. #include <brzo_i2c.h>
  31. #if F_CPU == 160000000L
  32. #define BRZO_I2C_SPEED 1000
  33. #else
  34. #define BRZO_I2C_SPEED 800
  35. #endif
  36. class SSD1306Brzo : public OLEDDisplay {
  37. private:
  38. uint8_t _address;
  39. uint8_t _sda;
  40. uint8_t _scl;
  41. public:
  42. SSD1306Brzo(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. brzo_i2c_setup(_sda, _scl, 0);
  49. return true;
  50. }
  51. void display(void) {
  52. #ifdef OLEDDISPLAY_DOUBLE_BUFFER
  53. uint8_t minBoundY = ~0;
  54. uint8_t maxBoundY = 0;
  55. uint8_t minBoundX = ~0;
  56. uint8_t maxBoundX = 0;
  57. uint8_t x, y;
  58. // Calculate the Y bounding box of changes
  59. // and copy buffer[pos] to buffer_back[pos];
  60. for (y = 0; y < (DISPLAY_HEIGHT / 8); y++) {
  61. for (x = 0; x < DISPLAY_WIDTH; x++) {
  62. uint16_t pos = x + y * DISPLAY_WIDTH;
  63. if (buffer[pos] != buffer_back[pos]) {
  64. minBoundY = _min(minBoundY, y);
  65. maxBoundY = _max(maxBoundY, y);
  66. minBoundX = _min(minBoundX, x);
  67. maxBoundX = _max(maxBoundX, x);
  68. }
  69. buffer_back[pos] = buffer[pos];
  70. }
  71. yield();
  72. }
  73. // If the minBoundY wasn't updated
  74. // we can savely assume that buffer_back[pos] == buffer[pos]
  75. // holdes true for all values of pos
  76. if (minBoundY == ~0) return;
  77. sendCommand(COLUMNADDR);
  78. sendCommand(minBoundX);
  79. sendCommand(maxBoundX);
  80. sendCommand(PAGEADDR);
  81. sendCommand(minBoundY);
  82. sendCommand(maxBoundY);
  83. byte k = 0;
  84. uint8_t sendBuffer[17];
  85. sendBuffer[0] = 0x40;
  86. brzo_i2c_start_transaction(this->_address, BRZO_I2C_SPEED);
  87. for (y = minBoundY; y <= maxBoundY; y++) {
  88. for (x = minBoundX; x <= maxBoundX; x++) {
  89. k++;
  90. sendBuffer[k] = buffer[x + y * DISPLAY_WIDTH];
  91. if (k == 16) {
  92. brzo_i2c_write(sendBuffer, 17, true);
  93. k = 0;
  94. }
  95. }
  96. yield();
  97. }
  98. brzo_i2c_write(sendBuffer, k + 1, true);
  99. brzo_i2c_end_transaction();
  100. #else
  101. // No double buffering
  102. sendCommand(COLUMNADDR);
  103. sendCommand(0x0);
  104. sendCommand(0x7F);
  105. sendCommand(PAGEADDR);
  106. sendCommand(0x0);
  107. sendCommand(0x7);
  108. uint8_t sendBuffer[17];
  109. sendBuffer[0] = 0x40;
  110. brzo_i2c_start_transaction(this->_address, BRZO_I2C_SPEED);
  111. for (uint16_t i=0; i<DISPLAY_BUFFER_SIZE; i++) {
  112. for (uint8_t x=1; x<17; x++) {
  113. sendBuffer[x] = buffer[i];
  114. i++;
  115. }
  116. i--;
  117. brzo_i2c_write(sendBuffer, 17, true);
  118. yield();
  119. }
  120. brzo_i2c_end_transaction();
  121. #endif
  122. }
  123. private:
  124. inline void sendCommand(uint8_t com) __attribute__((always_inline)){
  125. uint8_t command[2] = {0x80 /* command mode */, com};
  126. brzo_i2c_start_transaction(_address, BRZO_I2C_SPEED);
  127. brzo_i2c_write(command, 2, true);
  128. brzo_i2c_end_transaction();
  129. }
  130. };
  131. #endif