blob: 959b1329c3d37870a9ac22898a7efd1acaf40134 [file] [log] [blame]
Daniel Pettid6ff3d52014-01-02 11:24:39 -08001#include "gpo.h"
2
3#include <stdio.h>
4
5#include "aos/common/logging/logging.h"
6
7namespace bbb {
8
9Gpo::Gpo(int bank, int pin) {
10 // All the failures here are fatal, seeing as
Daniel Pettib23501c2014-01-06 16:57:52 -080011 // failure to initialize the pin would at best
12 // severely cripple the calling process.
Daniel Pettid6ff3d52014-01-02 11:24:39 -080013 if (!InitPin(bank, pin)) {
14 LOG(FATAL, "Failed to export the pin.\n");
15 }
16 if (!DoPinDirSet(2)) {
17 LOG(FATAL, "Failed to make the pin an output.\n");
18 }
19}
20
Daniel Pettife475252014-01-05 14:15:16 -080021bool Gpo::Set(const bool high) {
Daniel Pettid6ff3d52014-01-02 11:24:39 -080022 char val_path[64];
23 snprintf(val_path, sizeof(val_path), "/sys/class/gpio/gpio%d/value",
24 kernel_pin_);
25
26 if ((handle_ = fopen(val_path, "rb+")) == NULL) {
27 LOG(ERROR, "Unable open file for pin value setting.\n");
28 return false;
29 }
30
Daniel Pettife475252014-01-05 14:15:16 -080031 fprintf(handle_, "%d", high);
Daniel Pettid6ff3d52014-01-02 11:24:39 -080032 fclose(handle_);
33
34 return true;
35}
36
Daniel Pettife475252014-01-05 14:15:16 -080037} // namespace bbb