repl.cpp 5.28 KB
Newer Older
1
2
3
/*-----------------------------------------------------------------------------
 *  Author: Nelso G. Jost (nelsojost@gmail.com)
 *  License: GPLv2
4
 *  Purpose: Provide basic GPIO control commands for the REPL.
5
6
7
8
9
10
11
12
13
 *---------------------------------------------------------------------------*/
#include <Arduino.h>

#include "repl.h"


REPL::REPL(REPL_COMMAND * command_map)
{
    this->_fp_map = command_map;
14
    this->_history[0] = "";
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
}


void REPL::run(void)
{
    char c;
    while (1)
    {
        while (!Serial.available()) { yield(); }
        c = Serial.read();

        if (c == 8)
            this->_backspace();

        else if (c == 9)
            this->_clear_line();

        else if (c == '[')
            this->_history_previous();

35
        else if (c == ']')
36
37
38
39
            this->_history_next();

        else
        {
40
            this->_command_buffer += c;
41
42
43
44
45
            if (c != 13) Serial.write(c);
        }

        //Serial.println(int(c));

46
47
        if (this->_command_buffer.indexOf("\r") != -1 ||
            this->_command_buffer.indexOf("\n") != -1)
48
        {
49
50
51
            this->_command_buffer.trim();
            if (this->_command_buffer == "help") this->print_help();
            else if (this->_command_buffer != "") this->_execute_command();
52
            if (c != 13) Serial.print(this->prompt + " ");
53
            this->_command_buffer = "";
54
55
56
57
58
59
60
61
62
63
64
65
        }
        Serial.flush();
    }
}


void REPL::_clear_line(void)
{
    Serial.print("\r                                         "
                 "                                           "
                 "                                         \r");
    Serial.print("> ");
66
    this->_command_buffer = "";
67
68
69
70
}

void REPL::_backspace(void)
{
71
    if (this->_command_buffer.length() != 0)
72
73
74
75
    {
        Serial.write(8);
        Serial.write(32);
        Serial.write(8);
76
77
        this->_history_index = 0;
        this->_command_buffer.remove(this->_command_buffer.length() - 1);
78
79
80
81
82
83
    }
}


void REPL::_history_previous(void)
{
84
85
86
87
88
89
90
91
92
93
    if (this->_history_index < HISTORY_MAX - 1 && \
        this->_history[this->_history_index] != "")
    {
        this->_command_buffer = this->_history[this->_history_index];
        this->_history_index++;
        Serial.print("\r                                         \r");
        Serial.print(this->prompt + " [" + this->_history_index + "] ");
        Serial.print(this->_command_buffer);
    }
    //this->log("history previous" + String(this->_history_index));
94
95
96
97
98
}


void REPL::_history_next(void)
{
99
100
101
102
103
104
105
106
107
108
109
    if (this->_history_index > 0 && \
        this->_history[this->_history_index] != "")
    {
        this->_command_buffer = this->_history[this->_history_index];
        this->_history_index--;
        Serial.print("\r                                         \r");
        Serial.print(this->prompt + " [" + this->_history_index + "] ");
        Serial.print(this->_command_buffer);
    }
    //this->log("history next" + String(this->_history_index));
}
110

111
112
113
114
115
116
117
118
119
120
121
void REPL::_history_save(void)
{
    this->_history_index = 0;
    if (this->_history[0] != this->_command_buffer)
    {
        for (int i=0; i < HISTORY_MAX - 2; i++)
        {
            this->_history[i+1] = this->_history[i];
        }
        this->_history[0] = this->_command_buffer;
    }
122
123
124
125
126
}


int REPL::_execute_command(void)
{
127
    String command_name = get_arg(this->_command_buffer, 0);
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
    bool show_help=false;

    if (command_name.endsWith("?"))
    {
        show_help = true;
        command_name.remove(command_name.length() - 1);
    }

    //Serial.print("\n[INFO] executing command \"");
    //Serial.print(command_str);
    //Serial.print("\"");

    REPL_COMMAND * rc = this->_fp_map;

    while (rc->name != NULL)
    {
        if (command_name == rc->name)
        {
             if (show_help)
             {
                 Serial.print("\n\n[HELP] Syntax: ");
                 Serial.println(rc->prototype);
                 Serial.print("\n    ");
                 Serial.println(rc->help);
                 return 0;
             }
154
155
             rc->func(this);
             this->_history_save();
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
             return 0;
        }
        rc++;
    }

    Serial.print("\n[ERROR] invalid command \"");
    Serial.print(command_name);
    Serial.print("\"");

    return -1;
}


void REPL::print_help(void)
{
    REPL_COMMAND * rc = this->_fp_map;
    Serial.print("\n------------------");
    Serial.print("\nAvailable commands");
    Serial.print("\n------------------");
    while (rc->name != NULL)
    {
        Serial.print("\n  ");
        Serial.print(rc->prototype);
        rc++;
    }
    Serial.print("\n------------------");
    Serial.print("\nType \"command?\" for more details on the command.");
    Serial.print("\n------------------");
}


void REPL::log(String msg)
{
    Serial.print("\n[LOG] " + msg + "\n" + this->prompt + " ");
190
    if (this->_command_buffer.length() > 0)
191
    {
192
        Serial.print(this->_command_buffer);
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
    }
}


String get_arg(String command, int index)
{
    String arg="";
    int count=-1;
    bool found=false;
    for (int i=0; i < command.length(); i++)
    {
        if (command[i] != ' ')
        {
            found = true;
            arg += command[i];
        }
        else if (found == true)
        {
            count += 1;
            if (count == index) return arg;
            found = false;
            arg = "";
        }
    }
}