You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.3 KiB
102 lines
3.3 KiB
#pragma once
|
|
#include "base/appdep.hpp"
|
|
|
|
namespace iflytop {
|
|
using namespace transmit_disfection_protocol;
|
|
class PXXPSBus {
|
|
PXXPressureSensorBus psbus;
|
|
bool m_isInitialized = false;
|
|
osTimerId reportTimerId; // 压力传感器数值上报
|
|
|
|
public:
|
|
void initialize(UART_HandleTypeDef* huart) {
|
|
psbus.init(huart);
|
|
m_isInitialized = true;
|
|
|
|
osTimerDef(reportTimer, onTimer);
|
|
reportTimerId = osTimerCreate(osTimer(reportTimer), osTimerPeriodic, this);
|
|
|
|
BIND_FN(PXXPSBus, this, fn_psbus_read_data);
|
|
BIND_FN(PXXPSBus, this, fn_psbus_scan);
|
|
BIND_FN(PXXPSBus, this, fn_psbus_start_report);
|
|
BIND_FN(PXXPSBus, this, fn_psbus_stop_report);
|
|
}
|
|
bool isInitialized() { return m_isInitialized; }
|
|
|
|
private:
|
|
void fn_psbus_read_data(ProcessContext* cxt) {
|
|
CHECK_PARAM_LEN(PRAAM_LEN(), 1);
|
|
int32_t index = GET_PARAM(0);
|
|
|
|
int16_t val = 0;
|
|
int32_t reportVal = 0;
|
|
bool suc = psbus.readData(index, &val);
|
|
|
|
reportVal = val;
|
|
|
|
if (suc) {
|
|
zcanbus_send_ack(cxt->packet, (uint8_t*)&reportVal, sizeof(reportVal));
|
|
} else {
|
|
zcanbus_send_errorack(cxt->packet, kerr_subdevice_offline);
|
|
}
|
|
}
|
|
void fn_psbus_scan(ProcessContext* cxt) {
|
|
auto* sensors = psbus.sensors;
|
|
int numSensor = psbus.sensorNum;
|
|
|
|
static ack_psbus_scan_t result;
|
|
memset(&result, 0, sizeof(result));
|
|
static_assert(ZARRAY_SIZE(result.sensor) == ZARRAY_SIZE(psbus.sensors));
|
|
result.numOnlineId = numSensor;
|
|
for (int i = 0; i < PXX_PRESSURE_SENSOR_NUM; i++) {
|
|
if (sensors[i].online) {
|
|
result.sensor[i].ptype = sensors[i].type;
|
|
result.sensor[i].subid = sensors[i].id;
|
|
result.sensor[i].isOnline = 1;
|
|
result.sensor[i].precision = sensors[i].p100_sensor_info.precision;
|
|
result.sensor[i].uint = sensors[i].p100_sensor_info.pressure_unit;
|
|
result.sensor[i].zero = sensors[i].p100_sensor_info.zero_point;
|
|
result.sensor[i].full = sensors[i].p100_sensor_info.range_full_point;
|
|
}
|
|
}
|
|
zcanbus_send_ack(cxt->packet, (uint8_t*)&result, sizeof(result));
|
|
}
|
|
|
|
void fn_psbus_start_report(ProcessContext* cxt) {
|
|
int period = GET_PARAM(0);
|
|
if (period < 100) period = 100;
|
|
osTimerStop(reportTimerId);
|
|
osTimerStart(reportTimerId, period);
|
|
zcanbus_send_ack(cxt->packet, nullptr, 0);
|
|
}
|
|
|
|
void fn_psbus_stop_report(ProcessContext* cxt) {
|
|
osTimerStop(reportTimerId);
|
|
zcanbus_send_ack(cxt->packet, nullptr, 0);
|
|
}
|
|
|
|
public:
|
|
static void onTimer(const void* tid) {
|
|
PXXPSBus* bus = (PXXPSBus*)pvTimerGetTimerID((TimerHandle_t)tid);
|
|
bus->onTimer();
|
|
}
|
|
void onTimer() {
|
|
int16_t val = 0;
|
|
|
|
static uint8_t report_buf[100];
|
|
report_pressure_data_t* reportData = (report_pressure_data_t*)report_buf;
|
|
|
|
reportData->sensorDataNum = 0;
|
|
for (int i = 0; i < PXX_PRESSURE_SENSOR_NUM; i++) {
|
|
if (psbus.sensors[i].online) {
|
|
psbus.readData(i, &val);
|
|
reportData->data[reportData->sensorDataNum].subid = psbus.sensors[i].id;
|
|
reportData->data[reportData->sensorDataNum].pressureVal = val;
|
|
reportData->sensorDataNum++;
|
|
}
|
|
}
|
|
zcanbus_send_report(kreport_pressure_data, (uint8_t*)reportData, //
|
|
sizeof(*reportData) + sizeof(reportData->data[0]) * reportData->sensorDataNum, 30);
|
|
}
|
|
};
|
|
} // namespace iflytop
|