Command Shell: exit from a loop

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

User avatar
Cesare
Posts: 36
Joined: Tue Jul 11, 2017 11:51 am
Location: Milan, Italy
Has thanked: 3 times
Been thanked: 3 times

Command Shell: exit from a loop

Postby Cesare » Thu Feb 15, 2018 2:17 pm

Dear ChibiOs users,
I do an extensive use of the shell both for debug and user configuration of the application.
My aim is to print live data writing continuously into the serial device, something like this:

Code: Select all

void MyLiveCommand(BaseSequentialStream *chp, int argc, char *argv[])
{
   bool stop = 0;

   while(!stop)
      {
         chprintf(chp, "live application info\r\n");
         chThdSleepMilliseconds(500);
      }
}


Question: is there any way in order to set stop=TRUE from the command line typing, for instance, CTRL+C ?

The description of the shell function shellGetLine says:

Input chars are echoed on the same stream object with the
* following exceptions:
* - DEL and BS are echoed as BS-SPACE-BS.
* - CR is echoed as CR-LF.
* - 0x4 is echoed as "^D".
* - Other values below 0x20 are not echoed.


So it seems there isn't any possibility to detect extra keys combination plus a mechanism to exit from the loop.

I saw there is the symbol SHELL_CMD_EXIT_ENABLED but seems it is not related to my need.

Thank you in advance.

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Command Shell: exit from a loop

Postby Giovanni » Thu Feb 15, 2018 2:52 pm

From the USB demo:

Code: Select all

static void cmd_write(BaseSequentialStream *chp, int argc, char *argv[]) {
  static uint8_t buf[] =
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
      "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";

  (void)argv;
  if (argc > 0) {
    chprintf(chp, "Usage: write\r\n");
    return;
  }

  while (chnGetTimeout((BaseChannel *)chp, TIME_IMMEDIATE) == Q_TIMEOUT) {
#if 1
    /* Writing in channel mode.*/
    chnWrite(&PORTAB_SDU1, buf, sizeof buf - 1);
#else
    /* Writing in buffer mode.*/
    (void) obqGetEmptyBufferTimeout(&PORTAB_SDU1.obqueue, TIME_INFINITE);
    memcpy(PORTAB_SDU1.obqueue.ptr, buf, SERIAL_USB_BUFFERS_SIZE);
    obqPostFullBuffer(&PORTAB_SDU1.obqueue, SERIAL_USB_BUFFERS_SIZE);
#endif
  }
  chprintf(chp, "\r\n\nstopped\r\n");
}


It requires "chp" to be a pointer to a descendant of "BaseChannel", you may also check for the returned character.

Giovanni

User avatar
Cesare
Posts: 36
Joined: Tue Jul 11, 2017 11:51 am
Location: Milan, Italy
Has thanked: 3 times
Been thanked: 3 times

Re: Command Shell: exit from a loop

Postby Cesare » Thu Feb 15, 2018 4:12 pm

Oh yes! Why I couldn't think about this solution earlier :roll:

I tried and it works perfectly, I also found out I can use chnReadTimeout() when I need to intercept some particular sequence of chars.

Thank you Giovanni


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 54 guests