#include "AiEsp32RotaryEncoder.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <pgmspace.h>
#define ROTARY_ENCODER_A_PIN 35
#define ROTARY_ENCODER_B_PIN 32
#define ROTARY_ENCODER_BUTTON_PIN 33
#define ROTARY_ENCODER_STEPS 4
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define SCREEN_ADDRESS 0x3c
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, -1, ROTARY_ENCODER_STEPS);
int state=0;
bool button_state=false;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire,-1);
const uint8_t temperatureLogo[] PROGMEM = {
0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x3C,
0x7E, 0x7E, 0x7E, 0x7E, 0xFF, 0xFF, 0xE7, 0xE7,
0xC3, 0xC3, 0x81, 0x81, 0x81, 0x81, 0x00, 0x00,
0x3C, 0x3C, 0x7E, 0x7E, 0x7E, 0x7E, 0x3C, 0x3C
};
const uint8_t humidityIcon[] PROGMEM = {
0x00, 0x00, 0x07, 0xE0, 0x0F, 0xF0, 0x1C, 0x38, 0x38, 0x1C, 0x30, 0x0C,
0x70, 0x0E, 0x60, 0x06, 0xE0, 0x07, 0xE0, 0x07, 0xF0, 0x0F, 0x70, 0x0E,
0x38, 0x1C, 0x1E, 0x78, 0x0F, 0xF0, 0x03, 0xC0
};
const uint8_t settingsIcon[] PROGMEM = {
0x00, 0x00, 0x38, 0x1C, 0x7C, 0x3E, 0x66, 0x66, 0xC3, 0xC3, 0xE7, 0xE7,
0x7E, 0x3C, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x7E, 0xE7, 0xE7, 0xC3, 0xC3,
0x66, 0x66, 0x7C, 0x3E, 0x38, 0x1C, 0x00, 0x00
};
const uint8_t webBrowserIcon[] PROGMEM = {
0x00, 0x00, 0x3C, 0x3C, 0x66, 0x66, 0xC3, 0xC3, 0xC3, 0xC3, 0xDB, 0xDB,
0xDB, 0xDB, 0xFF, 0xFF, 0xFF, 0xFF, 0xDB, 0xDB, 0xDB, 0xDB, 0xC3, 0xC3,
0xC3, 0xC3, 0x66, 0x66, 0x3C, 0x3C, 0x00, 0x00
};
void IRAM_ATTR readEncoderISR()
{
rotaryEncoder.readEncoder_ISR();
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-S2!");
rotaryEncoder.begin();
rotaryEncoder.setup(readEncoderISR);
rotaryEncoder.setBoundaries(0,4,false);
rotaryEncoder.setAcceleration(250);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Hello world");
display.display();
delay(500);
display.clearDisplay();
display.display();
}
void loop() {
if(rotaryEncoder.encoderChanged()){
//Serial.println(rotaryEncoder.readEncoder());
state=rotaryEncoder.readEncoder();
}
if (rotaryEncoder.isEncoderButtonClicked())
{
button_state=!button_state;
Serial.println("button pressed");}
switch(state){
case 0:
if(button_state==true){
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.drawBitmap(56, 10, temperatureLogo, 16, 16, SSD1306_WHITE);
display.setCursor(56,13);
display.print("12 'C ");
display.display();
delay(200);
}
else{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.drawBitmap(56, 24, temperatureLogo, 16, 16, SSD1306_WHITE);
display.display();
delay(200);
}
//Serial.println("Temprature");
break;
case 1:
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
//display.print("Humidity");
display.drawBitmap(56,24,humidityIcon,16,16,SSD1306_WHITE);
display.display();
//Serial.println("humidity");
delay(200);
break;
case 2:
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.drawBitmap(56,24,settingsIcon,16,16,SSD1306_WHITE);
display.display();
//Serial.println("Settings");
delay(200);
break;
case 3:
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.drawBitmap(56, 24, webBrowserIcon, 16, 16, SSD1306_WHITE);
display.display();
//Serial.println("Request Server");
delay(200);
break;
case 4:
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("WhatElse");
display.display();
//Serial.println("What else");
delay(200);
break;
default:
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Switch to change options");
display.display();
//Serial.println("switch to change options");
delay(200);
}
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}