Clean up gpio code.
- Break out the input and output components into separate
subclasses.
- Fix some styleguide issues, including adding more useful
comments.
- Make stuff return bool instead of int like it should.
- Make the API simpler.
- Make failure to export or set pin direction FATAL.
(The rational for this is in the comments.)
It compiles, but remains untested.
diff --git a/bbb_cape/src/bbb/gpios.h b/bbb_cape/src/bbb/gpios.h
index a377d68..ba5da00 100644
--- a/bbb_cape/src/bbb/gpios.h
+++ b/bbb_cape/src/bbb/gpios.h
@@ -1,7 +1,6 @@
#ifndef BBB_CAPE_SRC_BBB_CAPE_CONTROL_H_
#define BBB_CAPE_SRC_BBB_CAPE_CONTROL_H_
-#include <stdint.h>
#include <stdio.h>
// As it turns out, controlling the BBB's GPIO pins
@@ -9,30 +8,31 @@
// code is to provide a simple wrapper that masks
// all the ugly stuff and exposes a nice API.
-// Based on example from
+// Based on example from
// <http://learnbuildshare.wordpress.com/2013/05/29/beaglebone-black-digital-ouput/>
+// This is a base class for all gpio related stuff.
+// Use either a gpi or gpo subclass if you want to do things.
namespace bbb {
class Pin {
- FILE *handle_;
- int direction_;
- int kernel_pin_;
- bool exported_;
+ public:
+ // Not strictly necessary for this to be virtual,
+ // but it's a good idea.
+ virtual ~Pin();
- int DoPinDirSet(int direction);
- int DoExport();
-
-public:
+ protected:
+ // Set the pin direction.
+ // 1 makes it an input.
+ // 2 makes it an output and sets the initial state to low.
+ bool DoPinDirSet(int direction);
+ // Export the pin, so we can use it.
// Here, for example, if you wanted to use
- // GPIO1_28, you would give the ctor
- // 1, 28 as arguments.
- Pin(int bank, int pin);
- ~Pin();
- int MakeInput();
- int MakeOutput();
- int Write(uint8_t value);
- int Read();
+ // GPIO1_28, you would give it 1, 28 as arguments.
+ bool InitPin(int bank, int pin);
+
+ FILE *handle_ = NULL;
+ int kernel_pin_ = -1;
};
} // namespace bbb