Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AP_Notify: add support for blinking 1 LED for notify #27533

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libraries/AP_HAL/board/sitl.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
#define AP_NOTIFY_GPIO_LED_RGB_GREEN_PIN 9
#define AP_NOTIFY_GPIO_LED_RGB_BLUE_PIN 10

// #define AP_NOTIFY_GPIO_LED_1_ENABLED 1
#define AP_NOTIFY_GPIO_LED_A_PIN 8 // these are set in SIM_PIN_MASK

#define HAL_HAVE_BOARD_VOLTAGE 1
#define HAL_HAVE_SERVO_VOLTAGE 1
#define HAL_HAVE_SAFETY_SWITCH 1
Expand Down
4 changes: 4 additions & 0 deletions libraries/AP_Notify/AP_Notify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "Buzzer.h"
#include "Display.h"
#include "ExternalLED.h"
#include "GPIO_LED_1.h"
#include "IS31FL3195.h"
#include "PCA9685LED_I2C.h"
#include "NavigatorLED.h"
Expand Down Expand Up @@ -313,6 +314,9 @@ void AP_Notify::add_backends(void)
ADD_BACKEND(NEW_NOTHROW AP_BoardLED());
#elif AP_NOTIFY_GPIO_LED_2_ENABLED
ADD_BACKEND(NEW_NOTHROW AP_BoardLED2());
#endif
#if AP_NOTIFY_GPIO_LED_1_ENABLED
ADD_BACKEND(NEW_NOTHROW GPIO_LED_1());
#endif
break;
#if AP_NOTIFY_TOSHIBALED_ENABLED
Expand Down
4 changes: 4 additions & 0 deletions libraries/AP_Notify/AP_Notify_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
#define AP_NOTIFY_GPIO_LED_2_ENABLED 0
#endif

#ifndef AP_NOTIFY_GPIO_LED_1_ENABLED
#define AP_NOTIFY_GPIO_LED_1_ENABLED 0
#endif

#ifndef AP_NOTIFY_GPIO_LED_RGB_ENABLED
#define AP_NOTIFY_GPIO_LED_RGB_ENABLED 0
#endif
Expand Down
77 changes: 77 additions & 0 deletions libraries/AP_Notify/GPIO_LED_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
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_Notify_config.h"

#if AP_NOTIFY_GPIO_LED_1_ENABLED

#include "GPIO_LED_1.h"

#include <AP_HAL/HAL.h>
#include "AP_Notify.h"

#ifndef AP_NOTIFY_GPIO_LED_A_PIN
#error "define AP_NOTIFY_GPIO_LED_A_PIN"
#endif

extern const AP_HAL::HAL& hal;

bool GPIO_LED_1::init(void)
{
// when HAL_GPIO_LED_ON is 0 then we must not use pinMode()
// as it could remove the OPENDRAIN attribute on the pin
#if HAL_GPIO_LED_ON != 0
hal.gpio->pinMode(AP_NOTIFY_GPIO_LED_A_PIN, HAL_GPIO_OUTPUT);
#endif
hal.gpio->write(AP_NOTIFY_GPIO_LED_A_PIN, HAL_GPIO_LED_OFF);
return true;
}

/*
main update function called at 50Hz
*/
void GPIO_LED_1::update(void)
{
uint32_t new_pattern;
if (AP_Notify::flags.initialising) {
new_pattern = INITIALIZING;
} else if (AP_Notify::flags.armed) {
new_pattern = ARMED;
} else if (AP_Notify::flags.pre_arm_check) {
new_pattern = READY_TO_ARM;
} else {
new_pattern = NOT_READY_TO_ARM;
}
if (new_pattern != current_pattern) {
next_bit = 0;
current_pattern = new_pattern;
last_timestep_ms = 0;
}

const uint32_t now_ms = AP_HAL::millis();
if (now_ms - last_timestep_ms < 100) {
return;
}
last_timestep_ms = now_ms;

const auto new_state = (current_pattern & (1U<<next_bit)) ? HAL_GPIO_LED_ON : HAL_GPIO_LED_OFF;
hal.gpio->write(AP_NOTIFY_GPIO_LED_A_PIN, new_state);
next_bit++;
if (next_bit > 31) {
next_bit = 0;
}
}

#endif // AP_NOTIFY_GPIO_LED_1_ENABLED
48 changes: 48 additions & 0 deletions libraries/AP_Notify/GPIO_LED_1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
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/>.
*/
#pragma once

#include "AP_Notify_config.h"

#if AP_NOTIFY_GPIO_LED_1_ENABLED

#include <AP_Common/AP_Common.h>
#include <AP_HAL/AP_HAL_Boards.h>

#include "NotifyDevice.h"

class GPIO_LED_1 : public NotifyDevice
{
public:
// initialise the LED driver
bool init(void) override;

// should be called at 50Hz
void update(void) override;

private:

// left-to-right, each bit represents 100ms
static const uint32_t INITIALIZING = 0b10101010101010101010101010101010UL;
static const uint32_t NOT_READY_TO_ARM = 0b11111111000000001111111100000000UL;
static const uint32_t READY_TO_ARM = 0b11111111111111100000000000000000UL;
static const uint32_t ARMED = 0b11111111111111111111111111111111UL;

uint32_t current_pattern = INITIALIZING;
uint32_t last_timestep_ms;
uint8_t next_bit;
};

#endif // AP_NOTIFY_GPIO_LED_1_ENABLED
3 changes: 3 additions & 0 deletions libraries/SITL/SIM_Aircraft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,9 @@ void Aircraft::update_external_payload(const struct sitl_input &input)
}
#endif

#if AP_SIM_GPIO_LED_1_ENABLED
sim_led1.update(*this);
#endif
#if AP_SIM_GPIO_LED_2_ENABLED
sim_led2.update(*this);
#endif
Expand Down
4 changes: 4 additions & 0 deletions libraries/SITL/SIM_Aircraft.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <Filter/Filter.h>
#include "SIM_JSON_Master.h"
#include "ServoModel.h"
#include "SIM_GPIO_LED_1.h"
#include "SIM_GPIO_LED_2.h"
#include "SIM_GPIO_LED_3.h"
#include "SIM_GPIO_LED_RGB.h"
Expand Down Expand Up @@ -377,6 +378,9 @@ class Aircraft {
#endif


#if AP_SIM_GPIO_LED_1_ENABLED
GPIO_LED_1 sim_led1{8}; // pin to match sitl.h
#endif
#if AP_SIM_GPIO_LED_2_ENABLED
GPIO_LED_2 sim_led2{13, 14}; // pins to match sitl.h
#endif
Expand Down
31 changes: 31 additions & 0 deletions libraries/SITL/SIM_GPIO_LED_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "SIM_config.h"

#if AP_SIM_GPIO_LED_1_ENABLED

#include "SIM_GPIO_LED_1.h"

#include <SITL/SITL.h>

using namespace SITL;

void GPIO_LED_1::init()
{
leds.init();
}

void GPIO_LED_1::update(const class Aircraft &aircraft)
{
if (!init_done) {
init();
init_done = true;
}

const uint16_t pin_mask = AP::sitl()->pin_mask.get();
const bool new_led_states[1] {
((pin_mask & uint16_t((1U<<LED_A_PIN))) != 0),
};

leds.set_state(new_led_states);
}

#endif
46 changes: 46 additions & 0 deletions libraries/SITL/SIM_GPIO_LED_1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*

Simulator for GPIO-based "Board LEDs"

./Tools/autotest/sim_vehicle.py -v ArduCopter --gdb --debug --rgbled

reboot

*/


#include "SIM_config.h"

#if AP_SIM_GPIO_LED_1_ENABLED

#include "SIM_LED_n.h"

namespace SITL {

class GPIO_LED_1
{
public:

GPIO_LED_1(uint8_t _LED_A_PIN) :
LED_A_PIN{_LED_A_PIN}
{ }

void update(const class Aircraft &aircraft);

private:

void init();
bool init_done;

SIM_LED_n<1>::LEDColour colours[1] {
SIM_LED_n<1>::LEDColour::RED,
};

SIM_LED_n<1> leds{"GPIO_LED_1", colours};

uint8_t LED_A_PIN;
};

} // namespace SITL

#endif // AP_SIM_GPIO_LED_2_ENABLED
peterbarker marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions libraries/SITL/SIM_LED_n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void SIM_LED_n<NUM_LEDS>::init()
pthread_create(&thread, NULL, update_thread_start, this);
}

template class SIM_LED_n<1>;
template class SIM_LED_n<2>;
template class SIM_LED_n<3>;

Expand Down
6 changes: 5 additions & 1 deletion libraries/SITL/SIM_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#define AP_SIM_LED_N_ENABLED (CONFIG_HAL_BOARD == HAL_BOARD_SITL) && defined(WITH_SITL_RGBLED)
#endif

#ifndef AP_SIM_GPIO_LED_1_ENABLED
#define AP_SIM_GPIO_LED_1_ENABLED AP_SIM_LED_N_ENABLED && 0
#endif

#ifndef AP_SIM_GPIO_LED_2_ENABLED
#define AP_SIM_GPIO_LED_2_ENABLED AP_SIM_LED_N_ENABLED && 0
#endif
Expand All @@ -36,7 +40,7 @@
#endif

#ifndef AP_SIM_GPIO_LED_RGB_ENABLED
#define AP_SIM_GPIO_LED_RGB_ENABLED AP_SIM_LED_N_ENABLED
#define AP_SIM_GPIO_LED_RGB_ENABLED AP_SIM_LED_N_ENABLED && 0
#endif

#ifndef AP_SIM_LOWEHEISER_ENABLED
Expand Down
Loading