#include <U8g2lib.h>
#include "AiEsp32RotaryEncoder.h"
#include <Preferences.h>
#include "html.h"
//display
U8X8_SSD1306_128X64_NONAME_HW_I2C display;
//rotary encoder
#define ROTARY_ENCODER_A_PIN 19
#define ROTARY_ENCODER_B_PIN 18
#define ROTARY_ENCODER_BUTTON_PIN 23
#define ROTARY_ENCODER_STEPS 4
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, -1, ROTARY_ENCODER_STEPS);
int selectedItem = 0;
int tempSelectedItem = 0;
bool menuDisplayed = false;
int MAX_DISPLAYED = 6;
int MAX_ITEMS = 10;
Preferences preferences;
struct menuItem {
String name;
String value;
};
menuItem items[12];
int itemsCount = 0;
void IRAM_ATTR readEncoderISR()
{
rotaryEncoder.readEncoder_ISR();
}
void savePrefs() {
preferences.putString("ssid", "my_ssid");
preferences.putString("pwd", "my_password");
preferences.putString("s1n", "Item 1");
preferences.putString("s1u", "Item 1 value");
preferences.putString("s2n", "Item 2");
preferences.putString("s2u", "Item 2 value");
preferences.putString("s3n", "Item 3");
preferences.putString("s3u", "Item 3 value");
preferences.putString("s4n", "Item 4");
preferences.putString("s4u", "Item 4 value");
preferences.putString("s5n", "Item 5");
preferences.putString("s5u", "Item 5 value");
preferences.putString("s6n", "Item 6");
preferences.putString("s6u", "Item 6 value");
preferences.putString("s7n", "Item 7");
preferences.putString("s7u", "Item 7 value");
preferences.putString("s8n", "Item 8");
preferences.putString("s8u", "Item 8 value");
}
void readPrefs() {
char key[10];
itemsCount = 0;
for(int i=1; i <= MAX_ITEMS; i++) {// we can have maximum 10 items
strcpy(key, "s");
char numberString[4];
itoa(i, numberString, 10);
strcat(key, numberString);
strcat(key, "n");
String itemName = preferences.getString(key, "");
key[0] = '\0';
strcpy(key, "s");
strcat(key, numberString);
strcat(key, "u");
String itemValue = preferences.getString(key, "");
key[0] = '\0';
if (itemName != "") {
while (itemName.length() < 16) {
itemName += " "; // Append a space
}
items[itemsCount] = {itemName, itemValue};
itemsCount++;
}
}
//add empty space and "Setup Mode"
items[itemsCount] = {" ", ""};
itemsCount++;
items[itemsCount] = {"Setup Mode ", ""};
itemsCount++;
}
String getAccessHTML() {
String html;
preferences.begin("my-prefs", false);
String ssid = preferences.getString("ssid", "");
String password = preferences.getString("pwd", "");
html = String(ACCESS_page_start);
html += "<tr><td>SSID:</td><td><input type='text' name='ssid' value='" + ssid + "'></td></tr>";
html += "<tr><td>Password:</td><td><input type='password' name='password' value='" + password + "'></td></tr>";
html += String(ACCESS_page_end);
preferences.end();
return html;
}
String getItemsHTML() {
char key[10];
String html;
preferences.begin("my-prefs", false);
html = String(ITEMS_page_start);
for(int i=1; i <= MAX_ITEMS; i++) {
strcpy(key, "s");
char numberString[4];
itoa(i, numberString, 10);
strcat(key, numberString);
strcat(key, "n");
String itemName = preferences.getString(key, "");
key[0] = '\0';
strcpy(key, "s");
strcat(key, numberString);
strcat(key, "u");
String itemValue = preferences.getString(key, "");
key[0] = '\0';
html += "<tr><td style='width:88px'>";
html += i;
html += "</td><td style='width:162px'><input type='text' name='s";
html += i;
html += "n' maxlength='16' value='";
html += itemName;
html += "'></td><td style='width:232px'><input type='text' name='s";
html += i;
html += "u' size='40' value='";
html += itemValue;
html += "'></td></tr>";
}
html += String(ITEMS_page_end);
preferences.end();
return html;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
preferences.begin("my-prefs", false);
savePrefs();
String ssid = preferences.getString("ssid", ""); // The second parameter is a default value
String password = preferences.getString("pwd", "");
readPrefs();
preferences.end();
delay(250); // wait for the OLED to power up
display.begin();
display.setFont(u8x8_font_chroma48medium8_r);
display.draw1x2String(0,0,"ESP32 Meniu!");
rotaryEncoder.begin();
rotaryEncoder.setup(readEncoderISR);
rotaryEncoder.setBoundaries(0, 100, false); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
rotaryEncoder.setEncoderValue(25);
rotaryEncoder.setAcceleration(50);
Serial.println("--------");
Serial.println(getItemsHTML());
Serial.println("--------");
int tries = 0;
Serial.println("Trying to connect.. ");
while (true) {
tries++;
Serial.print("try: ");
Serial.print(tries);
Serial.println("...");
delay(1500);
}
}
int getStart() {
if (itemsCount > MAX_DISPLAYED) {
if (tempSelectedItem >= MAX_DISPLAYED) {
return tempSelectedItem - MAX_DISPLAYED + 1;
}
return 0;
} else {
return 0;
}
}
void displayItems() {
int start = getStart();
int j = 2;
for (int i = start; i <= start + MAX_DISPLAYED - 1; i++) {
if (i == tempSelectedItem) {
display.setInverseFont(1);
}
display.drawString(0, j++, items[i].name.c_str());
display.setInverseFont(0);
}
}
void displayMenu() {
rotaryEncoder.setBoundaries(0, itemsCount - 1, false); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
rotaryEncoder.setEncoderValue(tempSelectedItem);
rotaryEncoder.disableAcceleration();
menuDisplayed = true;
display.clearDisplay();
display.drawString(0, 0, "Select item");
tempSelectedItem = selectedItem;
displayItems();
}
void hideMenu() {
//do not select last 2 items
if (tempSelectedItem == itemsCount - 1) {
return;
}
if (tempSelectedItem == itemsCount - 2) {
return;
}
menuDisplayed = false;
selectedItem = tempSelectedItem;
display.clearDisplay();
display.draw1x2String(0, 0, "ESP32 Meniu!");
rotaryEncoder.setBoundaries(0, 100, false); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
rotaryEncoder.setEncoderValue(25);
rotaryEncoder.setAcceleration(50);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
if (rotaryEncoder.isEncoderButtonClicked())
{
if (menuDisplayed) {
hideMenu();
} else {
displayMenu();
}
}
if (rotaryEncoder.encoderChanged())
{
if (menuDisplayed) {
tempSelectedItem = rotaryEncoder.readEncoder();
displayItems();
}
}
}