Skip to content

Commit

Permalink
commit again
Browse files Browse the repository at this point in the history
  • Loading branch information
NicksonYap committed Jan 30, 2016
1 parent b78af3f commit 8f29166
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
15 changes: 13 additions & 2 deletions README.md
Expand Up @@ -3,6 +3,9 @@ Arduino library for faster `digitalWrite()` using direct port manipulation and m
Which actually also does faster `pinMode()` and `digitalRead()`.

## Usage
Include the library:
`#include <digitalWriteFast.h>`

Macro definitions:
* `digitalWriteFast(pinNum, state)` (sets or clears pin/port faster)
* `pinModeFast(pinNum, mode)` (sets pin/port as input or output faster)
Expand All @@ -19,9 +22,12 @@ For example:
* use '#define pinNum 10' instead of `int pinNum = 10;`
* use 'const int pinNum 10' instead of `int pinNum = 10;`

Setting the parameter as a variable would cause the macro to revert back to the traditional `digitalWrite`, `pinMode` or `digitalRead` and operates more slowly.
Setting the parameter as a variable would cause the macro to return an error during compilation.

This makes sure `digitalWriteFast` that produces faster toggling, and notifies the programmer the specific area where toggling is slow. Otherwise, use normal `digitalWrite`

This is opposed to the forked library form Watterott, where if a variable is used as the parameter, the macro would revert to use sold `digitalWrite`, and remain undetected.

No error or warning will be thrown, but function works properly, without direct port manipualtion (slow).

## Speed

Expand All @@ -44,6 +50,11 @@ This library makes it easier by using `digitalWriteFast()` and the macro will re

If not in the list, the macro will revert back to `digitalWrite()`, `pinMode()` or `digitalRead()`

## Installation
1. Download the repo as zip, extract and place into Arduino IDE libraries folder.
2. Rename "digitalWriteFast-master" to "digitalWriteFast" Arduino IDE does not accept dash character.


## Reference
Fork of Watterott's https://github.com/watterott/Arduino-Libs/tree/master/digitalWriteFast
I just forked the whole repo, and delete unrelated files, I tried sparse checkout and gave up.
Expand Down
28 changes: 17 additions & 11 deletions digitalWriteFast.h
Expand Up @@ -301,20 +301,22 @@
//#endif //#ifndef digitalPinToPortReg


//ref: http://forum.arduino.cc/index.php?topic=140409.msg1054868#msg1054868
//void OutputsErrorIfCalled( void ) __attribute__ (( error( "Line: "__line__ "Variable used for digitalWriteFast") ));
void NonConstantUsed( void ) __attribute__ (( error("") ));


#ifndef digitalWriteFast
#if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR))
#define digitalWriteSlow(P, V) \
digitalWrite((P), (V)); \
OutputsErrorIfCalled();
#define digitalWriteFast(P, V) \
if (__builtin_constant_p(P) && __builtin_constant_p(V)) { \
BIT_WRITE(*__digitalPinToPortReg(P), __digitalPinToBit(P), (V)); \
} else { \
digitalWrite((P), (V)); \
NonConstantUsed(); \
}
#else
#define digitalWriteFast digitalWrite
//#define digitalWriteFast digitalWrite
#error Non-AVR device, unsupported.
#endif
#endif

Expand All @@ -325,10 +327,11 @@ if (__builtin_constant_p(P) && __builtin_constant_p(V)) { \
if (__builtin_constant_p(P) && __builtin_constant_p(V)) { \
BIT_WRITE(*__digitalPinToDDRReg(P), __digitalPinToBit(P), (V)); \
} else { \
pinMode((P), (V)); \
NonConstantUsed(); \
}
#else
#define pinModeFast pinMode
//#define pinModeFast pinMode
#error Non-AVR device, unsupported.
#endif
#endif

Expand All @@ -337,11 +340,14 @@ if (__builtin_constant_p(P) && __builtin_constant_p(V)) { \
#if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR))
#define digitalReadFast(P) ( (int) __digitalReadFast((P)) )
#define __digitalReadFast(P ) \
(__builtin_constant_p(P) ) ? ( \
( BIT_READ(*__digitalPinToPINReg(P), __digitalPinToBit(P))) ) : \
digitalRead((P))
if (__builtin_constant_p(P)) { \
BIT_READ(*__digitalPinToPINReg(P), __digitalPinToBit(P))); \
} else { \
NonConstantUsed(); \
}
#else
#define digitalReadFast digitalRead
//#define digitalReadFast digitalRead
#error Non-AVR device, unsupported.
#endif
#endif

Expand Down

0 comments on commit 8f29166

Please sign in to comment.