Compare commits

...

7 commits

5 changed files with 109 additions and 15 deletions

View file

@ -15,4 +15,7 @@ framework = arduino
board_build.partitions = huge_app.csv board_build.partitions = huge_app.csv
lib_deps = etlcpp/Embedded Template Library@^20.35.12 lib_deps = etlcpp/Embedded Template Library@^20.35.12
monitor_speed=115200 monitor_speed=115200
upload_port=/dev/ttyUSB1 build_flags=-Wall
check_tool = clangtidy
check_flags =
clangtidy: --checks=-*,clang-diagnostic-*,-clang-diagnostic-unused-value,clang-analyzer-*,-*,bugprone-*,performance-*,readability-*,-readability-magic-numbers,-readability-braces-around-statements,-readability-inconsistent-declaration-parameter-name,-readability-named-parameter --fix

View file

@ -8,7 +8,7 @@ freertos::Task::Task(TaskFunction_t taskFunction, const etl::string_view name,
params, priority, &handle, coreID); params, priority, &handle, coreID);
} }
freertos::Task::Task(Task &&other) freertos::Task::Task(Task &&other) noexcept
: name{etl::move(other.name)}, handle{etl::move(other.handle)}, : name{etl::move(other.name)}, handle{etl::move(other.handle)},
stackDepth{etl::move(other.stackDepth)}, stackDepth{etl::move(other.stackDepth)},
priority{etl::move(other.priority)}, coreID{etl::move(other.coreID)} {} priority{etl::move(other.priority)}, coreID{etl::move(other.coreID)} {}
@ -19,7 +19,7 @@ freertos::Task::~Task() {
} }
} }
auto freertos::Task::operator=(const Task &&other) -> Task & { auto freertos::Task::operator=(const Task &&other) noexcept -> Task & {
name = etl::move(other.name); name = etl::move(other.name);
handle = etl::move(other.handle); handle = etl::move(other.handle);
stackDepth = etl::move(other.stackDepth); stackDepth = etl::move(other.stackDepth);
@ -28,16 +28,16 @@ auto freertos::Task::operator=(const Task &&other) -> Task & {
return *this; return *this;
} }
auto freertos::Task::getName() -> etl::string_view { return name; } auto freertos::Task::getName() const -> etl::string_view { return name; }
auto freertos::Task::getStackDepth() -> uint32_t { return stackDepth; } auto freertos::Task::getStackDepth() const -> uint32_t { return stackDepth; }
auto freertos::Task::getPriority() -> UBaseType_t { return priority; } auto freertos::Task::getPriority() const -> UBaseType_t { return priority; }
auto freertos::Task::setPriority(const UBaseType_t newPriority) -> void { auto freertos::Task::setPriority(const UBaseType_t newPriority) -> void {
vTaskPrioritySet(handle, newPriority); vTaskPrioritySet(handle, newPriority);
} }
auto freertos::Task::getCoreID() -> BaseType_t { return coreID; } auto freertos::Task::getCoreID() const -> BaseType_t { return coreID; }
auto freertos::Task::isValid() -> bool { return status == pdPASS; } auto freertos::Task::isValid() const -> bool { return status == pdPASS; }

View file

@ -9,17 +9,17 @@ public:
const uint32_t stackDepth, void *const params, UBaseType_t priority, const uint32_t stackDepth, void *const params, UBaseType_t priority,
const BaseType_t coreID); const BaseType_t coreID);
Task(const Task &other) = delete; Task(const Task &other) = delete;
Task(Task &&other); Task(Task &&other) noexcept;
~Task(); ~Task();
Task &operator=(const Task &other) = delete; Task &operator=(const Task &other) = delete;
Task &operator=(const Task &&other); Task &operator=(const Task &&other) noexcept;
etl::string_view getName(); etl::string_view getName() const;
uint32_t getStackDepth(); uint32_t getStackDepth() const;
UBaseType_t getPriority(); UBaseType_t getPriority() const;
void setPriority(const UBaseType_t newPriority); void setPriority(const UBaseType_t newPriority);
BaseType_t getCoreID(); BaseType_t getCoreID() const;
bool isValid(); bool isValid() const;
private: private:
etl::string_view name; etl::string_view name;

View file

@ -0,0 +1,8 @@
#pragma once
#include <Arduino.h>
namespace freertos {
/// @brief Blocks the task for the specified amount of time
/// @param time in milliseconds
void sleep(int time) { vTaskDelay(time * portTICK_PERIOD_MS); }
} // namespace freertos

View file

@ -0,0 +1,83 @@
#pragma once
#include <Arduino.h>
#include <FreeRTOS/Queue.h>
#include <FreeRTOS/Task.h>
#include <etl/optional.h>
#include <etl/span.h>
namespace LightController {
enum class LightActionType { INCREASE, DECREASE, TOGGLE, ON, OFF };
using LightAction = etl::pair<LightActionType, etl::optional<uint8_t>>;
template <int... PIN> class Controller {
public:
Controller(size_t actionQueueSize, UBaseType_t priority, BaseType_t coreID)
: lightPins{PIN...}, lightActions{actionQueueSize},
lightControllerTask{exec, "control lights", stackDepth,
this, priority, coreID} {
for (int pin : lightPins) {
pinMode(pin, OUTPUT);
}
}
freertos::Queue<LightAction> &getActionQueue() { return lightActions; }
constexpr size_t lightCount() { return lightPins.size(); }
const etl::span<int> getPins() { return lightPins; }
private:
static void exec(void *controllerPtr) {
Controller<PIN...> *controller{
static_cast<Controller<PIN...> *>(controllerPtr)};
etl::array<uint8_t, sizeof...(PIN)> lightStates;
const uint8_t step = 5;
while (true) {
auto action{controller->getActionQueue().pop()};
if (action.has_value()) {
LightActionType type{action.value().first};
etl::optional<uint8_t> index{action.value().second};
switch (type) {
case LightActionType::INCREASE:
if (index.has_value() && index.value() < lightStates.size() &&
lightStates[index.value()] <= 255 - step) {
lightStates[index.value()] += step;
}
break;
case LightActionType::DECREASE:
if (index.has_value() && index.value() < lightStates.size() &&
lightStates[index.value()] >= 0) {
lightStates[index.value()] -= step;
}
break;
case LightActionType::TOGGLE:
if (index.has_value() && index.value() < lightStates.size()) {
lightStates[index.value()] =
lightStates[index.value()] == 0 ? 255 : 0;
}
break;
case LightActionType::ON:
lightStates.fill(255);
break;
case LightActionType::OFF:
lightStates.fill(0);
break;
default:
break;
}
for (size_t lightIndex = 0; lightIndex < lightStates.size();
++lightIndex) {
analogWrite(controller->getPins()[lightIndex],
lightStates[lightIndex]);
}
}
}
}
static const uint32_t stackDepth = 2048;
etl::array<int, sizeof...(PIN)> lightPins;
freertos::Queue<LightAction> lightActions;
freertos::Task lightControllerTask;
};
} // namespace LightController