Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flip (invert) an output pin value #3

Open
Rocco83 opened this issue May 22, 2021 · 0 comments
Open

Flip (invert) an output pin value #3

Rocco83 opened this issue May 22, 2021 · 0 comments

Comments

@Rocco83
Copy link
Contributor

Rocco83 commented May 22, 2021

I need to call digitalWrite (or maybe another new method) to say:
I don't care of the desired value, just read the current value and flip (invert) it.

AFAIK digitalRead can't be really used, as we speak about output pin, and in any case it will waste time for nothing.

Anyhow, the goal should be pretty much easy with the current code, as digitalWrite call WriteRegisterPin, which already read the port status and do bitwise operation.

In detail,

Currently, the WriteRegisterPin method take in input level to decide how to change the bit for the port, read, change and write the port back.

Seems to me that is possible to pass level with any int value, only 0 will have a different behavior than others.
level is taken, in example, from CS.digitalWrite without checking the value as well (AFAIK Arduino digitalWrite do the same just with uint8_t in place of int: https://github.com/arduino/ArduinoCore-avr/blob/9f8d27f09f3bbd1da1374b5549a82bda55d45d44/cores/arduino/wiring_digital.c#L156 ).
Eg i can set "6" as value, and it will be considered != 0.

Here the snippet

Centipede/Centipede.cpp

Lines 85 to 98 in 11bda08

void Centipede::WriteRegisterPin(int port, int regpin, int subregister, int level) {
ReadRegisters(port, subregister, 1);
if (level == 0) {
CSDataArray[0] &= ~(1 << regpin);
}
else {
CSDataArray[0] |= (1 << regpin);
}
WriteRegisters(port, subregister, 1);
}

So my idea is to

  1. Define a new int number which will invert the value (2?)
  2. Write the right bitwise operation that invert the bit value (xor, if I'm not wrong, https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit as first google result)

So as logic it could be like:

  if (level == 2) { // this is just a sample, likely a switch/case would be more appropriate
    // ^= -> XOR bitwise
    // << -> left shift
    CSDataArray[0] ^= ~(1 << regpin);
  }
  • Would the feature make sense in CS library?
  • Do you think that a new function would be preferable, like flipDigitalWrite, or just having a new value passed to digitalWrite?
  • would make sense to check in WriteRegisterPin (or in digitalWrite or...) the value passed? And if so, how to properly report back to the user that garbage has been passed?

Thank you very much,
Daniele

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant