bitlash - how to deceleration input string to function

Hi,

I’m using bitlash.
Why this code doesn’t recognize the input “?” symbol?

void io(int num,  char state) {
  
      int digital_value;
      num = getarg(1);
      state = getarg(2);
      
        switch (state){
          
         case 0:
         pinMode(num,1);
         digitalWrite(num,0);  
         break;
 
         case 1:
         pinMode(num,1);
         digitalWrite(num,1); 
         break;
 
         case '?':
         pinMode(num,INPUT);   
         digital_value = digitalRead(num);
         Serial.println(digital_value); 
         break;

So in terminal I type for example: io(33,?)
I get: unexpected char

Thanks in advance.

Why did you not post all of your code ?

tried io(33, '?') with quotes?

disclaimer: no bitlash user/expert.

robtillaart: tried io(33, '?') with quotes?

disclaimer: no bitlash user/expert.

Thanks robtillaart that's work!

There is any way that it can work without '?' io(33, '?') just io(33, ?) without ' '

So my next question is:

How can I recognize string ? for example:

         case 'abc':
         pinMode(num,INPUT);   
         digital_value = digitalRead(num);
         Serial.println(digital_value); 
         break;

What I need to config in the function ? void io(int num, string state) ? char * ?

How can I recognize string ?

It will be enclosed in double quotes, and cannot be used in a switch/case..

In this way?

If (strcmp(state,  "abc")  == 0)

What I need to definition in the function? How to declare the string?

void mx_io(int num, char* state) {
  
      int digital_value;
      
        if (strcmp(state, "0") == 0) {
           pinMode(num,1);
           digitalWrite(num,0);  
           Serial.println("done");
        }

        else if (strcmp(state, "abc") == 0) {
           pinMode(num,1);
           digitalWrite(num,1); 
           Serial.println("abc");
        }

        else if (strcmp(state, "?") == 0 ) {      
           pinMode(num,INPUT);   
           digital_value = digitalRead(num);
           Serial.println(digital_value); 
        }         
        
        else
        {
          Serial.println("problem");
        }
}

When I run this I got:

> mx_io(33,"abc")
problem
> mx_io(33,'abc')
'
--------------^
expected >

I tried “abc” and ‘abc’ and none of them works.

Please try this variation that print out state and its length

void mx_io(int num, char* state) {
  
      int digital_value;

       if (strcmp(state, "0") == 0) {
           pinMode(num,1);
           digitalWrite(num,0);  
           Serial.println("done");
        }

        else if (strcmp(state, "abc") == 0) {
           pinMode(num,1);
           digitalWrite(num,1); 
           Serial.println("abc");
        }

        else if (strcmp(state, "?") == 0 ) {      
           pinMode(num,INPUT);   
           digital_value = digitalRead(num);
           Serial.println(digital_value, DEC); 
        } 
        else
        {
         Serial.println(len(state));
         Serial.println(state);
         Serial.println("problem");
        }
}

This option not compile:

         Serial.println(len(state));

Maybe because the char* state?

How to config input string for function by bitlash?

I tried "abc" and 'abc'.

BTW this return ' symbol

         Serial.println(state);

len should be strlen sorry my mistake, did a lot of python lately :)

Thanks robtillaart

When I tried "abc" the output strlen=0.

So what I need to do?

That means the pointer state points to nowhere...

do the "0" and the "?" work ?

Nope. :disappointed_relieved:

that means that char* state parameter does not work how you intended it.

I think this is caused by these missing lines

num = getarg(1); state = getarg(2);

These somehow take care that the parameters are passed. where is getarg() defined? And what does it return?

What I need to define to fix this issue ?

Please use my code above.

Thanks.

      num = getarg(1);
      state = getarg(2);

I got compile error:

error: invalid conversion from 'numvar' to 'char*'

I'm out of options (as I wont dive into bitlash depths), Sorry

Nobody knows how to declaration char* string input to function in bitlash? :|

Getarg returns a numvar, but you can cast the result of getarg to another type:

    char *foo = (char *) getarg(2);

-br