Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
airspeed_otf.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Martin Mueller <martinmm@pfump.org>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
29 #include <stdbool.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include "mcu_periph/uart.h"
35 #include "messages.h"
37 
38 #include "met_module.h"
39 #include "airspeed_otf.h"
40 
41 
42 #define OTF_UNINIT 0x00
43 #define OTF_WAIT_START OTF_UNINIT
44 #define OTF_WAIT_COUNTER 0x01
45 #define OTF_WAIT_ANGLES 0x02
46 #define OTF_WAIT_ALTITUDE 0x03
47 #define OTF_WAIT_CHECKSUM 0x04
48 
49 #define OTF_START 0x0A
50 #define OTF_LIMITER ','
51 #define OTF_END 0x0D
52 
53 /* workaround for newlib */
54 void *_sbrk(int);
55 void *_sbrk(int a) {return 0;}
56 
57 /* airspeed_otf_parse */
58 void airspeed_otf_parse(char c)
59 {
60  static unsigned char otf_status = OTF_UNINIT, otf_idx = 0, otf_crs_idx;
61  static char otf_inp[64];
62  static unsigned int counter;
63  static short course[3];
64  static unsigned int altitude;
65  static unsigned char checksum;
66 
67  switch (otf_status) {
68 
69  case OTF_WAIT_START:
70  if (c == OTF_START) {
71  otf_status++;
72  otf_idx = 0;
73  } else {
74  otf_status = OTF_UNINIT;
75  }
76  break;
77 
78  case OTF_WAIT_COUNTER:
79  if (isdigit((int)c)) {
80  if (otf_idx == 0) {
81 //FIXME otf_timestamp = getclock();
82  }
83  otf_inp[otf_idx++] = c;
84  } else {
85  if ((otf_idx == 5) && (c == OTF_LIMITER)) {
86  otf_inp[otf_idx] = 0;
87  counter = atoi(otf_inp);
88  otf_idx = 0;
89  otf_crs_idx = 0;
90  otf_status++;
91  } else {
92  otf_status = OTF_UNINIT;
93  }
94  }
95  break;
96 
97  case OTF_WAIT_ANGLES:
98  if (isdigit((int)c) || (c == '-') || (c == '.')) {
99  otf_inp[otf_idx++] = c;
100  } else {
101  if ((otf_idx > 1) && (otf_idx < 9) && (c == OTF_LIMITER)) {
102  otf_inp[otf_idx] = 0;
103  course[otf_crs_idx] = (int16_t)(100. * atof(otf_inp));
104  otf_idx = 0;
105  if (otf_crs_idx++ == 2) {
106  otf_status++;
107  }
108  } else {
109  otf_status = OTF_UNINIT;
110  }
111  }
112  break;
113 
114  case OTF_WAIT_ALTITUDE:
115  if (isdigit((int)c) || (c == '-') || (c == '.')) {
116  otf_inp[otf_idx++] = c;
117  } else {
118  if ((otf_idx > 1) && (otf_idx < 9) && (c == OTF_LIMITER)) {
119  otf_inp[otf_idx] = 0;
120  altitude = (int32_t)(100. * atof(otf_inp));
121  otf_idx = 0;
122  otf_status++;
123  } else {
124  otf_status = OTF_UNINIT;
125  }
126  }
127  break;
128 
129  case OTF_WAIT_CHECKSUM:
130  if (isxdigit((int)c)) {
131  otf_inp[otf_idx++] = c;
132  } else {
133  if ((otf_idx == 2) && (c == OTF_END)) {
134  otf_inp[otf_idx] = 0;
135  checksum = strtol(otf_inp, NULL, 16);
136  otf_idx = 0;
137  DOWNLINK_SEND_FLOW_AP_OTF(DefaultChannel, DefaultDevice, &counter, &course[0], &course[1], &course[2], &altitude,
138  &checksum);
139  }
140  otf_status = OTF_UNINIT;
141  }
142  break;
143 
144  default:
145  otf_status = OTF_UNINIT;
146  break;
147  }
148 }
149 
151 {
152 }
153 
155 {
156  while (MetBuffer()) {
157  uint8_t ch = MetGetch();
158  airspeed_otf_parse(ch);
159  }
160 }
161 
163 {
164 }
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define OTF_START
Definition: airspeed_otf.c:49
void airspeed_otf_periodic(void)
Definition: airspeed_otf.c:162
#define OTF_WAIT_START
Definition: airspeed_otf.c:43
#define OTF_END
Definition: airspeed_otf.c:51
Device independent serial meteo code.
#define OTF_UNINIT
Definition: airspeed_otf.c:42
#define OTF_WAIT_ANGLES
Definition: airspeed_otf.c:45
#define OTF_LIMITER
Definition: airspeed_otf.c:50
signed short int16_t
Definition: types.h:17
int32_t counter
signed long int32_t
Definition: types.h:19
#define OTF_WAIT_CHECKSUM
Definition: airspeed_otf.c:47
unsigned char uint8_t
Definition: types.h:14
void * _sbrk(int)
Definition: airspeed_otf.c:55
#define MetBuffer()
Definition: met_module.h:42
void airspeed_otf_parse(char c)
Definition: airspeed_otf.c:58
#define OTF_WAIT_ALTITUDE
Definition: airspeed_otf.c:46
void airspeed_otf_init(void)
Definition: airspeed_otf.c:150
void airspeed_otf_event(void)
Definition: airspeed_otf.c:154
#define OTF_WAIT_COUNTER
Definition: airspeed_otf.c:44
#define MetGetch()
Definition: met_module.h:43