#include <Adafruit_SSD1306.h>
#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <SPI.h>
bool EN_145550790_3;
String in_145550790_3;
byte size_145550790_3;
byte X_145550790_3;
byte Y_145550790_3;
bool invert_145550790_3;
Adafruit_SSD1306 display(11, 13, 8, 9, 10);
float wire_mA_197767732_1;
float wire_A_197767732_1;
int sensorTA12_197767732_1= 0;
float nVPP_197767732_1;
float nCurrThruResistorPP_197767732_1;
float nCurrThruResistorRMS_197767732_1;
float nCurrentThruWire_197767732_1;
float result_197767732_1;
int readValue_197767732_1;
int maxValue_197767732_1;
unsigned long start_time_197767732_1;
bool EN_97961900_1;
byte X_97961900_1;
byte Y_97961900_1;
byte Radius_97961900_1;
bool EN_145550790_1;
String in_145550790_1;
byte size_145550790_1;
byte X_145550790_1;
byte Y_145550790_1;
bool invert_145550790_1;
bool _gtv1;
bool _gen2I = 0;
bool _gen2O = 0;
unsigned long _gen2P = 0UL;
bool _SEEPR2OSN = 0;
bool _tim1I = 0;
bool _tim1O = 0;
unsigned long _tim1P = 0UL;
String _mux1;
bool _bounseInputD4S = 0;
bool _bounseInputD4O = 0;
unsigned long _bounseInputD4P = 0UL;
bool _SEEPR1OSN = 0;
bool _gen1I = 0;
bool _gen1O = 0;
unsigned long _gen1P = 0UL;
bool _count1I = 0;
int _count1_Value = 0;
bool _count1MaxLO = 0;
bool _tim2I = 0;
bool _tim2O = 0;
unsigned long _tim2P = 0UL;
String _swi3;
String _swi1;
unsigned long _SCT_1 = 0UL;
unsigned long _SCT_1IC = 0UL;
unsigned long _SCT_1P = 0UL;
bool _bounseInputD6S = 0;
bool _bounseInputD6O = 0;
unsigned long _bounseInputD6P = 0UL;
bool _trgr1 = 0;
String _swi2;
bool _gen3I = 0;
bool _gen3O = 0;
unsigned long _gen3P = 0UL;
bool _bounseInputD5S = 0;
bool _bounseInputD5O = 0;
unsigned long _bounseInputD5P = 0UL;
bool _pzs1OES = 0;
void setup()
{
    pinMode(6, INPUT_PULLUP);
    pinMode(5, INPUT_PULLUP);
    pinMode(4, INPUT_PULLUP);
    pinMode(3, OUTPUT);
    digitalWrite(3, 0);
    _bounseInputD6O =  digitalRead(6);
    _bounseInputD5O =  digitalRead(5);
    _bounseInputD4O =  digitalRead(4);
    pinMode(2, INPUT_PULLUP);
    _SCT_1P=micros();
    attachInterrupt(digitalPinToInterrupt (2), _SCT_1coutFunction, FALLING);
    display.begin(SSD1306_SWITCHCAPVCC);
    display.display();
    display.clearDisplay();
    pinMode(sensorTA12_197767732_1, INPUT);
}
void loop()
{
    bool  _bounceInputTmpD5 =  (digitalRead (5));
    if (_bounseInputD5S)
    {
        if (millis() >= (_bounseInputD5P + 40))
        {
            _bounseInputD5O= _bounceInputTmpD5;
            _bounseInputD5S=0;
        }
    }
    else
    {
        if (_bounceInputTmpD5 != _bounseInputD5O)
        {
            _bounseInputD5S=1;
            _bounseInputD5P = millis();
        }
    }
    bool  _bounceInputTmpD6 =  (digitalRead (6));
    if (_bounseInputD6S)
    {
        if (millis() >= (_bounseInputD6P + 40))
        {
            _bounseInputD6O= _bounceInputTmpD6;
            _bounseInputD6S=0;
        }
    }
    else
    {
        if (_bounceInputTmpD6 != _bounseInputD6O)
        {
            _bounseInputD6S=1;
            _bounseInputD6P = millis();
        }
    }
    bool  _bounceInputTmpD4 =  (digitalRead (4));
    if (_bounseInputD4S)
    {
        if (millis() >= (_bounseInputD4P + 40))
        {
            _bounseInputD4O= _bounceInputTmpD4;
            _bounseInputD4S=0;
        }
    }
    else
    {
        if (_bounceInputTmpD4 != _bounseInputD4O)
        {
            _bounseInputD4S=1;
            _bounseInputD4P = millis();
        }
    }
    //Плата:1
    display.clearDisplay(); // Очищаем дисплей
    if(((analogRead (2))) >= (5)) 
    {
        _tim1O = 1;
        _tim1I = 1;
    }
     else 
    {
         if(_tim1I) 
        {
            _tim1I = 0;
            _tim1P = millis();
        }
         else 
        {
             if (_tim1O) 
            {
                if (_isTimer(_tim1P, 4000)) _tim1O = 0;
            }
        }
    }
    maxValue_197767732_1= 0;
    start_time_197767732_1 = millis();
    while((millis()-start_time_197767732_1) < 1000)
    {
        readValue_197767732_1 = analogRead(sensorTA12_197767732_1);
        if (readValue_197767732_1 > maxValue_197767732_1)
        {
            maxValue_197767732_1 = readValue_197767732_1;
        }
    }
    result_197767732_1 = (maxValue_197767732_1 * 5.0)/1024.0;
    nCurrThruResistorPP_197767732_1 = (result_197767732_1 / 200.0) * 1000.0;
    nCurrThruResistorRMS_197767732_1 = nCurrThruResistorPP_197767732_1 * 0.707;
    nCurrentThruWire_197767732_1 = nCurrThruResistorRMS_197767732_1 * 1000;
    wire_mA_197767732_1=nCurrentThruWire_197767732_1;
    wire_A_197767732_1=nCurrentThruWire_197767732_1 / 1000;
    if(_tim1O)
    {
        _swi1=(((_floatToStringWitRaz((wire_A_197767732_1)*(52),0))) + (String(" V")));
    }
    else
    {
        _swi1=(((String((analogRead (1)), DEC))) + (String(" Ohm")));
    }
    if(((analogRead (1))) >= (961))
    {
        _swi2=String("");
    }
    else
    {
        _swi2=_swi1;
    }
    if (_gtv1)
    {
        _count1_Value = 0;
    }
     else 
    {
        if (!(_bounseInputD5O))
        {
            if (! _count1I) 
            {
                _count1I = 1;
                _count1_Value = _count1_Value + 1;
            }
        }
         else 
        {
            _count1I = 0;
        }
    }
    _count1MaxLO = _count1_Value >= 3;
    if(!(_bounseInputD6O))
    {
         if(!_SEEPR2OSN)
        {
            (updateIntegerToEEPROM(2, 0, 0x0, ((analogRead (1)))));
            _SEEPR2OSN=1;
        }
    }
    else
    {
         if(_SEEPR2OSN)
        {
            _SEEPR2OSN=0;
        }
    }
    if(!(_bounseInputD6O))
    {
         if(!_SEEPR1OSN)
        {
            (updateIntegerToEEPROM(0, 0, 0x0, ((int((wire_A_197767732_1)*(52))))));
            _SEEPR1OSN=1;
        }
    }
    else
    {
         if(_SEEPR1OSN)
        {
            _SEEPR1OSN=0;
        }
    }
    if (!(_bounseInputD6O))
    {
         if (_tim2I) 
        {
             if (_isTimer(_tim2P, 1000)) 
            {
                _tim2O = 1;
            }
        }
         else 
        {
            _tim2I =1;
            _tim2P = millis();
        }
    }
     else 
    {
        _tim2O = 0;
        _tim2I = 0;
    }
    if (_tim2O) 
    {
        if (! _gen3I)
        {
            _gen3I = 1;
            _gen3O = 1;
            _gen3P = millis();
        }
    }
     else 
    {
        _gen3I = 0 ;
        _gen3O= 0;
    }
     if (_gen3I &&  _gen3O)   _gen3O = !(_isTimer(_gen3P , 1));
    if(((((!(_tim1O)) && (((analogRead (1))) <= (20)))) || (_gen3O) || (_gen2O)))
    {
        if((!_pzs1OES))
        {
             tone(7, 500);
            _pzs1OES =1;
        }
    }
     else 
    {
        if(_pzs1OES)
        {
            noTone(7);
            _pzs1OES =0;
        }
    }
    EN_145550790_1 = !(((_tim1O) || (((!(_tim1O)) && (((analogRead (1))) <= (20)))) || ((_count1_Value) == (1)) || ((_count1_Value) == (2)) || (_gen2O)));
    in_145550790_1 = ((String("Ubat")) + (String("=")) + ((_floatToStringWitRaz(((analogRead (4)))/(205.00),1))) + (String("V")));
    size_145550790_1 = 2;
    X_145550790_1 = 0;
    Y_145550790_1 = 5;
    invert_145550790_1 = 1;
    if (EN_145550790_1==1)
    {
        if (invert_145550790_1==1)
        	
        {
            display.setTextColor(WHITE, BLACK);
        }
        	else
        	 
        {
            display.setTextColor(BLACK, WHITE);
        }
        display.setCursor(X_145550790_1,Y_145550790_1);
        display.setTextSize(size_145550790_1);
        //display.setTextColor(WHITE);  
        display.print(utf8rus(in_145550790_1));
    }
    _gtv1 = _count1MaxLO;
    if((_count1_Value) == 0) 
    {
        _mux1 = _swi2;
    }
    if((_count1_Value) == 1) 
    {
        _mux1 = (((String((readIntegerFromEEPROM(0, 0, 0x0)), DEC))) + (String("V")));
    }
    if((_count1_Value) == 2) 
    {
        _mux1 = (((String((readIntegerFromEEPROM(2, 0, 0x0)), DEC))) + (String("R")));
    }
    if(((analogRead (3))) <= (613))
    {
        _swi3=String("BATTERY");
    }
    else
    {
        _swi3=_mux1;
    }
    EN_145550790_3 = 1;
    in_145550790_3 = _swi3;
    size_145550790_3 = 3;
    X_145550790_3 = 0;
    Y_145550790_3 = 5;
    invert_145550790_3 = 1;
    if (EN_145550790_3==1)
    {
        if (invert_145550790_3==1)
        	
        {
            display.setTextColor(WHITE, BLACK);
        }
        	else
        	 
        {
            display.setTextColor(BLACK, WHITE);
        }
        display.setCursor(X_145550790_3,Y_145550790_3);
        display.setTextSize(size_145550790_3);
        //display.setTextColor(WHITE);  
        display.print(utf8rus(in_145550790_3));
    }
    EN_97961900_1 = _gen2O;
    X_97961900_1 = 60;
    Y_97961900_1 = 30;
    Radius_97961900_1 = 20;
    if(EN_97961900_1==1)
    {
        display.fillCircle(X_97961900_1,Y_97961900_1,Radius_97961900_1, WHITE);
    }
    display.display();
    //Плата:2
//Наименование:УПРАВЛЕНИЕ ПИТАНИЕМ
    if (((analogRead (3))) >= (512)) 
    {
        if (! _gen1I)
        {
            _gen1I = 1;
            _gen1O = 1;
            _gen1P = millis();
        }
    }
     else 
    {
        _gen1I = 0 ;
        _gen1O= 0;
    }
     if (_gen1I &&  _gen1O)   _gen1O = !(_isTimer(_gen1P , 100));
    if(!(_bounseInputD4O)) _trgr1 = 0;
    if(_gen1O) _trgr1 = 1;
    digitalWrite(3, _trgr1);
    if(((micros()-_SCT_1P) >1000000)||((micros()-_SCT_1P) <0))
    {
        _SCT_1 = _SCT_1IC;
        _SCT_1IC=0;
        _SCT_1P = micros();
    }
    if ((((_SCT_1) <= (60)) && ((_SCT_1) >= (40)))) 
    {
         if (! _gen2I) 
        {
            _gen2I = 1;
            _gen2O = 1;
            _gen2P = millis();
        }
    }
     else 
    {
        _gen2I = 0 ;
        _gen2O= 0;
    }
    if (_gen2I) 
    {
          if (_isTimer (_gen2P , 200)) 
        {
             _gen2P = millis();
            _gen2O = ! _gen2O;
        }
    }
}
String  _floatToStringWitRaz(float value, int raz)
{
    return String(value,raz);
}
bool _isTimer(unsigned long startTime, unsigned long period)
{
    unsigned long currentTime;
    currentTime = millis();
    if (currentTime>= startTime) 
    {
        return (currentTime>=(startTime + period));
    }
     else 
    {
        return (currentTime >=(4294967295-startTime+period));
    }
}
void  _SCT_1coutFunction()
{
     _SCT_1IC++;
}
String utf8rus(String source)
{
    /* Recode russian fonts from UTF-8 to Windows-1251 */

    int i,k;
    String target;
    unsigned char n;
    char m[2] = { '0', '\0' };
    k = source.length();
    i = 0;
    while (i < k) 
    {
        n = source[i];
        i++;
        if (n >= 0xC0) 
        {
            switch (n) 
            {
                case 0xD0: 
                {
                    n = source[i];
                    i++;
                    if (n == 0x81) 
                    {
                        n = 0xA8;
                        break;
                    }

                    if (n >= 0x90 && n <= 0xBF) n = n + 0x30;
                    break;
                    

                }

                case 0xD1: 
                {
                    n = source[i];
                    i++;
                    if (n == 0x91) 
                    {
                        n = 0xB8;
                        break;
                    }

                    if (n >= 0x80 && n <= 0x8F) n = n + 0x70;
                    break;
                    

                }

                

            }

            

        }

        m[0] = n;
        target = target + String(m);
        

    }

    return target;

}

byte readByteFromEEPROM(int addres, byte bitAddres, byte chipAddres)

{
    return EEPROM.read(addres);
}

void updateByteToEEPROM(int addres, byte bitAddres, byte chipAddres, byte value)

{
    return EEPROM.update(addres, value);
}

int readIntegerFromEEPROM(int addres, byte bitAddres, byte chipAddres)

{
    byte hight = readByteFromEEPROM(addres,  bitAddres,  chipAddres);
    byte low = readByteFromEEPROM((addres+1),  bitAddres,  chipAddres);
    return  word(hight, low);

}

void updateIntegerToEEPROM(int addres, byte bitAddres, byte chipAddres, int value)

{
    updateByteToEEPROM(addres,  bitAddres,  chipAddres, highByte(value));
    updateByteToEEPROM((addres+1),  bitAddres,  chipAddres, lowByte(value));

}
