diff --git a/platform b/platform index 14f1879..38ed7f2 160000 --- a/platform +++ b/platform @@ -1 +1 @@ -Subproject commit 14f18793e5c83c2c4cf35382bea005f04b05ceb5 +Subproject commit 38ed7f27677040a71b34205c8cdcf8fc33cd956d diff --git a/src/interface.hpp b/src/interface.hpp index 81414f4..3374073 100644 --- a/src/interface.hpp +++ b/src/interface.hpp @@ -9,36 +9,37 @@ enum ConMode_e { }; enum IncommingCommands_e : uint16_t { - Qurey, // Qurey info - SetMaintanance, // Set Maintanance mode - SetPromoMode, // Enable PromoMode - SetGamingMode, // Enable in Game mode + Qurey = 0x00, // Qurey info + SetMaintanance = 0x01, // Set Maintanance mode + SetPromoMode = 0x02, // Enable PromoMode + SetGamingMode = 0x03, // Enable in Game mode }; enum ServiceCommands_e { - Open, - Close, - Play1, - Play2, - Play3, + SerQuery = 0x10, + Open = 0x11, + Close = 0x12, + Play1 = 0x13, + Play2 = 0x14, + Play3 = 0x15, }; enum GameModeCommands_e { - GameNextTryLocked, // Lock sword on next try - GameNextTryRelease // Release sword on next try + GameNextTryLocked = 0x21, // Lock sword on next try + GameNextTryRelease = 0x22 // Release sword on next try }; enum OutgoingCommands_e { - Status, // Send Status - Response, // Confirm last command - Error, // Send error status - PullHappend // Message if User tried to pull the sword + Status = 0x00, // Send Status + Response = 0x01, // Confirm last command + Error = 0x02, // Send error status + PullHappend = 0x03 // Message if User tried to pull the sword }; enum GameModeResponses_e { - SwordPullHappend, // Message if User tried to pull the sword - SwordReleased, // Message that User pulled the sword - SwordReturned // Message that User returned the sword + SwordPullHappend = 0x11, // Message if User tried to pull the sword + SwordReleased = 0x12, // Message that User pulled the sword + SwordReturned = 0x13 // Message that User returned the sword }; struct StatusFrame { diff --git a/src/swordMain.cpp b/src/swordMain.cpp index 5ae905c..ed0b22e 100644 --- a/src/swordMain.cpp +++ b/src/swordMain.cpp @@ -8,7 +8,7 @@ using namespace std::literals::chrono_literals; using namespace std; // using namespace std::literals; -const string BrockerUrl = "mqtts://nerdyssey.de:8883"; +const string BrockerUrl = "mqtts://nerdyssey.de:8883"; const string MqttUser = "exodususer"; const string MqttPassword = "awesomeexodusoutdooradventures"; @@ -167,7 +167,7 @@ void LoRaServiceTask(Thread::Context& ctx, Queue& rxQueue, Queue& rxQueue.enqueue(std::move(e)); return true; }; - auto spi = SpiPort({.mode = SpiPort::mode0, + auto spi = SpiPort({.mode = SpiPort::Mode0, .cs = SpiPort::Cs0, .frequ = 1'000'000}); @@ -196,16 +196,30 @@ void LoRaServiceTask(Thread::Context& ctx, Queue& rxQueue, Queue& int main(int argc, char* argv[]) { fmt::println("Sword Firmware"); + if (argc > 1) { + fmt::println("Got Startup Argument"); + auto arg1 = string{argv[1]}; + if (arg1 == "promo") { + fmt::println("starting with promo"); + Mode = OpMode::PromoMode; + } else if (arg1 == "game") { + fmt::println("starting with promo"); + Mode = OpMode::PromoMode; + } else { + fmt::println("unknown {}", arg1); + } + } + auto itrHandler = [&]() { TriggeredInput itr(26, "PItr"); while (true) { - std::cout << "Await next interrupt\n"; + cout << "Await next interrupt\n"; if (itr.await()) { - std::cout << "Interrupt Event\n"; + cout << "Interrupt Event\n"; } } }; - std::thread interruptThread(itrHandler); + thread interruptThread(itrHandler); InputPort spPort(SwordPulledPin, "PInSPull"); InputPort sbPort(SwordEntryPin, "PinSEntry"); @@ -227,159 +241,181 @@ int main(int argc, char* argv[]) { static Queue rxQueue; static Queue txQueue; - static auto conTask = (ConMode == ModeMqtt) ? Thread::createExplicit("MqttTask", MQttServiceTask, std::ref(rxQueue), std::ref(txQueue)) - : Thread::createExplicit("LoRaTask", LoRaServiceTask, std::ref(rxQueue), std::ref(txQueue)); + static auto conTask = (ConMode == ModeMqtt) ? Thread::createExplicit("MqttTask", MQttServiceTask, ref(rxQueue), ref(txQueue)) + : Thread::createExplicit("LoRaTask", LoRaServiceTask, ref(rxQueue), ref(txQueue)); SwordLatch.moveOut(); - const static map opCodes = { - {SetMaintanance, OpMode::Maintanance}, - {SetPromoMode, OpMode::PromoMode}, - {SetGamingMode, OpMode::GamingMode}, - }; - fmt::println("Running App"); while (true) { - auto msg = rxQueue.dequeue(); - fmt::println("MAIN: Got message {}", msg->code); + // auto msg = rxQueue.dequeue(); + // fmt::println("MAIN: Got message {}", msg->code); + // auto code = static_cast(msg->code); + // if (opCodes.contains(code)) { + // Mode == opCodes.at(code); + // } else { + // Mode = OpMode::Undefined; + // } + // fmt::println("running Maintanance"); + // auto e = make_unique(msg->code, 0); + // txQueue.enqueue(move(e)); - auto code = static_cast(msg->code); - if (opCodes.contains(code)) { - Mode == opCodes.at(code); - } else { - Mode = OpMode::Undefined; - } + while (true) { + /* code */ - switch (Mode) { - case OpMode::Maintanance: { - fmt::println("Got Maintanance"); - auto e = std::make_unique(msg->code, 0); - txQueue.enqueue(std::move(e)); - // txQueue.emplace(msg->code, 0); + switch (Mode) { + case OpMode::Maintanance: { + fmt::println("Running Maintanance"); + // txQueue.emplace(msg->code, 0); + while (true) { + auto serMsg = rxQueue.tryDequeueFor(1000ms); + if (serMsg) { + auto code = static_cast(serMsg->code); + switch (code) { + case ServiceCommands_e::Open: { + IoStat->SwordLatch.moveIn(); + } break; + case ServiceCommands_e::Close: { + IoStat->SwordLatch.moveOut(); + } break; + case ServiceCommands_e::Play1: { + playWavFile(soundPromo); + } break; + case ServiceCommands_e::Play2: { + playWavFile(soundDeny); + } break; + case ServiceCommands_e::Play3: { + playWavFile(soundAllow); + } break; + } + } + tick(); + auto e = make_unique( + (uint8_t)Mode, + IoStat->SwordEnd.get(), + IoStat->SwordEntry.get(), + IoStat->SwordPulled.get(), + IoStat->SwordLatch.isLocked()); + txQueue.enqueue(move(e)); + } + // auto e = make_unique(msg->code, 0); + // txQueue.enqueue(move(e)); + } break; + case OpMode::PromoMode: { + fmt::println("Running PromoMode"); + auto tn = chrono::steady_clock::now(); + while (true) { + auto serMsg = rxQueue.tryDequeueFor(10ms); + if (serMsg) { + auto code = static_cast(serMsg->code); + switch (code) { + case ServiceCommands_e::Open: { + IoStat->SwordLatch.moveIn(); + } break; + case ServiceCommands_e::Close: { + IoStat->SwordLatch.moveOut(); + } break; + case ServiceCommands_e::Play1: { + playWavFile(soundPromo); + } break; + case ServiceCommands_e::Play2: { + playWavFile(soundDeny); + } break; + case ServiceCommands_e::Play3: { + playWavFile(soundAllow); + } break; + } + } + tick(); + if (SwordPulled.isPosEdge()) { + playWavFile(soundPromo); + Thread::sleep(15s); + } - while (true) { - auto serMsg = rxQueue.tryDequeueFor(1000ms); - if (serMsg) { - auto code = static_cast(serMsg->code); + if (chrono::steady_clock::now() > (tn + 1s)) { + tn = chrono::steady_clock::now(); + auto e = make_unique( + (uint8_t)Mode, + IoStat->SwordEnd.get(), + IoStat->SwordEntry.get(), + IoStat->SwordPulled.get(), + IoStat->SwordLatch.isLocked()); + txQueue.enqueue(move(e)); + } + } + } break; + case OpMode::GamingMode: { + fmt::println("Running GamingMode"); + while (true) { + auto gameMsg = rxQueue.dequeue(); + auto code = static_cast(gameMsg->code); switch (code) { - case ServiceCommands_e::Open: { - IoStat->SwordLatch.moveIn(); + case GameModeCommands_e::GameNextTryLocked: { + auto e = make_unique(gameMsg->code, 0); + txQueue.enqueue(move(e)); + + fmt::println("Got NextTryLock"); + while (true) { + SwordPulled.sample(); + auto rx = txQueue.tryDequeueFor(20ms); + bool pulled = SwordPulled.read(); + if (SwordPulled.isPosEdge()) { + // say: You are not the one + Thread::sleep(5s); + } + } } break; - case ServiceCommands_e::Close: { - IoStat->SwordLatch.moveOut(); - } break; - case ServiceCommands_e::Play1: { - playWavFile(soundPromo); - } break; - case ServiceCommands_e::Play2: { - playWavFile(soundDeny); - } break; - case ServiceCommands_e::Play3: { - playWavFile(soundAllow); + case GameModeCommands_e::GameNextTryRelease: { + auto e = make_unique(gameMsg->code, 0); + txQueue.enqueue(move(e)); + SwordLatch.moveIn(); + + fmt::println("Got NextTryRelease"); + while (true) { + SwordEnd.sample(); + auto rx = txQueue.tryDequeueFor(20ms); + if (SwordEnd.isNegEdge()) { + break; + } + } + while (true) { + SwordEntry.sample(); + auto rx = txQueue.tryDequeueFor(20ms); + if (SwordEntry.isNegEdge()) { + break; + } + } + + // say: The prophecy is fulfilled + e = make_unique(gameMsg->code, 0); + txQueue.enqueue(move(e)); + + while (true) { + SwordEnd.sample(); + auto rx = txQueue.tryDequeueFor(20ms); + if (SwordEnd.isNegEdge()) { + break; + } + } + while (true) { + SwordEntry.sample(); + auto rx = txQueue.tryDequeueFor(20ms); + if (SwordEntry.isNegEdge()) { + break; + } + } + + e = make_unique(gameMsg->code, 0); + txQueue.enqueue(move(e)); + } break; } } + } break; - auto e = std::make_unique( - (uint8_t)Mode, - IoStat->SwordEnd.get(), - IoStat->SwordEntry.get(), - IoStat->SwordPulled.get(), - IoStat->SwordLatch.isLocked()); - txQueue.enqueue(std::move(e)); - } - e = std::make_unique(msg->code, 0); - txQueue.enqueue(std::move(e)); - } break; - case OpMode::PromoMode: { - fmt::println("Got SetPromoMode"); - auto e = std::make_unique(msg->code, 0); - txQueue.enqueue(std::move(e)); - - while (true) { - SwordPulled.sample(); - auto rx = txQueue.tryDequeueFor(10ms); - if (SwordPulled.isPosEdge()) { - // say: Play our tours - Thread::sleep(5s); - } - } - } break; - case OpMode::GamingMode: { - fmt::println("Got SetGamingMode"); - auto e = std::make_unique(msg->code, 0); - txQueue.enqueue(std::move(e)); - - while (true) { - auto gameMsg = txQueue.dequeue(); - auto code = static_cast(msg->code); - switch (code) { - case GameModeCommands_e::GameNextTryLocked: { - auto e = std::make_unique(msg->code, 0); - txQueue.enqueue(std::move(e)); - - fmt::println("Got NextTryLock"); - while (true) { - SwordPulled.sample(); - auto rx = txQueue.tryDequeueFor(20ms); - bool pulled = SwordPulled.read(); - if (SwordPulled.isPosEdge()) { - // say: You are not the one - Thread::sleep(5s); - } - } - } break; - case GameModeCommands_e::GameNextTryRelease: { - auto e = std::make_unique(msg->code, 0); - txQueue.enqueue(std::move(e)); - SwordLatch.moveIn(); - - fmt::println("Got NextTryRelease"); - while (true) { - SwordEnd.sample(); - auto rx = txQueue.tryDequeueFor(20ms); - if (SwordEnd.isNegEdge()) { - break; - } - } - while (true) { - SwordEntry.sample(); - auto rx = txQueue.tryDequeueFor(20ms); - if (SwordEntry.isNegEdge()) { - break; - } - } - - // say: The prophecy is fulfilled - e = std::make_unique(msg->code, 0); - txQueue.enqueue(std::move(e)); - - while (true) { - SwordEnd.sample(); - auto rx = txQueue.tryDequeueFor(20ms); - if (SwordEnd.isNegEdge()) { - break; - } - } - while (true) { - SwordEntry.sample(); - auto rx = txQueue.tryDequeueFor(20ms); - if (SwordEntry.isNegEdge()) { - break; - } - } - - e = std::make_unique(msg->code, 0); - txQueue.enqueue(std::move(e)); - - } break; - } - } - - } break; - - default: - break; + default: + break; + } } } interruptThread.join();