rk3399_arm_lvds/motorboard/SensorConfig.cpp

203 lines
6.7 KiB
C++

#include "SensorConfig.h"
#include <sys/stat.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <unistd.h>
SensorConfig m_static_sensorconfig;
SensorConfig::SensorConfig(/* args */)
{
{
std::lock_guard<std::mutex> m_lc(m_lock);
initconfigfile();
}
get_param();
}
SensorConfig::~SensorConfig()
{
}
SensorConfig::SensorConfig_Param SensorConfig::get_param()
{
std::lock_guard<std::mutex> m_lc(m_lock);
if(access(this->Path.c_str(),0) == -1)
{
initconfigfile();
return m_param;
}
std::ifstream i(this->Path);
auto pos = i.tellg();
i.seekg(0, std::ios::end);
if (i.tellg() <= 2)
{
initconfigfile();
return m_param;
}
i.seekg(pos);
json m_json;
i >> m_json;
i.close();
m_param = to_param(m_json);
return m_param;
}
void SensorConfig::set_param(SensorConfig::SensorConfig_Param param)
{
std::lock_guard<std::mutex> m_lc(m_lock);
m_param = param;
auto js = to_json(param);
std::ofstream o(this->Path);
o << std::setw(4) << js <<std::endl;
o.close();
o.flush();
}
SensorConfig::SensorConfig_Param& SensorConfig::get_mem_param()
{
return m_param;
}
json SensorConfig::get_mem_param_js()
{
return to_json(m_param);
}
void SensorConfig::initconfigfile()
{
if(access(this->Path.c_str(),F_OK) == 0)
return;
SensorConfig::SensorConfig_Param param{
.Sensor_Scan_Pwm = 40,
.Sensor_ConverOpen_Pwm = 40,
.Sensor_Screw_1_Pwm = 40,
.Sensor_Screw_2_Pwm = 40,
.Sensor_PaperOut_Pwm = 40,
.Sensor_PaperDetection_Pwm = 40,
.Sensor_Double_double_check_cyc = 4,
.Sensor_Double_paper_check_cyc = 8,
.Sensor_Double_double_max = 0x1c,
.Sensor_Double_single_max = 0x70,
.Sensor_enable_config = 0,
.Sensor_Double_enable_config = 0,
};
m_param = param;
printf("\nfile: %s not found creat ...",this->Path.c_str());
json js = to_json(param);
std::ofstream o(this->Path);
o << std::setw(4) << js <<std::endl;
o.close();
o.flush();
}
SensorConfig::SensorConfig_Param SensorConfig::get_default_param()
{
SensorConfig::SensorConfig_Param param{
.Sensor_Scan_Pwm = 40,
.Sensor_ConverOpen_Pwm = 40,
.Sensor_Screw_1_Pwm = 40,
.Sensor_Screw_2_Pwm = 40,
.Sensor_PaperOut_Pwm = 40,
.Sensor_PaperDetection_Pwm = 40,
.Sensor_Double_double_check_cyc = 4,
.Sensor_Double_paper_check_cyc = 8,
.Sensor_Double_double_max = 0x1c,
.Sensor_Double_single_max = 0x70,
.Sensor_enable_config = 0,
.Sensor_Double_enable_config = 0,
};
return param;
}
void SensorConfig::reset_json()
{
std::lock_guard<std::mutex> m_lc(m_lock);
remove(this->Path.c_str());
initconfigfile();
}
json SensorConfig::to_json(SensorConfig::SensorConfig_Param param)
{
json js;
js[Sensor_Scan_Pwm] = param.Sensor_Scan_Pwm;
js[Sensor_Screw_1_Pwm] = param.Sensor_Screw_1_Pwm;
js[Sensor_Screw_2_Pwm] = param.Sensor_Screw_2_Pwm;
js[Sensor_ConverOpen_Pwm] = param.Sensor_ConverOpen_Pwm;
js[Sensor_PaperOut_Pwm] = param.Sensor_PaperOut_Pwm;
js[Sensor_PaperDetection_Pwm] = param.Sensor_PaperDetection_Pwm;
js[Sensor_Double_double_check_cyc] = param.Sensor_Double_double_check_cyc;
js[Sensor_Double_paper_check_cyc] = param.Sensor_Double_paper_check_cyc;
js[Sensor_Double_double_max] = param.Sensor_Double_double_max;
js[Sensor_Double_single_max] = param.Sensor_Double_single_max;
js[Sensor_enable_config] = param.Sensor_enable_config;
js[Sensor_Double_enable_config] = param.Sensor_Double_enable_config;
return js;
}
SensorConfig::SensorConfig_Param SensorConfig::to_param(json js)
{
SensorConfig::SensorConfig_Param param{
.Sensor_Scan_Pwm = 40,
.Sensor_ConverOpen_Pwm = 40,
.Sensor_Screw_1_Pwm = 40,
.Sensor_Screw_2_Pwm = 40,
.Sensor_PaperOut_Pwm = 40,
.Sensor_PaperDetection_Pwm = 40,
.Sensor_Double_double_check_cyc = 4,
.Sensor_Double_paper_check_cyc = 8,
.Sensor_Double_double_max = 0x1c,
.Sensor_Double_single_max = 0x70,
.Sensor_enable_config = 0,
.Sensor_Double_enable_config = 0,
};
bool b_save= false;
if(js[Sensor_Scan_Pwm].is_object() && js[Sensor_Scan_Pwm].is_number_integer())
param.Sensor_Scan_Pwm = js[Sensor_Scan_Pwm];
else b_save = true;
if(js[Sensor_Screw_1_Pwm].is_object() && js[Sensor_Screw_1_Pwm].is_number_integer())
param.Sensor_Screw_1_Pwm = js[Sensor_Screw_1_Pwm];
else b_save = true;
if(js[Sensor_Screw_2_Pwm].is_object() && js[Sensor_Screw_2_Pwm].is_number_integer())
param.Sensor_Screw_2_Pwm = js[Sensor_Screw_2_Pwm];
else b_save = true;
if(js[Sensor_ConverOpen_Pwm].is_object() && js[Sensor_ConverOpen_Pwm].is_number_integer())
param.Sensor_ConverOpen_Pwm = js[Sensor_ConverOpen_Pwm];
else b_save = true;
if(js[Sensor_PaperOut_Pwm].is_object() && js[Sensor_PaperOut_Pwm].is_number_integer())
param.Sensor_PaperOut_Pwm = js[Sensor_PaperOut_Pwm];
else b_save = true;
if(js[Sensor_PaperDetection_Pwm].is_object() && js[Sensor_PaperDetection_Pwm].is_number_integer())
param.Sensor_PaperDetection_Pwm = js[Sensor_PaperDetection_Pwm];
else b_save = true;
if(js[Sensor_Double_double_check_cyc].is_object() && js[Sensor_Double_double_check_cyc].is_number_integer())
param.Sensor_Double_double_check_cyc = js[Sensor_Double_double_check_cyc];
else b_save = true;
if(js[Sensor_Double_paper_check_cyc].is_object() && js[Sensor_Double_paper_check_cyc].is_number_integer())
param.Sensor_Double_paper_check_cyc = js[Sensor_Double_paper_check_cyc];
else b_save = true;
if(js[Sensor_Double_double_max].is_object() && js[Sensor_Double_double_max].is_number_integer())
param.Sensor_Double_double_max = js[Sensor_Double_double_max];
else b_save = true;
if(js[Sensor_Double_single_max].is_object() && js[Sensor_Double_single_max].is_number_integer())
param.Sensor_Double_single_max = js[Sensor_Double_single_max];
else b_save = true;
if(js[Sensor_enable_config].is_object() && js[Sensor_enable_config].is_number_integer())
param.Sensor_enable_config = js[Sensor_enable_config];
else b_save = true;
if(js[Sensor_Double_enable_config].is_object() && js[Sensor_Double_enable_config].is_number_integer())
param.Sensor_enable_config = js[Sensor_Double_enable_config];
else b_save = true;
if(b_save)
{
m_param = param;
auto js = to_json(param);
std::ofstream o(this->Path);
o << std::setw(4) << js <<std::endl;
o.close();
o.flush();
}
return param;
}