Skip to content

Commit b55edff

Browse files
authored
Adds support for also running on NRF52 targets (#291)
* Fix problem of transitions getting faster with each call of setTargetFPS On my app I noticed that each time I called setTargetFPS the transitions would become faster and faster, until finally they don't even take one frame. The problem was that the FP math for calculating changeRatio would (because of rounding err) keep shrinking the number of ticksPerFrame. This change instead computs ticksPerFrame/perTransition based on the requested display time, so error can't accumulate from a series of calls. (In my app I only bump up target FPS before each transition but then I lower it down again afterwards to save CPU) * Allow this library to work on other Arduinos (NRF52) besides ESPs * Let users specify -1 for scl/sda to mean 'use current value' (Also makes it easier to share code when users of library are targeting multiple platforms) * fix missing dependency, std::max is defined in algorithm
1 parent 44379c3 commit b55edff

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

library.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"frameworks": "arduino",
2424
"platforms": [
2525
"espressif8266",
26-
"espressif32"
26+
"espressif32",
27+
"nordicnrf52"
2728
]
2829
}

src/SSD1306Wire.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include "OLEDDisplay.h"
3535
#include <Wire.h>
36+
#include <algorithm>
3637

3738
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_STM32)
3839
#define _min min
@@ -42,12 +43,12 @@
4243
class SSD1306Wire : public OLEDDisplay {
4344
private:
4445
uint8_t _address;
45-
uint8_t _sda;
46-
uint8_t _scl;
46+
int _sda;
47+
int _scl;
4748
bool _doI2cAutoInit = false;
4849

4950
public:
50-
SSD1306Wire(uint8_t _address, uint8_t _sda, uint8_t _scl, OLEDDISPLAY_GEOMETRY g = GEOMETRY_128_64) {
51+
SSD1306Wire(uint8_t _address, int _sda = -1, int _scl = -1, OLEDDISPLAY_GEOMETRY g = GEOMETRY_128_64) {
5152
setGeometry(g);
5253

5354
this->_address = _address;
@@ -56,10 +57,12 @@ class SSD1306Wire : public OLEDDisplay {
5657
}
5758

5859
bool connect() {
59-
#ifdef ARDUINO_ARCH_AVR
60+
#if !defined(ARDUINO_ARCH_ESP32) && !defined(ARDUINO_ARCH8266)
6061
Wire.begin();
6162
#else
62-
Wire.begin(this->_sda, this->_scl);
63+
// On ESP32 arduino, -1 means 'don't change pins', someone else has called begin for us.
64+
if(this->_sda != -1)
65+
Wire.begin(this->_sda, this->_scl);
6366
#endif
6467
// Let's use ~700khz if ESP8266 is in 160Mhz mode
6568
// this will be limited to ~400khz if the ESP8266 in 80Mhz mode.
@@ -84,10 +87,10 @@ class SSD1306Wire : public OLEDDisplay {
8487
for (x = 0; x < this->width(); x++) {
8588
uint16_t pos = x + y * this->width();
8689
if (buffer[pos] != buffer_back[pos]) {
87-
minBoundY = _min(minBoundY, y);
88-
maxBoundY = _max(maxBoundY, y);
89-
minBoundX = _min(minBoundX, x);
90-
maxBoundX = _max(maxBoundX, x);
90+
minBoundY = std::min(minBoundY, y);
91+
maxBoundY = std::max(maxBoundY, y);
92+
minBoundX = std::min(minBoundX, x);
93+
maxBoundX = std::max(maxBoundX, x);
9194
}
9295
buffer_back[pos] = buffer[pos];
9396
}
@@ -175,7 +178,7 @@ class SSD1306Wire : public OLEDDisplay {
175178

176179
void initI2cIfNeccesary() {
177180
if (_doI2cAutoInit) {
178-
#ifdef ARDUINO_ARCH_AVR
181+
#if !defined(ARDUINO_ARCH_ESP32) && !defined(ARDUINO_ARCH8266)
179182
Wire.begin();
180183
#else
181184
Wire.begin(this->_sda, this->_scl);

0 commit comments

Comments
 (0)