I have developed a new C++ library for fast digital I/O and would appreciate any comments and suggestions. The library is posted here code.google.com/p/beta-lib/downloads/list as the file DigitalPinBeta20120113.zip.
A number of people have developed fast versions of digitalRead/digitalWrite using C macros to generate fast inline code.
I decided to to design a new API and use a template class. Here is an example program that generates two 125 ns wide pulses for a scope timing test, assuming a 16 MHz CPU.
// scope test for write timing
#include <DigitalPin.h>
// class with compile time pin number
DigitalPin<13> pin13;
void setup() {
// set mode to OUTPUT
pin13.outputMode();
}
void loop() {
pin13.high();
pin13.low();
pin13.high();
pin13.low();
delay(1);
}
Each of the statements, pin13.outputMode(), pin13.low(), and pin13.high(), compiles into a two byte cbi/sbi instruction.
For high address Mega pins the functions are larger and slower but provide atomic access to the pins.