repl.cpp 5.75 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
void REPL::update(void)
19
20
21
{
    char c;

22
    if (!Serial.available()) { yield(); return; }
23

24
    c = Serial.read();
25

26
27
    if (c == 8)
        this->_backspace();
28

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

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

35
36
37
38
39
40
41
42
43
44
    else if (c == ']')
        this->_history_next();

    else if (c == 32 && !this->_command_buffer.length())
        return;
    else
    {
        this->_command_buffer += c;
        if (c != 13) Serial.write(c);
    }
45

46
47
48
49
50
51
52
    //Serial.println(int(c));

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


void REPL::_clear_line(void)
{
66
67
68
69
70
71
    if (this->_command_buffer.length())
    {
        this->_complete();
        return;
    }
    Serial.print("\r                                         \r");
72
    Serial.print("> ");
73
    this->_command_buffer = "";
74
75
}

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

void REPL::_complete(void)
{
    String partial_name = this->get_arg(0);
    REPL_COMMAND * rc = this->_fp_map;
    String full_name;

    while (rc->name != NULL)
    {
        full_name = String(rc->name);
        if (full_name.startsWith(partial_name))
        {
             Serial.print("\n\n[HELP] " + String(rc->prototype) + "\n" +
                          this->prompt + " " + full_name + " ");
             this->_command_buffer = full_name + " ";
        }
        rc++;
    }
}


97
98
void REPL::_backspace(void)
{
99
    if (this->_command_buffer.length() != 0)
100
101
102
103
    {
        Serial.write(8);
        Serial.write(32);
        Serial.write(8);
104
105
        this->_history_index = 0;
        this->_command_buffer.remove(this->_command_buffer.length() - 1);
106
107
108
109
110
111
    }
}


void REPL::_history_previous(void)
{
112
113
114
115
116
117
118
119
120
121
    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));
122
123
124
125
126
}


void REPL::_history_next(void)
{
127
128
129
130
131
132
133
134
135
136
137
    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));
}
138

139

140
141
142
143
144
145
146
147
148
149
150
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;
    }
151
152
153
154
155
}


int REPL::_execute_command(void)
{
156
    String command_name = get_arg(0);
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
    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;
             }
183
184
             rc->func(this);
             this->_history_save();
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
             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------------------");
}


216
void REPL::log(String msg, bool show_buffer)
217
{
218
219
    Serial.print(msg);
    if (this->_command_buffer.length() > 0 and show_buffer)
220
    {
221
        Serial.print("\n" + this->prompt + " " + this->_command_buffer);
222
223
224
225
    }
}


226
String REPL::get_arg(int index)
227
228
229
230
{
    String arg="";
    int count=-1;
    bool found=false;
231
    for (int i=0; i < this->_command_buffer.length(); i++)
232
    {
233
        if (this->_command_buffer[i] != ' ')
234
235
        {
            found = true;
236
            arg += this->_command_buffer[i];
237
238
239
240
241
242
243
244
245
246
        }
        else if (found == true)
        {
            count += 1;
            if (count == index) return arg;
            found = false;
            arg = "";
        }
    }
}