From 26c12b7e8013f3272251f8ecfde72a36b1665d24 Mon Sep 17 00:00:00 2001 From: Gustice Date: Thu, 25 Jun 2026 17:03:45 +0200 Subject: [PATCH] starting from GPIO and OS Example --- .gitignore | 2 + .gitmodules | 3 + CMakeLists.txt | 13 ++++ platform | 1 + src/CMakeLists.txt | 12 ++++ src/swordMain.cpp | 149 +++++++++++++++++++++++++++++++++++++++++++++ src/swordMain.hpp | 29 +++++++++ 7 files changed, 209 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 160000 platform create mode 100644 src/CMakeLists.txt create mode 100644 src/swordMain.cpp create mode 100644 src/swordMain.hpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b79a54 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +.cache/ \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..fa21bbb --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "platform"] + path = platform + url = git@github.com:Gustice/RPiLnxPlatform.git diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c2fcdfc --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.10) +project(SwordProp C CXX) + +include(platform/tools/project.cmake) +ProjectSetup() + +PlatformRequirements() + +add_subdirectory(platform/extern) + +add_subdirectory(src) +addSubDirs_ofRoot("${CMAKE_CURRENT_LIST_DIR}/platform/modules") +core_addSubDirs_inAbsPath("${CMAKE_CURRENT_LIST_DIR}/platform/palCore/modules") diff --git a/platform b/platform new file mode 160000 index 0000000..92c57ba --- /dev/null +++ b/platform @@ -0,0 +1 @@ +Subproject commit 92c57ba83f8ca7914c3ba6b7f15ec46be1993551 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..2c09439 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,12 @@ +add_executable(SwordMain swordMain.cpp) + +target_link_libraries(SwordMain + PkgConfig::LIBGPIOD + base + palAdapt + loRa + os + Mqtt + fmt::fmt + utils + ) diff --git a/src/swordMain.cpp b/src/swordMain.cpp new file mode 100644 index 0000000..cff7f69 --- /dev/null +++ b/src/swordMain.cpp @@ -0,0 +1,149 @@ +#include "swordMain.hpp" + +using namespace pal; +using namespace std::literals::string_view_literals; +using namespace std::literals::chrono_literals; +// using namespace std::literals; + +constexpr int OutPin = 23; + +enum IncommingCommands_e { + Qurey, // Qurey info + Disable, // Disable Station + Maintanance, // Set Maintanance mode + SetPromoMode, // Enable PromoMode + SetGamingMode, // Enable in Game mode + NextTryLock, // Lock sword on next try + NextTryRelease // Release sword on next try +}; +enum OutgoingCommands_e { + Version, // Send Version + Status, // Send Status + TryHappend, // Message if User tried to pull the sword + SwordReleased, // Message that User pulled the sword + SwordReturned // Message that User returned the sword +}; + +/// @brief incomming / outgoing message codes +struct TxMsg { + TxMsg(std::uint32_t m) + : message(m) {}; + uint32_t message; +}; +// struct RsrsCtx { +// std::string name; +// Mutex& mtx; +// decltype(std::chrono::steady_clock::duration()) duration; +// }; + + +/// @brief MQTT-Service provider +// void LoRaServiceTask(Thread::Context& ctx, Queue& queue) { +void MQttServiceTask(Thread::Context& ctx) { + printf("MQTT service: Starting\n"); + // uint32_t cnt{}; + while (!ctx.isCancelled()) { + Thread::sleep(2s); + // blinkSem.give(); + // auto e = std::make_unique(cnt++, "tick"); + // queue.enqueue(std::move(e)); + std::cout << "MQTT service: Running ...\n"; + } +} + +/// @brief LoRa-Service provider +// void LoRaServiceTask(Thread::Context& ctx, Queue& queue) { +void LoRaServiceTask(Thread::Context& ctx) { + printf("LoRa service: Starting\n"); + // uint32_t cnt{}; + while (!ctx.isCancelled()) { + Thread::sleep(2s); + // blinkSem.give(); + // auto e = std::make_unique(cnt++, "tick"); + // queue.enqueue(std::move(e)); + std::cout << "LoRa service: Running ...\n"; + } + + // while (!ctx.isCancelled()) { + // auto msg = queue.dequeue(); // Wait with infinit timeout, guaranteed to succeed + // printf("Message: %s %lu\n", msg->message.c_str(), msg->cnt); + // } + // while (!ctx.isCancelled()) { + // blinkSem.take(); + // led.write(true); // execute blink event + // Thread::sleep(100ms); + // led.write(false); // execute blink event + // printf("Blink tick\n"); + // } +} + +// void RessourceAccessor(Thread::Context& tCtx, RsrsCtx& ctx) { +// using namespace std::chrono; +// fmt::println("starting Ressource Accessor {}", ctx.name); +// while (!tCtx.isCancelled()) { +// { +// StopWatch sw; +// ctx.mtx.claim(); +// auto e = duration_cast(sw.getEleapsed()); +// auto d = duration_cast(ctx.duration); +// fmt::println("took {}ms to quire, now access ressource for {}ms", +// (int)e.count(), (int)d.count()); +// Thread::sleep(ctx.duration); +// ctx.mtx.release(); +// } +// Thread::sleep(100ms); +// } +// } + +void itrHandler() { + TriggeredInput itr(26, "PItr"); + while (true) { + std::cout << "Await next interrupt\n"; + if (itr.await()) { + std::cout << "Interrupt Event\n"; + } + } +} + +int main(int argc, char* argv[]) { + printf("Sword Firmware\n"); + + OutputPort out(OutPin, "POut"); + InputPort in(20, "PIn"); + std::cout << "setting GPIO21 as Out and GPIO20 as input: "; + + std::thread interruptThread(itrHandler); + + // Semaphore blinkSem; + auto mqttTask = Thread::createExplicit("MqttTask", MQttServiceTask); + auto loraTask = Thread::createExplicit("LoRaTask", LoRaServiceTask); + // static Queue txQueue; + // auto mqttTask = Thread::createExplicit("MqttTask", MQttServiceTask, std::ref(txQueue)); + // auto loraTask = Thread::createExplicit("LoRaTask", LoRaServiceTask, std::ref(txQueue)); + + // Mutex rsrMtx; + // RsrsCtx rsCtx1{ + // .name = "intense", + // .mtx = rsrMtx, + // .duration = 3000ms}; + // RsrsCtx rsCtx2{ + // .name = "light", + // .mtx = rsrMtx, + // .duration = 1000ms}; + // auto ressourceAccess1 = Thread::createExplicit("IntenseAccess", RessourceAccessor, std::ref(rsCtx1)); + // auto ressourceAccess2 = Thread::createExplicit("LightAccess", RessourceAccessor, std::ref(rsCtx2)); + + fmt::print("Running App\n"); + + while (true) { + std::cout << "in=" << in.read() << "\n"; + out.write(0); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + std::cout << "in=" << in.read() << "\n"; + out.write(1); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + // std::this_thread::sleep_for(1s); + } + interruptThread.join(); + return EXIT_SUCCESS; +} diff --git a/src/swordMain.hpp b/src/swordMain.hpp new file mode 100644 index 0000000..601d630 --- /dev/null +++ b/src/swordMain.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "MqttClient.hpp" +#include "coreExcept.hpp" +#include "gpioPort.hpp" +#include "loRa.hpp" +#include "mutex.hpp" +#include "queue.hpp" +#include "semaphore.hpp" +#include "spiPort.hpp" +#include "textUtils.hpp" +#include "thread.hpp"