#include <U8g2lib.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define ENCODER_CLK 2 // CLK pin van de rotary encoder
#define ENCODER_DT 3 // DT pin van de rotary encoder
#define BUTTON_SW 4 // SW (push button) pin van de rotary encoder
#define FONT_USED u8x8_font_pxplusibmcgathin_f
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
volatile int encoderPos = 0;
int lastEncoderPos = 0;
unsigned long lastInterruptTime = 0;
unsigned long debounceDelay = 20; // Vertraging tegen debouncing
int item_selected = 0; // which item in the menu is selected
int currentPage = 0; // เพจปัจจุบัน
const char* mainMenu[] = {"SPEED TEST", "MOTOR BREAK-IN", "BREAK-IN SETUP"};
const char* p1Menu[] = {"START", "EXIT" };
const char* p2Menu[] = {"START", "STOP", "EXIT"};
const char* p3Menu[] = {"SAVE", "REVERT", "EXIT"};
bool speedTestrun = false;
bool breakInrun = false;
bool pauseState = false;
void setup() {
u8g2.begin();
Serial.begin(9600);
u8g2.setPowerSave(0);
u8g2.setDrawColor(1);
u8g2.setBitmapMode(0);
u8g2.setFont(u8g2_font_helvR08_tr);
pinMode(ENCODER_CLK, INPUT_PULLUP);
pinMode(ENCODER_DT, INPUT_PULLUP);
pinMode(BUTTON_SW, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), handleEncoder, CHANGE);
}
void handleEncoder() {
unsigned long currentMillis = millis();
if (currentMillis - lastInterruptTime > debounceDelay) {
int CLKstate = digitalRead(ENCODER_CLK);
int DTstate = digitalRead(ENCODER_DT);
if (CLKstate == DTstate) {
item_selected--;
} else {
item_selected++;
}
lastInterruptTime = currentMillis;
}
}
void buttonClicked() {
int buttonState = digitalRead(BUTTON_SW);
if (buttonState == LOW) {
switch (currentPage) {
case 0: //MAIN PAGE
switch (item_selected) {
case 0:
currentPage = 1;
break;
case 1:
currentPage = 2;
item_selected = 0;
break;
case 2:
currentPage = 3;
item_selected = 0;
break;
}
break;
case 1: //SPEED TEST PAGE
if (item_selected == 0) {
switch (speedTestrun) {
case false:
Serial.println("START SPEED TEST");
speedTestrun = true;
//speedTest();
break;
case true:
Serial.println("STOP SPEED TEST");
speedTestrun = false;
break;
}
}
else if (item_selected == 1) {
currentPage = 0;
item_selected = 0;
}
break;
case 2: //BREAK-IN PAGE
switch (item_selected) {
case 0: //Press Button 1
/*
switch (breakInrun) {
case false:
Serial.println("START BREAK-IN");
breakInrun = true;
pauseState = false;
break;
case true:
Serial.println("PAUSE BREAK-IN");
pauseState = true;
break;
}
*/
break;
case 1: //Press STOP
Serial.println("STOP BREAK-IN");
breakInrun = false;
pauseState = false;
break;
case 2: //Press EXIT
currentPage = 0;
item_selected = 1;
break;
}
case 3: //SETTING PAGE
if (item_selected == 0) {
Serial.println("SAVE SETTING");
}
else if (item_selected == 1) {
Serial.println("REVERT SETTING");
}
else if (item_selected == 2) {
currentPage = 0;
item_selected = 2;
}
break;
}
delay(100);
}
}
void loop() {
u8g2.setDrawColor(1);
u8g2.setBitmapMode(0);
u8g2.setFont(u8g2_font_courB08_tr);
buttonClicked();
u8g2.firstPage();
do {
switch (currentPage) {
case 0: //Main Page
u8g2.clearBuffer(); // required for page drawing mode for u8g2 library
createButtons(mainMenu, 1);
break;
case 1:
u8g2.clearBuffer(); // required for page drawing mode for u8g2 library
if (speedTestrun == false) {p1Menu[0] = "START";}
else {p1Menu[0] = "STOP";}
u8g2.drawLine(78, 1, 78, 64);
u8g2.setFont(u8g2_font_courB08_tr);
u8g2.drawButtonUTF8(37, 8, U8G2_BTN_HCENTER|U8G2_BTN_BW0, 74, 3, 1, "SPEED TEST" );
u8g2.setFont(u8g2_font_crox4hb_tf);
u8g2.drawButtonUTF8(37, 31, U8G2_BTN_HCENTER|U8G2_BTN_INV|U8G2_BTN_BW0, 74, 3, 3, "30000" );
u8g2.setFont(u8g2_font_courB08_tr);
u8g2.drawButtonUTF8(37, 44, U8G2_BTN_HCENTER|U8G2_BTN_INV|U8G2_BTN_BW0, 74, 3, 0, "RPM" );
u8g2.setFont(u8g2_font_7x14B_tf);
u8g2.drawButtonUTF8(12, 59, U8G2_BTN_HCENTER|U8G2_BTN_BW1, 20, 1, 1, "3V" );
u8g2.drawButtonUTF8(51, 59, U8G2_BTN_HCENTER|U8G2_BTN_BW1, 47, 1, 1, "00:10" );
createButtons(p1Menu, 0);
//Speed Test UI
break;
case 2:
u8g2.clearBuffer(); // required for page drawing mode for u8g2 library
/*
switch (breakInrun) {
case false:
p2Menu[0] = "START";
break;
case true:
switch (pauseState) {
case false:
p2Menu[0] = "PAUSE";
break;
case true:
p2Menu[0] = "RESUME";
break;
}
break;
}
createButtons(p2Menu, 0); */
break;
case 3:
u8g2.clearBuffer(); // required for page drawing mode for u8g2 library
createButtons(p3Menu, 0);
break;
}
u8g2.sendBuffer(); // send buffer from RAM to display controller
} while ( u8g2.nextPage() );
}
void createButtons(const char* menuList[], int isMain) {
int size = sizeof(menuList);//sizeof(menuList[0]);
if (currentPage == 1) {
size = 1;
}
if (item_selected > size) {
item_selected = 0;
}
else if (item_selected < 0) {
item_selected = size - 1;
}
// Button dimensions and spacing
int buttonWidth = u8g2.getDisplayWidth()-6*2;
int buttonHeight = 16;
int buttonSpacing = 3;
// Initial button position
int xPos = 63;
int yPos = 14;
if (isMain != 1) {
buttonWidth = 36;
buttonHeight = 16;
buttonSpacing = 3;
// Initial button position
xPos = 103;
yPos = 14;
}
u8g2.setFont(u8g2_font_courB08_tr);
for (int i = 0; i < size + 1; i++) {
if (item_selected == i) {
u8g2.drawButtonUTF8(xPos, yPos, U8G2_BTN_HCENTER|U8G2_BTN_BW0|U8G2_BTN_XFRAME , buttonWidth, buttonSpacing, buttonSpacing, menuList[i] );
}
else {
u8g2.drawButtonUTF8(xPos, yPos, U8G2_BTN_HCENTER|U8G2_BTN_BW0 , buttonWidth, buttonSpacing, buttonSpacing, menuList[i] );
}
// Move to next button position
yPos += buttonHeight + buttonSpacing;
}
}
void speedTest() {
while (speedTestrun == true && pauseState == false) {
Serial.println("SPEED TEST IS RUN");
for (int i=0; i < 10; i++) {
Serial.println("time remain ");
Serial.print(10 - i);
}
speedTestrun = false;
// countDown(0,10,3); //Speed Test at 3V for 10 sec
}
}
void breakIn() {
while (breakInrun == true) {
Serial.println("BREAK-IN IS RUNING");
}
}
/*
void countDown(int inMin, int inSec, float inVolt)
{
boolean RUN = true;
int inPwm = inVolt / 9 * 255;
while (RUN)
{
inSec = inSec - 1;
delay(1000);
if (inSec == -1)
{
inSec = 59;
inMin = inMin - 1;
}
if (inMin == -1)
{
inMin = 59;
}
analogWrite(3, inPwm);
Serial.println(inPwm);
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,7);
if (inPwm != 0) {
display.print(F("Power"));
display.setTextSize(2);
display.setCursor(36,0);
display.print(inVolt);
display.print(F("V"));
}
else {
display.print(F("Cooldown"));
}
display.drawFastHLine(0,19,61,SSD1306_WHITE);
display.drawFastVLine(0,19,17,SSD1306_WHITE);
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
display.setCursor(1,20);
if (inMin == 0 && inSec == 0)
{
RUN = false;
display.println("DONE ");
delay(1000);
}
else {
if (inMin <= 9)
{
display.print('0');
}
display.print(inMin);
display.print(':');
if (inSec <= 9)
{
display.print('0');
}
display.print(inSec);
}
display.display();
}
}*/