This commit is contained in:
2026-02-01 12:56:05 +01:00
parent f51adeecca
commit 0bdbcb1657
857 changed files with 0 additions and 97661 deletions

View File

@@ -0,0 +1,4 @@
idf_component_register(
SRCS "com/swtimer.cpp"
INCLUDE_DIRS "com"
)

View File

@@ -0,0 +1,81 @@
/**
* @file swtimer.cpp
* @brief Software Timer component implementation
* @author Mahmoud Elmohtady
* @company Nabd solutions - ASF
* @copyright Copyright (c) 2025
*/
#include "swtimer.hpp"
SoftwareTimer::SoftwareTimer()
: m_isActive(false)
, m_interval(0)
, m_type(TimerType::ONE_SHOT)
{
}
SoftwareTimer::~SoftwareTimer()
{
stop();
}
bool SoftwareTimer::create(uint32_t interval_ms, TimerType type, TimerCallback callback)
{
if (interval_ms == 0)
{
return false;
}
m_interval = interval_ms;
m_type = type;
m_callback = callback;
return true;
}
bool SoftwareTimer::start()
{
if (m_callback == nullptr)
{
return false;
}
m_isActive = true;
// TODO: Implement actual timer start logic
return true;
}
bool SoftwareTimer::stop()
{
if (!m_isActive)
{
return false;
}
m_isActive = false;
// TODO: Implement actual timer stop logic
return true;
}
bool SoftwareTimer::reset()
{
if (!m_isActive)
{
return false;
}
stop();
return start();
}
bool SoftwareTimer::isActive() const
{
return m_isActive;
}
uint32_t SoftwareTimer::getInterval() const
{
return m_interval;
}

View File

@@ -0,0 +1,95 @@
/**
* @file swtimer.hpp
* @brief Software Timer component header - Manages time-based events and scheduling
* @author Mahmoud Elmohtady
* @company Nabd solutions - ASF
* @copyright Copyright (c) 2025
*/
#ifndef SWTIMER_HPP
#define SWTIMER_HPP
#include <cstdint>
#include <functional>
/**
* @brief Timer type enumeration
*/
enum class TimerType
{
ONE_SHOT, ///< Timer expires once and stops
PERIODIC ///< Timer repeats until stopped
};
/**
* @brief Timer callback function type
*/
using TimerCallback = std::function<void(void)>;
/**
* @brief Software Timer class
*
* Provides functionality to create, start, stop, and manage timers
* that can trigger callbacks after specified intervals.
*/
class SoftwareTimer
{
public:
/**
* @brief Constructor
*/
SoftwareTimer();
/**
* @brief Destructor
*/
~SoftwareTimer();
/**
* @brief Create a new timer
* @param interval_ms Timer interval in milliseconds
* @param type Timer type (ONE_SHOT or PERIODIC)
* @param callback Function to call when timer expires
* @return true if timer created successfully, false otherwise
*/
bool create(uint32_t interval_ms, TimerType type, TimerCallback callback);
/**
* @brief Start the timer
* @return true if started successfully, false otherwise
*/
bool start();
/**
* @brief Stop the timer
* @return true if stopped successfully, false otherwise
*/
bool stop();
/**
* @brief Reset the timer
* @return true if reset successfully, false otherwise
*/
bool reset();
/**
* @brief Check if timer is active
* @return true if timer is running, false otherwise
*/
bool isActive() const;
/**
* @brief Get the timer interval
* @return Timer interval in milliseconds
*/
uint32_t getInterval() const;
private:
bool m_isActive; ///< Timer active state
uint32_t m_interval; ///< Timer interval in milliseconds
TimerType m_type; ///< Timer type
TimerCallback m_callback; ///< Callback function
};
#endif // SWTIMER_HPP

View File

@@ -0,0 +1,108 @@
/**
* @file test_swtimer.cpp
* @brief Unit tests for Software Timer component
* @author Mahmoud Elmohtady
* @company Nabd solutions - ASF
* @copyright Copyright (c) 2025
*/
#include "unity.h"
#include "swtimer.hpp"
extern "C" {
void setUp(void)
{
// Set up test fixtures before each test
}
void tearDown(void)
{
// Clean up test fixtures after each test
}
/**
* @brief Test timer creation with valid parameters
*/
void test_swtimer_create_valid(void)
{
SoftwareTimer timer;
bool result = timer.create(1000, TimerType::ONE_SHOT, [](){});
TEST_ASSERT_TRUE(result);
TEST_ASSERT_FALSE(timer.isActive());
TEST_ASSERT_EQUAL(1000, timer.getInterval());
}
/**
* @brief Test timer creation with zero interval
*/
void test_swtimer_create_zero_interval(void)
{
SoftwareTimer timer;
bool result = timer.create(0, TimerType::ONE_SHOT, [](){});
TEST_ASSERT_FALSE(result);
}
/**
* @brief Test timer start
*/
void test_swtimer_start(void)
{
SoftwareTimer timer;
timer.create(1000, TimerType::ONE_SHOT, [](){});
bool result = timer.start();
TEST_ASSERT_TRUE(result);
TEST_ASSERT_TRUE(timer.isActive());
}
/**
* @brief Test timer stop
*/
void test_swtimer_stop(void)
{
SoftwareTimer timer;
timer.create(1000, TimerType::ONE_SHOT, [](){});
timer.start();
bool result = timer.stop();
TEST_ASSERT_TRUE(result);
TEST_ASSERT_FALSE(timer.isActive());
}
/**
* @brief Test timer reset
*/
void test_swtimer_reset(void)
{
SoftwareTimer timer;
timer.create(1000, TimerType::ONE_SHOT, [](){});
timer.start();
bool result = timer.reset();
TEST_ASSERT_TRUE(result);
TEST_ASSERT_TRUE(timer.isActive());
}
/**
* @brief Test timer one-shot type
*/
void test_swtimer_one_shot(void)
{
SoftwareTimer timer;
timer.create(1000, TimerType::ONE_SHOT, [](){});
TEST_ASSERT_TRUE(timer.start());
}
/**
* @brief Test timer periodic type
*/
void test_swtimer_periodic(void)
{
SoftwareTimer timer;
timer.create(500, TimerType::PERIODIC, [](){});
TEST_ASSERT_TRUE(timer.start());
}
} // extern "C"