#include <Wire.h>
#include "flprogUtilites.h"
#include <LiquidCrystal.h>
LiquidCrystal _lcd1(5, 6, 7, 8, 9, 10);
int _dispTempLength1=0;
boolean _isNeedClearDisp1;
const int _menuParametrsArray[] PROGMEM = {1, 4, 1, 0, 0, 0, 2, 4, 1, 4, 1, 0, 2, 4, 2, 0, 0, 0, 3, 4, 1, 4, 2, 0};
int _menuValueArray_int[2];
const int _menuConstantValuesArray_int[] PROGMEM = {1, 23, 59, 0};
const char _flprogMenuNameString1[] PROGMEM = "Hours";
const char _flprogMenuNameString2[] PROGMEM = "Minute";
const char* const _flprogMenuStringsArray[] PROGMEM = { _flprogMenuNameString1, _flprogMenuNameString2};
struct _menuItemStricture
{
int startInArrayIndex;
}
;
struct _menuMainStricture
{
int startIndex;
int stopIndex;
bool isSwitchMenuAroundRing;
_menuItemStricture currentItem;
}
;
_menuItemStricture _MenuItems[2];
_menuMainStricture _MainMenus[1];
struct _I2CRealRimeClockTime
{
byte seconds = 0;
byte minutes = 0;
byte Hours = 0;
byte weekday = 0;
byte day = 0;
byte month = 0;
byte year = 0;
unsigned long startTime = 0;
}
;
_I2CRealRimeClockTime _RTC2;
bool _gtv1 = 0;
String _MenuBlock_206002866_MNO;
String _MenuBlock_206002866_VNO;
bool _MenuBlock_206002866_OEIS = 0;
bool _MenuBlock_206002866_OMUIS = 0;
bool _MenuBlock_206002866_OMDIS = 0;
bool _MenuBlock_206002866_OVUIS = 0;
bool _MenuBlock_206002866_OVDIS = 0;
bool _bounseInputD14S = 0;
bool _bounseInputD14O = 0;
unsigned long _bounseInputD14P = 0UL;
String _RTC2_GetTime1_StrOut;
bool _bounseInputD16S = 0;
bool _bounseInputD16O = 0;
unsigned long _bounseInputD16P = 0UL;
int _disp3oldLength = 0;
bool _trgt1 = 0;
bool _trgt1I = 0;
bool _bounseInputD11S = 0;
bool _bounseInputD11O = 0;
unsigned long _bounseInputD11P = 0UL;
int _disp2oldLength = 0;
bool _bounseInputD15S = 0;
bool _bounseInputD15O = 0;
unsigned long _bounseInputD15P = 0UL;
int _disp1oldLength = 0;
bool _bounseInputD17S = 0;
bool _bounseInputD17O = 0;
unsigned long _bounseInputD17P = 0UL;
bool _RTCST_Ost1 = 0;
bool _tempVariable_bool;
int _tempVariable_int;
void setup()
{
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
pinMode(17, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
Wire.begin();
delay(10);
Wire.beginTransmission(68);
Wire.write(0x0E);
Wire.write(B00000000);
Wire.write(B10001000);
Wire.endTransmission();
_RTC2.startTime = millis() - 1000;
_lcd1.begin(16, 2);
_bounseInputD14O = digitalRead(14);
_bounseInputD15O = digitalRead(15);
_bounseInputD17O = digitalRead(17);
_bounseInputD16O = digitalRead(16);
_bounseInputD11O = digitalRead(11);
_MenuItems[0].startInArrayIndex = 0;
_MenuItems[1].startInArrayIndex = 12;
_MainMenus[0].startIndex = 1;
_MainMenus[0].isSwitchMenuAroundRing = 1;
_MainMenus[0].stopIndex = 2;
_MainMenus[0].currentItem = _MenuItems[0];
}
void loop()
{
if (_isNeedClearDisp1)
{
_lcd1.clear();
_isNeedClearDisp1= 0;
}
bool _bounceInputTmpD14 = (digitalRead (14));
if (_bounseInputD14S)
{
if (millis() >= (_bounseInputD14P + 40))
{
_bounseInputD14O= _bounceInputTmpD14;
_bounseInputD14S=0;
}
}
else
{
if (_bounceInputTmpD14 != _bounseInputD14O)
{
_bounseInputD14S=1;
_bounseInputD14P = millis();
}
}
bool _bounceInputTmpD15 = (digitalRead (15));
if (_bounseInputD15S)
{
if (millis() >= (_bounseInputD15P + 40))
{
_bounseInputD15O= _bounceInputTmpD15;
_bounseInputD15S=0;
}
}
else
{
if (_bounceInputTmpD15 != _bounseInputD15O)
{
_bounseInputD15S=1;
_bounseInputD15P = millis();
}
}
bool _bounceInputTmpD17 = (digitalRead (17));
if (_bounseInputD17S)
{
if (millis() >= (_bounseInputD17P + 40))
{
_bounseInputD17O= _bounceInputTmpD17;
_bounseInputD17S=0;
}
}
else
{
if (_bounceInputTmpD17 != _bounseInputD17O)
{
_bounseInputD17S=1;
_bounseInputD17P = millis();
}
}
bool _bounceInputTmpD16 = (digitalRead (16));
if (_bounseInputD16S)
{
if (millis() >= (_bounseInputD16P + 40))
{
_bounseInputD16O= _bounceInputTmpD16;
_bounseInputD16S=0;
}
}
else
{
if (_bounceInputTmpD16 != _bounseInputD16O)
{
_bounseInputD16S=1;
_bounseInputD16P = millis();
}
}
bool _bounceInputTmpD11 = (digitalRead (11));
if (_bounseInputD11S)
{
if (millis() >= (_bounseInputD11P + 40))
{
_bounseInputD11O= _bounceInputTmpD11;
_bounseInputD11S=0;
}
}
else
{
if (_bounceInputTmpD11 != _bounseInputD11O)
{
_bounseInputD11S=1;
_bounseInputD11P = millis();
}
}
//Плата:1
bool _tmp1 = !(_bounseInputD11O);
if (_tmp1)
{
if (! _trgt1I) _trgt1 = ! _trgt1;
}
_trgt1I = _tmp1;
_gtv1 = _trgt1;
if (_gtv1)
{
_tempVariable_bool = 1;
if (! _MenuBlock_206002866_OEIS)
{
_MenuBlock_206002866_OEIS = 1;
}
_tempVariable_int = pgm_read_byte(&_menuParametrsArray[((_MainMenus[0].currentItem).startInArrayIndex)+10]);
_MenuBlock_206002866_MNO = _readStringFromProgmem ((char*)pgm_read_word(&(_flprogMenuStringsArray[_tempVariable_int - 1])));
_MenuBlock_206002866_VNO = _menuOutputValueString (0);
}
else
{
_tempVariable_bool = 0;
if (_MenuBlock_206002866_OEIS)
{
_MenuBlock_206002866_OEIS = 0;
}
_MenuBlock_206002866_MNO = "";
_MenuBlock_206002866_VNO = "";
}
if(!(_bounseInputD14O))
{
if (! _MenuBlock_206002866_OMUIS)
{
_MenuBlock_206002866_OMUIS = 1;
if(_tempVariable_bool)
{
_menuUpEvents(0);
}
}
}
else
{
_MenuBlock_206002866_OMUIS = 0;
}
if(!(_bounseInputD15O))
{
if (! _MenuBlock_206002866_OMDIS)
{
_MenuBlock_206002866_OMDIS = 1;
if(_tempVariable_bool)
{
_menuDownEvents(0);
}
}
}
else
{
_MenuBlock_206002866_OMDIS = 0;
}
if(!(_bounseInputD17O))
{
if (! _MenuBlock_206002866_OVUIS)
{
_MenuBlock_206002866_OVUIS = 1;
if(_tempVariable_bool)
{
_valueUpEvents(0);
}
}
}
else
{
_MenuBlock_206002866_OVUIS = 0;
}
if(!(_bounseInputD16O))
{
if (! _MenuBlock_206002866_OVDIS)
{
_MenuBlock_206002866_OVDIS = 1;
if(_tempVariable_bool)
{
_valueDownEvents(0);
}
}
}
else
{
_MenuBlock_206002866_OVDIS = 0;
}
if (_gtv1)
{
_dispTempLength1 = ((_MenuBlock_206002866_VNO)).length();
if (_disp3oldLength > _dispTempLength1)
{
_isNeedClearDisp1 = 1;
}
_disp3oldLength = _dispTempLength1;
_lcd1.setCursor(int((16 - _dispTempLength1)/2), 1);
_lcd1.print((_MenuBlock_206002866_VNO));
}
else
{
if (_disp3oldLength > 0)
{
_isNeedClearDisp1 = 1;
_disp3oldLength = 0;
}
}
if (_gtv1)
{
_dispTempLength1 = ((_MenuBlock_206002866_MNO)).length();
if (_disp2oldLength > _dispTempLength1)
{
_isNeedClearDisp1 = 1;
}
_disp2oldLength = _dispTempLength1;
_lcd1.setCursor(int((16 - _dispTempLength1)/2), 0);
_lcd1.print((_MenuBlock_206002866_MNO));
}
else
{
if (_disp2oldLength > 0)
{
_isNeedClearDisp1 = 1;
_disp2oldLength = 0;
}
}
get3231Date(0x68, &_RTC2);
_RTC2_GetTime1_StrOut = (_convertRealTimeClockNumberToString(_RTC2.Hours))+
String(":")+
(_convertRealTimeClockNumberToString(_RTC2.minutes))+
String(":")+
(_convertRealTimeClockNumberToString(_RTC2.seconds));
if (!(_gtv1))
{
_dispTempLength1 = ((_RTC2_GetTime1_StrOut)).length();
if (_disp1oldLength > _dispTempLength1)
{
_isNeedClearDisp1 = 1;
}
_disp1oldLength = _dispTempLength1;
_lcd1.setCursor(int((16 - _dispTempLength1)/2), 0);
_lcd1.print((_RTC2_GetTime1_StrOut));
}
else
{
if (_disp1oldLength > 0)
{
_isNeedClearDisp1 = 1;
_disp1oldLength = 0;
}
}
if (_gtv1)
{
if (!(_RTCST_Ost1))
{
set3231Date(0x68, &_RTC2, -1, ((_menuValueArray_int[1])), ((_menuValueArray_int[0])), -1, -1, -1, -1);
get3231Date(0x68, &_RTC2);
_RTCST_Ost1 = 1;
}
}
else
{
_RTCST_Ost1 = 0;
}
}
String _readStringFromProgmem (char *string)
{
String result = String("");
while (pgm_read_byte(string)!='\0')
{
result=result+ char(pgm_read_byte(string));
string++;
}
return result;
}
void _menuUpEvents (int menuIndex)
{
int tempIndex = pgm_read_byte(&_menuParametrsArray[((_MainMenus[menuIndex]).currentItem).startInArrayIndex]);
int parIndex = pgm_read_byte(&_menuParametrsArray[ (((_MainMenus[menuIndex]).currentItem).startInArrayIndex)+5]);
int parentStartIndex;
int parentStopIndex;
if (parIndex == 0)
{
parentStartIndex = (_MainMenus[menuIndex]).startIndex;
parentStopIndex =(_MainMenus[menuIndex]).stopIndex;
}
else
{
parentStartIndex = pgm_read_byte(&_menuParametrsArray[((_MenuItems[parIndex - 1]).startInArrayIndex)+3]);
parentStopIndex = pgm_read_byte(&_menuParametrsArray[((_MenuItems[parIndex-1]).startInArrayIndex)+4]);
}
if (tempIndex == parentStartIndex)
{
if((_MainMenus[menuIndex]).isSwitchMenuAroundRing)
{
(_MainMenus[menuIndex]).currentItem = _MenuItems[parentStopIndex -1];
return;
}
else
{
return;
}
}
(_MainMenus[menuIndex]).currentItem = _MenuItems[tempIndex - 2];
return;
}
void _menuDownEvents (int menuIndex)
{
int tempIndex = pgm_read_byte(&_menuParametrsArray[((_MainMenus[menuIndex]).currentItem).startInArrayIndex]);
int parIndex = pgm_read_byte(&_menuParametrsArray[(((_MainMenus[menuIndex]).currentItem).startInArrayIndex)+5]);
int parentStartIndex;
int parentStopIndex;
if (parIndex == 0)
{
parentStartIndex = (_MainMenus[menuIndex]).startIndex;
parentStopIndex =(_MainMenus[menuIndex]).stopIndex;
}
else
{
parentStartIndex = pgm_read_byte(&_menuParametrsArray[((_MenuItems[parIndex-1]).startInArrayIndex)+3]);
parentStopIndex = pgm_read_byte(&_menuParametrsArray[((_MenuItems[parIndex-1]).startInArrayIndex)+4]);
}
if (tempIndex == parentStopIndex)
{
if((_MainMenus[menuIndex]).isSwitchMenuAroundRing)
{
(_MainMenus[menuIndex]).currentItem = _MenuItems[parentStartIndex -1];
return;
}
else
{
return;
}
}
(_MainMenus[menuIndex]).currentItem = _MenuItems[tempIndex];
return;
}
void _valueUpEvents (int menuIndex)
{
int valIndex = pgm_read_byte(&_menuParametrsArray[(((_MainMenus[menuIndex]).currentItem).startInArrayIndex)+2]);
int itemType = pgm_read_byte(&_menuParametrsArray[(((_MainMenus[menuIndex]).currentItem).startInArrayIndex)+1]);
int indexMax = pgm_read_byte(&_menuParametrsArray[(((_MainMenus[menuIndex]).currentItem).startInArrayIndex)+6]);
int indexStep = pgm_read_byte(&_menuParametrsArray[(((_MainMenus[menuIndex]).currentItem).startInArrayIndex)+8]);
if (itemType == 4)
{
if (! indexMax == 0)
{
if (! (int(pgm_read_word(&_menuConstantValuesArray_int[indexMax -1])) > int(_menuValueArray_int[valIndex -1])))
{
return;
}
}
_menuValueArray_int[valIndex -1] = _menuValueArray_int[valIndex -1] + (pgm_read_word(&_menuConstantValuesArray_int[indexStep -1]));
}
}
void _valueDownEvents (int menuIndex)
{
int valIndex = pgm_read_byte(&_menuParametrsArray[(((_MainMenus[menuIndex]).currentItem).startInArrayIndex)+2]);
int itemType = pgm_read_byte(&_menuParametrsArray[(((_MainMenus[menuIndex]).currentItem).startInArrayIndex)+1]);
int indexMin = pgm_read_byte(&_menuParametrsArray[(((_MainMenus[menuIndex]).currentItem).startInArrayIndex)+7]);
int indexStep = pgm_read_byte(&_menuParametrsArray[(((_MainMenus[menuIndex]).currentItem).startInArrayIndex)+8]);
if (itemType == 4)
{
if (! indexMin == 0)
{
if (! ((int(pgm_read_word(&_menuConstantValuesArray_int[indexMin - 1]))) < int(_menuValueArray_int[valIndex - 1])))
{
return;
}
}
_menuValueArray_int[valIndex - 1] = _menuValueArray_int[valIndex - 1] - (pgm_read_word(&_menuConstantValuesArray_int[indexStep - 1]));
}
}
String _menuOutputValueString (int menuIndex)
{
int itemType = pgm_read_byte(&_menuParametrsArray[ (((_MainMenus[menuIndex]).currentItem).startInArrayIndex)+1]);
int valIndex = pgm_read_byte(&_menuParametrsArray[(((_MainMenus[menuIndex]).currentItem).startInArrayIndex)+2]);
int indexMin = pgm_read_byte(&_menuParametrsArray[(((_MainMenus[menuIndex]).currentItem).startInArrayIndex)+7]);
int indexMax = pgm_read_byte(&_menuParametrsArray[(((_MainMenus[menuIndex]).currentItem).startInArrayIndex)+6]);
if(valIndex == 0)
{
return "";
}
int convFormat = pgm_read_byte(&_menuParametrsArray[(((_MainMenus[menuIndex]).currentItem).startInArrayIndex)+9]);
if(itemType == 4)
{
return _convertNamber(itemType, convFormat, valIndex, indexMax, indexMin);
}
return "";
}
String _convertNamber(int itemType, int convFormat, int valIndex, int indexMax, int indexMin)
{
if (itemType== 4)
{
if (convFormat == 4)
{
return String((_menuValueArray_int[valIndex - 1 ]),DEC);
}
if (convFormat == 5)
{
return String((_menuValueArray_int[valIndex - 1]),HEX);
}
if (convFormat == 6)
{
return String((_menuValueArray_int[valIndex -1]),BIN);
}
}
return "";
}
byte decToBcd(byte val)
{
return ((val/10*16) + (val%10));
}
void set3231Date(int addres, struct _I2CRealRimeClockTime *timeStruct, int sec, int minut, int hou, int dat, int mo, int ye, int daW)
{
byte temp;
Wire.beginTransmission(addres);
Wire.write(0x00);
if (sec < 0)
{
temp = timeStruct -> seconds;
}
else
{
temp = sec;
}
Wire.write(decToBcd(temp));
if (minut < 0)
{
temp = timeStruct -> minutes;
}
else
{
temp = minut;
}
Wire.write(decToBcd(temp));
if (hou < 0)
{
temp = timeStruct -> Hours;
}
else
{
temp = hou;
}
Wire.write(decToBcd(temp));
if (daW < 0)
{
temp = timeStruct -> weekday;
}
else
{
temp = daW;
}
Wire.write(decToBcd(temp));
if (dat < 0)
{
temp = timeStruct -> day;
}
else
{
temp = dat;
}
Wire.write(decToBcd(temp));
if (mo < 0)
{
temp = timeStruct -> month;
}
else
{
temp = mo;
}
Wire.write(decToBcd(temp));
if (ye < 0)
{
temp = timeStruct -> year;
}
else
{
temp = ye;
}
Wire.write(decToBcd(temp));
Wire.endTransmission();
timeStruct ->startTime = millis() - 1000;
}
void get3231Date(int addres, struct _I2CRealRimeClockTime *timeStruct)
{
if (!(flprog::isTimer ((timeStruct ->startTime), 250)))
{
return;
}
Wire.beginTransmission(addres);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(addres, 7);
if (Wire.available())
{
byte seconds = Wire.read();
byte minutes = Wire.read();
byte hours = Wire.read();
byte day = Wire.read();
byte date = Wire.read();
byte month = Wire.read();
byte year = Wire.read();
timeStruct -> seconds = (((seconds & B11110000) >> 4) * 10 + (seconds & B00001111));
timeStruct -> minutes = (((minutes & B11110000) >> 4) * 10 + (minutes & B00001111));
timeStruct -> Hours = (((hours & B00110000) >> 4) * 10 + (hours & B00001111));
timeStruct -> weekday = (day & B00000111);
timeStruct -> day = (((date & B00110000) >> 4) * 10 + (date & B00001111));
timeStruct -> month = (((month & B00010000) >> 4) * 10 + (month & B00001111));
timeStruct -> year = (((year & B11110000) >> 4) * 10 + (year & B00001111));
}
timeStruct ->startTime = millis();
}
String _convertRealTimeClockNumberToString(byte value)
{
if (value > 9)
{
return String(value, DEC);
}
return (String("0") + String(value, DEC));
}