4 * Copyright (C) 2013-11-07 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 #include "shell_strutl.h"
44 #include "uart_termio.h"
45 #include "term_shell.h"
48 char cmd_shellstr[255];
49 static char shell_strbuf[255];
50 static char xarg1[128];
51 static char xarg2[128];
52 static char xarg3[128];
54 #define SHELL_CMD_NONE -1
55 #define SHELL_CMD_NOTFOUND -2
56 #define SHELL_CMD_ILLTOKEN -3
57 #define SHELL_CMD_TOOLONG -4
58 #define SHELL_CMD_OK 0
60 #define SHELL_TOPCMDS 16
62 static void shell_printnum(unsigned int v)
70 * Set Frequency: Arg1 = Freq.
72 static void term_freq(char *p)
78 uart_term_putstr("\nSet Freq:");
79 if((l < 0) || (l > 30000)) {
80 uart_term_putstr("\nE:Illegal freq range.\n");
83 freq = (unsigned int)l;
85 shell_printnum(setup.amfreq);
89 shell_printnum(setup.fmfreq);
93 uart_term_putstr("->");
96 akc6955_set_freq(freq);
97 uart_term_putstr("\ndone.\n");
104 static void term_printstatus(char *p)
106 uart_term_putstr("\nStatus:\n");
108 uart_term_putstr("AM \nBand = ");
109 shell_printnum(setup.amband);
110 uart_term_putstr("\nFreq = ");
111 shell_printnum(setup.amfreq);
112 uart_term_putstr("\n");
114 uart_term_putstr("FM \nBand = ");
115 shell_printnum(setup.fmband);
116 uart_term_putstr("\nFreq = ");
117 shell_printnum(setup.fmfreq);
118 uart_term_putstr("\n");
121 uart_term_putstr("\n");
124 static void term_help(char *p)
131 * Arg1: 'N'/'n' : not save.
133 static void term_poweroff(char *p)
137 unsigned char savef = 0xff;
139 slen = shell_gettok(xarg1, p);
140 if(shell_strlen(xarg1) != 0) {
141 if((xarg1[0] == 'N') || (xarg1[0] == 'n')) savef = 0;
143 uart_term_putstr("\n*** Power OFF ***\nOK? (Yes = Y)");
144 uart_term_getstr(s, 15, 0xff);
145 if(shell_strlen(s) >= 3) return;
146 if(s[0] == 'Y') shutdown(savef);
150 static void term_poweron(char *p)
154 static unsigned char put_hexline(unsigned char *dat, unsigned int addr, unsigned char len)
157 l = str_bin2hex((unsigned char *)shell_strbuf, dat, addr, len);
158 uart_term_putstr(shell_strbuf);
162 static void put_hexheader(char *fname)
164 uart_term_putstr("Start of save value.\n--- BEGIN ---\n");
165 str_put_shexheader(shell_strbuf, fname);
166 uart_term_putstr(shell_strbuf);
170 static void put_hexfooter(void)
172 str_put_shexfooter(shell_strbuf);
173 uart_term_putstr(shell_strbuf);
174 uart_term_putstr("\n--- END---\n");
178 * Save ROM as S record.
180 static void save_to_term(unsigned char *p)
189 slen = shell_gettok(xarg1, p);
192 if(shell_strcmp(xarg1, "INT") == 0) { // Internal EEPROM
193 put_hexheader("INT_EEPROM");
194 len = sizeof(__radioset_t);
196 for(pp = 0; pp < sizeof(__radioset_t); pp += 16){
198 put_hexline(&pv[pp], pp, 16);
201 put_hexline(&pv[pp], pp, len);
205 } else if(shell_strcmp(xarg1, "FTBL") == 0) { // External EEPROM, Freq TBL
206 put_hexheader("EXT_FREQTBL");
208 } else if(shell_strcmp(xarg1, "BAND") == 0) { // band
209 put_hexheader("EXT_BANDTBL");
215 * Open the Shell: Return cmdcode.
217 const char *cmdstr[SHELL_TOPCMDS] = {
236 static void cmd_printhelp(char cmdnum)
238 uart_term_putstr("\nSynopsys:\n");
241 uart_term_putstr("HELP [about-command]\n");
242 uart_term_putstr(" -- Online help.\n");
245 uart_term_putstr("FREQ frequency\n");
246 uart_term_putstr(" -- Set frequency.\n");
249 uart_term_putstr("\nSTAT\n");
250 uart_term_putstr(" -- Print status.\n");
253 uart_term_putstr("\nLOAD direction [S]\n");
254 uart_term_putstr(" -- Load settings from terminal.\n");
255 uart_term_putstr(" Arg2: if 'S' then Save to internal EEPROM.\n");
258 uart_term_putstr("\nSAVE section\n");
259 uart_term_putstr(" -- Save settings to terminal.\n");
262 uart_term_putstr("\nFM\n");
263 uart_term_putstr(" -- Change to FM\n");
266 uart_term_putstr("\nAM\n");
267 uart_term_putstr(" -- Change to AM\n");
270 uart_term_putstr("\nPOFF [savef]\n");
271 uart_term_putstr(" -- Power OFF\n");
272 uart_term_putstr(" savef: if 'N' then power-off without saving.\n");
275 uart_term_putstr("\nPON\n");
276 uart_term_putstr(" -- Power ON\n");
279 uart_term_putstr("\nSEQ\n");
282 uart_term_putstr("\nLOG\n");
283 uart_term_putstr(" -- View LOG(Reserved command)\n");
286 uart_term_putstr("\nSCAN [direction]\n");
287 uart_term_putstr(" -- DO Scan\n");
288 uart_term_putstr(" direction : UP / DOWN\n");
291 uart_term_putstr("\nBAND [band-name]\n");
292 uart_term_putstr(" -- Set band\n");
295 uart_term_putstr("\nSET offset value\n");
296 uart_term_putstr(" -- Set system value.\n");
299 uart_term_putstr("\nPRINT offset\n");
300 uart_term_putstr(" -- PRINT system value.\n");
303 uart_term_putstr("\nEXIT\n");
304 uart_term_putstr(" -- EXIT shell\n");
311 static void cmd_help(char *p)
317 slen = shell_gettok(p, xarg1);
319 for(i = 0; i < SHELL_TOPCMDS; i++){
320 f = shell_strcmp(xarg1, cmdstr[i]);
327 uart_term_putstr("\nAvailable commands:\n");
328 for(i = 0; i < SHELL_TOPCMDS; i++){
329 uart_term_putstr(cmdstr[i]);
330 uart_term_putstr("\n");
332 uart_term_putstr("See Details : HELP [cmd].\n");
335 char term_shell(unsigned int timeout)
344 cmd_shellstr[0] = '\0';
347 while((uart_getstat() & UART_WAKEUP) == 0) {
348 if(t == 0) return SHELL_CMD_NONE;
353 if((uart_getstat() & UART_WAKEUP) == 0) return SHELL_CMD_NONE;
356 uart_term_putstr("\nOpen I2C Radio v2.0\n(C)2013- Kyuma Ohta\n");
357 uart_term_putstr("\n\nWelcome to shell mode (^^).\nPress help to show usage.\n");
359 uart_term_putstr("\n$>");
360 uart_term_getstr(cmd_shellstr, 128, 1); // With Echo
362 ii = shell_gettok(pool, cmd_shellstr);
363 // if(ii >= 128) return SHELL_CMD_TOOLONG;
364 if(ii >= 128) continue; // Discard
366 for(t = 0; t < SHELL_TOPCMDS; t++){
367 i = shell_strcmp((char *)cmdstr[t], pool);
370 if(cmd_shellstr[ii] == ' ') {
371 //if(i <= 0) return SHELL_CMD_NOTFOUND;
372 ii = shell_gettok(pool, &cmd_shellstr[ii + 1]);
383 term_printstatus(pool);
395 uart_term_putstr("\nBye... (^^)/~~\n");
400 uart_term_putstr("\n?? CMD Error\n");