#include <Wire.h>
#include <Arduino.h>
#include <U8g2lib.h>
#include <AiEsp32RotaryEncoder.h>
/////// Encoder
#define ROTARY_ENCODER_A_PIN 26
#define ROTARY_ENCODER_B_PIN 27
#define ROTARY_ENCODER_BUTTON_PIN 25
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
#define ROTARY_ENCODER_VCC_PIN -1 /* 27 put -1 of Rotary encoder Vcc is connected directly to 3,3V; else you can use declared output pin for powering rotary encoder */
//depending on your encoder - try 1,2 or 4 to get expected behaviour
//#define ROTARY_ENCODER_STEPS 1
//#define ROTARY_ENCODER_STEPS 2
#define ROTARY_ENCODER_STEPS 4
//instead of changing here, rather change numbers above
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
int myKelvin[] = {2200, 2700, 4000, 6500};
int k = 0;
int myBrightness[] ={0, 0, 0, 0};
int myPercents[] ={0, 0, 0, 0};
int obenLed[] = {3, 2, 1, 0};
int untenLed[] = {7, 6, 5, 4};
int idleTimer = 0;
int currentOption = 0;
int currentSelection = 0;
void setForOption(int newOption)
{
currentOption = newOption;
switch (newOption)
{
case 0:
rotaryEncoder.setAcceleration(0);
rotaryEncoder.setBoundaries(0, 3, false); //food type 0..3
rotaryEncoder.setEncoderValue(currentSelection);
break;
case 1:
rotaryEncoder.setAcceleration(250);
rotaryEncoder.setBoundaries(0, 100, false); //how many pieces 1 to 10; do not go from 10 to 1
rotaryEncoder.setEncoderValue(myPercents[currentSelection]);
break;
default:
break;
}
}
void rotary_onButtonClick()
{
static unsigned long lastTimePressed = 0;
idleTimer = millis();
//ignore multiple press in that time milliseconds
if (millis() - lastTimePressed < 500)
{
return;
}
lastTimePressed = millis();
switch (currentOption)
{
case 0:
setForOption(1);
break;
case 1:
setForOption(0);
break;
default:
break;
}
}
void rotary_loop()
{
//dont print anything unless value changed
if (rotaryEncoder.encoderChanged())
{
idleTimer = millis();
int selectedValue = rotaryEncoder.readEncoder();
switch (currentOption)
{
case 0: //"Select fast food"
currentSelection = selectedValue;
break;
case 1: //"Select quantity"
myPercents[currentSelection] = selectedValue;
break;
default:
break;
}
Serial.println("OK ");
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_profont12_tf);
u8g2.setCursor(3, 14);
u8g2.print("Set Brightness of");
u8g2.setCursor(3, 28);
u8g2.print(myKelvin[currentSelection]);
u8g2.setCursor(30, 28);
u8g2.print("K LED");
u8g2.setFont(u8g2_font_open_iconic_weather_2x_t);
u8g2.drawGlyph(1,63,66);
u8g2.drawGlyph(111,63,69);
u8g2.drawRFrame(21,50,86,12,4);
if (myPercents[currentSelection]<6){u8g2.drawRBox(23,52,6,8,3);}
else{ u8g2.drawRBox(23,52,ceil((myPercents[currentSelection]*82)/100),8,3);}
u8g2.sendBuffer();
}
if (rotaryEncoder.isEncoderButtonClicked())
{
rotary_onButtonClick();
}
}
void setSun(){
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_open_iconic_weather_6x_t);
u8g2.drawGlyph(40,56,69);
u8g2.sendBuffer();
}
void IRAM_ATTR readEncoderISR()
{
rotaryEncoder.readEncoder_ISR();
}
void setup() {
u8g2.begin();
//we must initialize rotary encoder
rotaryEncoder.begin();
rotaryEncoder.setup(readEncoderISR);
setForOption(0);
u8g2.setFont(u8g2_font_open_iconic_weather_6x_t);
u8g2.drawGlyph(40,56,69);
u8g2.sendBuffer();
Serial.begin(115200);
Serial.println("Hello, ESP32!");
}
void loop() {
rotary_loop();
if (millis() - idleTimer > 10000){setSun();}
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}