So, I’ve attempted to add the Adafruit library into my code and replace u8glib with it. However, I’m getting some compilation errors. Hopefully this is a simple fix for something that I’m overlooking or am ignorant of.
First, I added Adafruit_SSD1306 to my project via Spark Build. Then I tried to follow Adafruit’s I2C example (seen here: https://github.com/adafruit/Adafruit_SSD1306/blob/master/examples/ssd1306_128x64_i2c/ssd1306_128x64_i2c.ino ) as closely as possible. Unfortunately, I couldn’t get anything to show up on my screens.
The hardware setup is the same as I had running on Arduino, so that shouldn’t be the issue. Since I’m actually running four screens at once, I send the I2C lines from the Spark to a 4-channel multiplexer, which then sends individual I2C lines to each of the four screens. There’s a section that deals with the mux in the source code which I didn’t touch from the Arduino code. Could it be possible that the two platforms handle addresses slightly differently?
Here’s my code:
//#include "U8glib.h"
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI
#include "Adafruit_SSD1306/Adafruit_SSD1306.h"
#define MUX 0x70 //Multiplexer Address
#define DISPLAY 0x3C //OLED Display Address
#define OLED_RESET D4
Adafruit_SSD1306 display(OLED_RESET);
void draw(char str[]) {
/* graphic commands to redraw the complete screen should be placed here */
//u8g.setFont(u8g_font_fur17);
//u8g.setFont(u8g_font_osb21);
//u8g.drawStr( 5, 20, "Testing:");
//u8g.drawStr( 5, 40, str);
}
void setup()
{
/* MUX portion */
Wire.begin();
Serial.begin(115200);
mux(0); //select Display on channel 0
initializeDISP(DISPLAY);
sendDISP(DISPLAY,"first");
mux(1); //select Display on channel 1
initializeDISP(DISPLAY);
sendDISP(DISPLAY,"second");
mux(2); //select Display on channel 2
initializeDISP(DISPLAY);
sendDISP(DISPLAY,"third");
mux(3); //select Display on channel 3
initializeDISP(DISPLAY);
sendDISP(DISPLAY,"fourth");
mux(0xFF); //disable all channels. This is not required
}
void loop()
{
}
//This here's the mux section, which I downloaded from the manufacturer's website.
/********************************************************
* When selecting a channel bit2 of the control register
* must be set to a logic 1 to enable channel selection.
* If bit2 is a logic zero then all channels will be disabled.
* If 0xFF is the selected channel it will disable all channels.
********************************************************/
void mux(byte channel)
{
byte controlRegister = 0x04;
controlRegister |= channel;
Wire.beginTransmission(MUX);
if (channel == 0xFF){Wire.write(0x00);} //deselect all channels
else {Wire.write(controlRegister);} //set to selected channel
Wire.endTransmission();
}
void initializeDISP(uint8_t address)
{
Wire.beginTransmission(DISPLAY);
Wire.write(0x06);
Wire.write(0x40);
Wire.write(0x00);
Wire.endTransmission();
//u8g.begin();
//Adafruit stuff
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C address 0x3C
display.display();
delay(2000);
display.clearDisplay();
}
void sendDISP(int _disp, String rootStr)
{
Wire.beginTransmission(_disp);
Wire.write(0x04);
Wire.endTransmission();
/*
// picture loop
u8g.firstPage();
do {
draw(rootStr);
} while( u8g.nextPage() );
*/
//Adafruit: text display tests
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(rootStr);
display.display();
delay(2000);
}
I simply commented out the sections where I previously had u8glib, and replaced it with my best guess for the relevant Adafruit code, based on the example linked above. One thing that confused me was OLED_RESET, defined at the top of the file. I don’t think I use it, but I need it for the code to compile, so I set it to D4, which I don’t use. If you see anything I did wrong, I’d appreciate any help. Thanks!