StandardFirmata - How to Reverse Ouput Pins?

Hello,
I have a software that requires the SnandardFirmata sketch but I need to reverse the behaviour of the digital output pins so that when the PC says HIGH the pin goes LOW and vice versa.
I feel like a complete idiot but I really can't find a way to do this in the sketch.
Here is the link to the sketch if you don't want to open the IDE:

I would really appreciate any hint.

Here is the link to the sketch if you don’t want to open the IDE:

Personally I would prefer to see the sketch in the IDE so why not post it here ?

I assume your PC software is closed so you cannot alter it.
First you need to determine what method it uses to set the digital output pins as there are at lease two ways I can see.
First is digitalWriteCallback(byte port, int value) that sets several pins at the same time and the other is setPinValueCallback(byte pin, int value) that just sets a single pin at a time.

If the software uses the latter method then the code it calls to set a single pin is…

/*
 * Sets the value of an individual pin. Useful if you want to set a pin value but
 * are not tracking the digital port state.
 * Can only be used on pins configured as OUTPUT.
 * Cannot be used to enable pull-ups on Digital INPUT pins.
 */
void setPinValueCallback(byte pin, int value)
{
  if (pin < TOTAL_PINS && IS_PIN_DIGITAL(pin)) {
    if (Firmata.getPinMode(pin) == OUTPUT) {
      Firmata.setPinState(pin, value);
      digitalWrite(PIN_TO_DIGITAL(pin), value);
    }
  }
}

and to reverse the pin state is to change digitalWrite(PIN_TO_DIGITAL(pin), value) to digitalWrite(PIN_TO_DIGITAL(pin), !value)
Notice the ! in front of value

This will flip the way all digital pins using this call method work but does not alter the state the Firmata software sees the pin so the PC software setting a pin low will show low in Firmata but will be set high on the digital output.

UKHeliBob:
Personally I would prefer to see the sketch in the IDE so why not post it here ?

Sorry, I should have mentioned that the sketch is already amongst the examples (folder Firmata) in the Arduino IDEs so you can open it there if you want.

Riva:
I assume your PC software is closed so you cannot alter it.
First you need to determine what method it uses to set the digital output pins as there are at lease two ways I can see.
First is digitalWriteCallback(byte port, int value) that sets several pins at the same time and the other is setPinValueCallback(byte pin, int value) that just sets a single pin at a time.

If the software uses the latter method then the code it calls to set a single pin is…

/*
  • Sets the value of an individual pin. Useful if you want to set a pin value but
  • are not tracking the digital port state.
  • Can only be used on pins configured as OUTPUT.
  • Cannot be used to enable pull-ups on Digital INPUT pins.
    */
    void setPinValueCallback(byte pin, int value)
    {
     if (pin < TOTAL_PINS && IS_PIN_DIGITAL(pin)) {
       if (Firmata.getPinMode(pin) == OUTPUT) {
         Firmata.setPinState(pin, value);
         digitalWrite(PIN_TO_DIGITAL(pin), value);
       }
     }
    }


and to reverse the pin state is to change digitalWrite(PIN_TO_DIGITAL(pin), value) to digitalWrite(PIN_TO_DIGITAL(pin), !value)
Notice the ! in front of value

This will flip the way all digital pins using this call method work but does not alter the state the Firmata software sees the pin so the PC software setting a pin low will show low in Firmata but will be set high on the digital output.

Thank you very much. I’ll try this when I get home. I found these callbacks but forgot about the exclamation mark.

jetam27:
Sorry, I should have mentioned that the sketch is already amongst the examples (folder Firmata) in the Arduino IDEs so you can open it there if you want.

Only if you have Firmdata installed of course

Riva:
I assume your PC software is closed so you cannot alter it.
First you need to determine what method it uses to set the digital output pins as there are at lease two ways I can see.
First is digitalWriteCallback(byte port, int value) that sets several pins at the same time and the other is setPinValueCallback(byte pin, int value) that just sets a single pin at a time.

If the software uses the latter method then the code it calls to set a single pin is…

/*
  • Sets the value of an individual pin. Useful if you want to set a pin value but
  • are not tracking the digital port state.
  • Can only be used on pins configured as OUTPUT.
  • Cannot be used to enable pull-ups on Digital INPUT pins.
    */
    void setPinValueCallback(byte pin, int value)
    {
     if (pin < TOTAL_PINS && IS_PIN_DIGITAL(pin)) {
       if (Firmata.getPinMode(pin) == OUTPUT) {
         Firmata.setPinState(pin, value);
         digitalWrite(PIN_TO_DIGITAL(pin), value);
       }
     }
    }


and to reverse the pin state is to change digitalWrite(PIN_TO_DIGITAL(pin), value) to digitalWrite(PIN_TO_DIGITAL(pin), !value)
Notice the ! in front of value

This will flip the way all digital pins using this call method work but does not alter the state the Firmata software sees the pin so the PC software setting a pin low will show low in Firmata but will be set high on the digital output.

I tried negating the value but it doesn’t work. I also tried negating the value or the pinValue elswhere in the code but it doesn’t change anything or the program stops working. I already tried forcing most of these variables to HIGH or 1 yesterday without a success. Do yo have any other idea?
Thanks again.

UKHeliBob:
Only if you have Firmdata installed of course

Of course but if you don't, you can download or copy/paste it from the github link I posted. Sorry if I'm missing something.
By the way, do yo have any idea regarding my problem?

Sorry if I'm missing something.

Simply that it is much easier to copy it for examination when it is posted here in code tags

jetam27:
I tried negating the value but it doesn't work. I also tried negating the value or the pinValue elswhere in the code but it doesn't change anything or the program stops working. I already tried forcing most of these variables to HIGH or 1 yesterday without a success. Do yo have any other idea?
Thanks again.

Is the PC program using setPinValueCallback and not some other callback like digitalWriteCallback to set the output pins?

I put value=~value; in the void digitalWriteCallback(byte port, int value). This does the trick. Thank you all anyway.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.