summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <daniel.silverstone@codethink.co.uk>2018-07-05 13:22:43 +0100
committerDaniel Silverstone <daniel.silverstone@codethink.co.uk>2018-07-05 13:22:43 +0100
commit7b7086a2ad996daa8cabe44af5a2a7f88d893f60 (patch)
treef710b5fa83ca7adf7556cdfdd1f7366a65ae337f
parent03e566b26dca57c73b08966cf56e73348b481efe (diff)
downloadtemper-7b7086a2ad996daa8cabe44af5a2a7f88d893f60.tar.bz2
Temper reader
-rw-r--r--.gitignore2
-rw-r--r--.vscode/c_cpp_properties.json17
-rw-r--r--Makefile16
-rw-r--r--temper.c79
4 files changed, 114 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..053b900
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*.o
+temper
diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json
new file mode 100644
index 0000000..c621532
--- /dev/null
+++ b/.vscode/c_cpp_properties.json
@@ -0,0 +1,17 @@
+{
+ "configurations": [
+ {
+ "name": "Linux",
+ "includePath": [
+ "${workspaceFolder}/**",
+ "/usr/include/hidapi"
+ ],
+ "defines": [],
+ "compilerPath": "/usr/bin/clang++-3.9",
+ "cStandard": "c11",
+ "cppStandard": "c++17",
+ "intelliSenseMode": "clang-x64"
+ }
+ ],
+ "version": 4
+} \ No newline at end of file
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..db477b9
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,16 @@
+all: temper
+
+clean:
+ $(RM) temper temper.o
+
+WARN := -Wall -std=c99
+OPT := -O0
+
+CFLAGS += $(shell pkg-config --cflags hidapi-libusb) $(WARN) $(OPT)
+LIBS += $(shell pkg-config --libs hidapi-libusb)
+
+temper: temper.o
+ $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
+
+%.o: %.c
+ $(CC) $(CFLAGS) -o $@ -c $<
diff --git a/temper.c b/temper.c
new file mode 100644
index 0000000..9662cbf
--- /dev/null
+++ b/temper.c
@@ -0,0 +1,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;
+} \ No newline at end of file