AP_Notify: made Navio2LED a little easier to configure

- got rid of a lot of not needed defines
- allocated channels on init instead of accessing them every time
  through the HAL reference
- simpliefied hw_set_rgb()
This commit is contained in:
Staroselskii Georgii 2016-02-25 16:19:25 +03:00 committed by Lucas De Marchi
parent da550e5e98
commit 0bd7839b9f
2 changed files with 27 additions and 35 deletions

View File

@ -21,53 +21,44 @@
#define NAVIO_LED_DIM 1 // dim brightness #define NAVIO_LED_DIM 1 // dim brightness
#define NAVIO_LED_OFF 0 // off #define NAVIO_LED_OFF 0 // off
# define GPIO_A_LED_PIN 27 # define GREEN_PIN 27
# define GPIO_B_LED_PIN 6 # define BLUE_PIN 6
# define GPIO_C_LED_PIN 4 # define RED_PIN 4
# define GPIO_LED_ON 0
# define GPIO_LED_OFF 1
extern const AP_HAL::HAL& hal; extern const AP_HAL::HAL& hal;
Navio2LED::Navio2LED(): Navio2LED::Navio2LED()
RGBLed(NAVIO_LED_OFF, NAVIO_LED_BRIGHT, NAVIO_LED_MEDIUM, NAVIO_LED_DIM) : RGBLed(NAVIO_LED_OFF, NAVIO_LED_BRIGHT, NAVIO_LED_MEDIUM, NAVIO_LED_DIM)
{ {
} }
#include <unistd.h>
bool Navio2LED::hw_init(void) bool Navio2LED::hw_init(void)
{ {
// setup the main LEDs as outputs red_pin = hal.gpio->channel(RED_PIN);
hal.gpio->pinMode(GPIO_A_LED_PIN, HAL_GPIO_OUTPUT); green_pin = hal.gpio->channel(GREEN_PIN);
hal.gpio->pinMode(GPIO_B_LED_PIN, HAL_GPIO_OUTPUT); blue_pin = hal.gpio->channel(BLUE_PIN);
hal.gpio->pinMode(GPIO_C_LED_PIN, HAL_GPIO_OUTPUT);
// turn all lights off
hal.gpio->write(GPIO_A_LED_PIN, GPIO_LED_OFF);
hal.gpio->write(GPIO_B_LED_PIN, GPIO_LED_OFF);
hal.gpio->write(GPIO_C_LED_PIN, GPIO_LED_OFF);
red_pin->mode(HAL_GPIO_OUTPUT);
green_pin->mode(HAL_GPIO_OUTPUT);
blue_pin->mode(HAL_GPIO_OUTPUT);
red_pin->write(0);
green_pin->write(0);
blue_pin->write(0);
return true; return true;
} }
// set_rgb - set color as a combination of red, green and blue values // set_rgb - set color as a combination of red, green and blue values
bool Navio2LED::hw_set_rgb(uint8_t red, uint8_t green, uint8_t blue) bool Navio2LED::hw_set_rgb(uint8_t red, uint8_t green, uint8_t blue)
{ {
if (red == NAVIO_LED_OFF) { /* We fix the GPIO polarity right here */
hal.gpio->write(GPIO_C_LED_PIN, GPIO_LED_OFF);
} else { red_pin->write(!red);
hal.gpio->write(GPIO_C_LED_PIN, GPIO_LED_ON); green_pin->write(!green);
} blue_pin->write(!blue);
if (green == NAVIO_LED_OFF) {
hal.gpio->write(GPIO_A_LED_PIN, GPIO_LED_OFF);
} else {
hal.gpio->write(GPIO_A_LED_PIN, GPIO_LED_ON);
}
if (blue == NAVIO_LED_OFF) {
hal.gpio->write(GPIO_B_LED_PIN, GPIO_LED_OFF);
} else {
hal.gpio->write(GPIO_B_LED_PIN, GPIO_LED_ON);
}
return true; return true;
} }

View File

@ -16,8 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef __NAVIO2_LED_H__ #pragma once
#define __NAVIO2_LED_H__
#include "RGBLed.h" #include "RGBLed.h"
@ -27,6 +26,8 @@ public:
bool hw_init(void) override; bool hw_init(void) override;
bool hw_set_rgb(uint8_t r, uint8_t g, uint8_t b) override; bool hw_set_rgb(uint8_t r, uint8_t g, uint8_t b) override;
private:
AP_HAL::DigitalSource *red_pin;
AP_HAL::DigitalSource *green_pin;
AP_HAL::DigitalSource *blue_pin;
}; };
#endif