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