//Include the graphics library.
#include "U8glib.h"
//Initialize display.
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE | U8G_I2C_OPT_DEV_0);
#define outputA 2
#define outputB 3
int counter = 0;
int aState;
int aLastState;
#define MENU_ITEMS 3
const char *menu_strings[MENU_ITEMS] = { "DASHBOARD", "SETTINGS", "CALIBRATE"};
void setup(void)
{
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
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(){
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState){
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}
Serial.print("Position: ");
Serial.println(counter);
}
aLastState = aState; // Updates the previous state of the outputA with the current state
}