#include "U8g2lib.h"
#define LCDWidth u8g2.getDisplayWidth()
#define ALIGN_CENTER(c) ((LCDWidth - (u8g2.getStrWidth(c))) / 2)
#define ALIGN_RIGHT(r) (LCDWidth - u8g2.getUTF8Width(r))
#define ALIGN_LEFT(l) (LCDWidth - 126)
#define joystick_x_pin A0
#define joystick_y_pin A1
#define up 0
#define select 1
#define down 2
#define esc 3
#define none 5
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
int item_selected = 0;
int headitem_selected = 0;
int current_screen = 0;
int RelayOn = 1500;
int RelayOff = 500;
int RelayTyp = 0;
int last_joy_read;
boolean TaktState = false;
unsigned long previousMins = 0;
unsigned long currentMins = 0;
const unsigned long RTCinterval = 1000;
struct DCLOCK {
int8_t Dhour;
int8_t Dminute;
int8_t Dsecond;
};
struct DCLOCK Dclock;
const int MAX_ITEM_LENGTH = 20;
char menu_items[4][MAX_ITEM_LENGTH] = {
{ "-SETTINGS-" },
{ "-MULTIMETER-" },
{ "-BATTERY-" },
{ "-GPS INFO-" }
};
char page1_items[4][MAX_ITEM_LENGTH] = {
{ "Time Settings" },
{ "Relay Settings" },
{ "Schedule" },
{ "Back" }
};
char page2_items[7][MAX_ITEM_LENGTH] = {
{ "Time Ref :" },
{ "Relay ON :" },
{ "Relay OFF :" },
{ "Relay Typ :" },
{ "Relay Dir :" },
{ "Relay Int :" },
{ "Back" }
};
void testContrast(uint8_t contrast)
{
static char cstr[6];
static char vstr[4];
u8g2.setContrast(contrast);
strcpy( cstr, u8x8_u8toa(contrast, 3));
u8g2.firstPage();
do {
//u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.setFont(u8g2_font_courB24_tn);
u8g2.drawStr(0, 28, cstr);
u8g2.drawBox(64, 0, 10, 64);
u8g2.drawBox(76, 0, 2, 64);
u8g2.drawBox(80, 0, 1, 64);
u8g2.drawBox(82, 0, 1, 64);
u8g2.drawBox(0, 32, 128, 10);
u8g2.drawBox(0, 44, 128, 2);
u8g2.drawBox(0, 48, 128, 1);
u8g2.drawBox(0, 50, 128, 1);
} while ( u8g2.nextPage() );
}
void setup() {
u8g2.begin();
u8g2.setFont(u8g2_font_profont15_mr);
u8g2.setFontPosTop();
last_joy_read = none;
}
void loop() {
uint8_t i;
i = 255;
do
{
testContrast(255 - i);
delay(15);
i -= 5;
} while ( i != 0 );
i = 255;
delay(100);
do
{
testContrast(i);
delay(15);
i -= 5;
} while ( i != 0 );
delay(100);
// enCoder();
//pages();
// runRTC();
}
void runRTC() {
static unsigned long previousMillis;
unsigned long currentMillis = millis();
static int previousMinute;
if (currentMillis - previousMillis >= RTCinterval) {
previousMillis += RTCinterval;
++Dclock.Dsecond;
if (Dclock.Dsecond > 59) {
Dclock.Dsecond = 0;
++Dclock.Dminute;
if (Dclock.Dminute > 59) {
Dclock.Dminute = 0;
++Dclock.Dhour;
if (Dclock.Dhour > 23) {
Dclock.Dhour = 0;
}
}
if (Dclock.Dminute != previousMinute) {
previousMinute = Dclock.Dminute;
TaktState = true; // Set boolean to true to trigger TaktRun()
}
}
}
}
void pages() {
char RTCstring[9];
sprintf(RTCstring, "%02d:%02d:%02d", Dclock.Dhour, Dclock.Dminute, Dclock.Dsecond);
u8g2.clearBuffer();
if (current_screen == 0) { //////////////////////MAIN SCREEN///
u8g2.setFont(u8g2_font_profont29_tr);
u8g2.setCursor(0, 25);
u8g2.print(RTCstring);
u8g2.setFont(u8g2_font_streamline_interface_essential_other_t);
u8g2.drawGlyph(0, 0, 0x003C);
u8g2.drawGlyph(22, 0, 0x003D);
u8g2.drawGlyph(44, 0, 0x003E);
u8g2.setFont(u8g2_font_streamline_interface_essential_link_t);
u8g2.drawGlyph(66, 0, 0x0030);
u8g2.drawGlyph(88, 0, 0x0031);
u8g2.setFont(u8g2_font_open_iconic_all_2x_t);
u8g2.drawGlyph(0, 48, 0x0075);
u8g2.drawGlyph(112, 48, 0x0076);
u8g2.setFont(u8g2_font_profont17_mr);
u8g2.drawStr(ALIGN_CENTER(String(menu_items[item_selected]).c_str()) - 0, 50, menu_items[item_selected]);
} else if (current_screen == 1) { //////////////////////SETTINGS SCREEN///
u8g2.setFont(u8g2_font_profont15_mr);
u8g2.drawStr(ALIGN_LEFT(String(page1_items[0]).c_str()) - 0, 0, page1_items[0]);
u8g2.drawStr(ALIGN_LEFT(String(page1_items[1]).c_str()) - 0, 15, page1_items[1]);
u8g2.drawStr(ALIGN_LEFT(String(page1_items[2]).c_str()) - 0, 30, page1_items[2]);
u8g2.drawStr(ALIGN_LEFT(String(page1_items[3]).c_str()) - 0, 45, page1_items[3]);
} else if (current_screen == 2) { //////////////////////Page 3///
u8g2.setFont(u8g2_font_profont15_mr);
u8g2.drawStr(ALIGN_LEFT(String(page2_items[0]).c_str()) - 0, 0, page2_items[0]);
u8g2.drawStr(ALIGN_LEFT(String(page2_items[1]).c_str()) - 0, 15, page2_items[1]);
u8g2.drawStr(ALIGN_LEFT(String(page2_items[2]).c_str()) - 0, 30, page2_items[2]);
u8g2.drawStr(ALIGN_LEFT(String(page2_items[3]).c_str()) - 0, 45, page2_items[3]);
}
u8g2.sendBuffer();
}
void enCoder() {
int current_joy_read = read_joystick();
if (current_joy_read != last_joy_read) {
last_joy_read = current_joy_read;
switch (current_joy_read) {
case up: //move_up
current_screen++;
if (current_screen >= 3) {
current_screen = 0;
}
break;
case down: // move_down
current_screen--;
if (current_screen < 0) {
current_screen = 2;
}
break;
case select: // move right
item_selected++;
if (item_selected >= 3) {
item_selected = 0;
}
break;
case esc: // move left
item_selected--;
if (item_selected < 0) {
item_selected = 3;
}
break;
}
}
}
int read_joystick() {
int output = none;
int X_Axis = analogRead(joystick_x_pin); // read the x axis value
int Y_Axis = analogRead(joystick_y_pin); // read the y axis value
if (X_Axis >= 900) {
output = select;
} else if (X_Axis <= 100) {
output = esc;
} else if (Y_Axis >= 900) {
output = up;
} else if (Y_Axis <= 100) {
output = down;
}
return output;
}