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 78f12b4 commit e092a19
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions main.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <charconv>
#include <iomanip>
#include <iostream>

Expand All @@ -11,22 +12,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 +72,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();
if (temp > 0.0) {
break;
} else {
usleep(1'000'000);
attempts--;
}
}
std::cout << std::fixed << std::setprecision(1) << temp;
break;
}

Expand Down

0 comments on commit e092a19

Please sign in to comment.