Skip to content
Snippets Groups Projects
Commit 833f8169 authored by Federico Lolli's avatar Federico Lolli Committed by Emilio Corigliano
Browse files

[ARP] Add a Leds class utility

This aims to have a central class responsible for altering led workings
(both in blocking and non-blocking way)
parent 8ceb9b14
No related branches found
No related tags found
1 merge request!48[GS, ARP] New entrypoint for lyra_gs and ARP related things
......@@ -146,6 +146,7 @@ set(GROUNDSTATION_AUTOMATED
src/boards/Groundstation/Automated/Follower/Follower.cpp
src/boards/Groundstation/Automated/Ports/Ethernet.cpp
src/boards/Groundstation/Automated/Hub.cpp
src/boards/Groundstation/Automated/Leds/Leds.cpp
)
set(ANTENNAS
......
/* Copyright (c) 2024 Skyward Experimental Rocketry
* Author: Federico Lolli
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "Leds.h"
#include <miosix.h>
using namespace miosix;
using namespace Boardcore;
namespace Antennas
{
bool Leds::start()
{
// turn off all leds
ledOff();
if (!ActiveObject::start())
{
return false;
}
return true;
}
void Leds::run()
{
while (!shouldStop())
{
// we avoid using mutex to avoid waiting for the release of it
// data races here are not a problem
if (blinking_red)
{
userLed4::high();
Thread::sleep(100);
userLed4::low();
Thread::sleep(100);
}
else if (yellow_blink)
{
led2On();
Thread::sleep(50);
led2Off();
Thread::sleep(50);
}
else if (orange_blinking)
{
led3On();
Thread::sleep(50);
led3Off();
Thread::sleep(50);
}
}
}
void Leds::errorLoop()
{
while (true)
{
userLed4::high();
Thread::sleep(100);
userLed4::low();
Thread::sleep(100);
}
}
} // namespace Antennas
/* Copyright (c) 2024 Skyward Experimental Rocketry
* Author: Federico Lolli
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once
#include <ActiveObject.h>
#include <miosix.h>
#include <utils/ModuleManager/ModuleManager.hpp>
namespace Antennas
{
/**
* @brief Utility to handle blinking leds with non-blocking sleep
* (useful for state machines states that need to blink leds without blocking)
*/
class Leds : public Boardcore::Module, protected Boardcore::ActiveObject
{
public:
Leds() {}
~Leds() { ActiveObject::stop(); }
/**
* @brief Start the blinking LED thread
*/
bool start();
/**
* @brief start blinking red led
*/
void start_blinking_red() { blinking_red = true; }
/**
* @brief stop blinking red led
*/
void stop_blinking_red() { blinking_red = false; }
/**
* @brief turn on green led
*/
void turn_on_green() { led1On(); }
/**
* @brief start blinking yellow led
*/
void start_blinking_yellow() { yellow_blink = true; }
/**
* @brief stop blinking yellow led
*/
void stop_blinking_yellow()
{
yellow_blink = false;
Thread::sleep(101); // to avoid data races with led2On/Off
}
/**
* @brief turn on yellow led
*/
void turn_on_yellow() { led2On(); }
/**
* @brief turn off yellow led
*/
void turn_off_yellow() { led2Off(); }
/**
* @brief start blinking orange led
*/
void start_blinking_orange() { orange_blinking = true; }
/**
* @brief stop blinking orange led
*/
void stop_blinking_orange()
{
orange_blinking = false;
Thread::sleep(101); // to avoid data races with led3On/Off
}
/**
* @brief turn on orange led
*/
void turn_on_orange() { led3On(); }
/**
* @brief turn off orange led
*/
void turn_off_orange() { led3Off(); }
/**
* @brief Blink the red led in a loop, blocking the thread.
*/
static void errorLoop();
protected:
void run() override;
private:
std::atomic_bool blinking_red{false};
std::atomic_bool yellow_blink{false};
std::atomic_bool orange_blinking{false};
};
} // namespace Antennas
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment