Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
usb_serial_stm32_example1.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Michal Podhradsky
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 
31 #include <string.h>
32 
33 void send_command(void);
34 void cmd_execute(void);
35 
36 char cmd_buf[64];
38 bool cmd_avail;
40 
44 void init_usb_serial(void)
45 {
46  VCOM_init();
47  cmd_idx = 0;
48  cmd_avail = false;
49 }
50 
51 
59 {
60  if (data == -1) { return; }
61  char c = (char)data;
62 
63  if (c == '\r' || c == '\n') {
64  // command complete
65  cmd_avail = true;
66  // add termination characters and the prompt into buffer
67  VCOM_putchar('\r');
68  VCOM_putchar('\n');
70  } else {
71  // buffer command
72  cmd_buf[cmd_idx++] = c;
73  // echo char back and transmit immediately
76  }
77 }
78 
82 static inline void ReadUsbBuffer(void)
83 {
84  while (VCOM_check_available() && !cmd_avail) {
86  }
87 }
88 
93 void cmd_execute(void)
94 {
95  // copy command into tx buffer for user feedback
96  for (int i = 0; i < cmd_idx; i++) {
98  }
99 
100  if (strncmp("help", cmd_buf, cmd_idx) == 0) {
101  uint8_t response[] = " - user asked for help";
102  for (uint16_t i = 0; i < sizeof(response); i++) {
103  VCOM_putchar(response[i]);
104  }
105  } else {
106  uint8_t response[] = " Command not recognized";
107  for (uint16_t i = 0; i < sizeof(response); i++) {
108  VCOM_putchar(response[i]);
109  }
110  }
111 
112  // add termination characters and the prompt into buffer
113  VCOM_putchar('\r');
114  VCOM_putchar('\n');
116 
117  // reset counter
118  cmd_idx = 0;
119  // send complete message
121 }
122 
127 {
128  VCOM_event();
129  if (VCOM_check_available()) {
130  ReadUsbBuffer();
131  }
132  if (cmd_avail) {
133  cmd_execute();
134  cmd_avail = false;
135  }
136 }
void VCOM_event(void)
Poll usb (required by libopencm3).
Definition: usb_ser_hw.c:468
int VCOM_getchar(void)
Reads one character from VCOM port.
Definition: usb_ser_hw.c:425
int VCOM_putchar(int c)
Writes one character to VCOM port fifo.
Definition: usb_ser_hw.c:397
int VCOM_check_available(void)
Checks if data available in VCOM buffer.
Definition: usb_ser_hw.c:459
void VCOM_init(void)
Definition: usb_ser_hw.c:565
void VCOM_send_message(void)
Send data from fifo right now.
Definition: usb_ser_hw.c:487
header for serial over USB modules
static void ReadUsbBuffer(void)
Helper function.
uint8_t cmd_idx
void usb_serial_parse_packet(int data)
Parse data from buffer Note that the function receives int, not char Because we want to be able to ca...
void cmd_execute(void)
Execute command from user use strncmp.
void send_command(void)
bool cmd_avail
char cmd_buf[64]
uint8_t prompt
void init_usb_serial(void)
Init module, call VCOM_init() from here.
void event_usb_serial(void)
Call VCOM_poll() from module event function.
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98