X-Git-Url: http://sigaev.ru/git/gitweb.cgi?p=trinked.git;a=blobdiff_plain;f=trinket.c;h=8f2cdc96ef8d95e98ea06581708475d090e18d4a;hp=6ff5e4dcd58e6d54e51f8df9c1b66e6158d4d356;hb=8cbe60be9b46ed1bd7aa695ed1be732524994d7d;hpb=d4790deef78ab124caa9a32ab12743941c765fdf diff --git a/trinket.c b/trinket.c index 6ff5e4d..8f2cdc9 100644 --- a/trinket.c +++ b/trinket.c @@ -7,10 +7,10 @@ * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE @@ -75,7 +75,8 @@ typedef struct { uint32_t respLen; /* 0 means variable, > 0 fixed */ } TrinketProtoDesc; -static const TrinketProtoDesc protoDesc[] = { +/* hardcoded knowledge from docs/geyger.pdf */ +static const TrinketProtoDesc protoDesc[] = { {CMD_PING, 4, 5 }, {CMD_VCC_VALUE, 4, 8 }, {CMD_VCC_ALARM, 8, 8 }, @@ -104,6 +105,7 @@ dumpBuf(char *msg, uint8_t *buf, int len) { TrinketErrorCode trinketOpen() { + TrinketErrorCode r; #ifndef NDEBUG int i; @@ -118,7 +120,9 @@ trinketOpen() { return ERR_ERROR; } - return trinketPing(); + if ((r = trinketPing()) != ERR_OK) + trinketClose(); + return r; } void @@ -174,7 +178,7 @@ trinketPing() { } if (r != sizeof(resp)) { - fprintf(stderr, "hid_read_timeout returns %d instead of %u\n", r, sizeof(resp)); + fprintf(stderr, "hid_read_timeout returns %d instead of %u\n", r, (unsigned int)sizeof(resp)); return ERR_ERROR; } @@ -247,7 +251,7 @@ trinketGetCumDose(double *value) { } if (r != sizeof(resp)) { - fprintf(stderr, "hid_read_timeout returns %d instead of %u\n", r, sizeof(resp)); + fprintf(stderr, "hid_read_timeout returns %d instead of %u\n", r, (unsigned int)sizeof(resp)); return ERR_ERROR; }