OSDN Git Service

[SCHEMATIC] Modify SW/MW/LW Preamp, insert galbanic-isolator replace of common-mode...
[openi2cradio/OpenI2CRadio.git] / uart_termio.c
1 /*
2  * OpenI2CRADIO
3  * EUSART Handler
4  * Copyright (C) 2013-11-07 K.Ohta <whatisthis.sowhat ai gmail.com>
5  *
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.
14  *
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,
18  *  MA 02110-1301, USA.
19  *
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.
26  */
27
28 #if defined(__SDCC)
29 #include <sdcc-lib.h>
30 #include <pic18fregs.h> /* ONLY FOR PIC18x */
31 #include <delay.h>
32 #else
33 #include <xc.h>
34 #endif
35 #include <signal.h>
36
37 #include "iodef.h"
38 #include "idle.h"
39 #include "ioports.h"
40 #include "euart.h"
41 #include "menu.h"
42 #include "uart_termio.h"
43 #include "shell_strutl.h"
44 #include "power.h"
45
46 unsigned char uart_term_putstr(unsigned char *s)
47 {
48     unsigned char p = 0;
49     if((uart_getstat() & UART_WAKEUP) == 0) return 0; // Error
50     while(s[p] != 0x00)
51     {
52         uart_pushchar(s[p], 200); // Timeout = 10ms
53         p++;
54         if(p >= 255) return 0; // Overlen
55     }
56     return 0xff;
57 }
58
59 void uart_term_getstr(unsigned char *s, unsigned int timeout, unsigned char echo)
60 {
61     unsigned int i = 0;
62     unsigned char c = 0x00;
63     unsigned int tim;
64     unsigned char pwr;
65     unsigned char cnt;
66     tim = 0;
67     cnt = 0;
68     while(1){
69         ClrWdt();
70         c = uart_pullchar();
71         if(c != 0x00) {
72             if(cnt > 10) {
73                 if(chk_powerbutton() != 0) {
74                     shutdown(0xff);
75                 }
76                cnt = 0;
77             }
78             cnt++;
79             if(echo != 0) uart_pushchar(c, 200); // Echoback
80             if(c == '\b') { // BS
81                 if(i > 0) {
82                    s[i] = '\0';
83                    i--;
84                 }
85             } else
86             if(c < 0x80) {
87                 s[i] = c;
88                 i++;
89                 if((c == '\t') || (c == '\n') || (c == '\r')) break; // TAB OR CR
90                 if(i >= 128) break;
91             }
92             tim = 0;
93        } else if(timeout != 0) { // ZAP n * 100mSec.
94             if(chk_powerbutton() != 0) shutdown(0xff); 
95             idle_time_ms(100 - 48); // Poll 0.1Sec
96             tim++;
97             if(tim > timeout) {
98                s[0] = TERM_CHAR_TIMEOUT;
99                i = 1;
100                break;
101             }
102        } else {
103           idle_time_ms(100 - 48); // Wait 100ms if none (and no Timeout).
104          if(chk_powerbutton() != 0) shutdown(0xff); 
105        }
106     }
107     s[i] = '\0';
108 }
109
110
111