#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#define BUTTON_LEFT_PIN 5
#define BUTTON_RIGHT_PIN 6
#define BUTTON_LOCK_SET_PIN 7
#define BUTTON_LED_SET_PIN 34
#define BUTTON_LED_LOCK_PIN 35
#define BUTTON_ESC_PIN 8
#define BUTTON_LED_ESC_PIN 28
#define ENCODER_CLK_PIN 2
#define ENCODER_DT_PIN 3
#define BUTTON_SW_PIN 4 //ENTER
#define BUTTON_MEMORY_PIN 23
#define BUTTON_LED_MEMORY_PIN 26
#define BUTTON_MENU_PIN 22
#define BUTTON_LED_MENU_PIN 27
#define BUTTON_HOME_PIN 24
#define BUTTON_LED_HOME_PIN 30
#define BUTTON_LED_FUNC_PIN 31
#define BUTTON_ON_OFF_PIN 25
#define BUTTON_LED_ON_PIN 32
#define BUTTON_LED_OFF_PIN 33
U8G2_SSD1306_128X64_ALT0_F_HW_I2C u8g2(U8G2_R3, /* reset=*/ U8X8_PIN_NONE); // same as the NONAME variant, but may solve the "every 2nd line skipped" problem
//U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, /* clock=*/ PA5, /* data=*/ PA7, /* CS=*/ PB0, /* reset=*/ U8X8_PIN_NONE);
//U8G2_ST7920_128X64_F_HW_SPI u8g2(U8G2_R0, /* CS=*/ PB0, /* reset=*/ U8X8_PIN_NONE);
float rnd;
float U_Set = 0.000;
float U_MAX = 40.000;
float I_Set = 0.000;
float I_MAX = 4.000;
float P_Set = 0.00;
float P_MAX = 160.00;
float U_Out = 0;
float I_Out = 0;
float P_Out = 0;
const int ledPin = 13;
byte LE = 10;
byte H_Set = 0;
byte H_Out = 58;
long count_Encoder;
bool clicked_BUTTON_Enter;
bool Enter;
bool clicked_BUTTON_LEFT;
bool Left;
bool clicked_BUTTON_RIGHT;
bool Right;
bool clicked_BUTTON_MEMORY;
bool Memory;
bool LED_MEMORY;
bool clicked_BUTTON_MENU;
bool Menu;
bool LED_MENU;
bool clicked_BUTTON_ESC;
bool Esc;
bool LED_ESC;
bool clicked_BUTTON_HOME_FUNC;
bool Home;
bool Func;
bool LED_HOME;
bool LED_FUNC;
bool clicked_BUTTON_ON_OFF;
bool ON_OFF;
bool LED_ON;
bool LED_OFF;
bool clicked_BUTTON_LOCK_SET;
bool Setup;
bool Lock;
bool LED_LOCK;
bool LED_SET;
bool one_Hz;
byte status_Set; // вибір налаштування (напруга, струм, потужність)
bool flag_Set;
byte status_U_Set; // розряд числа наруги (1 - наймолодший розряд 0.001, 5 - найстарший розряд 10,000)
byte status_I_Set; // розряд числа струму (1 - наймолодший розряд 0.001, 5 - найстарший розряд 10,000)
byte status_P_Set; // розряд числа потудності (1 - наймолодший розряд 0.01, 5 - найстарший розряд 100,000)
bool const_P_Set_UI; // чи вибрана константа напруга чи струм
byte status_P_Set_UI;// яка константа вибрано
long count_Set;
bool flag_U1_Set;
bool flag_U2_Set;
bool flag_U3_Set;
bool flag_U4_Set;
bool flag_U5_Set;
bool flag_I1_Set;
bool flag_I2_Set;
bool flag_I3_Set;
bool flag_I4_Set;
bool flag_I5_Set;
bool flag_P_UI_Set;
bool flag_P1_Set;
bool flag_P2_Set;
bool flag_P3_Set;
bool flag_P4_Set;
bool flag_P5_Set;
int PASSWORD_A[] = {1, 1, 1, 1};
int PASSWORD_LOCK[] = {0, 0, 0, 0};
byte status_PASSWORD;
bool flag_PASSWORD_OK;
bool flag_PASSWORD_1;
bool flag_PASSWORD_2;
bool flag_PASSWORD_3;
bool flag_PASSWORD_4;
byte page;
byte alar_war;
#define lock_width 9
#define lock_height 9
static unsigned char lock_bits[] = {
0xc7, 0x01, 0x93, 0x01, 0xbb, 0x01, 0x01, 0x01, 0x01, 0x01, 0x11, 0x01,
0x11, 0x01, 0x01, 0x01, 0x01, 0x01
};
void setup() {
u8g2.begin();
Serial.begin(9600);
pinMode(BUTTON_LEFT_PIN, INPUT_PULLUP);
pinMode(BUTTON_RIGHT_PIN, INPUT_PULLUP);
pinMode(BUTTON_ESC_PIN, INPUT_PULLUP);
pinMode(BUTTON_LED_ESC_PIN, OUTPUT);
pinMode(ENCODER_CLK_PIN, INPUT);
pinMode(ENCODER_DT_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK_PIN), readEncoder, FALLING);
pinMode(BUTTON_SW_PIN, INPUT_PULLUP);
pinMode(BUTTON_MEMORY_PIN, INPUT_PULLUP);
pinMode(BUTTON_LED_MEMORY_PIN, OUTPUT);
pinMode(BUTTON_MENU_PIN, INPUT_PULLUP);
pinMode(BUTTON_LED_MENU_PIN, OUTPUT);
pinMode(BUTTON_HOME_PIN, INPUT_PULLUP);
pinMode(BUTTON_LED_HOME_PIN, OUTPUT);
pinMode(BUTTON_LED_FUNC_PIN, OUTPUT);
pinMode(BUTTON_ON_OFF_PIN, INPUT_PULLUP);
pinMode(BUTTON_LED_ON_PIN, OUTPUT);
pinMode(BUTTON_LED_OFF_PIN, OUTPUT);
pinMode(BUTTON_LOCK_SET_PIN, INPUT_PULLUP);
pinMode(BUTTON_LED_SET_PIN, OUTPUT);
pinMode(BUTTON_LED_LOCK_PIN, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void readEncoder () {
bool dtValue = digitalRead(ENCODER_DT_PIN);
if (dtValue == 1) {
count_Encoder--; // Clockwise
}
if (dtValue == 0) {
count_Encoder++; // Counterclockwise
}
}
void loop() {
button_Enter ();
button_Esc ();
button_Left ();
button_Right ();
button_Lock_Set ();
calculations ();
static uint32_t tmr1;
if (millis() - tmr1 >= 100) {
tmr1 = millis();
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_profont15_tr);
//u8g2.setFont(u8g2_font_frigidaire_mr);
//u8g2.setFont(u8g2_font_crox1h_tr);
switch (page) {
case 1:
main_page_Set();
main_page_Out();
break;
case 2:
P_Set_UI_page();
main_page_Out();
break;
case 3:
unlocking_page();
main_page_Out();
break;
default:
u8g2.drawButtonUTF8(5, 20, U8G2_BTN_INV, u8g2.getDisplayWidth() - 5 * 2, 5, 2, "LOADING" );
}
if (alar_war > 0) {
alarm_warning ();
}
// ... write something to the buffer
u8g2.sendBuffer();
}
static uint32_t tmr2;
if (millis() - tmr2 >= 500) {
tmr2 = millis();
rnd = random(1000);
if (U_Out > 10) {
U_Out = U_Out - rnd / 1000;
} else {
U_Out = U_Out + rnd / 1000;
}
I_Out = U_Out;
P_Out = I_Out * U_Out;
}
static uint32_t tmr_1Hz;
if (millis() - tmr_1Hz >= 1000) {
tmr_1Hz = millis();
one_Hz = !one_Hz;
}
if (page == 0 && one_Hz == 1) {
page = 1;
}
}