#include <Wire.h>
#include "flprogUtilites.h"
#include <Wire.h>
#include <TimerOne.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C _lcd1(0x27, 16, 2);
int _dispTempLength1=0;
boolean _isNeedClearDisp1;
unsigned long period_75068646_1;
int PWM_1_75068646_1;
int PWM_2_75068646_1;
int PWM_3_75068646_1;
boolean flag_75068646_1= false;
unsigned int P_75068646_1=2000;
int S1_75068646_1=0;
int S2_75068646_1=0;
int S3_75068646_1=0;
int R_shunt_48176999_1;
float Voltage_48176999_1;
float Current_48176999_1;
float V_Shunt_48176999_1;
bool MAX_Current_48176999_1;
unsigned long old_time_48176999_1;
bool _gtv1;
bool _gtv2;
int _swi3;
int _swi1;
int _disp4oldLength = 0;
int _disp2oldLength = 0;
String _mux2;
int _swi9;
int _swi2;
String _swi6;
bool _bounseInputD6S = 0;
bool _bounseInputD6O = 0;
unsigned long _bounseInputD6P = 0UL;
int _disp1oldLength = 0;
bool _gen1I = 0;
bool _gen1O = 0;
unsigned long _gen1P = 0UL;
int _mux1;
int _swi4;
int _disp3oldLength = 0;
bool _count1I = 0;
bool _count1_SCST = 0;
int _count1_Value = 0;
bool _count1MaxLO = 0;
bool _bounseInputD7S = 0;
bool _bounseInputD7O = 0;
unsigned long _bounseInputD7P = 0UL;
bool _gen2I = 0;
bool _gen2O = 0;
unsigned long _gen2P = 0UL;
bool _count2I = 0;
int _count2_Value = 0;
bool _count2MaxLO = 0;
String _swi8;
int _swi7;
bool _bounseInputD8S = 0;
bool _bounseInputD8O = 0;
unsigned long _bounseInputD8P = 0UL;
bool _gen3I = 0;
bool _gen3O = 0;
unsigned long _gen3P = 0UL;
bool _trgt1 = 0;
bool _trgt1I = 0;
bool _count3I = 0;
int _count3_Value = 0;
bool _count3MaxLO = 0;
String _swi5;
void setup()
{
    pinMode(8, INPUT_PULLUP);
    pinMode(7, INPUT_PULLUP);
    pinMode(6, INPUT_PULLUP);
    pinMode(13, INPUT_PULLUP);
    pinMode(10, OUTPUT);
    digitalWrite(10, 0);
    Wire.begin();
    delay(10);
    _lcd1.init();
    _lcd1.backlight();
    _bounseInputD6O =    digitalRead(6);
    _bounseInputD8O =    digitalRead(8);
    _bounseInputD7O =    digitalRead(7);
    Timer1.initialize(); // инициализировать timer1
    Timer1.setPeriod(P_75068646_1);
    if(9!=0)
    {
          	Timer1.pwm(9 , 0); // задать шим сигнал на выводе pin, коэффициент заполнения 0%
    }
    if(0!=0)
    {
          	Timer1.pwm(0 , 0); // задать шим сигнал на выводе pin, коэффициент заполнения 0%
    }
    if(0!=0)
    {
          	Timer1.pwm(0 , 0); // задать шим сигнал на выводе pin, коэффициент заполнения 0%
    }
    Wire.begin();
    	delay(200);
    Wire.beginTransmission(64);
    Wire.write(0x00);
    Wire.write(0x39);
    Wire.write(0xE7);
    Wire.endTransmission();
}
void loop()
{
    if (_isNeedClearDisp1) 
    {
        _lcd1.clear();
        _isNeedClearDisp1= 0;
    }
    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();
        }
    }
    bool  _bounceInputTmpD8 =  (digitalRead (8));
    if (_bounseInputD8S)
    {
        if (millis() >= (_bounseInputD8P + 40))
        {
            _bounseInputD8O= _bounceInputTmpD8;
            _bounseInputD8S=0;
        }
    }
    else
    {
        if (_bounceInputTmpD8 != _bounseInputD8O)
        {
            _bounseInputD8S=1;
            _bounseInputD8P = millis();
        }
    }
    //Плата:1
    if (_gtv1)
    {
        _count1_Value = 0;
    }
     else 
    {
        if (!(_bounseInputD8O))
        {
            if (! _count1I) 
            {
                _count1I = 1;
                _count1_Value = _count1_Value + 1;
            }
        }
         else 
        {
            _count1I = 0;
        }
        if (!(_bounseInputD7O))
        {
            if (! _count1_SCST) 
            {
                _count1_SCST = 1;
                _count1_Value = _count1_Value - 1;
            }
        }
         else 
        {
            _count1_SCST = 0;
        }
    }
    _count1MaxLO = _count1_Value >= 8;
    if((_count1_Value) == 0) 
    {
        _mux1 = 270;
    }
    if((_count1_Value) == 1) 
    {
        _mux1 = 300;
    }
    if((_count1_Value) == 2) 
    {
        _mux1 = 310;
    }
    if((_count1_Value) == 3) 
    {
        _mux1 = 320;
    }
    if((_count1_Value) == 4) 
    {
        _mux1 = 330;
    }
    if((_count1_Value) == 5) 
    {
        _mux1 = 340;
    }
    if((_count1_Value) == 6) 
    {
        _mux1 = 350;
    }
    if((_count1_Value) == 7) 
    {
        _mux1 = 365;
    }
    R_shunt_48176999_1 = 100;
    if (500 < (millis() - old_time_48176999_1)) 
    {
        get_Parametr__48176999_1();
        old_time_48176999_1 = millis();
    }
    if((Voltage_48176999_1) > (14.3))
    {
        _swi5=String("0.0");
    }
    else
    {
        _swi5=(_floatToStringWitRaz(Current_48176999_1,1));
    }
    if((Voltage_48176999_1) > (14.3))
    {
        _swi1=0;
    }
    else
    {
        _swi1=_mux1;
    }
    if((Voltage_48176999_1) > (13.6))
    {
        _swi4=(_swi1)+(15);
    }
    else
    {
        _swi4=_mux1;
    }
    if((Voltage_48176999_1) > (13.00))
    {
        _swi3=(_swi4)+(15);
    }
    else
    {
        _swi3=_mux1;
    }
    if((Voltage_48176999_1) > (12.6))
    {
        _swi2=(_swi3)+(30);
    }
    else
    {
        _swi2=_mux1;
    }
    bool  _tmp1 = !(_bounseInputD6O);
    if (_tmp1)  
    {
         if (! _trgt1I) _trgt1 = ! _trgt1;
    }
    _trgt1I = _tmp1;
    if((_count1_Value) == 0) 
    {
        _mux2 = String("LEVEL 0");
    }
    if((_count1_Value) == 1) 
    {
        _mux2 = String("LEVEL 1");
    }
    if((_count1_Value) == 2) 
    {
        _mux2 = String("LEVEL 2");
    }
    if((_count1_Value) == 3) 
    {
        _mux2 = String("LEVEL 3");
    }
    if((_count1_Value) == 4) 
    {
        _mux2 = String("LEVEL 4");
    }
    if((_count1_Value) == 5) 
    {
        _mux2 = String("LEVEL 5");
    }
    if((_count1_Value) == 6) 
    {
        _mux2 = String("LEVEL 6");
    }
    if((_count1_Value) == 7) 
    {
        _mux2 = String("LEVEL 7");
    }
    if (_trgt1) 
    {
         if (! _gen2I) 
        {
            _gen2I = 1;
            _gen2O = 1;
            _gen2P = millis();
        }
    }
     else 
    {
        _gen2I = 0 ;
        _gen2O= 0;
    }
    if (_gen2I) 
    {
          if (flprog::isTimer (_gen2P , 60000)) 
        {
             _gen2P = millis();
            _gen2O = ! _gen2O;
        }
    }
    if (_gtv2)
    {
        _count2_Value = 0;
    }
     else 
    {
        if (_gen2O)
        {
            if (! _count2I) 
            {
                _count2I = 1;
                _count2_Value = _count2_Value + 1;
            }
        }
         else 
        {
            _count2I = 0;
        }
    }
    _count2MaxLO = _count2_Value >= 60;
    _gtv2 = _count2MaxLO;
    if (0)
    {
        _count3_Value = 0;
    }
     else 
    {
        if (_gtv2)
        {
            if (! _count3I) 
            {
                _count3I = 1;
                if(_count3_Value < 10) 
                {
                    _count3_Value = _count3_Value + 1;
                }
            }
        }
         else 
        {
            _count3I = 0;
        }
    }
    _count3MaxLO = _count3_Value >= 10;
    if (_trgt1) 
    {
         if (! _gen1I) 
        {
            _gen1I = 1;
            _gen1O = 1;
            _gen1P = millis();
        }
    }
     else 
    {
        _gen1I = 0 ;
        _gen1O= 0;
    }
    if (_gen1I) 
    {
          if (flprog::isTimer (_gen1P , 2000)) 
        {
             _gen1P = millis();
            _gen1O = ! _gen1O;
        }
    }
    digitalWrite(10, ((_gen1O) && (!(_count3MaxLO))));
    if (_trgt1) 
    {
         if (! _gen3I) 
        {
            _gen3I = 1;
            _gen3O = 1;
            _gen3P = millis();
        }
    }
     else 
    {
        _gen3I = 0 ;
        _gen3O= 0;
    }
    if (_gen3I) 
    {
          if (flprog::isTimer (_gen3P , 1000)) 
        {
             _gen3P = millis();
            _gen3O = ! _gen3O;
        }
    }
    if(_gen3O)
    {
        _swi8=String(" ");
    }
    else
    {
        _swi8=String(":");
    }
    if(_trgt1)
    {
        _swi6=((String("DESULF  ")) + ((String(_count3_Value, DEC))) + (_swi8) + ((String(_count2_Value, DEC))) + (String("")));
    }
    else
    {
        _swi6=_mux2;
    }
    if (!((Voltage_48176999_1) > (14.3))) 
    {
        _dispTempLength1 = ((_swi6)).length();
        if (_disp2oldLength > _dispTempLength1) 
        {
            _isNeedClearDisp1 = 1;
        }
        _disp2oldLength = _dispTempLength1;
        _lcd1.setCursor(0, 1);
        _lcd1.print((_swi6));
    }
     else 
    {
        if (_disp2oldLength > 0) 
        {
            _isNeedClearDisp1 = 1;
            _disp2oldLength = 0;
        }
    }
    if(_gen1O)
    {
        _swi7=0;
    }
    else
    {
        _swi7=_swi2;
    }
    if(!((digitalRead (13))))
    {
        _swi9=0;
    }
    else
    {
        _swi9=_swi7;
    }
    period_75068646_1 = 200UL;
    PWM_1_75068646_1 = _swi9;
    PWM_2_75068646_1 = 0;
    PWM_3_75068646_1 = 0;
    if ((period_75068646_1!=P_75068646_1)&&(period_75068646_1>0))
    {
        	Timer1.setPeriod(period_75068646_1);
        	flag_75068646_1 = true;
    }
    if(9!=0)
    {
        	if(PWM_1_75068646_1!= S1_75068646_1||flag_75068646_1==true)
        {
            Timer1.setPwmDuty(9 ,  PWM_1_75068646_1);
        }
    }
    if(0!=0)
    {
        	if(PWM_2_75068646_1!= S2_75068646_1 ||flag_75068646_1==true)
        {
            Timer1.setPwmDuty(0 ,  PWM_2_75068646_1);
        }
    }
    if(0!=0)
    {
        	if(PWM_3_75068646_1!= S3_75068646_1 ||flag_75068646_1==true)
        {
            Timer1.setPwmDuty(0 ,  PWM_3_75068646_1);
        }
    }
    S1_75068646_1= PWM_1_75068646_1;
    S2_75068646_1= PWM_2_75068646_1;
    S3_75068646_1= PWM_3_75068646_1;
    if (period_75068646_1>0)
    {
        P_75068646_1=period_75068646_1;
    }
    if (!((digitalRead (13)))) 
    {
        _dispTempLength1 = ((String("POLARITY"))).length();
        if (_disp4oldLength > _dispTempLength1) 
        {
            _isNeedClearDisp1 = 1;
        }
        _disp4oldLength = _dispTempLength1;
        _lcd1.setCursor(0, 0);
        _lcd1.print((String("POLARITY")));
    }
     else 
    {
        if (_disp4oldLength > 0) 
        {
            _isNeedClearDisp1 = 1;
            _disp4oldLength = 0;
        }
    }
    if (!(!((digitalRead (13))))) 
    {
        _dispTempLength1 = ((((String("I=")) + (_swi5) + (String("A  ")) + (String("U=")) + ((_floatToStringWitRaz(Voltage_48176999_1,1))) + (String("V ")) + (String(""))))).length();
        if (_disp1oldLength > _dispTempLength1) 
        {
            _isNeedClearDisp1 = 1;
        }
        _disp1oldLength = _dispTempLength1;
        _lcd1.setCursor(0, 0);
        _lcd1.print((((String("I=")) + (_swi5) + (String("A  ")) + (String("U=")) + ((_floatToStringWitRaz(Voltage_48176999_1,1))) + (String("V ")) + (String("")))));
    }
     else 
    {
        if (_disp1oldLength > 0) 
        {
            _isNeedClearDisp1 = 1;
            _disp1oldLength = 0;
        }
    }
    if ((Voltage_48176999_1) > (14.3)) 
    {
        _dispTempLength1 = ((String("END CHARGING"))).length();
        if (_disp3oldLength > _dispTempLength1) 
        {
            _isNeedClearDisp1 = 1;
        }
        _disp3oldLength = _dispTempLength1;
        _lcd1.setCursor(0, 1);
        _lcd1.print((String("END CHARGING")));
    }
     else 
    {
        if (_disp3oldLength > 0) 
        {
            _isNeedClearDisp1 = 1;
            _disp3oldLength = 0;
        }
    }
    _gtv1 = _count1MaxLO;
}
String  _floatToStringWitRaz(float value, int raz)
{
    return String(value,raz);
}
void get_Parametr__48176999_1()
{
    	uint16_t vallue; // объявляем промеж переменную
          vallue = 0;
    	int16_t pptt; // объявляем 2 промеж переменную
    	wire_Read_48176999_1(0x01, &vallue); //считываем в промеж переменную регистр шунта
    	pptt = (int16_t)vallue; // получаем напряжение на шунте, в *10мкВ
    	V_Shunt_48176999_1 = pptt * 0.010; // получаем напряжение на шунте, тип Float, в мВ
    	Current_48176999_1 = V_Shunt_48176999_1 * 10 / R_shunt_48176999_1; // получаем амперы
          vallue = 0;
    	wire_Read_48176999_1(0x02, &vallue); //считываем в промеж переменную регистр напряжения
    	int16_t vollt = ((int16_t)((vallue >> 3) * 4)); // сдвигаем на 3 бита и умножаем на 4
    	Voltage_48176999_1 = vollt * 0.001; // получаем флоат, переводим мВ в Вольты
    	if ((V_Shunt_48176999_1 >319) | (V_Shunt_48176999_1 < -319)) MAX_Current_48176999_1 = 1;
    	else MAX_Current_48176999_1=0;
}
void wire_Read_48176999_1(uint8_t reg, uint16_t *value)
{
    	Wire.beginTransmission(64);
    Wire.write(reg);
    	Wire.endTransmission();
    	delay(50);
    	Wire.requestFrom((uint8_t) 64, (uint8_t)2);
    *value = ((Wire.read() << 8) | Wire.read());
}
