//Include the graphics library.
#include "U8glib.h"
//Initialize display.
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE | U8G_I2C_OPT_DEV_0);
#define DT 2
#define CLK 3
#define SW 4
int counter = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir ="";
unsigned long lastButtonPress = 0;
#define MENU_ITEMS 3
const char *menu_strings[MENU_ITEMS] = { "DASHBOARD", "SETTINGS", "CALIBRATE"};
void setup(void)
{
pinMode (DT,INPUT);
pinMode (CLK,INPUT);
pinMode(SW, INPUT_PULLUP);
lastStateCLK = digitalRead(CLK);
Serial.begin(115200);
//Set font.
u8g.setFont(u8g_font_unifont);
}
void loop(void)
{
rotary();
u8g.firstPage();
do {
dashboard();
// footer();
// draw();
// drawMenu();
} while (u8g.nextPage());
//Delay before repeating the loop.
delay(50);
}
void draw(void)
{
//Write text. (x, y, text)
u8g.setFont(u8g_font_unifont);
u8g.drawStr(25, 10, "MAIN MENU");
u8g.drawHLine(0,12,128);
}
void drawMenu(){
u8g.setFont(u8g_font_5x7);
int MenuY=25;
for(int i=0;i<=MENU_ITEMS;i++){
u8g.drawStr(5, MenuY, menu_strings[i]);
MenuY += i+14;
}
}
void dashboard(){
//For Header Specification
u8g.setFont(u8g_font_6x10r);
u8g.drawStr(5, 8, "AXIS");
u8g.drawStr(40, 8, "SPEED");
u8g.drawStr(80, 8, "POSITION");
u8g.drawHLine(0,12,128);
//For Axis Specification
u8g.setFont(u8g_font_5x8r);
u8g.drawStr(10, 25, "X :");
u8g.drawStr(10, 37, "Y :");
u8g.drawStr(10, 49, "Z :");
//For Speed Specification
u8g.setFont(u8g_font_5x8r);
u8g.drawStr(40, 25, "2300");
u8g.drawStr(40, 37, "5400");
u8g.drawStr(40, 49, "1600");
//For Position Specification
u8g.setFont(u8g_font_5x8r);
u8g.drawStr(80, 25, "2300");
u8g.drawStr(80, 37, "5400");
u8g.drawStr(80, 49, "1600");
}
void footer(){
u8g.setFont(u8g_font_04b_24);
u8g.drawStr(2, 60, "MAIN MENU > DASHBOARD");
}
void rotary(){
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
counter --;
currentDir ="CCW";
} else {
// Encoder is rotating CW so increment
counter ++;
currentDir ="CW";
}
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
}
// Remember last CLK state
lastStateCLK = currentStateCLK;
// Read the button state
int btnState = digitalRead(SW);
//If we detect LOW signal, button is pressed
if (btnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
Serial.println("Button pressed!");
}
// Remember last button press event
lastButtonPress = millis();
}
// Put in a slight delay to help debounce the reading
delay(1);
}