ppedro74 / Arduino-SerialCommands

BSD 3-Clause "New" or "Revised" License
87 stars 29 forks source link

handle input editing #11

Open GralfR opened 3 years ago

GralfR commented 3 years ago

With putty as client SerialCommands has problems matching commands when using backspace during input.

As an example with Debug-mode enabled enter: 1 2 3 4 [BACKSPACE] [ENTER]

The expexted input command should be "123" and SerialCommands does try to compare that. But it does not match to a command "123". If You just enter

1 2 3 [ENTER] without editing the input, the same command "123" is matched and the function for that command is executed.

There seams to be an additional non-printable char inside the string.

GralfR commented 3 years ago

I think this problem only occurs when using an echo the way I did in issue #10. So I've extended the code to enable a simple input-editing supporting:

Inside the SerialCommands.cpp in the ReadSerial() after int ch = serial_->read(); I've added:

    if (isPrintable(ch)) {serial_->write(ch);} //ECHO
    switch (ch) { 
      case 8:   //Backspace
      case 127: //Backspace
        if (buffer_pos_>0) {serial_->write(ch);} //ECHO BACKSPACE
        buffer_[buffer_pos_]='\0';  //delete last char
        if (buffer_pos_-- < 0) {buffer_pos_=0;} //do not delete before start of line
        continue;
        break;

      case 27:  //ESC
        buffer_[0]='\e';
        if (CheckOneKeyCmd()) {return SERIAL_COMMANDS_SUCCESS;}
        break;
    }

I've also added a second buffer to store the last input in. So the last input can be recalled with the TAB-key. Because this is my second project with Arduino (I programmed AVR with assembler before) using this expansion makes it incompatible with other/public sources. So I do not like to make it public, yet, but if anybody is interested in writing a proper pull-request, please ask.