blob: 5dc5e710cac5710da311272f016f9eafe119fc79 [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>
6#include <errno.h>
7
8#define DIRECTORY "/sys/class/leds/beaglebone:green:usr%d/"
9
10namespace bbb {
11
12LED::LED(int number) : number_(number) {
13 char trigger_path[64];
14 snprintf(trigger_path, sizeof(trigger_path), DIRECTORY "trigger", number_);
15 FILE *trigger_handle = fopen(trigger_path, "w");
16 if (trigger_handle == nullptr) {
17 LOG(FATAL, "couldn't open trigger file for LED %d because of %d: %s\n",
18 number_, errno, strerror(errno));
19 }
20 if (fputs("none", trigger_handle) < 0) {
21 LOG(FATAL,
22 "writing 'none' to file %p (trigger for LED %d) failed with %d: %s\n",
23 trigger_handle, number_, errno, strerror(errno));
24 }
25 if (fclose(trigger_handle) == -1) {
26 LOG(WARNING, "fclose(%p) failed with %d: %s\n",
27 trigger_handle, errno, strerror(errno));
28 }
29
30 char brightness_path[64];
31 snprintf(brightness_path, sizeof(brightness_path),
32 DIRECTORY "brightness", number_);
33
34 brightness_handle_ = fopen(brightness_path, "w");
35 if (brightness_handle_ == nullptr) {
36 LOG(FATAL, "fopen('%s', 'w') failed with %d: %s\n",
37 brightness_path, errno, strerror(errno));
38 }
39}
40
41LED::~LED() {
42 if (fclose(brightness_handle_) == -1) {
43 LOG(WARNING, "fclose(%p) failed with %d: %s\n",
44 brightness_handle_, errno, strerror(errno));
45 }
46}
47
48void LED::Set(bool on) {
49 rewind(brightness_handle_);
50 if (fputs(on ? "255" : "0", brightness_handle_) == EOF) {
51 LOG(FATAL, "fputs(255|0, %p) for LED %d failed with %d: %s\n",
52 brightness_handle_, number_, errno, strerror(errno));
53 }
54 if (fflush(brightness_handle_) == EOF) {
55 LOG(FATAL, "fflush(%p) for LED %d failed with %d: %s\n",
56 brightness_handle_, number_, errno, strerror(errno));
57 }
58}
59
60} // namespace bbb