4 * Copyright (C) 2013-06-20 K.Ohta <whatisthis.sowhat ai gmail.com>
6 * This program 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,
9 * or (at your option) any later version.
10 * This library / program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; see the file COPYING. If not, write to the
17 * Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
20 * As a special exception, if you link this(includeed from sdcc) library
21 * with other files, some of which are compiled with SDCC,
22 * to produce an executable, this library does not by itself cause
23 * the resulting executable to be covered by the GNU General Public License.
24 * This exception does not however invalidate any other reasons why
25 * the executable file might be covered by the GNU General Public License.
30 #include <pic18fregs.h> /* ONLY FOR PIC18x */
43 #define TERM_NONSREC -1
44 #define TERM_STRERR -2
45 #define TERM_SUMERR -3
47 unsigned char uart_term_putstr(unsigned char *s)
50 if((uart_getstat() & UART_WAKEUP) == 0) return 0; // Error
53 uart_pushchar(s[p], 0x0000);
55 if(p >= 255) return 0; // Overlen
60 void uart_term_getstr(unsigned char *s, unsigned char maxlen, unsigned char echo)
63 unsigned char c = 0x00;
68 if(echo != 0) uart_pushchar(c, 0); // Echoback
70 if(i >= maxlen) break;
76 unsigned char c2h(unsigned char c)
78 if(c < '1') return 0xff;
79 if(c > 'F') return 0xff;
80 if(c <= '9') return c - '0';
81 if(c >= 'A') return c - 'A' + 10;
85 char str_hex2bin(unsigned char *s, unsigned char *p, unsigned char len)
87 unsigned char sum = 0;
93 if((s[0] != 'S') || (s[1] != '0')) {
98 for(pp = 0; pp <= len ; pp++){
100 if(h >= 0x10) return TERM_STRERR;
102 if(l >= 0x10) return TERM_STRERR;
109 if(h >= 0x10) return TERM_STRERR;
111 if(l >= 0x10) return TERM_STRERR;
113 if(c != sum) return TERM_SUMERR;
117 unsigned char shell_strlen(char *s)
121 if(s[i] == '\0') break;
127 unsigned char shell_gettok(char *dst, char *src)
131 while(src[i] != '\0') {
132 if(src[i] == ' ') break;
133 if(src[i] == '\t') break;
142 int shell_strcmp(char *from, char *to)
153 if(from[p] != to[p]) return -1;
160 #define SHELL_CMD_NONE -1
161 #define SHELL_CMD_NOTFOUND -2
162 #define SHELL_CMD_ILLTOKEN -3
163 #define SHELL_CMD_TOOLONG -4
164 #define SHELL_CMD_OK 0
168 #define SHELL_TOPCMDS 16
169 const char *cmdstr[SHELL_TOPCMDS] = {
188 long term_getuint(char *pool)
196 if((c < '0') || (c > '9')) return -1;
206 void term_printnum(char *pool, unsigned int num)
209 unsigned char i, j, ii;
211 for(i = 0; i < 5; i++){
226 for(j = 0; j <= ii; j++) {
227 pool[j] = p[i] + '0';
234 * Set Frequency: Arg1 = Freq.
236 static void term_freq(char *p)
243 uart_term_putstr("\nSet Freq:");
244 if((l < 0) || (l > 30000)) {
245 uart_term_putstr("\nE:Illegal freq range.\n");
248 freq = (unsigned int)l;
250 term_printnum(pp, setup.amfreq);
254 term_printnum(pp, setup.fmfreq);
258 uart_term_putstr(pp);
259 uart_term_putstr("->");
260 term_printnum(pp, freq);
261 uart_term_putstr(pp);
263 akc6955_set_freq(freq);
264 uart_term_putstr("\ndone.\n");
271 static void term_printstatus(char *p)
274 uart_term_putstr("\nStatus:\n");
276 uart_term_putstr("AM \nBand = ");
277 term_printnum(pp, setup.amband);
278 uart_term_putstr(pp);
279 uart_term_putstr("\nFreq = ");
280 term_printnum(pp, setup.amfreq);
281 uart_term_putstr(pp);
282 uart_term_putstr("\n");
284 uart_term_putstr("FM \nBand = ");
285 term_printnum(pp, setup.fmband);
286 uart_term_putstr(pp);
287 uart_term_putstr("\nFreq = ");
288 term_printnum(pp, setup.fmfreq);
289 uart_term_putstr(pp);
290 uart_term_putstr("\n");
293 uart_term_putstr("\n");
298 * Open the Shell: Return cmdcode.
300 char term_shell(unsigned int timeout)
312 while((uart_getstat() & UART_WAKEUP) == 0) {
313 if(t == 0) return SHELL_CMD_NONE;
318 if((uart_getstat() & UART_WAKEUP) == 0) return SHELL_CMD_NONE;
320 uart_term_putstr("\n$>");
321 uart_term_getstr(shellstr, 128, 1); // With Echo
323 ii = shell_gettok(pool, shellstr);
324 if(ii >= 128) return SHELL_CMD_TOOLONG;
326 for(t = 0; t < SHELL_TOPCMDS; t++)
328 i = shell_strcmp((char *)cmdstr[t], pool);
331 if(i <= 0) return SHELL_CMD_NOTFOUND;
332 ii = shell_gettok(pool, &shellstr[ii]);
338 term_printstatus(pool);