//
// Created by Matthew on 2023/12/27.
//

#ifndef MICROPHOTO_GPIOCONTROL_H
#define MICROPHOTO_GPIOCONTROL_H

#include <string>
#include <chrono>
#include <thread>

#define CMD_GET_LIGHT_ADC                       101
#define CMD_SET_LIGHT_ADC                       102
#define CMD_GET_KEY_LOCKSTATE                   103
#define CMD_GET_BAT_ADC                         104
#define CMD_SET_FLASH_LED                       105
#define CMD_SET_NETWORK_STATE                   106
#define CMD_SET_OTG_STATE                       107
#define CMD_GET_OTG_STATE                       108
#define CMD_GET_CHARGING_VOL_STATE              110
#define CMD_GET_CHARGING_SHUNT_VOLTAGE_STATE    111
#define CMD_GET_CHARGING_BUS_VOLTAGE_STATE      112
#define CMD_GET_CHARGING_POWER_STATE            113
#define CMD_GET_CHARGING_CURRENT_STATE          114
#define CMD_GET_BAT_VOL_STATE                   115
#define CMD_GET_BAT_SHUNT_VOLTAGE_STATE         116
#define CMD_GET_BAT_BUS_VOLTAGE_STATE           117
#define CMD_GET_BAT_POWER_STATE                 118
#define CMD_GET_BAT_CURRENT_STATE               119
#define CMD_SET_485_STATE                       121
#define CMD_SET_SPI_MODE                        123
#define CMD_SET_SPI_BITS_PER_WORD               124
#define CMD_SET_SPI_MAXSPEEDHZ                  125
#define CMD_SET_PWM_BEE_STATE                   126
#define CMD_SET_ALM_MODE                        128
#define CMD_SET_SPI_POWER                       129
#define CMD_SET_485_EN_STATE					131
#define CMD_SET_CAM_3V3_EN_STATE				132
#define CMD_SET_12V_EN_STATE					133
#define CMD_SET_SYSTEM_RESET  		            202


#ifdef USING_N938


#define CMD_SET_485_EN_STATE		            131
#define CMD_SET_CAM_3V3_EN_STATE		        132
#define CMD_SET_12V_EN_STATE		            133
#define CMD_SET_485_STATE			            121
#define CMD_SET_SPI_MODE			            123
#define CMD_SET_SPI_BITS_PER_WORD		        124
#define CMD_SET_SPI_MAXSPEEDHZ		            125
#define CMD_SET_SPI_POWER			            129
#define CMD_SET_WTH_POWER		                490
#define CMD_SET_PULL_POWER		                491
#define CMD_SET_ANGLE_POWER		                492
#define CMD_SET_OTHER_POWER		                493
#define CMD_SET_PIC1_POWER		                494
#define CMD_SET_GPIO157_POWER		            510
#define CMD_SET_GPIO5_POWER		                511
#define CMD_SET_PWM_BEE_STATE		            126
#define CMD_SET_ALM_MODE			            128
#define CMD_SET_485_en0			                301
#define CMD_SET_485_en1			                302
#define CMD_SET_485_en2			                303
#define CMD_SET_485_en3			                304
#define CMD_SET_485_en4			                305
#define CMD_SET_OTG_STATE			            107
#define CMD_GET_OTG_STATE			            108

#if 0

#define CMD_485_0_DE                            156     // 485_0 DE信号
#define CMD_485_0_PWR_EN                        157     // 485_0 电源使能
#define CMD_485_0_1_DE_EN                       171     // 485_0&1DE电平转换芯片使能信号
#define CMD_485_1_DE                            172     //

#define CMD_SET_CAM_3V3_EN_STATE				72      // 整板3V3上电使能
#define CMD_3V3_SWITCH_EN                       45      // 整板485_3V3信号电平转换电源使能

#define CMD_UART0_EN                            73      // 预留UART0电平转换芯片使能
#define CMD_485_1_PWR_EN                        5       // 485_1 电源使能

#define CMD_485_3_DE                            6       // 485_3 DE信号
#define CMD_485_2_DE                            7       // 485_2 DE信号
#define CMD_485_4_DE                            13      // 485_4 DE信号
#define CMD_NETWORK_PWR_EN                      94      // 100M网络电源使能

#define CMD_485_2_PWR_EN                        92      // 485_2 电源使能
#define CMD_485_3_PWR_EN                        91      // 485_3 电源使能
#define CMD_485_4_PWR_EN                        90      // 485_4 电源使能

#define CMD_SEC_EN                              27      // 加密芯片上电使能

#define CMD_485_2_3_DE_EN                       26      // 485_2&3 DE电平转换芯片使能信号

#define CMD_5V_PWR_EN                           14      // 整板5V0上电使能
#define CMD_SD_CARD_DECT                        15      // SD CARD DECT
#define CMD_PIC1_EN                             16

#define CMD_OTHER_EN                            21
#define CMD_ANGLE_EN                            22
#define CMD_PULL_EN                             23
#define CMD_WEATHER_EN                          24

#define CMD_LED_CTRL                            46
#define CMD_BD_EN                               47
#define CMD_ADC_EN                              44

#define CMD_SPI_PWR_EN                          43      // SPI转串口电源使能

#endif

#endif // USING_N938

#ifdef USING_N938
#define GPIO_NODE_N938          "/sys/devices/platform/1000b000.pinctrl/mt_gpio"
#else
#define GPIO_NODE_MP            "/dev/mtkgpioctrl"
#endif // USING_N938


class GpioControl
{
public:

    static void setInt(int cmd, int value);
    static int getInt(int cmd);
    static void setLong(int cmd, long value);
    static long getLong(int cmd);
    static void setString(int cmd, const std::string& value);
    static std::string getString(int cmd);

    static void setOtgState(bool on)
    {
        setInt(CMD_SET_OTG_STATE, on ? 1 : 0);
    }

    static bool getOtgState()
    {
        return getInt(CMD_SET_OTG_STATE) != 0;
    }

    static void setCam3V3Enable(bool enabled)
    {
#ifdef ENABLE_3V3_ALWAYS
        setInt(CMD_SET_CAM_3V3_EN_STATE, 1);
#else
        setInt(CMD_SET_CAM_3V3_EN_STATE, enabled ? 1 : 0);
#endif
    }

    static void reboot()
    {
        setInt(CMD_SET_SYSTEM_RESET, 1);
    }

    static void setLightAdc(int i)
    {
        setInt(CMD_SET_LIGHT_ADC, i);
    }

    static int getLightAdc()
    {
        return getInt(CMD_GET_LIGHT_ADC);
    }

    static int getChargingVoltage()
    {
        return getInt(CMD_GET_CHARGING_VOL_STATE);
    }

    static int getChargingShuntVoltage()
    {
        return getInt(CMD_GET_CHARGING_SHUNT_VOLTAGE_STATE);
    }

    static int getChargingBusVoltage() {
        return getInt(CMD_GET_CHARGING_BUS_VOLTAGE_STATE);
    }

    static int getChargingPower() {
        return getInt(CMD_GET_CHARGING_POWER_STATE);
    }

    static int getChargingCurrent() {
        return getInt(CMD_GET_CHARGING_CURRENT_STATE);
    }

    static int getBatteryVoltage() {
        return getInt(CMD_GET_BAT_VOL_STATE);
    }

    static int getBatteryShuntVoltage() {
        return getInt(CMD_GET_BAT_SHUNT_VOLTAGE_STATE);
    }

    static int getBatteryBusVoltage() {
        return getInt(CMD_GET_BAT_BUS_VOLTAGE_STATE);
    }

    static int getBatteryPower() {
        return getInt(CMD_GET_BAT_POWER_STATE);
    }

    static int getBatteryCurrent() {
        return getInt(CMD_GET_BAT_CURRENT_STATE);
    }

    static void set485WriteMode() {
        setInt(CMD_SET_485_STATE, 1);
    }

    static void set485ReadMode() {
        setInt(CMD_SET_485_STATE, 0);
    }

    static void setSpiMode(int i) {
        setInt(CMD_SET_SPI_MODE, i);
    }

    static void setSpiBitsPerWord(int i) {
        setInt(CMD_SET_SPI_BITS_PER_WORD, i);
    }

    static void setSpiMaxSpeedHz(long j) {
        setLong(CMD_SET_SPI_MAXSPEEDHZ, j);
    }

    static void setBeeOn(bool z) {
        setInt(CMD_SET_PWM_BEE_STATE, z ? 1 : 0);
    }

    static void setJidianqiState(bool z) {
        setInt(CMD_SET_ALM_MODE, z ? 1 : 0);
    }

    static void setSpiPower(bool on) {
        setInt(CMD_SET_SPI_POWER, on ? 1 : 0);
        if (on)
        {
            std::this_thread::sleep_for(std::chrono::milliseconds(40));
        }
    }

    static void setRS485Enable(bool z) {
        setInt(CMD_SET_485_EN_STATE, z ? 1 : 0);
    }


    static void set12VEnable(bool z) {
        setInt(CMD_SET_12V_EN_STATE, z ? 1 : 0);
    }

#ifdef USING_N938
    static bool SetN938Cmd(int cmd, int val);
    static bool OpenSensors();
    static bool CloseSensors();
#endif

};


#endif //MICROPHOTO_GPIOCONTROL_H
