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