rpi-rgb-led-matrix/include/gpio.h
2014-09-06 12:45:15 -07:00

51 lines
1.5 KiB
C++

// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
#ifndef RPI_GPIO_H
#define RPI_GPIO_H
#include <stdint.h>
// Putting this in our namespace to not collide with other things called like
// this.
namespace rgb_matrix {
// For now, everything is initialized as output.
class GPIO {
public:
// Available bits that actually have pins.
static const uint32_t kValidBits;
GPIO();
// Initialize before use. Returns 'true' if successful, 'false' otherwise
// (e.g. due to a permission problem).
bool Init();
// Initialize outputs.
// Returns the bits that are actually set.
uint32_t InitOutputs(uint32_t outputs);
// Set the bits that are '1' in the output. Leave the rest untouched.
inline void SetBits(uint32_t value) {
gpio_port_[0x1C / sizeof(uint32_t)] = value;
}
// Clear the bits that are '1' in the output. Leave the rest untouched.
inline void ClearBits(uint32_t value) {
gpio_port_[0x28 / sizeof(uint32_t)] = value;
}
// Write all the bits of "value" mentioned in "mask". Leave the rest untouched.
inline void WriteMaskedBits(uint32_t value, uint32_t mask) {
// Writing a word is two operations. The IO is actually pretty slow, so
// this should probably be unnoticable.
ClearBits(~value & mask);
SetBits(value & mask);
}
inline void Write(uint32_t value) { WriteMaskedBits(value, output_bits_); }
private:
uint32_t output_bits_;
volatile uint32_t *gpio_port_;
};
} // end namespace rgb_matrix
#endif // RPI_GPIO_H