You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

brauniger_iq.cc 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Serial download of barograph data from a Brauniger IQ Variometer.
  3. *
  4. * Copyright (C) 2004 Chris Jones
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 59 Temple Place - Suite 330, Boston, MA 02111 USA
  19. */
  20. #include "defs.h"
  21. #include "gbser.h"
  22. #include <stdio.h>
  23. static void* serial_handle;
  24. #define MYNAME "BRAUNIGER-IQ"
  25. #define PRESTRKNAME "PRESALTTRK"
  26. typedef enum {
  27. st_sync,
  28. st_fl_num,
  29. st_data_len,
  30. st_ser_num,
  31. st_pilot_name,
  32. st_start_date,
  33. st_start_year,
  34. st_max_alt_1,
  35. st_max_alt_2,
  36. st_max_climb,
  37. st_flight_dur,
  38. st_log_ival,
  39. st_start_time,
  40. st_end_time,
  41. st_sample_alt,
  42. st_sample_spd,
  43. num_states
  44. } state_t;
  45. state_t state;
  46. #if __cplusplus
  47. inline state_t operator++(state_t& rs, int)
  48. {
  49. return rs = (state_t)((int)rs + 1);
  50. }
  51. #endif
  52. static const int reqd_bytes[num_states] = { 6, 1, 2, 2, 25, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1 };
  53. static void rd_init(const QString& fname)
  54. {
  55. if (serial_handle = gbser_init(qPrintable(fname)), NULL == serial_handle) {
  56. fatal(MYNAME ": Can't open port '%s'\n", qPrintable(fname));
  57. }
  58. if (gbser_set_port(serial_handle, 9600, 8, 0, 1) != gbser_OK) {
  59. fatal(MYNAME ": Can't configure port '%s'\n", qPrintable(fname));
  60. }
  61. }
  62. static void rd_deinit(void)
  63. {
  64. gbser_deinit(serial_handle);
  65. serial_handle = NULL;
  66. }
  67. /**
  68. * Process a data record.
  69. * @return zero when all expected data has been received
  70. */
  71. static int process_data(const unsigned char* data)
  72. {
  73. static int remaining = 100;
  74. static struct tm tm;
  75. static time_t start, creation;
  76. static route_head* track;
  77. static unsigned char interval;
  78. time_t finish;
  79. Waypoint* wpt = NULL;
  80. int i;
  81. if (global_opts.debug_level >= 3) {
  82. for (i = 0; i < reqd_bytes[state]; i++) {
  83. printf("%.2x ", data[i]);
  84. }
  85. puts("");
  86. }
  87. remaining -= reqd_bytes[state];
  88. switch (state) {
  89. case st_sync:
  90. if (memcmp(data, "\x30\x31\x32\x33\x34\x35", 6) != 0) {
  91. fatal(MYNAME ": Could not synchronise\n");
  92. }
  93. break;
  94. case st_fl_num:
  95. if (global_opts.debug_level >= 1) {
  96. printf(MYNAME ": Flight Number: %d\n", data[0]);
  97. }
  98. break;
  99. case st_data_len:
  100. remaining = (data[0] << 8) + data[1] - 2;
  101. if (global_opts.debug_level >= 1) {
  102. printf(MYNAME ": Data Length: %d\n", remaining);
  103. }
  104. break;
  105. case st_ser_num:
  106. if (global_opts.debug_level >= 1) {
  107. printf(MYNAME ": Serial Number: %d\n", (data[0] << 8) + data[1]);
  108. }
  109. break;
  110. case st_pilot_name:
  111. if (global_opts.debug_level >= 1) {
  112. printf(MYNAME ": Pilot Name: %.25s\n", data);
  113. }
  114. break;
  115. case st_start_date:
  116. i = (data[0] << 8) + data[1];
  117. tm.tm_mday = i / 100;
  118. tm.tm_mon = (i % 100) - 1;
  119. break;
  120. case st_start_year:
  121. tm.tm_year = ((data[0] << 8) + data[1]) - 1900;
  122. break;
  123. case st_max_alt_1:
  124. if (global_opts.debug_level >= 1) {
  125. printf(MYNAME ": Max Altitude 1: %dm\n", (data[0] << 8) + data[1]);
  126. }
  127. break;
  128. case st_max_alt_2:
  129. if (global_opts.debug_level >= 1) {
  130. printf(MYNAME ": Max Altitude 2: %dm\n", (data[0] << 8) + data[1]);
  131. }
  132. break;
  133. case st_max_climb:
  134. if (global_opts.debug_level >= 1) {
  135. i = (data[0] << 8) + data[1];
  136. printf(MYNAME ": Max climb: %d.%dm/s\n", i / 10, i % 10);
  137. }
  138. break;
  139. case st_flight_dur:
  140. if (global_opts.debug_level >= 1) {
  141. i = (data[0] << 8) + data[1];
  142. printf(MYNAME ": Flight Time: %d:%d\n", i / 100, i % 100);
  143. }
  144. break;
  145. case st_log_ival:
  146. interval = data[0];
  147. if (global_opts.debug_level >= 1) {
  148. printf(MYNAME ": Logging Interval: %ds\n", interval);
  149. }
  150. break;
  151. case st_start_time:
  152. i = (data[0] << 8) + data[1];
  153. tm.tm_hour = i / 100;
  154. tm.tm_min = (i % 100) - 1;
  155. tm.tm_sec = 0;
  156. creation = start = mktime(&tm);
  157. if (global_opts.debug_level >= 1) {
  158. printf(MYNAME ": Start Time: %s", ctime(&start));
  159. }
  160. break;
  161. case st_end_time:
  162. i = (data[0] << 8) + data[1];
  163. tm.tm_hour = i / 100;
  164. tm.tm_min = (i % 100) - 1;
  165. finish = mktime(&tm);
  166. if (global_opts.debug_level >= 1) {
  167. printf(MYNAME ": End Time: %s", ctime(&finish));
  168. }
  169. if (remaining) {
  170. track = route_head_alloc();
  171. track->rte_name = PRESTRKNAME;
  172. track->rte_desc = "Brauniger-IQ Barograph";
  173. track_add_head(track);
  174. } else {
  175. warning(MYNAME ": No barograph recorded for this flight\n");
  176. }
  177. break;
  178. case st_sample_alt:
  179. wpt = new Waypoint;
  180. wpt->latitude = wpt->longitude = 0.0;
  181. wpt->SetCreationTime(creation);
  182. creation += interval;
  183. wpt->altitude = (data[0] << 8) + data[1];
  184. track_add_wpt(track, wpt);
  185. if (global_opts.debug_level >= 2) {
  186. printf(MYNAME ": remaining=%d, Altitude=%fm, ", remaining, wpt->altitude);
  187. }
  188. break;
  189. case st_sample_spd:
  190. if (global_opts.debug_level >= 2) {
  191. printf("Airspeed=%dkmh\n", data[0]);
  192. }
  193. state = st_sample_alt;
  194. return remaining;
  195. default:
  196. fatal(MYNAME ": Bad internal state\n");
  197. }
  198. state++;
  199. return remaining;
  200. }
  201. static void data_read(void)
  202. {
  203. unsigned char ibuf[25];
  204. int rd_cnt;
  205. if (global_opts.debug_level >= 0) {
  206. puts(MYNAME ": Select recorded flight in memo mode.");
  207. puts(MYNAME ": Press Memo button for two seconds...");
  208. }
  209. // Wait until something arrives
  210. if (global_opts.debug_level >= 0) {
  211. puts(MYNAME ": Downloading flight...");
  212. }
  213. // Read data until there is none left to read
  214. state = st_sync;
  215. for (;;) {
  216. /* wait up to 5 seconds for more data */
  217. rd_cnt = gbser_read_wait(serial_handle, ibuf, reqd_bytes[state], 5000);
  218. if (rd_cnt < 0) {
  219. fatal(MYNAME ": Serial error\n");
  220. } else if (rd_cnt < reqd_bytes[state]) {
  221. fatal(MYNAME ": Incomplete download\n");
  222. }
  223. if (!process_data(ibuf)) {
  224. if (global_opts.debug_level >= 0) {
  225. puts(MYNAME " ...Finished");
  226. }
  227. return;
  228. }
  229. }
  230. }
  231. static arglist_t brauniger_iq_args[] = {
  232. ARG_TERMINATOR
  233. };
  234. ff_vecs_t brauniger_iq_vecs = {
  235. ff_type_serial,
  236. { ff_cap_none, ff_cap_read, ff_cap_none},
  237. rd_init,
  238. NULL,
  239. rd_deinit,
  240. NULL,
  241. data_read,
  242. NULL,
  243. NULL,
  244. brauniger_iq_args,
  245. CET_CHARSET_UTF8, 1 /* master process: don't convert anything | CET-REVIEW */
  246. };