AP_HAL_Linux: add Led_Sysfs class.
Add a class to control leds via linux sysfs API.
This commit is contained in:
parent
dd0d09175b
commit
7d4d14a409
92
libraries/AP_HAL_Linux/Led_Sysfs.cpp
Normal file
92
libraries/AP_HAL_Linux/Led_Sysfs.cpp
Normal file
@ -0,0 +1,92 @@
|
||||
/*
|
||||
Copyright (C) 2017 Mathieu Othacehe. All rights reserved.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "Led_Sysfs.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <AP_HAL/AP_HAL.h>
|
||||
|
||||
static const AP_HAL::HAL &hal = AP_HAL::get_HAL();
|
||||
|
||||
namespace Linux {
|
||||
|
||||
Led_Sysfs::Led_Sysfs(const char *led_name)
|
||||
: _led_name(led_name)
|
||||
{
|
||||
}
|
||||
|
||||
Led_Sysfs::~Led_Sysfs()
|
||||
{
|
||||
if (_brightness_fd) {
|
||||
close(_brightness_fd);
|
||||
}
|
||||
}
|
||||
|
||||
bool Led_Sysfs::init()
|
||||
{
|
||||
char *br_path;
|
||||
char *max_br_path;
|
||||
|
||||
if (asprintf(&br_path, "/sys/class/leds/%s/brightness", _led_name) == -1) {
|
||||
AP_HAL::panic("LinuxLed_Sysfs : Couldn't allocate brightness path\n");
|
||||
}
|
||||
|
||||
_brightness_fd = open(br_path, O_WRONLY | O_CLOEXEC);
|
||||
if (_brightness_fd < 0) {
|
||||
printf("LinuxLed_Sysfs: Unable to open file %s\n", br_path);
|
||||
free(br_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (asprintf(&max_br_path, "/sys/class/leds/%s/max_brightness", _led_name) == -1) {
|
||||
AP_HAL::panic("LinuxLed_Sysfs : Couldn't allocate max_brightness path\n");
|
||||
}
|
||||
|
||||
if (Util::from(hal.util)->read_file(max_br_path, "%u", &_max_brightness) < 0) {
|
||||
AP_HAL::panic("LinuxLed_Sysfs : Unable to read max_brightness in %s\n",
|
||||
max_br_path);
|
||||
}
|
||||
|
||||
free(max_br_path);
|
||||
free(br_path);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Led_Sysfs::set_brightness(uint8_t brightness)
|
||||
{
|
||||
if (_brightness_fd < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int br = brightness * _max_brightness / UINT8_MAX;
|
||||
|
||||
/* Don't log fails since this could spam the console */
|
||||
if (dprintf(_brightness_fd, "%u", br) < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
41
libraries/AP_HAL_Linux/Led_Sysfs.h
Normal file
41
libraries/AP_HAL_Linux/Led_Sysfs.h
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
Copyright (C) 2017 Mathieu Othacehe. All rights reserved.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <AP_HAL/AP_HAL.h>
|
||||
|
||||
#include "AP_HAL_Linux.h"
|
||||
#include "Util.h"
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace Linux {
|
||||
|
||||
class Led_Sysfs {
|
||||
public:
|
||||
bool init();
|
||||
bool set_brightness(uint8_t brightness);
|
||||
|
||||
Led_Sysfs(const char* led_name);
|
||||
~Led_Sysfs();
|
||||
|
||||
private:
|
||||
int _brightness_fd = -1;
|
||||
int _max_brightness = 0;
|
||||
const char *_led_name = nullptr;
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user