-
Notifications
You must be signed in to change notification settings - Fork 2
Description
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
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
- Define a new int number which will invert the value (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