Page 1 of 1

Arithmetic in GPIO command with dummy values [solved]

Posted: 27 Nov 2020, 04:48
by mrh_icb
I have a simple rule which sets 2 GPIOs to opposite polarity.
I am using a dummy device CFG with a value inv0 to configure the polarity, like this:

Code: Select all

gpio 0,1-[CFG#inv0]
gpio 2,[CFG#inv0]
CFG#inv0 is either 0 or 1, The second GPIO command works as expected, however the expression in the first GPIO command always evaluates to 1. I also tried brackets. What could be the problem?

Re: Arithmetic with dummy values

Posted: 27 Nov 2020, 05:05
by mrh_icb
Here is corresponding log output. CFG#inv0 was 1, so I'd expect GPIO-0 to be set to 0, but it ended up a 1:

Code: Select all

15473617: ACT : gpio 0,1-1
15473619: GPIO : port#0: set to 1
15473622: ACT : gpio 2,1
15473624: GPIO : port#2: set to 1
15473710: SW : GPIO=0 State=1 Output value=1
15473746: SW : GPIO=2 State=1 Output value=1

Re: Arithmetic with dummy values

Posted: 27 Nov 2020, 06:46
by grovkillen

Code: Select all

gpio 0,(1-[CFG#inv0])
gpio 2,[CFG#inv0]

Re: Arithmetic with dummy values

Posted: 27 Nov 2020, 15:04
by TD-er
See also documentation examples for doing these kind of simple operations by simply formatting the data.
https://espeasy.readthedocs.io/en/lates ... red-values

Code: Select all

[!CFG#inv0] // Added ! at the start

Re: Arithmetic with dummy values

Posted: 28 Nov 2020, 00:27
by mrh_icb
Thanks for the replies, I tried both but neither worked:

With CFG#inv0=0, The first solution gave me:

Code: Select all

gpio 0,(1-[CFG#inv0])

log:
84991164: ACT : gpio 0,(1-0)
84991166: GPIO : port#0: set to 0
The second solution gave me:

Code: Select all

gpio 0,[!CFG#inv0]

log:
85147628: ACT : gpio 0,
85147630: GPIO : port#0: set to 0
Maybe the gpio command does support evaluation terms in the value argument. (I am using mega-20201022)

Re: Arithmetic with dummy values

Posted: 28 Nov 2020, 01:03
by mrh_icb
I had a look at the source code, and wow, there was this little bugger:

Code: Select all

  // Only if TmpStr starts with '=' then call Calculate(). Otherwise do not call it
  if (TmpStr[0] != '=') {
    returnValue = str2int(TmpStr);
  } else {
So the following works:

Code: Select all

gpio 0,=1-[CFG#inv0]

log:
87328352: ACT : gpio 0,=1-1
87328354: GPIO : port#0: set to 0
Would be good to document this in the wiki!