From 3b1882a6a8c48c7553c0285c7b046b2525ad1e31 Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Sun, 1 May 2011 17:12:39 +0300 Subject: [PATCH] Separate initialization and data reading into a separate steps This makes it possible in future to do something useful with the data instead of just dumping it out to stdout. Signed-off-by: Timo Kokkonen --- contour-protocol.c | 27 ++++++++++++++++++--------- contour-protocol.h | 3 ++- main.c | 15 +++++++++++++-- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/contour-protocol.c b/contour-protocol.c index 0637555..1a86c96 100644 --- a/contour-protocol.c +++ b/contour-protocol.c @@ -136,12 +136,11 @@ static int send_pattern(int fd, int uc, unsigned char byte1, unsigned char byte2 return 0; } -int communicate(int fd, int uc) +int contour_initialize(int fd, int uc) { int i, j; - struct msg msg, in; + struct msg msg; msg.direction = OUT; - trace(0, "Initializing..\n"); read_msgs(fd); SET_FIRST_BYTE(0x01); @@ -265,17 +264,27 @@ int communicate(int fd, int uc) send_msg_with_proggress_note(&msg, fd, uc); read_msgs(fd); - trace(0, "\nGlucose readings:\n"); usleep(100 * 1000); - do { - send_msg(&msg, fd, uc); - read_and_verify(&in, fd); - print_ascii(in.data, datalen(in.data)); - } while (datalen(in.data) > 45); + trace(0, "\n"); return 0; } +int contour_read_entry(int fd, int uc, struct msg *in) +{ + struct msg msg; + int j; + + msg.direction = OUT; + SET_FIRST_BYTE(0x01); + SET_BYTE(1, 0x06); + + send_msg(&msg, fd, uc); + read_and_verify(in, fd); + + return datalen(in->data); +} + int wait_for_device(int vendor, int product, int *usage_code) { int fd; diff --git a/contour-protocol.h b/contour-protocol.h index 049ccc9..f1774ce 100644 --- a/contour-protocol.h +++ b/contour-protocol.h @@ -14,7 +14,8 @@ enum direction { OUT, }; -int communicate(int fd, int uc); +int contour_initialize(int fd, int uc); +int contour_read_entry(int fd, int uc, struct msg *in); int wait_for_device(int vendor, int product, int *usage_code); diff --git a/main.c b/main.c index 76b32d7..79dc67d 100644 --- a/main.c +++ b/main.c @@ -11,8 +11,9 @@ int main(int argc, char *argv[]) { - int fd, usage_code; + int fd, usage_code, ret; struct user_options opts; + struct msg msg; read_args(argc, argv, &opts); trace_level = opts.trace_level; @@ -25,7 +26,17 @@ int main(int argc, char *argv[]) if (fd < 0) return 1; - communicate(fd, usage_code); + trace(0, "Initializing\n"); + contour_initialize(fd, usage_code); + + trace(0, "Done! Reading data\n"); + while (1) { + ret = contour_read_entry(fd, usage_code, &msg); + print_ascii(msg.data, ret); + + if (ret < 45) + break; + } return 0; } -- 2.45.0