cpp
#include <cstdint>
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include <unistd.h>
#define SENSOR_PATH "/sys/bus/iio/devices/iio:device0/"
#define XSysMonPsu_RawToTemperature_OnChip(AdcData) \
((((float)(AdcData) / 65536.0f) / 0.00196342531f) - 280.2309f)
#define XSysMonPsu_RawToVoltage(AdcData) \
((((float)(AdcData)) * (3.0f)) / 65536.0f)
int SysMonPsuFractionToInt(float FloatNum) {
float Temp;
Temp = FloatNum;
if (FloatNum < 0) {
Temp = -(FloatNum);
}
return (((int)((Temp - (float)((int)Temp)) * (1000.0f))));
}
int32_t main(int32_t argc, char *argv[]) {
if (argc != 2) {
return -2;
}
std::string sensor = std::string(SENSOR_PATH) + std::string(argv[1]);
if (access(sensor.c_str(), F_OK) != 0) {
printf("ERR:%s %s\n", strerror(errno), argv[1]);
return 1;
}
std::regex reg1("(.*)temp(.*)");
std::ifstream ifs;
char temp[32];
std::string device;
float t;
ifs.open(sensor);
ifs >> temp;
if (std::regex_match(temp, reg1)) {
t = XSysMonPsu_RawToTemperature_OnChip(strtol(temp, NULL, 10));
printf("%s:%d.%d\n", argv[1], (int)t, SysMonPsuFractionToInt(t));
} else {
t = XSysMonPsu_RawToVoltage(strtol(temp, NULL, 10));
printf("%s:%d.%d\n", argv[1], (int)t, SysMonPsuFractionToInt(t));
}
return 0;
}