Pages: [1]   Go Down
Author Topic: suggestion to add TOGGLE in digitalWrite()  (Read 5293 times)
0 Members and 1 Guest are viewing this topic.
Athens - Greece
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello,
i didn't do much of a research about it (not much free time), but i think the library does not provide an easy/efficient way to toggle a pin.

anyway, i modified wiring.h and added:
...
#define HIGH 0x1
#define LOW  0x0
#define TOGGLE  0x2   // added line
...

and wiring_digital.c and changed to:
      if (val == LOW) *out &= ~bit;
      if (val == HIGH) *out |= bit; // added line
      if (val == TOGGLE) *out ^= bit; // added line
      // replaced line // else *out |= bit;


now it looks like i can write for example:
digitalWrite(ledPin, TOGGLE);
instead of calling other functions or keeping the state of the pin in the program.

if this modification does not create any issues, i think it would be a nice addition to a next version.

 - chris
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what speed does it toggle at? (and what if I want it faster or slower)

 smiley-wink

and it already keeps the state of the pin that is why

pin = !pin works
[edit]
oh nevermind, i just re-read that, yea I guess that could be more noob friendly than the above, so why not [/edit]
« Last Edit: August 18, 2010, 03:48:00 pm by Osgeld » Logged


SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 135
Posts: 6763
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
so why not
Well, changing the "any non-zero value causes a 1 to be written" behavior may screw up any number of existing sketches...
Logged

Offline Offline
Edison Member
*
Karma: 3
Posts: 1001
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Well, changing the "any non-zero value causes a 1 to be written" behavior may screw up any number of existing sketches...
I agree to this - it really is not wise to change this behavior considering backwards compatibility.

That AVR's support a lesser known direct pin toggle, so for those who don't mind using direct port IO, you can do toggle as follows:

Code:
// Example is for digital pin 2.

PIND = _BV(PIND2);  // toggle pin2 on AtMega168/328
Logged

0
Offline Offline
Edison Member
*
Karma: 0
Posts: 1103
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There seems to be a problem with the forum's software
The code should read
Code:
PIND ^= _BV(PIND2)
« Last Edit: August 19, 2010, 08:10:53 am by mpeuser » Logged

Offline Offline
Edison Member
*
Karma: 3
Posts: 1001
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The code should read
No, it should not! Go and look it up.
Logged

0
Offline Offline
Edison Member
*
Karma: 0
Posts: 1103
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 :o

I see....
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1266933248/3#3
---
And it works for the 168 and 328 only, not with old mega8
« Last Edit: August 20, 2010, 04:40:50 am by mpeuser » Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 135
Posts: 6763
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah.  Using the PINx magic toggle port, and a variation of the FastDigitalWrite macro that has been floating around, it should be possible to create a "digitalToggle()" function (macro) that takes up no space unless it is used, and operates in a single cycle if all the arguments are constant, and IT DOESN'T HAVE TO BE BACKWARD COMPATIBLE WITH ANYTHING!
 :-)
Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 5581
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Well, changing the "any non-zero value causes a 1 to be written" behavior may screw up any number of existing sketches...

I don't necessarily think this is "one of those times", but sometimes you have to decide to say "fsck backwards compatibility" and make a change "for the better".

Deprecate and move on...

 smiley-wink
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

coughheaderspacingcough (although its way too late for that)

now what were you saying?

Logged


0
Offline Offline
Edison Member
*
Karma: 0
Posts: 1103
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

IMHO he wanted to say "To hell with compatibility!"  ;D
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I think it is a good idea.
The same thing should be done to reverse the direction of a pin
(an input becomes an output, and an output becomes an input).
It would be great to write
pinMode(ThisPin, REVERSE);
Best regards,
Logged

Athens - Greece
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well, i can not see why one should use macro's, or secret codes, or any other tricks to do what the digitalWrite() function should do anyway.

where is the logic in it then? if we do direct port manipulation, then digitalWrite() is completely useless (it is bigger and slower anyway),

on the other hand, if for any reasons we want a digitalWrite(), it should do all 3 useful bit manipulations (and, or, xor)

as for backwards compatibility:
this thing: "any non-zero value causes a 1 to be written"
looks seriously wrong anyway...

Logged

Pages: [1]   Go Up
Jump to: