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

Adding a toggle function to wiring_digital.c #11777

Closed
Perehama opened this issue Mar 31, 2022 · 1 comment
Closed

Adding a toggle function to wiring_digital.c #11777

Perehama opened this issue Mar 31, 2022 · 1 comment
Labels
Component: Core Related to the code for the standard Arduino API feature request A request to make an enhancement (not a bug fix) Type: Duplicate Another item already exists for this topic

Comments

@Perehama
Copy link

Perehama commented Mar 31, 2022

I suggest the code below be added to wiring_digital.c and the Arduino language. This would make the task easier for beginning programmers and eliminate the need to check the state of the pin first, making a speed improvement at the same time.

void digitalToggle(uint8_t pin) {
  uint8_t timer = digitalPinToTimer(pin);
  uint8_t bit = digitalPinToBitMask(pin);
  uint8_t port = digitalPinToPort(pin);
  volatile uint8_t *out;
  if (port == NOT_A_PIN) return;
  if (timer != NOT_ON_TIMER) turnOffPWM(timer);
  out = portOutputRegister(port);
  uint8_t oldSREG = SREG;
  cli();
  *out ^= bit;
  SREG = oldSREG;
}
@matthijskooijman
Copy link
Collaborator

Thanks for you suggestion. I edited your post to use triple backticks to create a more readable code block.

Also, change to the Arduino API like this should be coordinated between cores, this coordination of happens in the ArduinoCore-API repository. There is already an issue about this, see arduino/ArduinoCore-API#130, so I'm going to close this issue and direct you to that one (feel free to add more info and new arguments to it, but if you just want to voice support for it, please use a thumbsup rather than a "me too" comment).

Also, note that on AVR, toggling can be implemented even faster by writing to the input register, which makes the hardware toggle pins.

@per1234 per1234 added Component: Core Related to the code for the standard Arduino API feature request A request to make an enhancement (not a bug fix) Type: Duplicate Another item already exists for this topic labels Mar 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Core Related to the code for the standard Arduino API feature request A request to make an enhancement (not a bug fix) Type: Duplicate Another item already exists for this topic
Projects
None yet
Development

No branches or pull requests

3 participants