Skip to content

Commit

Permalink
Add option to poll the temperature for n times until a valid value is…
Browse files Browse the repository at this point in the history
… found
  • Loading branch information
enen92 committed Aug 30, 2023
1 parent b4b2e3b commit a084945
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions main.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <charconv>
#include <cmath>
#include <iomanip>
#include <iostream>

Expand All @@ -11,22 +13,33 @@ void usage(char* prog) {
std::cout << " -h : help" << std::endl;
std::cout << " -l : list all keys and values" << std::endl;
std::cout << " -v : version" << std::endl;
std::cout << " -n : tries to query the temperature sensor for n times";
std::cout << " (1 second interval) until a valid value is returned" << std::endl;
}

int main(int argc, char *argv[]) {
int c;
extern char *optarg;
unsigned int attempts = 1;

kern_return_t result;
int op = smctemp::kOpNone;
smctemp::UInt32Char_t key = { 0 };
smctemp::SmcVal_t val;

while ((c = getopt(argc, argv, "clvh")) != -1) {
while ((c = getopt(argc, argv, "clvhn::")) != -1) {
switch(c) {
case 'c':
op = smctemp::kOpReadCpuTemp;
break;
case 'n':
if (optarg) {
auto [ptr, ec] = std::from_chars(optarg, optarg + strlen(optarg), attempts);
if (ec != std::errc()) {
std::cerr << "Invalid argument provided for -n (integer is required)" << std::endl;
return 1;
}
}
break;
case 'l':
op = smctemp::kOpList;
break;
Expand Down Expand Up @@ -60,7 +73,17 @@ int main(int argc, char *argv[]) {
}
break;
case smctemp::kOpReadCpuTemp:
std::cout << std::fixed << std::setprecision(1) << smc_temp.GetCpuTemp();
double temp = 0.0;
while (attempts > 0) {
temp = smc_temp.GetCpuTemp();
attempts--;
if (attempts > 0 && (temp == 0.0 || std::isnan(temp))) {
usleep(1000000);
} else {
break;
}
}
std::cout << std::fixed << std::setprecision(1) << temp;
break;
}

Expand Down

0 comments on commit a084945

Please sign in to comment.