// SPDX-License-Identifier: GPL-3.0-or-later #pragma once #include "config_export.h" #include #include #include // Decoding and firmware compatibility belong to Commands. This adapter only // projects its typed result; it never builds or parses vendor packets. template static QJsonObject toolNumbers(const T &value) { QJsonObject out; for (int i=0; i codes{{"adc",COMM_GET_DECODED_ADC},{"chuk",COMM_GET_DECODED_CHUK}, {"imu",COMM_GET_IMU_DATA},{"bms",COMM_BMS_GET_VALUES}}; require(codes.contains(tool),"Unknown peripheral tool"); if (!cmd->getLimitedCompatibilityCommands().contains(codes.value(tool))) return {{"tool",tool},{"state","unsupported"}}; QEventLoop loop; QTimer timeout; timeout.setSingleShot(true); QObject observer; QJsonObject values; bool received=false; auto finish=[&](const QJsonObject &v){values=v;received=true;loop.quit();}; QObject::connect(cmd,&Commands::decodedAdcReceived,&observer,[&](double v,double volts,double v2,double volts2){ if(tool=="adc")finish({{"level_1",v},{"voltage_1_v",volts},{"level_2",v2},{"voltage_2_v",volts2}}); }); QObject::connect(cmd,&Commands::decodedChukReceived,&observer,[&](double v){if(tool=="chuk")finish({{"level",v}});}); QObject::connect(cmd,&Commands::valuesImuReceived,&observer,[&](IMU_VALUES v,unsigned int mask){ if(tool=="imu"){auto out=toolNumbers(v);out.insert("mask",double(mask));finish(out);} }); QObject::connect(cmd,&Commands::bmsValuesRx,&observer,[&](BMS_VALUES v){ if(tool!="bms")return; auto out=toolNumbers(v);QJsonArray cells,temps,balancing; for(auto x:v.v_cells)cells.append(x);for(auto x:v.temps)temps.append(x);for(auto x:v.is_balancing)balancing.append(x); out.insert("v_cells",cells);out.insert("temps",temps);out.insert("is_balancing",balancing);finish(out); }); QObject::connect(&timeout,&QTimer::timeout,&loop,&QEventLoop::quit);timeout.start(2000); if(tool=="adc")cmd->getDecodedAdc(); if(tool=="chuk")cmd->getDecodedChuk(); if(tool=="imu")cmd->getImuData(0xffff); if(tool=="bms")cmd->bmsGetValues(); if(!received)loop.exec(); require(vesc.isPortConnected(),"Peripheral read disconnected"); return {{"tool",tool},{"state",received?"received":"no_response"},{"values",values}}; }