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 bf6eb0a
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 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 @@ -7,26 +9,37 @@ void usage(char* prog) {
std::cout << "Check Temperature by using Apple System Management Control (Smc) tool " << smctemp::kVersion << std::endl;
std::cout << "Usage:" << std::endl;
std::cout << prog << " [options]" << std::endl;
std::cout << " -c : list CPU temperatures (Celsius)" << std::endl;
std::cout << " -c : list CPU temperatures (Celsius)." << std::endl;
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";

Check failure on line 16 in main.cc

View workflow job for this annotation

GitHub Actions / runner / cpplint

[cpplint] reported by reviewdog 🐶 Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] Raw Output: main.cc:16: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
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 bf6eb0a

Please sign in to comment.