#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <iarduino_RTC.h>
iarduino_RTC _RTC1(RTC_DS1302, 4, 2, 3);
LiquidCrystal_I2C _lcd1(0x27, 16, 2);
int _dispTempLength1=0;
boolean _isNeedClearDisp1;
const int _menuParametrsArray[] PROGMEM = {1, 4, 1, 0, 0, 0, 2, 1, 1, 4, 1, 0, 2, 4, 2, 0, 0, 0, 3, 1, 1, 4, 2, 0};
int _menuValueArray_int[2];
const int _menuConstantValuesArray_int[] PROGMEM = {1, 24, 60};
const char _flprogMenuNameString1[] PROGMEM = "Chas";
const char _flprogMenuNameString2[] PROGMEM = "Min";
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];
bool _gtv1;
bool _bounseInputD8S = 0;
bool _bounseInputD8O = 0;
unsigned long _bounseInputD8P = 0UL;
bool _bounseInputD9S = 0;
bool _bounseInputD9O = 0;
unsigned long _bounseInputD9P = 0UL;
bool _RTCST_Ost1 = 0;
bool _bounseInputD10S = 0;
bool _bounseInputD10O = 0;
unsigned long _bounseInputD10P = 0UL;
bool _bounseInputD6S = 0;
bool _bounseInputD6O = 0;
unsigned long _bounseInputD6P = 0UL;
String _MenuBlock_197478283_MNO;
String _MenuBlock_197478283_VNO;
bool _MenuBlock_197478283_OEIS = 0;
bool _MenuBlock_197478283_OMUIS = 0;
bool _MenuBlock_197478283_OMDIS = 0;
bool _MenuBlock_197478283_OVUIS = 0;
bool _MenuBlock_197478283_OVDIS = 0;
int _disp3oldLength = 0;
String _RTC1_GetTime1_StrOut;
int _disp1oldLength = 0;
int _disp4oldLength = 0;
bool _trgt1 = 0;
bool _trgt1I = 0;
bool _bounseInputD7S = 0;
bool _bounseInputD7O = 0;
unsigned long _bounseInputD7P = 0UL;
bool _tempVariable_bool;
int _tempVariable_int;
void setup()
{
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
Wire.begin();
delay(10);
_RTC1.begin();
_RTC1.period(1);
_lcd1.init();
_lcd1.backlight();
_bounseInputD9O = digitalRead(9);
_bounseInputD10O = digitalRead(10);
_bounseInputD8O = digitalRead(8);
_bounseInputD7O = digitalRead(7);
_bounseInputD6O = digitalRead(6);
_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 _bounceInputTmpD9 = (digitalRead (9));
if (_bounseInputD9S)
{
if (millis() >= (_bounseInputD9P + 40))
{
_bounseInputD9O= _bounceInputTmpD9;
_bounseInputD9S=0;
}
}
else
{
if (_bounceInputTmpD9 != _bounseInputD9O)
{
_bounseInputD9S=1;
_bounseInputD9P = millis();
}
}
bool _bounceInputTmpD10 = (digitalRead (10));
if (_bounseInputD10S)
{
if (millis() >= (_bounseInputD10P + 40))
{
_bounseInputD10O= _bounceInputTmpD10;
_bounseInputD10S=0;
}
}
else
{
if (_bounceInputTmpD10 != _bounseInputD10O)
{
_bounseInputD10S=1;
_bounseInputD10P = millis();
}
}
bool _bounceInputTmpD8 = (digitalRead (8));
if (_bounseInputD8S)
{
if (millis() >= (_bounseInputD8P + 40))
{
_bounseInputD8O= _bounceInputTmpD8;
_bounseInputD8S=0;
}
}
else
{
if (_bounceInputTmpD8 != _bounseInputD8O)
{
_bounseInputD8S=1;
_bounseInputD8P = millis();
}
}
bool _bounceInputTmpD7 = (digitalRead (7));
if (_bounseInputD7S)
{
if (millis() >= (_bounseInputD7P + 40))
{
_bounseInputD7O= _bounceInputTmpD7;
_bounseInputD7S=0;
}
}
else
{
if (_bounceInputTmpD7 != _bounseInputD7O)
{
_bounseInputD7S=1;
_bounseInputD7P = millis();
}
}
bool _bounceInputTmpD6 = (digitalRead (6));
if (_bounseInputD6S)
{
if (millis() >= (_bounseInputD6P + 40))
{
_bounseInputD6O= _bounceInputTmpD6;
_bounseInputD6S=0;
}
}
else
{
if (_bounceInputTmpD6 != _bounseInputD6O)
{
_bounseInputD6S=1;
_bounseInputD6P = millis();
}
}
//Плата:1
bool _tmp1 = !(_bounseInputD6O);
if (_tmp1)
{
if (! _trgt1I) _trgt1 = ! _trgt1;
}
_trgt1I = _tmp1;
_gtv1 = _trgt1;
if (_gtv1)
{
if (!(_RTCST_Ost1))
{
_RTC1.settime(-1, ((_menuValueArray_int[1])), ((_menuValueArray_int[0])), -1, -1, -1, -1);
_RTCST_Ost1 = 1;
}
}
else
{
_RTCST_Ost1 = 0;
}
if (_gtv1)
{
_tempVariable_bool = 1;
if (! _MenuBlock_197478283_OEIS)
{
_MenuBlock_197478283_OEIS = 1;
}
_tempVariable_int = pgm_read_byte(&_menuParametrsArray[((_MainMenus[0].currentItem).startInArrayIndex)+10]);
_MenuBlock_197478283_MNO = _readStringFromProgmem ((char*)pgm_read_word(&(_flprogMenuStringsArray[_tempVariable_int - 1])));
_MenuBlock_197478283_VNO = _menuOutputValueString (0);
}
else
{
_tempVariable_bool = 0;
if (_MenuBlock_197478283_OEIS)
{
_MenuBlock_197478283_OEIS = 0;
}
_MenuBlock_197478283_MNO = "";
_MenuBlock_197478283_VNO = "";
}
if(!(_bounseInputD7O))
{
if (! _MenuBlock_197478283_OMUIS)
{
_MenuBlock_197478283_OMUIS = 1;
if(_tempVariable_bool)
{
_menuUpEvents(0);
}
}
}
else
{
_MenuBlock_197478283_OMUIS = 0;
}
if(!(_bounseInputD8O))
{
if (! _MenuBlock_197478283_OMDIS)
{
_MenuBlock_197478283_OMDIS = 1;
if(_tempVariable_bool)
{
_menuDownEvents(0);
}
}
}
else
{
_MenuBlock_197478283_OMDIS = 0;
}
if(!(_bounseInputD9O))
{
if (! _MenuBlock_197478283_OVUIS)
{
_MenuBlock_197478283_OVUIS = 1;
if(_tempVariable_bool)
{
_valueUpEvents(0);
}
}
}
else
{
_MenuBlock_197478283_OVUIS = 0;
}
if(!(_bounseInputD10O))
{
if (! _MenuBlock_197478283_OVDIS)
{
_MenuBlock_197478283_OVDIS = 1;
if(_tempVariable_bool)
{
_valueDownEvents(0);
}
}
}
else
{
_MenuBlock_197478283_OVDIS = 0;
}
if (_gtv1)
{
_dispTempLength1 = ((_MenuBlock_197478283_VNO)).length();
if (_disp4oldLength > _dispTempLength1)
{
_isNeedClearDisp1 = 1;
}
_disp4oldLength = _dispTempLength1;
_lcd1.setCursor(int((16 - _dispTempLength1)/2), 1);
_lcd1.print((_MenuBlock_197478283_VNO));
}
else
{
if (_disp4oldLength > 0)
{
_isNeedClearDisp1 = 1;
_disp4oldLength = 0;
}
}
if (_gtv1)
{
_dispTempLength1 = ((_MenuBlock_197478283_MNO)).length();
if (_disp3oldLength > _dispTempLength1)
{
_isNeedClearDisp1 = 1;
}
_disp3oldLength = _dispTempLength1;
_lcd1.setCursor(int((16 - _dispTempLength1)/2), 0);
_lcd1.print((_MenuBlock_197478283_MNO));
}
else
{
if (_disp3oldLength > 0)
{
_isNeedClearDisp1 = 1;
_disp3oldLength = 0;
}
}
_RTC1_GetTime1_StrOut = _RTC1.gettime("H:i:s");
if (!(_gtv1))
{
_dispTempLength1 = ((_RTC1_GetTime1_StrOut)).length();
if (_disp1oldLength > _dispTempLength1)
{
_isNeedClearDisp1 = 1;
}
_disp1oldLength = _dispTempLength1;
_lcd1.setCursor(int((16 - _dispTempLength1)/2), 0);
_lcd1.print((_RTC1_GetTime1_StrOut));
}
else
{
if (_disp1oldLength > 0)
{
_isNeedClearDisp1 = 1;
_disp1oldLength = 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 "";
}