// ESP32 Arduino implementation of Apple Juice
// Author: David Martinez
// DOIT ESP32 DEVKIT V1
// Based on the previous work of Ronald Stoner Github: https://github.com/ronaldstoner
// INCLUDE
#include "AiEsp32RotaryEncoder.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <Wire.h>
// END INCLUDE
// DEFINITION
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define ROTARY_ENCODER_A_PIN 32
#define ROTARY_ENCODER_B_PIN 33
#define ROTARY_ENCODER_BUTTON_PIN 14
#define ROTARY_ENCODER_STEPS 7
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define CE 9
// END DEFINITION
// LIBRARY SET
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, -1, ROTARY_ENCODER_STEPS);
// END LIBRARY SET
// GLOBAL VARIABLE
String options[] = {"Scroll for search:", "Send Advertisement?"};
String devices[] =
{
"Airpods",
"Airpods Pro",
"Airpods Max",
"Airpods Gen 2",
"Airpods Gen 3",
"Airpods Pro Gen 2",
"Power Beats"
};
int deviceType = 0;
String selectedDevice = "";
int selectedConfirmation = 0;
int currentOption = 0;
// END GLOBAL VARIABLE
// PROGRAM
void setForOption(int newOption)
{
currentOption = newOption;
switch (newOption)
{
case 0:
rotaryEncoder.setBoundaries(0, ROTARY_ENCODER_STEPS - 1 , true); //device type 0..n
rotaryEncoder.setEncoderValue(0);
break;
case 1:
rotaryEncoder.setBoundaries(0, 1, false); // Send Advertisement? 1 = Yes, 0 = No
rotaryEncoder.setEncoderValue(1);
break;
default:
break;
}
Serial.println(options[currentOption]);
showSelection();
}
void rotary_onButtonClick()
{
static unsigned long lastTimePressed = 0;
if (millis() - lastTimePressed < 200)
return;
lastTimePressed = millis();
int selecedValue = rotaryEncoder.readEncoder();
switch (currentOption)
{
case 0: //"Select device"
selectedDevice = devices[selecedValue];
deviceType = selecedValue;
setForOption(1);
break;
case 1: //"Send Advertisement?"
selectedConfirmation = selecedValue;
if (selecedValue == 1) {
sendAdvertisement();
}else {
cancellAdvertisement();
}
setForOption(0);
break;
default:
break;
}
}
void IRAM_ATTR readEncoderISR()
{
rotaryEncoder.readEncoder_ISR();
}
void setup()
{
Serial.begin(115200);
display.begin(SSD1306_EXTERNALVCC, 0x3C);
discovery();
// Core encoder
rotaryEncoder.begin();
rotaryEncoder.setup(readEncoderISR);
rotaryEncoder.setAcceleration(0);
rotaryEncoder.correctionOffset=2; //try with zero or ROTARY_ENCODER_STEPS/2
rotaryEncoder.isButtonPulldown = false;
rotaryEncoder.areEncoderPinsPulldownforEsp32 = true;
setForOption(0);
}
void loop()
{
if (rotaryEncoder.encoderChanged())
{
showSelection();
}
if (rotaryEncoder.isEncoderButtonClicked())
{
rotary_onButtonClick();
}
}
void showSelection()
{
int selecedValue = rotaryEncoder.readEncoder();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("APPLE-JUICE-BOX");
printlnDisplay(2);
display.println(options[currentOption]);
switch (currentOption){
case 0: //"Select device"
display.print(">");
display.print(devices[selecedValue]);
Serial.print(">");
Serial.println(devices[selecedValue]);
break;
case 1: //"send advertisement"
display.print("1 = Y, 0 = N : ");
display.print(selecedValue);
Serial.println(selecedValue);
break;
default:
break;
}
display.display();
}
void sendAdvertisement()
{
Serial.print("Device Id: ");
Serial.println(deviceType);
displayText("Sending Advertisement...", 3000, 1);
}
void cancellAdvertisement()
{
displayText("Advertisement cancell", 1000, 1);
}
void discovery()
{
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("APPLEJUICE");
display.setTextSize(1);
display.setCursor(20, 20);
display.println("box - by DMB");
display.display();
delay(3000);
}
void displayText(char* text, int delayTime, int fontSize)
{
Serial.println(text);
display.clearDisplay();
display.setTextSize(fontSize);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("APPLE-JUICE-BOX");
printlnDisplay(2);
display.println(text);
display.display();
delay(delayTime);
}
void printlnDisplay(int num)
{
for (int i = 0; i < num; i++) {
display.println("");
}
}