Small addon to modify variables

This forum is about you. Feel free to discuss anything is related to embedded and electronics, your awesome projects, your ideas, your announcements, not necessarily related to ChibiOS but to embedded in general.
robert_o
Posts: 36
Joined: Fri Feb 17, 2017 11:25 pm
Has thanked: 1 time
Been thanked: 4 times

Small addon to modify variables

Postby robert_o » Thu Jan 04, 2018 1:05 pm

I often need to change variables via shell and it is a little annoing that there is no increase or decrease with just one button. I wrote a little function which does that. No need to patch anything just add the shell function to your shell command list.
For now there are four different functions (one for 16 and 32bit, signed and unsigned) other types are easily added.
Features:
+ increases by one
- decreases by one
u increases by 10
d decreases by 10
U increases by 100
D decreases by 100
* multiplies by 10
/ divides by 10
x exits

other modifiers are also easily added

Example:

Code: Select all

#include "ch.h"
#include "hal.h"
#include "chprintf.h" //to get rid of compiler warnings
#include "stdlib.h" //to get rid of compiler warnings
#include "shell.h"
uint16_t one = 10;
int16_t two = -22;
uint32_t three = 0xffffffff;
int32_t four = -645599;

/*===========================================================================*/
/* Command line related.                                                     */
/*===========================================================================*/
#define SHELL_WA_SIZE   THD_WORKING_AREA_SIZE(2048)
//#define TEST_WA_SIZE    THD_WORKING_AREA_SIZE(256)
void shellModifyu16(BaseSequentialStream *chp, uint16_t * modify_p) {
  chprintf(chp, "Welcome to modify u16\r\n");
  chprintf(chp, "press '+' to increase one, '-' to decrease one \r\n");
  chprintf(chp, "'u' to increase 10, 'd' to decrease 10 \r\n");
  chprintf(chp, "'U' to increase 100, 'D' to decrease 100 \r\n");
  chprintf(chp, "'*' to multiply with 10, '/' to divide by 10 \r\n");
  chprintf(chp, "press 'x' to exit \r\n");

  chprintf(chp, "Status of var is: %u\r\n", *modify_p);
  while (true) {
    char c;

    if (chSequentialStreamRead(chp, (uint8_t *)&c, 1) == 0)
      chThdSleepMilliseconds(250);

    if (c == '+') {
      (*modify_p)++;
    }
    if (c == '-') {
      (*modify_p)--;
    }
    if (c == 'u') {
      *modify_p += 10;
    }
    if (c == 'd') {
      *modify_p -= 10;
    }
    if (c == 'U') {
      *modify_p += 100;
    }
    if (c == 'D') {
      *modify_p -= 100;
    }
    if (c == '*') {
      *modify_p *= 10;
    }
    if (c == '/') {
      *modify_p /= 10;
    }
    if (c == 'x') {
      chprintf(chp, "EXIT\r\n");
      return;
    }
    if (c < 0x20)
      continue;
    chprintf(chp, "%u\r\n", *modify_p);
  }
}
void shellModifys16(BaseSequentialStream *chp, int16_t * modify_p) {
  chprintf(chp, "Welcome to modify s16\r\n");
  chprintf(chp, "press '+' to increase one, '-' to decrease one \r\n");
  chprintf(chp, "'u' to increase 10, 'd' to decrease 10 \r\n");
  chprintf(chp, "'U' to increase 100, 'D' to decrease 100 \r\n");
  chprintf(chp, "'*' to multiply with 10, '/' to divide by 10 \r\n");
  chprintf(chp, "press 'x' to exit \r\n");

  chprintf(chp, "Status of var is: %d\r\n", *modify_p);
  while (true) {
    char c;

    if (chSequentialStreamRead(chp, (uint8_t *)&c, 1) == 0)
      chThdSleepMilliseconds(250);

    if (c == '+') {
      (*modify_p)++;
    }
    if (c == '-') {
      (*modify_p)--;
    }
    if (c == 'u') {
      *modify_p += 10;
    }
    if (c == 'd') {
      *modify_p -= 10;
    }
    if (c == 'U') {
      *modify_p += 100;
    }
    if (c == 'D') {
      *modify_p -= 100;
    }
    if (c == '*') {
      *modify_p *= 10;
    }
    if (c == '/') {
      *modify_p /= 10;
    }
    if (c == 'x') {
      chprintf(chp, "EXIT\r\n");
      return;
    }
    if (c < 0x20)
      continue;
    chprintf(chp, "%d\r\n", *modify_p);
  }
}
void shellModifyu32(BaseSequentialStream *chp, uint32_t * modify_p) {
  chprintf(chp, "Welcome to modify u32\r\n");
  chprintf(chp, "press '+' to increase one, '-' to decrease one \r\n");
  chprintf(chp, "'u' to increase 10, 'd' to decrease 10 \r\n");
  chprintf(chp, "'U' to increase 100, 'D' to decrease 100 \r\n");
  chprintf(chp, "'*' to multiply with 10, '/' to divide by 10 \r\n");
  chprintf(chp, "press 'x' to exit \r\n");

  chprintf(chp, "Status of var is: %u\r\n", *modify_p);
  while (true) {
    char c;

    if (chSequentialStreamRead(chp, (uint8_t *)&c, 1) == 0)
      chThdSleepMilliseconds(250);

    if (c == '+') {
      (*modify_p)++;
    }
    if (c == '-') {
      (*modify_p)--;
    }
    if (c == 'u') {
      *modify_p += 10;
    }
    if (c == 'd') {
      *modify_p -= 10;
    }
    if (c == 'U') {
      *modify_p += 100;
    }
    if (c == 'D') {
      *modify_p -= 100;
    }
    if (c == '*') {
      *modify_p *= 10;
    }
    if (c == '/') {
      *modify_p /= 10;
    }
    if (c == 'x') {
      chprintf(chp, "EXIT\r\n");
      return;
    }
    if (c < 0x20)
      continue;
    chprintf(chp, "%u\r\n", *modify_p);
  }
}
void shellModifys32(BaseSequentialStream *chp, int32_t * modify_p) {
  chprintf(chp, "Welcome to modify s32\r\n");
  chprintf(chp, "press '+' to increase one, '-' to decrease one \r\n");
  chprintf(chp, "'u' to increase 10, 'd' to decrease 10 \r\n");
  chprintf(chp, "'U' to increase 100, 'D' to decrease 100 \r\n");
  chprintf(chp, "'*' to multiply with 10, '/' to divide by 10 \r\n");
  chprintf(chp, "press 'x' to exit \r\n");

  chprintf(chp, "Status of var is: %d\r\n", *modify_p);
  while (true) {
    char c;

    if (chSequentialStreamRead(chp, (uint8_t *)&c, 1) == 0)
      chThdSleepMilliseconds(250);

    if (c == '+') {
      (*modify_p)++;
    }
    if (c == '-') {
      (*modify_p)--;
    }
    if (c == 'u') {
      *modify_p += 10;
    }
    if (c == 'd') {
      *modify_p -= 10;
    }
    if (c == 'U') {
      *modify_p += 100;
    }
    if (c == 'D') {
      *modify_p -= 100;
    }
    if (c == '*') {
      *modify_p *= 10;
    }
    if (c == '/') {
      *modify_p /= 10;
    }
    if (c == 'x') {
      chprintf(chp, "EXIT\r\n");
      return;
    }
    if (c < 0x20)
      continue;
    chprintf(chp, "%d\r\n", *modify_p);
  }
}

static void cmd_status(BaseSequentialStream *chp, int argc, char *argv[]) {
  (void)argc;
  (void)* argv;
  chprintf(chp, "Status of Variables is: %u %d %u %d\r\n", one, two, three, four);
}

static void cmd_modify(BaseSequentialStream *chp, int argc, char *argv[]) {
  const char * const usage = "Usage: ch NUMBER\r\n";

  if (argc != 1) {
    chprintf(chp, usage);
    return;
  }
  //uint16_t nr = (uint16_t)atoi(argv[0]);

  switch (atoi(argv[0])){
  case 0:
    shellModifyu16(chp, &one);
    break;
  case 1:
    shellModifys16(chp, &two);
    break;
  case 2:
    shellModifyu32(chp, &three);
   break;
  case 3:
    shellModifys32(chp, &four);
    break;
  default:
    chprintf(chp, "something went wrong\r\n");
    break;
  }
}

static const ShellCommand commands[] = {
  {"stat", cmd_status},
  {"mod", cmd_modify},
  {NULL, NULL}
 };

static const ShellConfig shell_cfg1 = {
  (BaseSequentialStream *)&SD1,
  commands
};

/*
 * Blinker thread #1.
 */
static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg) {

  (void)arg;

  chRegSetThreadName("blinker");
  while (true) {
    palSetPad(GPIOC, GPIOC_LED4);
    chThdSleepMilliseconds(250);
    palClearPad(GPIOC, GPIOC_LED4);
    chThdSleepMilliseconds(250);
  }
}

/*
 * Application entry point.
 */
int main(void) {
  thread_t *shelltp = NULL;
  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();
  /*
   * Shell manager initialization.
   */
  shellInit();
  /*
   * Activates the serial driver 1 using the driver default configuration.
   * PA9(TX) and PA10(RX) are routed to USART1.
   */
  sdStart(&SD1, NULL);
  chprintf((BaseSequentialStream *)&SD1, "\033[2J\033[1;1H");
  chprintf((BaseSequentialStream *)&SD1, "ChibiOS Disco F100 Shell v0.2\r\n");
  /*
   * Creates the example threads.
   */
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO+1, Thread1, NULL);
  /*
   * Normal main() thread activity, in this demo it does nothing except
   * sleeping in a loop and check the button state, when the button is
   * pressed the test procedure is launched.
   */
  while (true) {
    if (!shelltp) {
              /* Spawns a new shell.*/
              shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO+10);
          }
      if (chThdTerminatedX(shelltp)) {
         /* Recovers memory of the previous shell.*/
         chThdRelease(shelltp);
         shelltp = NULL;
       }
    chThdSleepMilliseconds(500);
  }
}

Return to “User Projects”

Who is online

Users browsing this forum: No registered users and 2 guests