SH1106Brzo.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 SH1106Brzo_h
  28. #define SH1106Brzo_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 SH1106Brzo : public OLEDDisplay {
  37. private:
  38. uint8_t _address;
  39. uint8_t _sda;
  40. uint8_t _scl;
  41. public:
  42. SH1106Brzo(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. byte k = 0;
  78. uint8_t sendBuffer[17];
  79. sendBuffer[0] = 0x40;
  80. // Calculate the colum offset
  81. uint8_t minBoundXp2H = (minBoundX + 2) & 0x0F;
  82. uint8_t minBoundXp2L = 0x10 | ((minBoundX + 2) >> 4 );
  83. brzo_i2c_start_transaction(this->_address, BRZO_I2C_SPEED);
  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. 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. if (k != 0) {
  97. brzo_i2c_write(sendBuffer, k + 1, true);
  98. k = 0;
  99. }
  100. yield();
  101. }
  102. if (k != 0) {
  103. brzo_i2c_write(sendBuffer, k + 1, true);
  104. }
  105. brzo_i2c_end_transaction();
  106. #else
  107. #endif
  108. }
  109. private:
  110. inline void sendCommand(uint8_t com) __attribute__((always_inline)){
  111. uint8_t command[2] = {0x80 /* command mode */, com};
  112. brzo_i2c_start_transaction(_address, BRZO_I2C_SPEED);
  113. brzo_i2c_write(command, 2, true);
  114. brzo_i2c_end_transaction();
  115. }
  116. };
  117. #endif