Skip to content

Commit fb46a05

Browse files
authored
Merge pull request #57 from squix78/add-sh1106-support
Add support for SH1106
2 parents e4d3a42 + 269a937 commit fb46a05

File tree

3 files changed

+291
-0
lines changed

3 files changed

+291
-0
lines changed

SH1106.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 SH1106_h
29+
#define SH1106_h
30+
#include "SH1106Wire.h"
31+
32+
// For make SH1106 an alias for SH1106Wire
33+
typedef SH1106Wire SH1106;
34+
35+
36+
#endif

SH1106Brzo.h

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 SH1106Brzo_h
29+
#define SH1106Brzo_h
30+
31+
#include "OLEDDisplay.h"
32+
#include <brzo_i2c.h>
33+
34+
#if F_CPU == 160000000L
35+
#define BRZO_I2C_SPEED 1000
36+
#else
37+
#define BRZO_I2C_SPEED 800
38+
#endif
39+
40+
class SH1106Brzo : public OLEDDisplay {
41+
private:
42+
uint8_t _address;
43+
uint8_t _sda;
44+
uint8_t _scl;
45+
46+
public:
47+
SH1106Brzo(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+
brzo_i2c_setup(_sda, _scl, 0);
55+
return true;
56+
}
57+
58+
void display(void) {
59+
#ifdef OLEDDISPLAY_DOUBLE_BUFFER
60+
uint8_t minBoundY = ~0;
61+
uint8_t maxBoundY = 0;
62+
63+
uint8_t x, y;
64+
65+
// Calculate the Y bounding box of changes
66+
// and copy buffer[pos] to buffer_back[pos];
67+
for (y = 0; y < (DISPLAY_HEIGHT / 8); y++) {
68+
for (x = 0; x < DISPLAY_WIDTH; x++) {
69+
uint16_t pos = x + y * DISPLAY_WIDTH;
70+
if (buffer[pos] != buffer_back[pos]) {
71+
minBoundY = _min(minBoundY, y);
72+
maxBoundY = _max(maxBoundY, y);
73+
}
74+
buffer_back[pos] = buffer[pos];
75+
}
76+
yield();
77+
}
78+
79+
// If the minBoundY wasn't updated
80+
// we can savely assume that buffer_back[pos] == buffer[pos]
81+
// holdes true for all values of pos
82+
if (minBoundY == ~0) return;
83+
84+
byte k = 0;
85+
uint8_t sendBuffer[17];
86+
sendBuffer[0] = 0x40;
87+
brzo_i2c_start_transaction(this->_address, BRZO_I2C_SPEED);
88+
for (y = minBoundY; y <= maxBoundY; y++) {
89+
sendCommand(0xB0 + y);
90+
sendCommand(0x02);
91+
sendCommand(0x10);
92+
for (x = 0; x < DISPLAY_WIDTH; x++) {
93+
k++;
94+
sendBuffer[k] = buffer[x + y * DISPLAY_WIDTH];
95+
if (k == 16) {
96+
brzo_i2c_write(sendBuffer, 17, true);
97+
k = 0;
98+
}
99+
}
100+
yield();
101+
}
102+
brzo_i2c_write(sendBuffer, k + 1, true);
103+
brzo_i2c_end_transaction();
104+
#else
105+
#endif
106+
}
107+
108+
private:
109+
inline void sendCommand(uint8_t com) __attribute__((always_inline)){
110+
uint8_t command[2] = {0x80 /* command mode */, com};
111+
brzo_i2c_start_transaction(_address, BRZO_I2C_SPEED);
112+
brzo_i2c_write(command, 2, true);
113+
brzo_i2c_end_transaction();
114+
}
115+
};
116+
117+
#endif

SH1106Wire.h

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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

Comments
 (0)