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