Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
29 jimmy 1
/******************************************************************************
2
 * Copyright (C) 2005 Martin THOMAS, Kaiserslautern, Germany
3
 * <eversmith@heizung-thomas.de>
4
 * http://www.siwawi.arubi.uni-kl.de/avr_projects
5
 *****************************************************************************
6
 *
7
 * File    : terminal.c
8
 * Version : 0.9
9
 *
10
 * Summary : Terminal
11
 *
12
 *****************************************************************************/
13
 
14
#include <stdlib.h>
15
#include <stdio.h>
16
#include <string.h>
17
#include <avr/pgmspace.h>
18
 
19
#include "termio.h"
20
#include "terminal.h"
21
 
22
#include "debughelper.h"
23
#include "gpio.h"
24
#include "mcp2515.h"
25
 
26
#define CEOS        '\0'
27
#define CENTER      '\r'
28
#define CNEWLINE    '\n'
29
#define CBS         '\b'
30
#define CBLANK      ' '
31
 
32
#define CMAXCL          50
33
#define strncmp_flash(sram,sflash,n) strncmp_P(sram,PSTR(sflash),n)
34
 
35
#define GET_TERMSTRING_STATE_INIT    (0)
36
#define GET_TERMSTRING_STATE_SAMPLE  (1)
37
#define GET_TERMSTRING_STATE_RETURN  (2)
38
#define GET_TERMSTRING_STATE_BREAK   (3)
39
 
40
uint8_t gTimedSend = CANAUTOOFF;
41
 
42
static void ShowHelp(void)
43
{
44
    term_puts_P("exit - exit from shell\n");
45
    term_puts_P("show sys - print system values\n");
46
    term_puts_P("led on|off - all LEDs on/off\n");
47
    term_puts_P("led toogle num - toogle LED num (1-3)\n");
48
    term_puts_P("mcp stat - show mcp status byte\n");
49
    term_puts_P("mcp error - show mcp error counters and error flag\n");
50
    term_puts_P("can read - print all available messages\n");
51
    term_puts_P("can send ext|std num - send can mesg std. or extended with 3rd byte=num\n");
52
    term_puts_P("can timed on|off - (de)activte sending messages (to debug receiver)\n");
53
    term_puts_P("can auto on|off - (de)activte autoprocessing of incoming messages\n\n");
54
 
55
}
56
 
57
static void DumpConfigValues(void)
58
{
59
    term_puts_P("Config-Dump :\n");
60
    term_puts_P("Auto Processing of CAN-Message: ");
61
    if (gCANAutoProcess) {
62
        term_puts_P("On");
63
    }
64
    else {
65
        term_puts_P("Off");
66
    }
67
    term_puts_P("\n");
68
 
69
    term_puts_P("Auto Send of CAN-Message: ");
70
    (gTimedSend) ? term_puts_P("On") : term_puts_P("Off");
71
    term_puts_P("\n");
72
}
73
 
74
static void GetConsoleString(uint8_t *state, char* s, uint8_t mxc)
75
{
76
    static uint8_t len; // current length
77
 
78
    uint16_t getcres;
79
    uint8_t c;
80
 
81
    if (*state == GET_TERMSTRING_STATE_INIT ) {
82
        *state = GET_TERMSTRING_STATE_SAMPLE;
83
        s[0] = CEOS;
84
        len = 0;
85
    }
86
 
87
    getcres = term_getc();
88
 
89
    if ( (getcres & 0xff00) == 0 ) {
90
        // got a character
91
        //Debug_ByteToUart_p(PSTR("getMSB"), getcres>>8);
92
        //Debug_ByteToUart_p(PSTR("getLSB"), getcres & 0xff);
93
        c = (uint8_t) (getcres & 0x00ff);
94
        switch (c) {
95
        case CBS:
96
            if (len>0) {
97
                s[--len]=CEOS;
98
                term_putc(CBS);
99
                term_putc(CBLANK);
100
                term_putc(CBS);
101
            }
102
            break;
103
        case CENTER:
104
            s[len]=CEOS;
105
            // term_putc(CNEWLINE);
106
            *state = GET_TERMSTRING_STATE_RETURN;
107
            break;
108
        default:
109
            if (len<mxc-1) {
110
                s[len++]=c;
111
                term_putc(c);
112
            }
113
            break;
114
        }
115
    }
116
}
117
 
118
static char* GetCLParmStr(char cl[], uint8_t n)
119
{
120
    uint8_t i=0, ac=0;
121
 
122
    // skip leading whitepaces if any
123
    while( (cl[i]!= CEOS) && (cl[i]==CBLANK)) i++;
124
 
125
    // go to n-th arg (0 is the command)
126
    for(ac=0; ac<n; ac++)
127
    {
128
        // find first character of argument
129
        while( (cl[i]!= CEOS) && (cl[i]!=CBLANK)) i++;
130
        // skip whitespaces separating arguments
131
        while( (cl[i]!= CEOS) && (cl[i]==CBLANK)) i++;
132
    }
133
    // return address of first character in n-th argument
134
    return &cl[i];
135
}
136
 
137
#define ERRUNKNOWN 2
138
static void TellError(uint8_t ecode)
139
{
140
    switch (ecode) {
141
    case (ERRUNKNOWN):
142
        term_puts_P("Command or Option unknown\n");
143
        break;
144
    default:
145
        term_puts_P("Invalid Error-Code\n");
146
        break;
147
    }
148
}
149
 
150
void CommandShell(uint8_t *state)
151
{
152
    static uint8_t cl[CMAXCL];
153
    static uint8_t getState;
154
 
155
    CanMessage msg;
156
    uint8_t i;
157
    // char *s;
158
 
159
    if (*state == COMMANDSHELL_MODEINIT) {
160
        term_puts_P("\nMTSHELL>");
161
        getState = GET_TERMSTRING_STATE_INIT;
162
        *state = COMMANDSHELL_MODERUN;
163
    }
164
 
165
    else if (*state == COMMANDSHELL_REDRAW) {
166
        term_puts_P("\nMTSHELL>");
167
        term_puts(cl);
168
        *state = COMMANDSHELL_MODERUN;
169
    }
170
 
171
    else {
172
        GetConsoleString(&getState,cl,CMAXCL);
173
    }
174
 
175
    if (getState == GET_TERMSTRING_STATE_RETURN) {
176
 
177
        getState = GET_TERMSTRING_STATE_INIT;
178
        *state = COMMANDSHELL_MODEINIT;
179
 
180
        term_put_cr();
181
 
182
        if ( !strncmp_flash(cl,"help",4) ) {
183
            ShowHelp();
184
        }
185
 
186
        else if ( !strncmp_flash(cl,"exit",4) ) {
187
            term_puts_P("\nShell exit\n");
188
            *state = COMMANDSHELL_MODEEXIT;
189
        }
190
 
191
        else if ( !strncmp_flash(cl,"show",4) ) {
192
            if (!strncmp_flash(GetCLParmStr(cl,1),"sys",3)) {
193
                DumpConfigValues();
194
            }
195
            else {
196
                TellError(ERRUNKNOWN);
197
            }
198
        }
199
 
200
        else if ( !strncmp_flash(cl,"led",3) ) {
201
            if (!strncmp_flash(GetCLParmStr(cl,1),"off",2)) {
202
                gpio_clear_led ( LED0 | LED1 | LED2 );
203
            }
204
            else if (!strncmp_flash(GetCLParmStr(cl,1),"on",2)) {
205
                gpio_set_led ( LED0 | LED1 | LED2 );
206
            }
207
            else if (!strncmp_flash(GetCLParmStr(cl,1),"toggle",1)) {
208
                i=atoi(GetCLParmStr(cl,2));
209
                if (i<4) {
210
                    i = (1<<i);
211
                    // Debug_ByteToUart_P("toogle",i);
212
                    gpio_toggle_led ( i );
213
                }
214
            }
215
        }
216
 
217
        else if ( !strncmp_flash(cl,"mcp",3) ) {
218
            if (!strncmp_flash(GetCLParmStr(cl,1),"stat",3)) {
219
                Debug_ByteToUart_p(PSTR("MCP-Status-Byte"),mcp2515_readStatus());
220
            }
221
            else if (!strncmp_flash(GetCLParmStr(cl,1),"error",3)) {
222
                Debug_ByteToUart_p(PSTR("MCP Transmit Error Count"),
223
                    mcp2515_readRegister(MCP_TEC));
224
                Debug_ByteToUart_p(PSTR("MCP Receiver Error Count"),
225
                    mcp2515_readRegister(MCP_REC));
226
                Debug_ByteToUart_p(PSTR("MCP Error Flag"),
227
                    mcp2515_readRegister(MCP_EFLG));
228
            }
229
            else TellError(ERRUNKNOWN);
230
        }
231
 
232
        else if ( !strncmp_flash(cl,"can",3) ) {
233
            if (!strncmp_flash(GetCLParmStr(cl,1),"read",4)) {
234
                while ( can_readMessage(&msg) == CAN_OK ) {
235
                    can_dumpMessage(&msg);
236
                }  
237
            }
238
            else if (!strncmp_flash(GetCLParmStr(cl,1),"send",3)) {
239
                i=atoi(GetCLParmStr(cl,3));
240
                if (!strncmp_flash(GetCLParmStr(cl,2),"std",3)) {
241
                    can_testTransmit(CAN_STDID, i);
242
                }
243
                else if (!strncmp_flash(GetCLParmStr(cl,2),"ext",3)) {
244
                    can_testTransmit(CAN_EXTID, i);
245
                }
246
                else {
247
                    TellError(ERRUNKNOWN);
248
                }
249
            }
250
 
251
            else if (!strncmp_flash(GetCLParmStr(cl,1),"auto",3)) {
252
                if (!strncmp_flash(GetCLParmStr(cl,2),"off",2)) {
253
                    gCANAutoProcess = CANAUTOOFF;
254
                }
255
                else if (!strncmp_flash(GetCLParmStr(cl,2),"on",2)) {
256
                    gCANAutoProcess = CANAUTOON;
257
                }
258
                else {
259
                    TellError(ERRUNKNOWN);
260
                }
261
            }
262
 
263
            else if (!strncmp_flash(GetCLParmStr(cl,1),"timed",3)) {
264
                if (!strncmp_flash(GetCLParmStr(cl,2),"off",2)) {
265
                    gTimedSend = CANAUTOOFF;
266
                }
267
                else if (!strncmp_flash(GetCLParmStr(cl,2),"on",2)) {
268
                    gTimedSend = CANAUTOON;
269
                }
270
                else {
271
                    TellError(ERRUNKNOWN);
272
                }
273
            }
274
 
275
            else TellError(ERRUNKNOWN);
276
        }
277
 
278
        else {
279
            TellError(ERRUNKNOWN);
280
        }
281
 
282
        cl[0] = CEOS;
283
 
284
    } // endif enter-key
285
}
286