Skip to content

Commit

Permalink
Remove gpio.h from public interface. Not needed to operate RGBMatrix.
Browse files Browse the repository at this point in the history
Having gpio.h public prevented to use compile-time #defines, and it
also made the public interface complicated than needed.
  • Loading branch information
hzeller committed Aug 15, 2020
1 parent 8267b1a commit 3ea0f83
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 28 deletions.
4 changes: 1 addition & 3 deletions examples-api-use/input-example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <stdio.h>
#include <signal.h>

using rgb_matrix::GPIO;
using rgb_matrix::RGBMatrix;
using rgb_matrix::Canvas;

Expand Down Expand Up @@ -39,8 +38,7 @@ int main(int argc, char *argv[]) {
// Let's request all input bits and see which are actually available.
// This will differ depending on which hardware mapping you use and how
// many parallel chains you have.
const gpio_bits_t available_inputs
= matrix->gpio()->RequestInputs(0xffffffff);
const uint64_t available_inputs = matrix->RequestInputs(0xffffffff);
fprintf(stderr, "Available GPIO-bits: ");
for (int b = 0; b < 32; ++b) {
if (available_inputs & (1<<b))
Expand Down
27 changes: 17 additions & 10 deletions include/led-matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
#include <string>
#include <vector>

#include "gpio.h"
#include "canvas.h"
#include "thread.h"
#include "pixel-mapper.h"

namespace rgb_matrix {
class RGBMatrix;
class FrameCanvas; // Canvas for Double- and Multibuffering
class GPIO;

namespace internal {
class Framebuffer;
Expand Down Expand Up @@ -156,6 +156,10 @@ class RGBMatrix : public Canvas {
int limit_refresh_rate_hz; // Flag: --led-limit-refresh
};

// -- Note, these don't work anymore as there is no access to an GPIO
// instance anymore for the user.
// TODO: remove in further cleanup of API.

// Create an RGBMatrix.
//
// Needs an initialized GPIO object and configuration options from the
Expand Down Expand Up @@ -243,15 +247,16 @@ class RGBMatrix : public Canvas {

//-- GPIO interaction

// Return pointer to GPIO object for your own interaction with free
// pins. But don't mess with bits already in use by the matrix :)
GPIO *gpio() { return io_; }
// Request inputs.
// This function allows you to request pins you'd like to read with
// AwaitInputChange().
uint64_t RequestInputs(uint64_t);

// This function will return whenever the GPIO input pins
// change (pins that are not already in use for output, that is) or the
// timeout is reached. You need to have reserved the inputs with
// gpio()->RequestInputs(...) first (e.g.
// gpio()->RequestInputs((1<<25)|(1<<24));
// matrix->RequestInputs(...) first (e.g.
// matrix->RequestInputs((1<<25)|(1<<24));
//
// A positive timeout waits the given amount of milliseconds for a change
// (e.g. a button-press) to occur; if there is no change, it will just
Expand All @@ -260,14 +265,16 @@ class RGBMatrix : public Canvas {
// timeout.
// A negative number waits forever and will only return if there is a change.
//
// Note, while you can poll the gpio()->Read() function directly, it is
// not recommended as this might occur during the display update which might
// result in flicker.
// This function only samples between display refreshes and is more
// convenient as it allows to wait for a change.
//
// Returns the bitmap of all GPIO input pins.
gpio_bits_t AwaitInputChange(int timeout_ms);
uint64_t AwaitInputChange(int timeout_ms);

// Legacy way to set gpio pins. We're not doing this anymore but need to
// be compatible with old calls in the form matrix->gpio()->RequestInputs(..)
// Don't use.
RGBMatrix *gpio() __attribute__((deprecated)) { return this; }

//-- Double- and Multibuffering.

Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions lib/gpio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ GPIO::GPIO() : output_bits_(0), input_bits_(0), reserved_bits_(0),
{
}

uint64_t GPIO::InitOutputs(uint64_t outputs,
bool adafruit_pwm_transition_hack_needed) {
gpio_bits_t GPIO::InitOutputs(gpio_bits_t outputs,
bool adafruit_pwm_transition_hack_needed) {
if (s_GPIO_registers == NULL) {
fprintf(stderr, "Attempt to init outputs but not yet Init()-ialized.\n");
return 0;
Expand Down Expand Up @@ -201,7 +201,7 @@ uint64_t GPIO::InitOutputs(uint64_t outputs,
return outputs;
}

uint64_t GPIO::RequestInputs(uint64_t inputs) {
gpio_bits_t GPIO::RequestInputs(gpio_bits_t inputs) {
if (s_GPIO_registers == NULL) {
fprintf(stderr, "Attempt to init inputs but not yet Init()-ialized.\n");
return 0;
Expand Down
17 changes: 6 additions & 11 deletions include/gpio.h → lib/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://gnu.org/licenses/gpl-2.0.txt>

#ifndef RPI_GPIO_H
#define RPI_GPIO_H
#ifndef RPI_GPIO_INTERNAL_H
#define RPI_GPIO_INTERNAL_H

#include "gpio-bits.h"

#include <vector>

// TODO: reduce the public interface, so that we're not depending on
// compile-time gpio-bits.h.
// All of the interface that should be needed for users is RequestInputs()
// and Read().

// Putting this in our namespace to not collide with other things called like
// this.
namespace rgb_matrix {
Expand All @@ -51,12 +46,12 @@ class GPIO {
// Returns the bits that were available and could be set for output.
// (never use the optional adafruit_hack_needed parameter, it is used
// internally to this library).
uint64_t InitOutputs(uint64_t outputs,
bool adafruit_hack_needed = false);
gpio_bits_t InitOutputs(gpio_bits_t outputs,
bool adafruit_hack_needed = false);

// Request given bitmap of GPIO inputs.
// Returns the bits that were available and could be reserved.
uint64_t RequestInputs(uint64_t inputs);
gpio_bits_t RequestInputs(gpio_bits_t inputs);

// Set the bits that are '1' in the output. Leave the rest untouched.
inline void SetBits(gpio_bits_t value) {
Expand Down Expand Up @@ -159,4 +154,4 @@ uint32_t GetMicrosecondCounter();

} // end namespace rgb_matrix

#endif // RPI_GPIO_H
#endif // RPI_GPIO_INGERNALH
6 changes: 5 additions & 1 deletion lib/led-matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ RGBMatrix::~RGBMatrix() {
delete shared_pixel_mapper_;
}

uint64_t RGBMatrix::RequestInputs(uint64_t bits) {
return io_->RequestInputs(bits);
}

void RGBMatrix::ApplyNamedPixelMappers(const char *pixel_mapper_config,
int chain, int parallel) {
if (pixel_mapper_config == NULL || strlen(pixel_mapper_config) == 0)
Expand Down Expand Up @@ -441,7 +445,7 @@ FrameCanvas *RGBMatrix::SwapOnVSync(FrameCanvas *other,
return previous;
}

gpio_bits_t RGBMatrix::AwaitInputChange(int timeout_ms) {
uint64_t RGBMatrix::AwaitInputChange(int timeout_ms) {
if (!updater_) return 0;
return updater_->AwaitInputChange(timeout_ms);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/options-initialize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include "multiplex-mappers-internal.h"

#include "gpio.h"

namespace rgb_matrix {
RuntimeOptions::RuntimeOptions() :
#ifdef RGB_SLOWDOWN_GPIO
Expand Down

0 comments on commit 3ea0f83

Please sign in to comment.