From 512fc42a8439492edc312de7ea0a0511b1b1c281 Mon Sep 17 00:00:00 2001 From: John L Chen Date: Thu, 25 Jul 2024 23:34:48 +0800 Subject: [PATCH 1/2] Basic Functions (Some problems in Task) --- fw/Core/Hitcon/Logic/IrController.cc | 69 ++++++++++++++++------------ fw/Core/Hitcon/Logic/IrController.h | 18 +++++--- 2 files changed, 52 insertions(+), 35 deletions(-) diff --git a/fw/Core/Hitcon/Logic/IrController.cc b/fw/Core/Hitcon/Logic/IrController.cc index 10923199..c570dd30 100644 --- a/fw/Core/Hitcon/Logic/IrController.cc +++ b/fw/Core/Hitcon/Logic/IrController.cc @@ -1,55 +1,66 @@ -#include - -namespace { - -// constexpr int IrAllowedBroadcastCol[] = {...}; - -} +#include "IrController.h" +#include "game.h" +#include +#include +using namespace hitcon::service::sched; namespace hitcon { namespace ir { +constexpr int IrAllowedBroadcastCol[] = {0, 2, 3, 4, 7}; +constexpr int IrAllowBroadcastCnt = 5; + IrController::IrController() - : routine_task(950, (callback_t)&IrController::RoutineTask, this, 1000) {} - -void IrController::SendIr2Game(IrPacket& packet) { - /*if (packet.size_ != sizeof(IrData)) { - Error & return; - }*/ - IrData* ir_data = reinterpret_cast(packet.data_); - // TODO: Send ir_data to game.cc + : routine_task(950, (callback_t)&IrController::RoutineTask, this, 1000), + recv_lock(true), send_lock(true); + { + srand(time(NULL)); + Init(); + } +} + +void IrController::Send2Game() { + game_accept_data(callback_col, callback_data); + send_lock = true; } void IrController::Init() { irLogic.SetOnPacketReceived((callback_t)&IrController::OnPacketReceived, this); + // Initialize v[0] } + void IrController::OnPacketReceived(void* arg) { IrPacket* packet = reinterpret_cast(arg); + callback_col = packet->size; + callback_data = packet->data; + + if (send_lock) { + send_lock = false; + task(800, (task_callback_t)&IrController::Send2Game, (void*)this, 1000); + } +} - // Handle this packet. +int IrController::prob_f(int lf) { + return v[0] * lf * lf + v[1] * lf + v[2]; } void IrController::RoutineTask(void* unused) { // Update parameters from load factor. - // int lf = irLogic.GetLoadFactor(); - // param_xxx = f(lf); + int lf = irLogic.GetLoadFactor(); // Determine if we want to send a packet. - // int rand_num = ... - // if (rand_num < param_xxx) { - // We want to send a packet. - // int col = rand... - - // irLogic.SendPacket(..., ...); - //} + int rand_num = rand() % RAND_MAX; + if (rand_num > prob_f(lf) && send_lock) { + send_lock = false; + task(800, (callback_t)&IrController::BroadcastIr, (void*)this, 1000); + } } -void IrController::InitBroadcastService(uint8_t game_types) { - for (int i = 0; i < game_types; ++i) { - // BroadcastIr() - } +void IrController::BroadcastIr() { + int type = rand() % IrAllowBroadcastCnt; + send_lock = true; } } // namespace ir diff --git a/fw/Core/Hitcon/Logic/IrController.h b/fw/Core/Hitcon/Logic/IrController.h index a2b06f6c..9fb550ea 100644 --- a/fw/Core/Hitcon/Logic/IrController.h +++ b/fw/Core/Hitcon/Logic/IrController.h @@ -10,14 +10,12 @@ #include namespace hitcon { - namespace ir { /*Definition of IR content.*/ struct IrData { - uint8_t type; + uint8_t col; uint8_t data[GAME_DATA_SIZE]; - score_t score; }; class IrController { @@ -25,12 +23,18 @@ class IrController { IrController(); void Init(); - - void SendIr2Game(IrPacket &packet); + void Send2Game(); void InitBroadcastService(uint8_t game_types); private: + bool send_lock; + bool recv_lock; + uint8_t v[3] = {1, 1, 0}; + + uint8_t callback_col; + uint8_t *callback_data; hitcon::service::sched::PeriodicTask routine_task; + hitcon::service::sched::Task task; // Called every 1s. void RoutineTask(void* unused); @@ -38,7 +42,9 @@ class IrController { // Called on every packet. void OnPacketReceived(void* arg); - IrPacket BroadcastIr(IrData ir_data); + int prob_f(int); + + void BroadcastIr(); }; } // namespace ir From 94d97eb368cccbfc4eac9a9c95a8a5738bd1b766 Mon Sep 17 00:00:00 2001 From: John L Chen Date: Thu, 25 Jul 2024 23:37:44 +0800 Subject: [PATCH 2/2] Basic functions (Successfully Build) --- fw/Core/Hitcon/Logic/IrController.cc | 47 +++++++++++++++------------- fw/Core/Hitcon/Logic/IrController.h | 36 ++++++++++++++------- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/fw/Core/Hitcon/Logic/IrController.cc b/fw/Core/Hitcon/Logic/IrController.cc index c570dd30..04b7e51c 100644 --- a/fw/Core/Hitcon/Logic/IrController.cc +++ b/fw/Core/Hitcon/Logic/IrController.cc @@ -1,9 +1,11 @@ -#include "IrController.h" -#include "game.h" +#include +#include +#include #include #include using namespace hitcon::service::sched; + namespace hitcon { namespace ir { @@ -12,39 +14,37 @@ constexpr int IrAllowBroadcastCnt = 5; IrController::IrController() : routine_task(950, (callback_t)&IrController::RoutineTask, this, 1000), - recv_lock(true), send_lock(true); - { - srand(time(NULL)); - Init(); - } -} + broadcast_task(800, (callback_t)&IrController::BroadcastIr, this), + send2game_task(800, (callback_t)&IrController::Send2Game, this), + send_lock(true), recv_lock(true) {} -void IrController::Send2Game() { - game_accept_data(callback_col, callback_data); +void IrController::Send2Game(void* arg) { + GamePacket* game = reinterpret_cast(arg); + game_accept_data(game->col, game->data); send_lock = true; } void IrController::Init() { irLogic.SetOnPacketReceived((callback_t)&IrController::OnPacketReceived, this); - // Initialize v[0] + // TODO: Remove the srand and time + srand(time(NULL)); } - void IrController::OnPacketReceived(void* arg) { IrPacket* packet = reinterpret_cast(arg); - callback_col = packet->size; - callback_data = packet->data; + IrData* data = reinterpret_cast(packet->data_); - if (send_lock) { - send_lock = false; - task(800, (task_callback_t)&IrController::Send2Game, (void*)this, 1000); + // Game + if (data->packet_type == 0) { + if (send_lock) { + send_lock = false; + scheduler.Queue(&send2game_task, &data->game); + } } } -int IrController::prob_f(int lf) { - return v[0] * lf * lf + v[1] * lf + v[2]; -} +int IrController::prob_f(int lf) { return v[0] * lf * lf + v[1] * lf + v[2]; } void IrController::RoutineTask(void* unused) { // Update parameters from load factor. @@ -54,12 +54,15 @@ void IrController::RoutineTask(void* unused) { int rand_num = rand() % RAND_MAX; if (rand_num > prob_f(lf) && send_lock) { send_lock = false; - task(800, (callback_t)&IrController::BroadcastIr, (void*)this, 1000); + scheduler.Queue(&broadcast_task, nullptr); } } -void IrController::BroadcastIr() { +void IrController::BroadcastIr(void* unused) { int type = rand() % IrAllowBroadcastCnt; + for (int i = 0; i < GAME_DATA_SIZE; ++i) { + // Get data and send to packet + } send_lock = true; } diff --git a/fw/Core/Hitcon/Logic/IrController.h b/fw/Core/Hitcon/Logic/IrController.h index 9fb550ea..87368ade 100644 --- a/fw/Core/Hitcon/Logic/IrController.h +++ b/fw/Core/Hitcon/Logic/IrController.h @@ -1,40 +1,54 @@ #ifndef LOGIC_IRCONTROLLER_DOT_H_ #define LOGIC_IRCONTROLLER_DOT_H_ -#include -#include - -#include #include +#include +#include #include #include +#include +#include namespace hitcon { namespace ir { /*Definition of IR content.*/ -struct IrData { +struct GamePacket { uint8_t col; uint8_t data[GAME_DATA_SIZE]; }; +struct ShowPacket { + char message[16]; +}; + +/*Definition of IR content.*/ +struct IrData { + uint8_t ttl; + uint8_t packet_type; + union { + struct GamePacket game; + struct ShowPacket show; + }; +}; + class IrController { public: IrController(); void Init(); - void Send2Game(); + void Send2Game(void* game); void InitBroadcastService(uint8_t game_types); + private: bool send_lock; bool recv_lock; + // TODO: Calculate the v[] in Init uint8_t v[3] = {1, 1, 0}; - uint8_t callback_col; - uint8_t *callback_data; - hitcon::service::sched::PeriodicTask routine_task; - hitcon::service::sched::Task task; + hitcon::service::sched::Task send2game_task; + hitcon::service::sched::Task broadcast_task; // Called every 1s. void RoutineTask(void* unused); @@ -44,7 +58,7 @@ class IrController { int prob_f(int); - void BroadcastIr(); + void BroadcastIr(void* unused); }; } // namespace ir