avr-classes
General Purpose Input Output

The core project contains a bunch of classes that can be used to manipulate the AVR's GPIO pins. The two main types of components to distinguish here are Ports and Pins.

Ports implement one of the port interfaces (InputPort, OutputPort, GpioPort) which allows them to read or write an 8 bit value. The prototype of such ports are the GPIO ports built into the AVR (e.g. HAM32A::PortA).

Pins implement one of the pin interfaces (InputPin, OutputPin, GpioPin) which allows them to read or write a boolean value. A pin can be located on a compatible port (e.g. an InputPin on an InputPort or on a GpioPort, but not on an OutputPort).

While the heavy usage of interfaces makes the GPIO classes rather expensive in terms of RAM (due to the virtual method tables), the classes also are highly combinable. Example:

PortA pa; // physical GPIO port
pa.setDirection(OUTPUT, 0x03); // configure pins 0 and 1 as output
OutputPortPin clk(&pa, 0); // output pin PA0 as clock for shift register
OutputPortPin data(&pa, 1); // output pin PA1 as data for shift register
ShiftRegister74HC164 sreg(&data, nullptr, &clk, nullptr); // shift register attached to PA0 and PA1
OutputShiftRegisterPort srport(&sreg); // output port on top of shift register
OutputPortPin led1(&srport, 0); // output pin on output pin A of shift register
OutputPortPin led2(&srport, 1); // output pin on output pin B of shift register
OutputPortPin led3(&srport, 2); // output pin on output pin C of shift register
OutputPortPin led4(&srport, 3); // output pin on output pin D of shift register

Additionally the classes VirtualInputPort and VirtualOutputPort allow to form ports using pins that are located virtually anywhere. This is especially useful when interfacing external hardware while not having consecutive pins on a real port left. But don't expect big data throughput from such virtual ports. Internally the virtual ports use the PinArray template class which allows even larger ports than 8 bit.

For using external interrupts ExternalInterrupt and its derived classes exist. These classes manage the external interrupts only, to control the underlying pin a separate instance of a GpioPortPin is required.

Summary of interfaces and classes

Interface Description
InputPin Pin that can get a boolean value
OutputPin Pin that can set a boolean value
GpioPin Pin that can be input and output, supports configuration of direction and internal pullup resistors
InputPort Port that can get an 8 bit value (or parts, controlled by a bit mask)
OutputPort Port that can set an 8 bit value (or parts, controlled by a bit mask)
GpioPort Port that can be input and output, supports configuration of direction and internal pullup resistors
Class Description
InputPortPin Input pin located on a device that implements the InputPort interface
OutputPortPin Output pin located on a device that implements the OutputPort interface
GpioPortPin Bi-directional pin located on a device that implements the GpioPort interface
PinArray Template class forming port-like pin sequences that can be physically located anywhere
VirtualInputPort Convenience wrapper of PinArray forming an 8 bit port that implements the InputPort interface
VirtualOutputPort Convenience wrapper of PinArray forming an 8 bit port that implements the OutputPort interface