25
25
*/
26
26
27
27
#include <stdint.h>
28
+ #include <ctype.h>
28
29
#include <libopencm3/stm32/gpio.h>
29
30
#include <libopencm3/stm32/rcc.h>
30
31
#include <libopencm3/stm32/usart.h>
47
48
/* This is a ring buffer to holding characters as they are typed
48
49
* it maintains both the place to put the next character received
49
50
* from the UART, and the place where the last character was
50
- * read by the program. See the README file for a discussion of
51
- * the failure semantics.
51
+ * read by the program.
52
52
*/
53
+
53
54
#define RECV_BUF_SIZE 128 /* Arbitrary buffer size */
54
55
char recv_buf [RECV_BUF_SIZE ];
55
56
volatile int recv_ndx_nxt ; /* Next place to store */
@@ -116,9 +117,6 @@ void console_putc(char c)
116
117
* Check the console for a character. If the wait flag is
117
118
* non-zero. Continue checking until a character is received
118
119
* otherwise return 0 if called and no character was available.
119
- *
120
- * The implementation is a bit different however, now it looks
121
- * in the ring buffer to see if a character has arrived.
122
120
*/
123
121
char console_getc (int wait )
124
122
{
@@ -138,6 +136,9 @@ char console_getc(int wait)
138
136
* Send a string to the console, one character at a time, return
139
137
* after the last character, as indicated by a NUL character, is
140
138
* reached.
139
+ *
140
+ * Translate '\n' in the string (newline) to \n\r (newline +
141
+ * carraige return)
141
142
*/
142
143
void console_puts (char * s )
143
144
{
@@ -154,9 +155,11 @@ void console_puts(char *s)
154
155
/*
155
156
* int console_gets(char *s, int len)
156
157
*
157
- * Wait for a string to be entered on the console, limited
158
- * support for editing characters (back space and delete)
159
- * end when a <CR> character is received.
158
+ * Wait for a string to be entered on the console, with
159
+ * support for editing characters (delete letter, word,
160
+ * entire line). It returns when the length is reached
161
+ * or a carrige return is entered. <CR> is changed to newline
162
+ * before the buffer is returned.
160
163
*/
161
164
int console_gets (char * s , int len )
162
165
{
@@ -165,13 +168,23 @@ int console_gets(char *s, int len)
165
168
166
169
* t = '\000' ;
167
170
/* read until a <CR> is received */
168
- while ((c = console_getc (1 )) != '\r' ) {
171
+ while ((( c = console_getc (1 )) != '\r' ) && (( t - s ) < len ) ) {
169
172
if ((c == '\010' ) || (c == '\127' )) {
170
173
if (t > s ) {
171
174
/* send ^H ^H to erase previous character */
172
175
console_puts ("\010 \010" );
173
176
t -- ;
174
177
}
178
+ } else if (c == 0x17 ) { // ^W erase a word
179
+ while ((t > s ) && (!(isspace ((int ) (* t ))))) {
180
+ t -- ;
181
+ console_puts ("\010 \010" );
182
+ }
183
+ } else if (c == 0x15 ) { // ^U erase the line
184
+ while (t > s ) {
185
+ t -- ;
186
+ console_puts ("\010 \010" );
187
+ }
175
188
} else {
176
189
* t = c ;
177
190
console_putc (c );
@@ -182,6 +195,10 @@ int console_gets(char *s, int len)
182
195
/* update end of string with NUL */
183
196
* t = '\000' ;
184
197
}
198
+ if ((t < s ) < len ) {
199
+ * t ++ = '\n' ;
200
+ * t = 0 ;
201
+ }
185
202
return t - s ;
186
203
}
187
204
@@ -226,3 +243,11 @@ void console_setup(int baud)
226
243
/* Specifically enable recieve interrupts */
227
244
usart_enable_rx_interrupt (CONSOLE_UART );
228
245
}
246
+
247
+ /*
248
+ * Set a different baud rate for the console.
249
+ */
250
+ void console_baud (int baud_rate )
251
+ {
252
+ usart_set_baudrate (CONSOLE_UART , baud_rate );
253
+ }
0 commit comments