summaryrefslogtreecommitdiff
path: root/temper.c
blob: 9662cbfe8b562b5711cef0d793be1eee0763ca06 (plain)
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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#include <hidapi.h>

uint8_t cmd[] = {0x00, 0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00};

hid_device *find_hid(uint16_t vid, uint16_t pid, uint16_t iface)
{
    struct hid_device_info *list = hid_enumerate(vid, pid);
    struct hid_device_info *p = list;
    hid_device *ret = NULL;
    while (p != NULL)
    {
        if (p->interface_number == iface)
        {
            ret = hid_open_path(p->path);
        }
        p = p->next;
    }
    if (list != NULL)
        hid_free_enumeration(list);
    return ret;
}

int main(int argc, char **argv)
{
    int res;
    int retcode = EXIT_FAILURE;
    uint8_t buf[1024];
    hid_device *temper;

    res = hid_init();

    temper = find_hid(0x413d, 0x2107, 1);

    if (temper == NULL)
    {
        fprintf(stderr, "Unable to open temper device\n");
        goto close_out;
    }

    res = hid_write(temper, cmd, 9);
    if (res != 9)
    {
        fprintf(stderr, "Unable to write command to temper\n");
        goto close_out;
    }

    res = hid_read_timeout(temper, &buf[0], 1024, 1000);
    if (res < 0)
    {
        fprintf(stderr, "Failure reading from temper\n");
        goto close_out;
    }
    if (res == 0)
    {
        fprintf(stderr, "Unable to read from temper, no response\n");
        goto close_out;
    }

    if (res < 6)
    {
        fprintf(stderr, "Read too few bytes from temper: %d\n", res);
        goto close_out;
    }

    float temp = (((float)buf[2]) * 256.0) + ((float)buf[3]);
    temp = temp / 100;

    printf("%.2f\n", temp);

    retcode = EXIT_SUCCESS;
close_out:
    hid_close(temper);

    return retcode;
}