fixed the gpio code
diff --git a/bbb_cape/src/bbb/gpios.cc b/bbb_cape/src/bbb/gpios.cc
index 8594ba4..79d38bc 100644
--- a/bbb_cape/src/bbb/gpios.cc
+++ b/bbb_cape/src/bbb/gpios.cc
@@ -2,67 +2,72 @@
#include <stdio.h>
#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
#include "aos/common/logging/logging_impl.h"
namespace bbb {
-Pin::Pin() {}
-
-Pin::~Pin() {
- // Unexport the pin.
- if ((handle_ = fopen("/sys/class/gpio/unexport", "ab")) == NULL) {
- LOG(WARNING, "Could not open file to unexport pin.\n");
- // There's nothing intelligent we can really do here.
- return;
- }
-
- fprintf(handle_, "%d", kernel_pin_);
- fclose(handle_);
-}
-
-bool Pin::InitPin(int bank, int pin) {
- kernel_pin_ = bank * 32 + pin;
-
+GpioPin::GpioPin(int bank, int pin, bool input, bool initial_value)
+ : bank_(bank), pin_(pin), kernel_pin_(bank * 32 + pin) {
// Export the pin.
- if ((handle_ = fopen("/sys/class/gpio/export", "ab")) == NULL) {
- LOG(ERROR, "Could not open file for exporting pin.\n");
- return false;
+ FILE *export_handle = fopen("/sys/class/gpio/export", "a");
+ if (export_handle == NULL) {
+ LOG(WARNING,
+ "Could not open file to export pin (%d,%d) because of %d: %s.\n",
+ bank_, pin_, errno, strerror(errno));
+ } else {
+ if (fprintf(export_handle, "%d", kernel_pin_) < 0) {
+ LOG(WARNING, "Could not write to file %p to export pin (%d,%d) because "
+ "of %d: %s.\n",
+ export_handle, bank_, pin_, errno, strerror(errno));
+ }
+ if (fclose(export_handle) == -1) {
+ LOG(WARNING, "fclose(%p) failed with %d: %s\n", export_handle, errno,
+ strerror(errno));
+ }
}
- fprintf(handle_, "%d", kernel_pin_);
- fclose(handle_);
- return true;
+ char direction_path[64];
+ snprintf(direction_path, sizeof(direction_path),
+ "/sys/class/gpio/gpio%d/direction", kernel_pin_);
+
+ FILE *direction_handle = fopen(direction_path, "w");
+ if (direction_handle == NULL) {
+ LOG(FATAL, "fopen(%s, \"w+\") failed with %d: %s\n",
+ direction_path, errno, strerror(errno));
+ }
+
+ if (fputs(input ? "in" : (initial_value ? "high" : "low"),
+ direction_handle) < 0) {
+ LOG(FATAL, "setting direction for pin (%d,%d) failed with %d: %s\n",
+ bank_, pin_, errno, strerror(errno));
+ }
+ if (fclose(direction_handle) == -1) {
+ LOG(WARNING, "fclose(%p) failed with %d: %s\n", direction_handle, errno,
+ strerror(errno));
+ }
}
-bool Pin::DoPinDirSet(int direction) {
- char type_path[64];
- snprintf(type_path, sizeof(type_path), "/sys/class/gpio/gpio%d/direction",
- kernel_pin_);
-
- if ((handle_ = fopen(type_path, "rb+")) == NULL) {
- LOG(ERROR, "Unable open file for pin direction setting.\n");
- return false;
+GpioPin::~GpioPin() {
+ // Unexport the pin.
+ FILE *unexport_handle = fopen("/sys/class/gpio/unexport", "a");
+ if (unexport_handle == NULL) {
+ LOG(WARNING,
+ "Could not open file to unexport pin (%d,%d) because of %d: %s.\n",
+ bank_, pin_, errno, strerror(errno));
+ } else {
+ if (fprintf(unexport_handle, "%d", kernel_pin_) < 0) {
+ LOG(WARNING, "Could not write to file %p to unexport pin (%d,%d) because "
+ "of %d: %s.\n",
+ unexport_handle, bank_, pin_, errno, strerror(errno));
+ }
+ if (fclose(unexport_handle) == -1) {
+ LOG(WARNING, "fclose(%p) failed with %d: %s\n", unexport_handle, errno,
+ strerror(errno));
+ }
}
-
- switch (direction) {
- case 1:
- // Input.
- fprintf(handle_, "in");
- break;
- case 2:
- // Output.
- // If it's an output, we should specify an initial direction.
- fprintf(handle_, "low");
- break;
- default:
- LOG(ERROR, "Invalid direction identifier %d.\n", direction);
- fclose(handle_);
- return false;
- }
-
- fclose(handle_);
- return true;
}
} // namespace bbb