Login
Forgot Password?

OR

Login with Google Login with Twitter Login with Facebook
  • Join
  • Profiles
  • Groups
  • SuicideGirls
  • Photos
  • Videos
  • Shop
Vital Stats

enigma11235

Member Since 2008

Followers 2 Following 1

  • Everything
  • Photos
  • Video
  • Blogs
  • Groups
  • From Others

Friday Dec 18, 2009

Dec 18, 2009
0
  • Facebook
  • Tweet
  • Email
Alright, why not. I don't think anyone is going to read this, but here goes...

So apparently the digitalWrite() function in the Arduino library is slow, according to this. As the article states, PORTD |= _BV(2) and PORTD &= ~_BV(2) really are much faster; however, they are also more difficult to understand. So essentially we end up sacrificing code readability for performance. This isn't ideal. Is there a way to improve code readability? One way to do this is with macros:


#define __digital_high(L, N) PORT ## L |= _BV(N);
#define __digital_low(L, N) PORT ## L &= ~_BV(N);


Now we can write __digital_high(D, 2) instead of PORTD |= _BV(2). Not only does simplify the code, it also expresses intent; without reading any documentation __digital_high(D, 2) clearly sets a digital pin to high, while it isn't clear what PORTD |= _BV(2) does.

This is better, but the code can be further improved. The issue is with the pin mappings: the instruction __digital_high(B, 4) sets digital pin 12 to high. That is, (B,4) is mapped to pin 12. You can view the pin mappings here. Anyways, we can hard-code this information at compile time using traits classes. This, combined with a small helper function, allows us to write digital_write<12>(HIGH) instead of PORTD |= _BV(2).

Note that the preprocessor is used to generate the 13 classes for us. Writing 13 template specializations by hand would really suck.



#define __digital_high(L, N) PORT ## L |= _BV(N);
#define __digital_low(L, N) PORT ## L &= ~_BV(N);

template <uint8_t Pin> class pin_traits;

#define PIN_TRAITS_GENERATOR(L, N, P) \
template <> class pin_traits<P> \
{ \
public: \
static void digital_write(boolean state) \
{ \
if (state == HIGH) \
__digital_high(L, N) \
else \
__digital_low(L, N) \
} \
};



All of the code can be found here: http://pastebin.com/f5769c4f1
trilby:
?????????

I read your blog and im confused
Dec 18, 2009
tristane:
Strangely, I don't find it weird at all. I am the type of person who always gets fascinated by complete random things, who, when watching a movie with friends, often find herself more puzzled by the construction of a strange tool seemingly defying the laws of nature, than, say, the events in the movie. And I like grammar too, to a slightly disturbing level (although I did make a mistake in typing "keeps" instead of "keep"). Iit is nice to find people who are just as off the norm as I am.

Although your favourite drink does sound like a sad traffic accident. What is an Irish car bomb?
Mar 11, 2010

More Blogs

  • 03.21.10
    0

    Sunday Mar 21, 2010

    "Your friend has a big dick" -- Haha, when she said that, it was the …
  • 03.14.10
    1

    Sunday Mar 14, 2010

    Read More
  • 03.10.10
    4

    Thursday Mar 11, 2010

    I have been wondering what I am going to do with this blog for quite …
  • 12.18.09
    2

    Friday Dec 18, 2009

    Alright, why not. I don't think anyone is going to read this, but he…
  • 12.12.09
    1

    Saturday Dec 12, 2009

    What the... someone renewed my subscription. Thanks!

We at SuicideGirls have been celebrating alternative pin-up girls for:

23
years
8
months
7
days
  • 5,509,826 fans
  • 41,393 fans
  • 10,327,617 followers
  • 4,589 SuicideGirls
  • 1,127,231 followers
  • 14,901,976 photos
  • 321,315 followers
  • 61,343,008 comments
  • Join
  • Profiles
  • Groups
  • Photos
  • Videos
  • Shop
  • Help
  • About
  • Press
  • LIVE

Legal/Tos | DMCA | Privacy Policy | 18 U.S.C. 2257 Record-Keeping Requirements Compliance Statement | Contact Us | Vendo Payment Support
©SuicideGirls 2001-2025

Press enter to search
Fast Hi-res

Click here to join & see it all...

Crop your photo