Update comments in main.c header
[soft/lpc82x/exanh] / host / exanh_dump / main.c
1 /****************************************************************************
2  *  Get data from sensors and decode
3  *
4  *   main.c
5  *
6  * Copyright 2013-2014 Nathael Pajani <nathael.pajani@ed3l.fr>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  ****************************************************************************/
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <termios.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <getopt.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/select.h>
36 #include <sys/time.h>
37 #include <arpa/inet.h>
38 #include "serial_utils.h"
41 #define BUF_SIZE  100
43 #define PROG_NAME  "Sensors polling and decode"
44 #define VERSION  "0.1"
47 void help(char *prog_name)
48 {
49         fprintf(stderr, "---------------- "PROG_NAME" --------------------------------\n");
50         fprintf(stderr, "Usage: %s [options]\n" \
51                 "  Available options:\n" \
52                 "  \t -d | --device : Serial device to use for serial communication with the module\n" \
53                 "  \t -h | --help : Print this help\n" \
54                 "  \t -v | --version : Print programm version\n" \
55                 "  All other arguments are data for the command, handled depending on the command.\n", prog_name);
56         fprintf(stderr, "-----------------------------------------------------------------------\n");
57 }
59 char data[150];
60 int data_idx = 0;
63 int protocol_decode(char c)
64 {
65         if (data_idx == 0) {
66                 if (c == '#') {
67                         data[0] = c;
68                         data_idx = 1;
69                 }
70                 return 0;
71         }
72         if (data_idx > 0) {
73                 data[data_idx] = c;
74                 if (data_idx == 19) {
75                         data_idx = 0;
76                         return 1;
77                 }
78                 data_idx++;
79         }
80         return 0;
81 }
83 int main(int argc, char* argv[])
84 {
85         /* Serial */
86         char* device = NULL;
87         int slave_fd = 0;
89         while(1) {
90                 int option_index = 0;
91                 int c = 0;
93                 struct option long_options[] = {
94                         {"device", required_argument, 0, 'd'},
95                         {"help", no_argument, 0, 'h'},
96                         {"version", no_argument, 0, 'v'},
97                         {0, 0, 0, 0}
98                 };
100                 c = getopt_long(argc, argv, "d:hv", long_options, &option_index);
102                 /* no more options to parse */
103                 if (c == -1) break;
104                 switch (c) {
106                         /* d, device */
107                         case 'd':
108                                 device = optarg;
109                                 break;
111                         /* v, version */
112                         case 'v':
113                                 printf("%s Version %s\n", PROG_NAME, VERSION);
114                                 return 0;
115                                 break;
117                         /* h, help */
118                         case 'h':
119                         default:
120                                 help(argv[0]);
121                                 return 0;
122                 }
123         }
126         /* Need Serial port as parameter */
127         if (device == NULL) {
128                 printf("Error, need serial (tty) device\n");
129                 help(argv[0]);
130                 return -1;
131         }
133         /* Open tty */
134         slave_fd = serial_setup(device);
135         if (slave_fd < 0) {
136                 printf("Unable to open specified serial port %s\n", device);
137                 return -2;
138         }
140         /* ************************************************* */
141         /* And never stop handling data ! */
142         while (1) {
143                 int len = 0, ret = 0;
144                 struct timeval now;
145                 char buf[BUF_SIZE];
146                 int idx = 0;
148                 /* Send periodic requests for temperature */
149                 gettimeofday(&now, NULL);
151                 memset(buf, 0, BUF_SIZE);
152                 /* Get serial data and try to build a packet */
153                 len = read(slave_fd, buf, BUF_SIZE);
154                 if (len < 0) {
155                         if (len == 0) {
156                                 printf("\nError, got activity on serial link, but no data ... End of file.\n");
157                         } else {
158                                 perror("serial read error");
159                         }
160                         close(slave_fd);
161                         idx = 0;
162                         len = 0;
163                         do {
164                                 slave_fd = serial_setup(device);
165                                 usleep(10 * 1000);
166                         } while (slave_fd < 0);
167                 } else {
168                         write(2, buf, len);
169                 }
170                 while (idx < len) {
171                         ret = protocol_decode(buf[idx]);
172                         /* Check return code to know if we have a valid packet */
173                         if (ret == 1) {
174                                 /* Valid packet received, parse data */
175                                 char addr;
176                                 int got_tsl, got_veml, got_bme;
177                                 uint16_t raw_humidity;
178                                 uint16_t lux, ir, uv;
179                                 uint16_t pressure, comp_temp, humidity; /* From BME */
180                                 uint16_t* vars = (uint16_t*)data;
182                                 addr = data[1] & 0x1F;
183                                 raw_humidity = (uint16_t)htons(vars[1]);
184                                 lux = (uint16_t)htons(vars[2]);
185                                 ir = (uint16_t)htons(vars[3]);
186                                 uv = (uint16_t)htons(vars[4]);
187                                 pressure = (uint16_t)htons(vars[5]);
188                                 comp_temp = (uint16_t)htons(vars[6]);
189                                 humidity = (uint16_t)htons(vars[7]);
190                                         
191                                 /* Display data */
192                                 printf("\e[H"); /* Goto terminal home (top left) */
193                                 printf("\e[KSensor %d\n", addr);
194                                 printf("\e[K\tSoil: %d\n", raw_humidity);
195                                 printf("\e[K\tLux: %d, IR: %d, UV: %d\n", lux, ir, uv);
196                                 printf("\e[K\tPatm: %d hPa, Temp: %d,%02d degC, Humidity: %d,%d rH\n\n",
197                                                 pressure,
198                                                 comp_temp / 10,  (comp_temp > 0) ? (comp_temp % 10) : ((-comp_temp) % 10),
199                                                 humidity / 10, humidity % 10);
200                                 printf("\e[K\n");
201                         }
203                         idx++;
204                 }
206         } /* End of infinite loop */
208         close(slave_fd);
209         return 0;