/*
Simple "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include "lcdgfx.h"
#include "lcdgfx_gui.h"
#include "EncoderTool.h"
#if 1
#define DBG(x) Serial.print(__func__); \
Serial.print(":"); \
Serial.print(__LINE__); \
Serial.print(" \""); \
Serial.print(String(x).c_str()); \
Serial.println("\"");
#else
#define DBG(x) void(0);
#endif
#define ENCODER_PRESSED(x) \
x.buttonChanged() && (x.getButton() == LOW)
#define ENCODER_CLK 3
#define ENCODER_DT 4
#define ENCODER_SW 2
#define FREQUENCY_MAX 12000000
#define FREQUENCY_MIN 0
enum {
SCREEN_MENU,
SCREEN_MODE,
SCREEN_FREQ
} Screen_E;
char *menuOptions[] =
{
"Mode",
"Freqency"
};
char *modeOptions[] =
{
"off",
"Sine",
"Square1",
"Square2",
"Triangle"
};
DisplaySSD1306_128x64_I2C display(-1);
LcdGfxMenu menu(menuOptions, sizeof(menuOptions)/sizeof(char*), (NanoRect){{0,16},{0,0}});
LcdGfxMenu modeMenu(modeOptions, sizeof(modeOptions)/sizeof(char*), (NanoRect){{0,16},{0,0}});
EncoderTool::PolledEncoder encoder;
bool dirty = false;
bool selectedStatus = false;
uint8_t mode = 0;
uint8_t screen = SCREEN_MENU;
uint32_t frequency = 0;
void setup() {
Serial.begin(115200);
display.begin();
encoder.begin(ENCODER_DT, ENCODER_CLK, ENCODER_SW);
display.setFixedFont(ssd1306xled_font6x8);
display.clear();
dirty = true;
}
void printHeader()
{
display.printFixed(0,0,"Status");
display.printFixed(128-(strlen(modeOptions[mode])*8),0,modeOptions[mode], STYLE_BOLD);
display.printFixed(0,8, String(frequency).c_str());
}
void loop()
{
encoder.tick();
switch (screen)
{
case SCREEN_MENU:
handleMenu();
break;
case SCREEN_MODE:
handleMode();
break;
case SCREEN_FREQ:
handleFreq();
break;
}
}
void handleMenu()
{
if (dirty)
{
dirty = false;
display.clear();
printHeader();
menu.show(display);
}
if (ENCODER_PRESSED(encoder))
{
screen = menu.selection()+1;
dirty = true;
}
if (encoder.valueChanged())
{
dirty = true;
if (-1 == encoder.getValue())
{
menu.down();
}
else
{
menu.up();
}
encoder.setValue(0);
}
}
void handleMode()
{
if (dirty)
{
dirty = false;
display.clear();
printHeader();
modeMenu.show(display);
}
if (ENCODER_PRESSED(encoder))
{
mode = modeMenu.selection();
dirty = true;
screen = SCREEN_MENU;
}
if (encoder.valueChanged())
{
dirty = true;
if (-1 == encoder.getValue())
{
modeMenu.down();
}
else
{
modeMenu.up();
}
encoder.setValue(0);
}
}
void handleFreq()
{
if (dirty)
{
dirty = false;
display.clear();
printHeader();
}
if (ENCODER_PRESSED(encoder))
{
mode = modeMenu.selection();
dirty = true;
screen = SCREEN_MENU;
}
if (encoder.valueChanged())
{
dirty = true;
if (-1 == encoder.getValue())
{
if (frequency < FREQUENCY_MAX)
{
frequency++;
}
}
else
{
if (frequency > FREQUENCY_MIN)
{
frequency--;
}
}
encoder.setValue(0);
}
}