Athens - Greece
Offline
Newbie
Karma: 0
Posts: 7
Arduino rocks
|
 |
« on: August 17, 2010, 09:55:29 pm » |
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
Faraday Member
Karma: 33
Posts: 3626
@ssh0le
|
 |
« Reply #1 on: August 18, 2010, 03:45:21 pm » |
what speed does it toggle at? (and what if I want it faster or slower)  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
|
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL
|
|
|
|
SF Bay Area (USA)
Offline
Faraday Member
Karma: 78
Posts: 5454
Strongly opinionated, but not official!
|
 |
« Reply #2 on: August 19, 2010, 01:36:42 am » |
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
God Member
Karma: 3
Posts: 985
Arduino rocks
|
 |
« Reply #3 on: August 19, 2010, 02:20:52 am » |
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: // Example is for digital pin 2.
PIND = _BV(PIND2); // toggle pin2 on AtMega168/328
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Edison Member
Karma: 0
Posts: 1103
Arduino rocks
|
 |
« Reply #4 on: August 19, 2010, 08:10:28 am » |
There seems to be a problem with the forum's software The code should read PIND ^= _BV(PIND2)
|
|
|
|
« Last Edit: August 19, 2010, 08:10:53 am by mpeuser »
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 3
Posts: 985
Arduino rocks
|
 |
« Reply #5 on: August 19, 2010, 09:15:22 am » |
The code should read
No, it should not! Go and look it up.
|
|
|
|
|
Logged
|
|
|
|
|
|
|
SF Bay Area (USA)
Offline
Faraday Member
Karma: 78
Posts: 5454
Strongly opinionated, but not official!
|
 |
« Reply #7 on: August 19, 2010, 04:50:00 pm » |
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
Faraday Member
Karma: 27
Posts: 5083
Where's the beer?
|
 |
« Reply #8 on: August 19, 2010, 04:54:22 pm » |
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... 
|
|
|
|
|
Logged
|
|
|
|
|
SE USA
Offline
Faraday Member
Karma: 33
Posts: 3626
@ssh0le
|
 |
« Reply #9 on: August 20, 2010, 09:00:17 am » |
coughheaderspacingcough (although its way too late for that)
now what were you saying?
|
|
|
|
|
Logged
|
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL
|
|
|
|
0
Offline
Edison Member
Karma: 0
Posts: 1103
Arduino rocks
|
 |
« Reply #10 on: August 21, 2010, 04:46:38 am » |
IMHO he wanted to say "To hell with compatibility!" ;D
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 20
Arduino rocks
|
 |
« Reply #11 on: August 26, 2010, 05:21:05 am » |
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
Newbie
Karma: 0
Posts: 7
Arduino rocks
|
 |
« Reply #12 on: August 30, 2010, 07:18:48 pm » |
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
|
|
|
|
|
|