|
|
|
//
|
|
|
|
// Created by Matthew on 2023/12/27.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef MICROPHOTO_GPIOCONTROL_H
|
|
|
|
#define MICROPHOTO_GPIOCONTROL_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
#include <vector>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include <SemaphoreEx.h>
|
|
|
|
|
|
|
|
#ifndef USING_N938
|
|
|
|
|
|
|
|
#ifndef USING_PLZ // MicroPhoto
|
|
|
|
|
|
|
|
#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_SYSTEM_RESET 202
|
|
|
|
#define CMD_SET_485_EN_STATE 131
|
|
|
|
#define CMD_SET_12V_EN_STATE 133
|
|
|
|
#if 1
|
|
|
|
#define CMD_SET_SPI_POWER 129
|
|
|
|
#define CMD_SET_CAM_3V3_EN_STATE 132
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else // defined(USING_PLZ)
|
|
|
|
|
|
|
|
#define CMD_SET_485_ENABLE 512
|
|
|
|
#define CMD_SET_3V3_PWR_ENABLE 516
|
|
|
|
#define CMD_SET_5V_PWR_ENABLE 517
|
|
|
|
#define CMD_SET_SENSOR_ENABLE 504
|
|
|
|
#define CMD_SET_SENSOR_PWR_ENABLE 505
|
|
|
|
#define CMD_SET_SENSOR2_ENABLE 506
|
|
|
|
#define CMD_SET_SENSOR4_ENABLE 510
|
|
|
|
#define CMD_SET_SENSOR1_PWR_ENABLE 513
|
|
|
|
#define CMD_SET_SENSOR2_PWR_ENABLE 514
|
|
|
|
#define CMD_SET_SENSOR3_PWR_ENABLE 509
|
|
|
|
#define CMD_SET_SENSOR4_PWR_ENABLE 525
|
|
|
|
#define CMD_SET_PHOTO_IN 520
|
|
|
|
#define CMD_SET_PHOTO_OUT 515
|
|
|
|
|
|
|
|
#define CMD_SET_ADC_ENABLE 500
|
|
|
|
#define CMD_SET_MIPI_SWITCH 501
|
|
|
|
#define CMD_SET_CAM_RSTN1 502
|
|
|
|
#define CMD_SET_CAM_RSTN0 503
|
|
|
|
#define CMD_SET_SD_DECT 507
|
|
|
|
#define CMD_SET_PTZ_PWR_ENABLE 508
|
|
|
|
#define CMD_SET_RTC_ENABLE 511
|
|
|
|
#define CMD_SET_100M_ENABLE 518
|
|
|
|
#define CMD_SET_100M_SWITCH_PWR_ENABLE 519
|
|
|
|
#define CMD_SET_AM_POWER_ENABLE 521
|
|
|
|
#define CMD_SET_NRSEC_POWER_ENABLE 522
|
|
|
|
#define CMD_SET_AMP_ENABLE 523
|
|
|
|
#define CMD_SET_LIGHT1_RESISTOR_ENABLE 524
|
|
|
|
#define CMD_SET_100M_RESET 526
|
|
|
|
|
|
|
|
#endif // USING_PLZ
|
|
|
|
|
|
|
|
#else // defined(USING_N938)
|
|
|
|
|
|
|
|
#define CMD_SET_SYSTEM_RESET 202
|
|
|
|
#define CMD_SET_485_EN1 302
|
|
|
|
#define CMD_SET_CAM_3V3_EN_STATE 360
|
|
|
|
#define CMD_SET_UART0_EN 361
|
|
|
|
#define CMD_SET_485_EN0 301
|
|
|
|
#define CMD_SET_NETWORK_POWER_EN 362
|
|
|
|
#define CMD_SET_485_EN3 304
|
|
|
|
#define CMD_SET_485_EN2 303
|
|
|
|
#define CMD_SET_SPI_POWER 129
|
|
|
|
#define CMD_SET_5V_EN 363
|
|
|
|
#define CMD_SDCARD_DETECT_EN 364
|
|
|
|
#define CMD_SET_PIC1_POWER 494
|
|
|
|
#define CMD_SET_OTHER_POWER 493
|
|
|
|
#define CMD_SET_ANGLE_POWER 492
|
|
|
|
#define CMD_SET_PULL_POWER 491
|
|
|
|
#define CMD_SET_WTH_POWER 490
|
|
|
|
#define CMD_SET_485_EN4 305
|
|
|
|
#define CMD_LED_CTRL 365
|
|
|
|
#define CMD_BD_EN 366
|
|
|
|
#define CMD_ADC_EN 367
|
|
|
|
#define CMD_SPI2SERIAL_POWER_EN 368
|
|
|
|
#define CMD_RS485_3V3_EN 369
|
|
|
|
|
|
|
|
// Others
|
|
|
|
#define CMD_SET_485_EN_STATE 131
|
|
|
|
#define CMD_SET_OTG_STATE 107
|
|
|
|
|
|
|
|
#endif // USING_N938
|
|
|
|
|
|
|
|
|
|
|
|
#define GPIO_NODE_MP "/dev/mtkgpioctrl"
|
|
|
|
|
|
|
|
#define MAX_STRING_LEN 32
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int cmd;
|
|
|
|
int value;
|
|
|
|
int result;
|
|
|
|
long value2;
|
|
|
|
char str[MAX_STRING_LEN];
|
|
|
|
}IOT_PARAM;
|
|
|
|
|
|
|
|
class GpioControl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct ITEM
|
|
|
|
{
|
|
|
|
int cmd;
|
|
|
|
size_t references;
|
|
|
|
size_t closeCmds;
|
|
|
|
time_t closeTime;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
static std::mutex m_locker;
|
|
|
|
static CSemaphore m_semaphore;
|
|
|
|
static std::vector<ITEM> m_items;
|
|
|
|
static bool m_exitSignal;
|
|
|
|
static std::thread m_thread;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static size_t turnOnImpl(const IOT_PARAM& param);
|
|
|
|
static size_t turnOffImpl(const IOT_PARAM& param);
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Power
|
|
|
|
static size_t TurnOn(int cmd);
|
|
|
|
static size_t TurnOn(const std::vector<int>& cmds);
|
|
|
|
|
|
|
|
static size_t TurnOff(int cmd, uint32_t delayedCloseTime = 0);
|
|
|
|
static size_t TurnOff(const std::vector<int>& cmds, uint32_t delayedCloseTime = 0);
|
|
|
|
static size_t TurnOff(const std::vector<std::pair<int, uint32_t> >& cmds);
|
|
|
|
|
|
|
|
static void PowerControlThreadProc();
|
|
|
|
|
|
|
|
static bool Startup();
|
|
|
|
static void Stop();
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
on ? TurnOn(CMD_SET_OTG_STATE) : TurnOff(CMD_SET_OTG_STATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool getOtgState()
|
|
|
|
{
|
|
|
|
return getInt(CMD_SET_OTG_STATE) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setCam3V3Enable(bool enabled)
|
|
|
|
{
|
|
|
|
enabled ? TurnOn(CMD_SET_CAM_3V3_EN_STATE) : TurnOff(CMD_SET_CAM_3V3_EN_STATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void setBeeOn(bool z)
|
|
|
|
{
|
|
|
|
#ifndef USING_N938
|
|
|
|
z ? TurnOn(CMD_SET_PWM_BEE_STATE) : TurnOff(CMD_SET_PWM_BEE_STATE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setJidianqiState(bool z) {
|
|
|
|
#ifndef USING_N938
|
|
|
|
z ? TurnOn(CMD_SET_ALM_MODE) : TurnOff(CMD_SET_ALM_MODE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setSpiPower(bool on) {
|
|
|
|
on ? TurnOn(CMD_SET_SPI_POWER) : TurnOff(CMD_SET_SPI_POWER);
|
|
|
|
if (on)
|
|
|
|
{
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(40));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setRS485Enable(bool z)
|
|
|
|
{
|
|
|
|
#ifndef USING_N938
|
|
|
|
z ? TurnOn(CMD_SET_485_EN_STATE) : TurnOff(CMD_SET_485_EN_STATE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set12VEnable(bool z)
|
|
|
|
{
|
|
|
|
#ifndef USING_N938
|
|
|
|
z ? TurnOn(CMD_SET_12V_EN_STATE) : TurnOff(CMD_SET_12V_EN_STATE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reboot()
|
|
|
|
{
|
|
|
|
setInt(CMD_SET_SYSTEM_RESET, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setLightAdc(int i)
|
|
|
|
{
|
|
|
|
#ifndef USING_N938
|
|
|
|
setInt(CMD_SET_LIGHT_ADC, i);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getLightAdc()
|
|
|
|
{
|
|
|
|
#ifndef USING_N938
|
|
|
|
return getInt(CMD_GET_LIGHT_ADC);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getChargingVoltage()
|
|
|
|
{
|
|
|
|
#ifndef USING_N938
|
|
|
|
return getInt(CMD_GET_CHARGING_VOL_STATE);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getChargingShuntVoltage()
|
|
|
|
{
|
|
|
|
#ifndef USING_N938
|
|
|
|
return getInt(CMD_GET_CHARGING_SHUNT_VOLTAGE_STATE);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getChargingBusVoltage() {
|
|
|
|
#ifndef USING_N938
|
|
|
|
return getInt(CMD_GET_CHARGING_BUS_VOLTAGE_STATE);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getChargingPower() {
|
|
|
|
#ifndef USING_N938
|
|
|
|
return getInt(CMD_GET_CHARGING_POWER_STATE);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getChargingCurrent() {
|
|
|
|
#ifndef USING_N938
|
|
|
|
return getInt(CMD_GET_CHARGING_CURRENT_STATE);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getBatteryVoltage() {
|
|
|
|
#ifndef USING_N938
|
|
|
|
return getInt(CMD_GET_BAT_VOL_STATE);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getBatteryShuntVoltage() {
|
|
|
|
#ifndef USING_N938
|
|
|
|
return getInt(CMD_GET_BAT_SHUNT_VOLTAGE_STATE);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getBatteryBusVoltage() {
|
|
|
|
#ifndef USING_N938
|
|
|
|
return getInt(CMD_GET_BAT_BUS_VOLTAGE_STATE);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getBatteryPower() {
|
|
|
|
#ifndef USING_N938
|
|
|
|
return getInt(CMD_GET_BAT_POWER_STATE);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getBatteryCurrent() {
|
|
|
|
#ifndef USING_N938
|
|
|
|
return getInt(CMD_GET_BAT_CURRENT_STATE);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set485WriteMode() {
|
|
|
|
#if 0
|
|
|
|
setInt(CMD_SET_485_STATE, 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set485ReadMode() {
|
|
|
|
#if 0
|
|
|
|
setInt(CMD_SET_485_STATE, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setSpiMode(int i) {
|
|
|
|
#ifndef USING_N938
|
|
|
|
setInt(CMD_SET_SPI_MODE, i);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setSpiBitsPerWord(int i) {
|
|
|
|
#ifndef USING_N938
|
|
|
|
setInt(CMD_SET_SPI_BITS_PER_WORD, i);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setSpiMaxSpeedHz(long j) {
|
|
|
|
#ifndef USING_N938
|
|
|
|
setLong(CMD_SET_SPI_MAXSPEEDHZ, j);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif //MICROPHOTO_GPIOCONTROL_H
|