Story
//==========================================================//
// //
// Bean OLED Test Code //
// //
//==========================================================//
//#include <avr/pgmspace.h>
#include <Wire.h>
//---------------FONT + GRAPHIC-----------------------------//
#include "data.h"
//==========================================================//
// OLED I2C bus address
#define OLED_address 0x3c
//static int d0 = 0;
const int PWR_ON = 0; //Using Pin 0 tp power the oleds VCC
void setup()
{
//pinMode(PWR_ON, INPUT_PULLUP);
pinMode(PWR_ON, OUTPUT);
digitalWrite(PWR_ON, HIGH);
delay(1000); //Needed!
// Initialize I2C and OLED Display
Wire.begin();
init_OLED();
delay(1500);
reset_display(); // Clear logo and load saved mode
}
//==========================================================//
void loop()
{
char sz[]= "****";
char ab[]= "****";
uint16_t batteryV = 0;
char tmp[5];
displayOn();
while(1)
{
clear_display();
sendStrXY("Temp:",0,4);
float temperature = Bean.getTemperature();
dtostrf(temperature,4,1,sz);
sendStrXY(sz,1,4);
batteryV = Bean.getBatteryLevel();
sendStrXY("Bat Lev:",4,4);
sprintf(tmp,"%d",batteryV);
sendStrXY(tmp,5,4);
sendStrXY("%",5,6);
delay(5000);
//reset_display();
//digitalWrite(PWR_ON, LOW); //Display EN Off
//Bean.sleep(2);
}
}
//==========================================================//
// Resets display depending on the actual mode.
static void reset_display(void)
{
displayOff();
clear_display();
displayOn();
}
//==========================================================//
// Turns display on.
void displayOn(void)
{
sendcommand(0xaf); //display on
}
//==========================================================//
// Turns display off.
void displayOff(void)
{
sendcommand(0xae); //display off
}
//==========================================================//
// Clears the display by sendind 0 to all the screen map.
static void clear_display(void)
{
unsigned char i,k;
for(k=0;k<8;k++) //8
{
setXY(k,0);
{
for(i=0;i<128;i++) //was 128
{
SendChar(0); //clear all COL
//delay(10);
}
}
}
}
//==========================================================//
static void printBigTime(char *string)
{
int Y;
int lon = strlen(string);
if(lon == 3) {
Y = 0;
} else if (lon == 2) {
Y = 3;
} else if (lon == 1) {
Y = 6;
}
int X = 2;
while(*string)
{
printBigNumber(*string, X, Y);
Y+=3;
X=2;
setXY(X,Y);
*string++;
}
}
//==========================================================//
// Prints a display big number (96 bytes) in coordinates X Y,
// being multiples of 8. This means we have 16 COLS (0-15)
// and 8 ROWS (0-7).
static void printBigNumber(char string, int X, int Y)
{
setXY(X,Y);
int salto=0;
for(int i=0;i<96;i++)
{
if(string == ' ') {
SendChar(0);
} else
SendChar(pgm_read_byte(bigNumbers[string-0x30]+i));
if(salto == 23) {
salto = 0;
X++;
setXY(X,Y);
} else {
salto++;
}
}
}
//==========================================================//
// Actually this sends a byte, not a char to draw in the display.
// Display's chars uses 8 byte font the small ones and 96 bytes
// for the big number font.
static void SendChar(unsigned char data)
{
Wire.beginTransmission(OLED_address); // begin transmitting
Wire.write(0x40);//data mode
Wire.write(data);
Wire.endTransmission(); // stop transmitting
}
//==========================================================//
// Prints a display char (not just a byte) in coordinates X Y,
// being multiples of 8. This means we have 16 COLS (0-15)
// and 8 ROWS (0-7).
static void sendCharXY(unsigned char data, int X, int Y)
{
setXY(X, Y);
Wire.beginTransmission(OLED_address); // begin transmitting
Wire.write(0x40);//data mode
for(int i=0;i<8;i++) //8
Wire.write(pgm_read_byte(myFont[data-0x20]+i));
Wire.endTransmission(); // stop transmitting
}
//==========================================================//
// Used to send commands to the display.
static void sendcommand(unsigned char com)
{
Wire.beginTransmission(OLED_address); //begin transmitting
Wire.write(0x80); //command mode
Wire.write(com);
Wire.endTransmission(); // stop transmitting
}
//==========================================================//
// Set the cursor position in a 16 COL * 8 ROW map.
static void setXY(unsigned char row,unsigned char col)
{
sendcommand(0xb0+row); //set page address
sendcommand(0x00+(8*col&0x0f)); //set low col address //8
sendcommand(0x10+((8*col>>4)&0x0f)); //set high col address //8
//Possible NEW!!
//sendcommand((0x10|(col>>4))+0x02);
//sendcommand((0x0f&col));
}
//==========================================================//
// Prints a string regardless the cursor position.
static void sendStr(unsigned char *string)
{
unsigned char i=0;
while(*string)
{
for(i=0;i<8;i++)
{
SendChar(pgm_read_byte(myFont[*string-0x20]+i));
}
*string++;
}
}
//==========================================================//
// Prints a string in coordinates X Y, being multiples of 8.
// This means we have 16 COLS (0-15) and 8 ROWS (0-7).
static void sendStrXY( char *string, int X, int Y)
{
setXY(X,Y);
unsigned char i=0;
while(*string)
{
for(i=0;i<8;i++)
{
SendChar(pgm_read_byte(myFont[*string-0x20]+i)); //0x20Hex=32Dec 128x64 or 64x48 48/2=24
//SendChar(pgm_read_byte(myFont[*string-0x20]+i));
}
*string++;
}
}
//==========================================================//
// Inits oled and draws logo at startup
static void init_OLED(void)
{
sendcommand(0xae); //display off
sendcommand(0xa6); //Set Normal Display (default)
// Adafruit Init sequence for 128x64 OLED module
sendcommand(0xAE); //DISPLAYOFF
sendcommand(0xD5); //SETDISPLAYCLOCKDIV
sendcommand(0x80); // the suggested ratio 0x80
sendcommand(0xA8); //SSD1306_SETMULTIPLEX
sendcommand(0x2F); //--1/48 duty //NEW!!!
sendcommand(0xD3); //SETDISPLAYOFFSET
sendcommand(0x0); //no offset
sendcommand(0x40 | 0x0); //SETSTARTLINE
sendcommand(0x8D); //CHARGEPUMP
sendcommand(0x14);
sendcommand(0x20); //MEMORYMODE
sendcommand(0x00); //0x0 act like ks0108
sendcommand(0xA0 | 0x1); //SEGREMAP //Rotate screen 180 deg
//sendcommand(0xA0);
sendcommand(0xC8); //COMSCANDEC Rotate screen 180 Deg
//sendcommand(0xC0);
sendcommand(0xDA); //0xDA
sendcommand(0x12); //COMSCANDEC
sendcommand(0x81); //SETCONTRAS
sendcommand(0xCF); //
sendcommand(0xd9); //SETPRECHARGE
sendcommand(0xF1);
sendcommand(0xDB); //SETVCOMDETECT
sendcommand(0x40);
sendcommand(0xA4); //DISPLAYALLON_RESUME
sendcommand(0xA6); //NORMALDISPLAY
clear_display();
sendcommand(0x2e); // stop scroll
//----------------------------REVERSE comments----------------------------//
// sendcommand(0xa0); //seg re-map 0->127(default)
// sendcommand(0xa1); //seg re-map 127->0
// sendcommand(0xc8);
// delay(1000);
//----------------------------REVERSE comments----------------------------//
// sendcommand(0xa7); //Set Inverse Display
// sendcommand(0xae); //display off
sendcommand(0x20); //Set Memory Addressing Mode
sendcommand(0x00); //Set Memory Addressing Mode ab Horizontal addressing mode
// sendcommand(0x02); // Set Memory Addressing Mode ab Page addressing mode(RESET)
setXY(0,0);
for(int i=0;i<128*8;i++) // show 128* 64 Logo
{
SendChar(pgm_read_byte(logo+i));
}
sendcommand(0xaf); //display on
}
PunchThrough_Bean_i2c_Oled
More details: https://github.com/mike-rankin/PunchThrough_Bean_i2c_Oled. This is GitHub account: https://github.com/mike-rankin where you will explore more excellent projects. He is a good DIY maker who designs custom ESP32 IoT projects using Altium Designer, Arduino IDE and Fusion360. Welcome everyone to follow.




