#include <SPI.h>
#include <Wire.h>
#include <U8g2lib.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
//U8G2 -> what library is being used
//SSD1306 -> which OLED is being used
//128X64 -> the resolution size of the OLED
//NONAME -> suggests that it's a generic or unspecified variant OLED (just a normal OLED really)
//F_HW_I2C -> indicates that it uses hardware I2C communication.
// U8G2_R0 -> this parameter specifies the rotation of the display. U8G2_R0 means no rotation. You can change this parameter based on how your display is physically oriented.
// U8X8_PIN_NONE -> this part is specifying that the reset pin is set to U8X8_PIN_NONE, meaning that no reset pin is used. If your display has a reset pin and you want to use it, you can replace U8X8_PIN_NONE with the appropriate pin number.
const int buttonPin = 2; // Define the pin for the button
int lastButtonState = HIGH; // Variable to store the last state of the button
int currentButtonState; // Variable to store the current state of the button
bool isDisplayOn = true; // Flag to track the OLED display state
static const unsigned char image_CoolHi_25x27_bits[] U8X8_PROGMEM = {0xf8,0xff,0x3f,0x00,0x04,0x00,0x40,0x00,0x02,0x00,0x80,0x00,0x01,0x10,0x00,0x01,0x01,0x92,0x00,0x01,0x81,0x54,0x02,0x01,0x81,0x38,0x02,0x01,0xe1,0x10,0x0e,0x01,0x01,0x11,0x01,0x01,0x11,0x92,0x10,0x01,0x21,0x54,0x08,0x01,0x41,0x38,0x04,0x01,0xf9,0xff,0x3f,0x01,0x41,0x38,0x04,0x01,0x21,0x54,0x08,0x01,0x11,0x92,0x10,0x01,0x01,0x11,0x01,0x01,0xe1,0x10,0x0e,0x01,0x81,0x38,0x02,0x01,0x81,0x54,0x02,0x01,0x01,0x92,0x00,0x01,0x01,0x10,0x00,0x01,0x01,0x00,0x00,0x01,0x03,0x00,0x80,0x01,0x06,0x00,0xc0,0x00,0xfc,0xff,0x7f,0x00,0xf8,0xff,0x3f,0x00};
static const unsigned char image_Dehumidify_25x27_bits[] U8X8_PROGMEM = {0xf8,0xff,0x3f,0x00,0x04,0x00,0x40,0x00,0x02,0x00,0x80,0x00,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x01,0x01,0x10,0x00,0x01,0x01,0x10,0x00,0x01,0x01,0x18,0x00,0x01,0x01,0x38,0x00,0x01,0x01,0x3c,0x00,0x01,0x01,0x7c,0x00,0x01,0x01,0xfe,0x00,0x01,0x01,0xfe,0x00,0x01,0x01,0xbf,0x01,0x01,0x01,0x7f,0x01,0x01,0x81,0x7f,0x03,0x01,0x81,0xff,0x03,0x01,0x01,0xff,0x01,0x01,0x01,0xff,0x01,0x01,0x01,0xfe,0x00,0x01,0x01,0x78,0x00,0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x01,0x03,0x00,0x80,0x01,0x06,0x00,0xc0,0x00,0xfc,0xff,0x7f,0x00,0xf8,0xff,0x3f,0x00};
static const unsigned char image_HeatHi_25x27_bits[] U8X8_PROGMEM = {0xf8,0xff,0x3f,0x00,0x04,0x00,0x40,0x00,0x02,0x00,0x80,0x00,0x01,0x10,0x00,0x01,0x11,0x10,0x10,0x01,0x21,0x10,0x08,0x01,0x41,0x10,0x04,0x01,0x81,0x00,0x02,0x01,0x01,0x7c,0x00,0x01,0x01,0x82,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x79,0x01,0x3d,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x82,0x00,0x01,0x01,0x7c,0x00,0x01,0x81,0x00,0x02,0x01,0x41,0x10,0x04,0x01,0x21,0x10,0x08,0x01,0x11,0x10,0x10,0x01,0x01,0x10,0x00,0x01,0x01,0x00,0x00,0x01,0x03,0x00,0x80,0x01,0x06,0x00,0xc0,0x00,0xfc,0xff,0x7f,0x00,0xf8,0xff,0x3f,0x00};
static const unsigned char image_Mute_25x27_bits[] U8X8_PROGMEM = {0xf8,0xff,0x3f,0x00,0x04,0x00,0x40,0x00,0x02,0x00,0x80,0x00,0x01,0x00,0x00,0x01,0x01,0x00,0x03,0x01,0x21,0x80,0x14,0x01,0x41,0x40,0x0c,0x01,0x81,0x20,0x04,0x01,0x01,0x1f,0x06,0x01,0x81,0x02,0x05,0x01,0x81,0x84,0x04,0x01,0x81,0x48,0x04,0x01,0x81,0x30,0x04,0x01,0x81,0x30,0x04,0x01,0x81,0x48,0x04,0x01,0x81,0x84,0x04,0x01,0x81,0x02,0x05,0x01,0x01,0x1f,0x06,0x01,0x81,0x20,0x04,0x01,0x41,0x40,0x0c,0x01,0x21,0x80,0x14,0x01,0x01,0x00,0x03,0x01,0x01,0x00,0x00,0x01,0x03,0x00,0x80,0x01,0x06,0x00,0xc0,0x00,0xfc,0xff,0x7f,0x00,0xf8,0xff,0x3f,0x00};
static const unsigned char image_Power_25x27_bits[] U8X8_PROGMEM = {0xf8,0xff,0x3f,0x00,0x04,0x00,0x40,0x00,0x02,0x00,0x80,0x00,0x01,0x00,0x00,0x01,0x01,0x10,0x00,0x01,0x01,0x38,0x00,0x01,0x01,0x38,0x00,0x01,0x81,0x39,0x03,0x01,0xc1,0x39,0x07,0x01,0xe1,0x38,0x0e,0x01,0x61,0x38,0x0c,0x01,0x61,0x38,0x0c,0x01,0x61,0x10,0x0c,0x01,0x61,0x00,0x0c,0x01,0x61,0x00,0x0c,0x01,0x61,0x00,0x0c,0x01,0xe1,0x00,0x0e,0x01,0xc1,0x01,0x07,0x01,0x81,0x83,0x03,0x01,0x01,0xff,0x01,0x01,0x01,0x7c,0x00,0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x01,0x03,0x00,0x80,0x01,0x06,0x00,0xc0,0x00,0xfc,0xff,0x7f,0x00,0xf8,0xff,0x3f,0x00};
static const unsigned char image_Off_25x27_bits[] U8X8_PROGMEM = {0xf8,0xff,0x3f,0x00,0x04,0x00,0x40,0x00,0x02,0x00,0x80,0x00,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x01,0xe1,0xf8,0x3e,0x01,0xb1,0x19,0x06,0x01,0x19,0x1b,0x06,0x01,0x19,0x7b,0x1e,0x01,0x19,0x1b,0x06,0x01,0x19,0x1b,0x06,0x01,0xb1,0x19,0x06,0x01,0xe1,0x18,0x06,0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x01,0x03,0x00,0x80,0x01,0x06,0x00,0xc0,0x00,0xfc,0xff,0x7f,0x00,0xf8,0xff,0x3f,0x00};
void setup() {
Serial.begin(115200);
u8g2.begin();
pinMode(buttonPin, INPUT);
}
void loop() {
u8g2.clearBuffer(); // display.clearDisplay();
u8g2.setBitmapMode(1);
u8g2.drawXBMP( 5, 3, 25, 27, image_CoolHi_25x27_bits); // the first four parameters (x1, y2, width, height) define the dimension for the image to be drawn in (which is the fifth parameter)
u8g2.drawXBMP( 52, 3, 25, 27, image_Dehumidify_25x27_bits);
u8g2.drawXBMP( 98, 3, 25, 27, image_HeatHi_25x27_bits);
u8g2.drawXBMP( 5, 34, 25, 27, image_Mute_25x27_bits);
u8g2.drawXBMP( 52, 34, 25, 27, image_Power_25x27_bits);
u8g2.drawXBMP( 98, 34, 25, 27, image_Off_25x27_bits);
u8g2.drawFrame(2, 0, 31, 33); // frames and whatnot are different as whatever dimension is defined in the parameters are where the pixels will be drawn
// x1 = 2 for first icon
// x1 = 49 for second icon
// x1 = 95 for third icon
// x1 = 2, y2 = 31 for fourth icon
// x1 = 49, y2 = 31 for fifth icon
// x1 = 95, y2 = 31 for sixth icon
// ON/OFF CODE
if (digitalRead(buttonPin) != lastButtonState) {
delay(50); // Delay for debounce
currentButtonState = digitalRead(buttonPin);
if (currentButtonState == LOW && lastButtonState == HIGH) { // when sketch is run for the first time, this if-code block actually runs as *lastButtonState* is set to HIGH initially which then changes when I press the button
// Button is pressed for the first time
isDisplayOn = !isDisplayOn; // Toggle the display state
if (isDisplayOn) {
displayOn();
} else {
displayOff();
}
}
lastButtonState = currentButtonState;
}
u8g2.sendBuffer(); // display.display();
}
void displayOn() {
u8g2.setPowerSave(0); // Wake up the display (display.ssd1306_command(SSD1306_DISPLAYON);)
}
void displayOff() {
u8g2.setPowerSave(1); // Put the display to sleep (display.ssd1306_command(SSD1306_DISPLAYOFF);)
}