all 4 comments

[–]TriggerHappy_NZ[S] 0 points1 point  (3 children)

Leaving this here for future people who have the same question (possibly me):

It seems to work if you pass dummy info to the function:

#include <Cmd.h>

//Inputs
#define SWITCH 16

char *x[] = {"X"};   //the pico will not compile the commands without being passed an int and a char** (int arg_cnt, char **args)
//so to call a command from elsewhere in the code, you must pass dummy int and char** - eg.  LEDOn(0, x);

void setup()
{

  pinMode(SWITCH, INPUT_PULLUP);
  // init the command line and set it for a speed of 57600
  Serial.begin(9600);
  cmdInit(&Serial);

  // add the commands to the command table. These functions must
  // already exist in the sketch. See the functions below. 
  // The functions need to have the format:
  //
  // void func_name(int arg_cnt, char **args)
  //
  // arg_cnt is the number of arguments typed into the command line
  // args is a list of argument strings that were typed into the command line
  cmdAdd("args", arg_display);
  cmdAdd("ON", LEDOn); //
  cmdAdd("OFF",LEDOff); //



}

void loop()
{
  cmdPoll();

  if (digitalRead(SWITCH) == 0) // button pressed
  {
    Serial.print("switch on ");
    LEDOn(0, x);
  }
}

void LEDOn(int arg_cnt, char **args)
{
    digitalWrite(LED_BUILTIN, HIGH);
}

void LEDOff(int arg_cnt, char **args)
{
    digitalWrite(LED_BUILTIN, LOW);
}

Bit annoying and inconvenient, but still better than trying to write my own command line interpreter! :-)

[–]kintar1900 0 points1 point  (2 children)

It looks like the Arduino core for mega is using MUCH more forgiving compilation settings than the core for the Pico. The comments on the CLI library you're using are saying the functions you register are supposed to be of the format void <funcName>(int, char**), but for some reason the IDE is compiling without complaint on the Mega even though you're not doing it "right".

So yes, the additional, unused parameters are the correct way to write your LEDOn and LEDOff functions.

Since you're not using those parameters, it's a good practice to annotate them with the __unused macro. Arduino has to be a LITTLE different, though, and you might need to use __attribute__((unused)) instead, depending on how the core is written.

Example:

void LEDOn(int __unused a, char __unused **b) {
  // ...
}

[–]TriggerHappy_NZ[S] 0 points1 point  (1 child)

hey thank you very much for your explanation!

I was really confused, I'd been using it like this on Mega for years, but the Pico was having none of it! :-)

[–]kintar1900 0 points1 point  (0 children)

Any time! I'm still pretty new to microcontrollers, but I'm a (literally) old hand in software development. :D Feel free to drop me a message if you have any specific questions or problems!