-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
104 lines (97 loc) · 2.56 KB
/
main.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#if defined(OS_LINUX) || defined(OS_MACOSX)
#include <sys/ioctl.h>
#include <termios.h>
#elif defined(OS_WINDOWS)
#include <conio.h>
#endif
#include "hid.h"
//*----------------------------------------------------------------------------
static char getkey(void);
//*----------------------------------------------------------------------------
int main()
{
int i, r, num, temp;
char c, buf[64], *pwr;
r = rawhid_open(1, 0x16C0, 0x0480, 0xFFAB, 0x0200);
if (r <= 0) {
fprintf(stdout, "No Temp-Sensor found\n");
return r;
}
// fprintf(stdout, "Found Temp-Sensor\n");
while (1) {
//....................................
// check if any Raw HID packet has arrived
//....................................
num = rawhid_recv(0, buf, 64, 220);
if (num < 0) {
fprintf(stdout, "\nError Reading\n");
rawhid_close(0);
return num;
}
if (num == 64) {
temp = *(short *)&buf[4];
if(buf[2]) { pwr = "Extern"; }
else { pwr = "Parasite"; }
fprintf(stdout, "Sensor #%d of %d: %+.1f\xF8""C Power: %-10s ID: ",
buf[1], buf[0], temp / 10.0, pwr);
for (i = 0x08; i < 0x10; i++) {
fprintf(stdout, "%02X ", (unsigned char)buf[i]);
}
fprintf(stdout, "\n");
break;
}
//....................................
// check if any input on stdin
//....................................
c = getkey();
if(c == 0x1B) { break; } // ESC
if(c >= 32) {
fprintf(stdout, "\ngot key '%c', sending...\n", c);
buf[0] = c;
for (i=1; i<64; i++) {
buf[i] = 0;
}
rawhid_send(0, buf, 64, 100);
}
}
rawhid_close(0);
}
//*----------------------------------------------------------------------------
#if defined(OS_LINUX) || defined(OS_MACOSX)
// Linux (POSIX) implementation of _kbhit().
// Morgan McGuire, morgan@cs.brown.edu
static int _kbhit() {
static const int STDIN = 0;
static int initialized = 0;
int bytesWaiting;
if (!initialized) {
// Use termios to turn off line buffering
struct termios term;
tcgetattr(STDIN, &term);
term.c_lflag &= ~ICANON;
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);
initialized = 1;
}
ioctl(STDIN, FIONREAD, &bytesWaiting);
return bytesWaiting;
}
static char _getch(void) {
char c;
if (fread(&c, 1, 1, stdin) < 1) return 0;
return c;
}
#endif
//*----------------------------------------------------------------------------
static char getkey(void)
{
if (_kbhit()) {
char c = _getch();
if (c != 0) return c;
}
return 0;
}
//*----------------------------------------------------------------------------