Skip to content

Commit 9520e17

Browse files
authored
Add GEOMETRY_64_48 and drawStringf() (ThingPulse#198)
1 parent 593ddc0 commit 9520e17

File tree

6 files changed

+69
-32
lines changed

6 files changed

+69
-32
lines changed

README_GEOMETRY_64_48.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# GEOMETRY_64_48
2+
3+
The 64x48 geometry setting are working with the `Wire.h` and `brzo_i2c` libraries.
4+
5+
I've tested it successfully with a WEMOS D1 mini Lite and a WEMOS OLED shield
6+
7+
Initialization code:
8+
9+
- Wire
10+
```
11+
#include <Wire.h>
12+
#include <SSD1306Wire.h>
13+
SSD1306Wire display(0x3c, D2, D1, GEOMETRY_64_48 ); // WEMOS OLED shield
14+
```
15+
16+
- BRZO i2c
17+
```
18+
#include <SSD1306Brzo.h>
19+
SSD1306Brzo display(0x3c, D2, D1, GEOMETRY_64_48 ); // WEMOS OLED Shield
20+
```

src/OLEDDisplay.cpp

+17-3
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,15 @@ void OLEDDisplay::drawString(int16_t xMove, int16_t yMove, String strUser) {
549549
free(text);
550550
}
551551

552+
void OLEDDisplay::drawStringf( int16_t x, int16_t y, char* buffer, String format, ... )
553+
{
554+
va_list myargs;
555+
va_start(myargs, format);
556+
vsprintf(buffer, format.c_str(), myargs);
557+
va_end(myargs);
558+
drawString( x, y, buffer );
559+
}
560+
552561
void OLEDDisplay::drawStringMaxWidth(int16_t xMove, int16_t yMove, uint16_t maxLineWidth, String strUser) {
553562
uint16_t firstChar = pgm_read_byte(fontData + FIRST_CHAR_POS);
554563
uint16_t lineHeight = pgm_read_byte(fontData + HEIGHT_POS);
@@ -836,15 +845,20 @@ int OLEDDisplay::_putc(int c) {
836845
// Private functions
837846
void OLEDDisplay::setGeometry(OLEDDISPLAY_GEOMETRY g, uint16_t width, uint16_t height) {
838847
this->geometry = g;
848+
839849
switch (g) {
840850
case GEOMETRY_128_64:
841851
this->displayWidth = 128;
842852
this->displayHeight = 64;
843853
break;
844-
case GEOMETRY_128_32:
854+
case GEOMETRY_128_32:
845855
this->displayWidth = 128;
846856
this->displayHeight = 32;
847857
break;
858+
case GEOMETRY_64_48:
859+
this->displayWidth = 64;
860+
this->displayHeight = 48;
861+
break;
848862
case GEOMETRY_RAWMODE:
849863
this->displayWidth = width > 0 ? width : 128;
850864
this->displayHeight = height > 0 ? height : 64;
@@ -872,15 +886,15 @@ void OLEDDisplay::sendInitCommands(void) {
872886
sendCommand(COMSCANINC);
873887
sendCommand(SETCOMPINS);
874888

875-
if (geometry == GEOMETRY_128_64) {
889+
if (geometry == GEOMETRY_128_64 || geometry == GEOMETRY_64_48) {
876890
sendCommand(0x12);
877891
} else if (geometry == GEOMETRY_128_32) {
878892
sendCommand(0x02);
879893
}
880894

881895
sendCommand(SETCONTRAST);
882896

883-
if (geometry == GEOMETRY_128_64) {
897+
if (geometry == GEOMETRY_128_64 || geometry == GEOMETRY_64_48) {
884898
sendCommand(0xCF);
885899
} else if (geometry == GEOMETRY_128_32) {
886900
sendCommand(0x8F);

src/OLEDDisplay.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ enum OLEDDISPLAY_TEXT_ALIGNMENT {
137137

138138
enum OLEDDISPLAY_GEOMETRY {
139139
GEOMETRY_128_64 = 0,
140-
GEOMETRY_128_32,
141-
GEOMETRY_RAWMODE,
140+
GEOMETRY_128_32 = 1,
141+
GEOMETRY_64_48 = 2,
142+
GEOMETRY_RAWMODE = 4
142143
};
143144

144145
enum HW_I2C {
@@ -237,6 +238,9 @@ class OLEDDisplay : public Stream {
237238
// Draws a string at the given location
238239
void drawString(int16_t x, int16_t y, String text);
239240

241+
// Draws a formatted string (like printf) at the given location
242+
void drawStringf(int16_t x, int16_t y, char* buffer, String format, ... );
243+
240244
// Draws a String with a maximum width at the given location.
241245
// If the given String is wider than the specified width
242246
// The text will be wrapped to the next line at a space or dash
@@ -280,7 +284,7 @@ class OLEDDisplay : public Stream {
280284
// normal brightness & contrast: contrast = 100
281285
void setContrast(uint8_t contrast, uint8_t precharge = 241, uint8_t comdetect = 64);
282286

283-
// Convenience method to access
287+
// Convenience method to access
284288
void setBrightness(uint8_t);
285289

286290
// Reset display rotation or mirroring

src/SSD1306Brzo.h

+24-19
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class SSD1306Brzo : public OLEDDisplay {
6161
}
6262

6363
void display(void) {
64+
const int x_offset = (128 - this->width()) / 2;
65+
6466
#ifdef OLEDDISPLAY_DOUBLE_BUFFER
6567
uint8_t minBoundY = UINT8_MAX;
6668
uint8_t maxBoundY = 0;
@@ -72,9 +74,9 @@ class SSD1306Brzo : public OLEDDisplay {
7274

7375
// Calculate the Y bounding box of changes
7476
// and copy buffer[pos] to buffer_back[pos];
75-
for (y = 0; y < (displayHeight / 8); y++) {
76-
for (x = 0; x < displayWidth; x++) {
77-
uint16_t pos = x + y * displayWidth;
77+
for (y = 0; y < (this->height() / 8); y++) {
78+
for (x = 0; x < this->width(); x++) {
79+
uint16_t pos = x + y * this->width();
7880
if (buffer[pos] != buffer_back[pos]) {
7981
minBoundY = _min(minBoundY, y);
8082
maxBoundY = _max(maxBoundY, y);
@@ -92,23 +94,26 @@ class SSD1306Brzo : public OLEDDisplay {
9294
if (minBoundY == UINT8_MAX) return;
9395

9496
sendCommand(COLUMNADDR);
95-
sendCommand(minBoundX);
96-
sendCommand(maxBoundX);
97+
sendCommand(x_offset + minBoundX);
98+
sendCommand(x_offset + maxBoundX);
9799

98100
sendCommand(PAGEADDR);
99101
sendCommand(minBoundY);
100102
sendCommand(maxBoundY);
101103

102104
byte k = 0;
103-
uint8_t sendBuffer[17];
105+
106+
int buflen = ( this->width() / 8 ) + 1;
107+
108+
uint8_t sendBuffer[buflen];
104109
sendBuffer[0] = 0x40;
105110
brzo_i2c_start_transaction(this->_address, BRZO_I2C_SPEED);
106111
for (y = minBoundY; y <= maxBoundY; y++) {
107112
for (x = minBoundX; x <= maxBoundX; x++) {
108113
k++;
109-
sendBuffer[k] = buffer[x + y * displayWidth];
110-
if (k == 16) {
111-
brzo_i2c_write(sendBuffer, 17, true);
114+
sendBuffer[k] = buffer[x + y * this->width()];
115+
if (k == (buflen-1)) {
116+
brzo_i2c_write(sendBuffer, buflen, true);
112117
k = 0;
113118
}
114119
}
@@ -119,28 +124,28 @@ class SSD1306Brzo : public OLEDDisplay {
119124
#else
120125
// No double buffering
121126
sendCommand(COLUMNADDR);
122-
sendCommand(0x0);
123-
sendCommand(0x7F);
127+
128+
sendCommand(x_offset);
129+
sendCommand(x_offset + (this->width() - 1));
124130

125131
sendCommand(PAGEADDR);
126132
sendCommand(0x0);
133+
sendCommand((this->height() / 8) - 1);
127134

128-
if (geometry == GEOMETRY_128_64) {
129-
sendCommand(0x7);
130-
} else if (geometry == GEOMETRY_128_32) {
131-
sendCommand(0x3);
132-
}
135+
int buflen = ( this->width() / 8 ) + 1;
133136

134-
uint8_t sendBuffer[17];
137+
uint8_t sendBuffer[buflen];
135138
sendBuffer[0] = 0x40;
139+
136140
brzo_i2c_start_transaction(this->_address, BRZO_I2C_SPEED);
141+
137142
for (uint16_t i=0; i<displayBufferSize; i++) {
138-
for (uint8_t x=1; x<17; x++) {
143+
for (uint8_t x=1; x<buflen; x++) {
139144
sendBuffer[x] = buffer[i];
140145
i++;
141146
}
142147
i--;
143-
brzo_i2c_write(sendBuffer, 17, true);
148+
brzo_i2c_write(sendBuffer, buflen, true);
144149
yield();
145150
}
146151
brzo_i2c_end_transaction();

src/SSD1306Spi.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class SSD1306Spi : public OLEDDisplay {
130130
sendCommand(PAGEADDR);
131131
sendCommand(0x0);
132132

133-
if (geometry == GEOMETRY_128_64) {
133+
if (geometry == GEOMETRY_128_64 || geometry == GEOMETRY_64_48) {
134134
sendCommand(0x7);
135135
} else if (geometry == GEOMETRY_128_32) {
136136
sendCommand(0x3);

src/SSD1306Wire.h

-6
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,6 @@ class SSD1306Wire : public OLEDDisplay {
167167
sendCommand(PAGEADDR);
168168
sendCommand(0x0);
169169

170-
if (geometry == GEOMETRY_128_64) {
171-
sendCommand(0x7);
172-
} else if (geometry == GEOMETRY_128_32) {
173-
sendCommand(0x3);
174-
}
175-
176170
for (uint16_t i=0; i < displayBufferSize; i++) {
177171
_wire->beginTransmission(this->_address);
178172
_wire->write(0x40);

0 commit comments

Comments
 (0)