blob: df8139e7c77a35dbc79e63aa4b66ceb7053730fd [file] [log] [blame]
Brian Silverman80fc94c2014-03-09 16:56:01 -07001#include "bbb_cape/src/bbb/led.h"
2
3#include "aos/common/logging/logging.h"
4
5#include <string.h>
Brian Silverman80fc94c2014-03-09 16:56:01 -07006
7#define DIRECTORY "/sys/class/leds/beaglebone:green:usr%d/"
8
9namespace bbb {
10
11LED::LED(int number) : number_(number) {
12 char trigger_path[64];
13 snprintf(trigger_path, sizeof(trigger_path), DIRECTORY "trigger", number_);
14 FILE *trigger_handle = fopen(trigger_path, "w");
15 if (trigger_handle == nullptr) {
Brian Silvermand7844882014-05-10 23:35:39 -070016 PLOG(FATAL, "couldn't open trigger file for LED %d", number_);
Brian Silverman80fc94c2014-03-09 16:56:01 -070017 }
18 if (fputs("none", trigger_handle) < 0) {
Brian Silvermand7844882014-05-10 23:35:39 -070019 PLOG(FATAL, "writing 'none' to file %p (trigger for LED %d) failed",
20 trigger_handle, number_);
Brian Silverman80fc94c2014-03-09 16:56:01 -070021 }
22 if (fclose(trigger_handle) == -1) {
Brian Silvermand7844882014-05-10 23:35:39 -070023 PLOG(WARNING, "fclose(%p) failed", trigger_handle);
Brian Silverman80fc94c2014-03-09 16:56:01 -070024 }
25
26 char brightness_path[64];
27 snprintf(brightness_path, sizeof(brightness_path),
28 DIRECTORY "brightness", number_);
29
30 brightness_handle_ = fopen(brightness_path, "w");
31 if (brightness_handle_ == nullptr) {
Brian Silvermand7844882014-05-10 23:35:39 -070032 PLOG(FATAL, "fopen('%s', 'w') failed", brightness_path);
Brian Silverman80fc94c2014-03-09 16:56:01 -070033 }
34}
35
36LED::~LED() {
37 if (fclose(brightness_handle_) == -1) {
Brian Silvermand7844882014-05-10 23:35:39 -070038 PLOG(WARNING, "fclose(%p) failed", brightness_handle_);
Brian Silverman80fc94c2014-03-09 16:56:01 -070039 }
40}
41
42void LED::Set(bool on) {
43 rewind(brightness_handle_);
44 if (fputs(on ? "255" : "0", brightness_handle_) == EOF) {
Brian Silvermand7844882014-05-10 23:35:39 -070045 PLOG(FATAL, "fputs(255|0, %p) for LED %d failed",
46 brightness_handle_, number_);
Brian Silverman80fc94c2014-03-09 16:56:01 -070047 }
48 if (fflush(brightness_handle_) == EOF) {
Brian Silvermand7844882014-05-10 23:35:39 -070049 PLOG(FATAL, "fflush(%p) for LED %d failed",
50 brightness_handle_, number_);
Brian Silverman80fc94c2014-03-09 16:56:01 -070051 }
52}
53
54} // namespace bbb