forked from jam3sward/tsic-pigpio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
72 lines (65 loc) · 1.88 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include "tsic.h"
using namespace std;
int main( int argc, char **argv )
{
TSIC tsic;
bool bShowFahrenheit(false);
char *cvalue = NULL;
int iGpio = 20;
int c;
opterr = 0;
while ((c = getopt (argc, argv, "hg::f")) != -1)
switch (c)
{
case 'h':
cout << "TSIC 206/306 temperature sensor application" << endl;
cout << " usage: tsic-temp [arguments]" << endl;
cout << " -f show temperature in Fahrenheit" << endl;
cout << " -gN Read data from GPIO pin N (default is 20)" << endl;
cout << " -h Show this help" << endl;
return 0;
case 'f':
bShowFahrenheit = true;
break;
case 'g':
cvalue = optarg;
try {
iGpio = std::stoi(optarg);
if (iGpio < 0 || iGpio > 27) {
throw;
}
}
catch(...) {
cerr << "GPIO option -g requires an argument between 1 and 27" << endl;
cerr << "Example: -g24 -- see -h for help." << endl;
return 1;
}
break;
case '?':
if (optopt == 'g')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'. Check -h for help\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
if ( !tsic.open(iGpio) ) {
cerr << "open failed\n";
return 1;
}
double temp = 0.0;
if ( !tsic.getDegrees( temp ) )
cerr << "getDegrees failed\n";
if (bShowFahrenheit) {
temp = temp*1.8 + 32;
}
cout << temp << endl;
tsic.close();
return 0;
}