Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct library to support different pins for I2C , add example, and … #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 49 additions & 17 deletions ESP_SSD1306.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ void ESP_SSD1306::drawPixel(int16_t x, int16_t y, uint16_t color) {

}

ESP_SSD1306::ESP_SSD1306(int8_t SID, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) {
//constructor for SPI display , SPI value false dosnt have sance
ESP_SSD1306::ESP_SSD1306(bool SPI, int8_t SID, int8_t SCLK, int8_t DC, int8_t CS, int8_t RST) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) {
spi = true;
cs = CS;
rst = RST;
dc = DC;
Expand All @@ -160,28 +162,58 @@ ESP_SSD1306::ESP_SSD1306(int8_t SID, int8_t SCLK, int8_t DC, int8_t RST, int8_t
hwSPI = false;
}

// constructor for hardware SPI - we indicate DataCommand, ChipSelect, Reset
ESP_SSD1306::ESP_SSD1306(int8_t DC, int8_t RST, int8_t CS) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) {
dc = DC;
rst = RST;
cs = CS;
hwSPI = true;
// constructor for hardware SPI - we indicate DataCommand, ChipSelect, Reset, or I2C with reset pin
// if SPI false DC is SDA, CS is SCL and RST is reset pin
ESP_SSD1306::ESP_SSD1306(bool SPI, int8_t DC,int8_t CS,int8_t RST) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) {
spi = SPI;
if(spi){
dc = DC;
rst = RST;
cs = CS;
hwSPI = true;
sda = scl = -1;
} else {
sclk = dc = cs = sid = -1;
sda = DC;
scl = CS;
rst = RST;
}
}

// initializer for I2C - we only indicate the reset pin!
ESP_SSD1306::ESP_SSD1306(int8_t reset) :
Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) {
// SPI value of true dosnt have sance
ESP_SSD1306::ESP_SSD1306(bool SPI, int8_t SDA, int8_t SCL) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) {
spi = false;
rst = sclk = dc = cs = sid = -1;
sda = SDA;
scl = SCL;
}

// initializer for I2C - we only indicate the reset pin!
// SPI value of true dosnt have sance
ESP_SSD1306::ESP_SSD1306(bool SPI, int8_t RST) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) {
spi = false;
sclk = dc = cs = sid = -1;
rst = reset;
rst = RST;
sda = 4;
scl = 5;
}

// initializer for I2C - we only indicate the reset pin!
// SPI value of true dosnt have sance
ESP_SSD1306::ESP_SSD1306(bool SPI) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) {
spi = false;
rst = sclk = dc = cs = sid = -1;
sda = 4;
scl = 5;
}

void ESP_SSD1306::begin(uint8_t vccstate, uint8_t i2caddr, bool reset) {
void ESP_SSD1306::begin(uint8_t vccstate, uint8_t i2caddr) {
_vccstate = vccstate;
_i2caddr = i2caddr;

// set pin directions
if (sid != -1){
if (spi){
pinMode(dc, OUTPUT);
pinMode(cs, OUTPUT);
//commented for ESP8266 compatibility
Expand Down Expand Up @@ -215,15 +247,15 @@ void ESP_SSD1306::begin(uint8_t vccstate, uint8_t i2caddr, bool reset) {
else
{
// I2C Init
Wire.begin();
Wire.begin(sda,scl);
#ifdef __SAM3X8E__
// Force 400 KHz I2C, rawr! (Uses pins 20, 21 for SDA, SCL)
TWI1->TWI_CWGR = 0;
TWI1->TWI_CWGR = ((VARIANT_MCK / (2 * 400000)) - 4) * 0x101;
#endif
}

if (reset) {
if (rst != -1) {
// Setup reset pin direction (used by both SPI and I2C)
pinMode(rst, OUTPUT);
digitalWrite(rst, HIGH);
Expand Down Expand Up @@ -359,7 +391,7 @@ void ESP_SSD1306::invertDisplay(uint8_t i) {
}

void ESP_SSD1306::ssd1306_command(uint8_t c) {
if (sid != -1)
if (spi)
{
// SPI
digitalWrite(cs, HIGH); //uncommented for ESP8266 compatibility
Expand Down Expand Up @@ -474,7 +506,7 @@ void ESP_SSD1306::dim(boolean dim) {
}

void ESP_SSD1306::ssd1306_data(uint8_t c) {
if (sid != -1)
if (spi)
{
// SPI
digitalWrite(cs, HIGH); //uncommented for ESP8266 compatibility
Expand Down Expand Up @@ -515,7 +547,7 @@ void ESP_SSD1306::display(void) {
ssd1306_command(1); // Page end address
#endif

if (sid != -1)
if (spi)
{
// SPI
digitalWrite(cs, HIGH); //added for ESP8266 compatibility
Expand Down
21 changes: 15 additions & 6 deletions ESP_SSD1306.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,19 @@ Otherwise comment the line #define pgm_read_byte(addr)...

class ESP_SSD1306 : public Adafruit_GFX {
public:
ESP_SSD1306(int8_t SID, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS);
ESP_SSD1306(int8_t DC, int8_t RST, int8_t CS); //Constructor for hardware SPI
ESP_SSD1306(int8_t RST);

void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC, uint8_t i2caddr = SSD1306_I2C_ADDRESS, bool reset=true);
// SPI settings
ESP_SSD1306(bool SPI, int8_t SID, int8_t SCLK, int8_t DC, int8_t CS, int8_t RST);
// SPI and I2C settings
// if SPI false DC is SDA, CS is SCL and RST is reset pin
ESP_SSD1306(bool SPI, int8_t DC,int8_t CS,int8_t RST);
// I2C settings
ESP_SSD1306(bool SPI, int8_t SDA, int8_t SCL);
// I2C settings
ESP_SSD1306(bool SPI, int8_t RST);
// I2C settings
ESP_SSD1306(bool SPI = false);

void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC, uint8_t i2caddr = SSD1306_I2C_ADDRESS);
void ssd1306_command(uint8_t c);
void ssd1306_data(uint8_t c);

Expand All @@ -173,7 +181,8 @@ class ESP_SSD1306 : public Adafruit_GFX {
virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);

private:
int8_t _i2caddr, _vccstate, sid, sclk, dc, rst, cs;
boolean spi;
int8_t _i2caddr, _vccstate, sid, sclk, dc, rst, cs, sda, scl;
void fastSPIwrite(uint8_t c);

boolean hwSPI;
Expand Down
4 changes: 1 addition & 3 deletions examples/ESP_ssd1306_128x64_I2C/ESP_ssd1306_128x64_I2C.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
// Import required libraries
#include <ESP_SSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility
#include <Adafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file)
#include <SPI.h> // For SPI comm (needed for not getting compile error)
#include <Wire.h> // For I2C comm, but needed for not getting compile error

/*
HardWare OLED ESP8266 I2C pins
Expand Down Expand Up @@ -126,7 +124,7 @@ const unsigned char PROGMEM demo [] = {
};


ESP_SSD1306 display(OLED_RESET); // FOR I2C
ESP_SSD1306 display(false,OLED_RESET); // FOR I2C


void setup(void)
Expand Down
Loading