Skip to content

Commit

Permalink
Basic Functions (Some problems in Task)
Browse files Browse the repository at this point in the history
  • Loading branch information
john0312 committed Jul 25, 2024
1 parent 9efe979 commit 1ff2a73
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 35 deletions.
69 changes: 40 additions & 29 deletions fw/Core/Hitcon/Logic/IrController.cc
Original file line number Diff line number Diff line change
@@ -1,55 +1,66 @@
#include <Logic/IrController.h>

namespace {

// constexpr int IrAllowedBroadcastCol[] = {...};

}
#include "IrController.h"
#include "game.h"
#include <stdlib.h>
#include <time.h>

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<IrData*>(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<IrPacket*>(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
Expand Down
18 changes: 12 additions & 6 deletions fw/Core/Hitcon/Logic/IrController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,41 @@
#include <Service/Sched/Scheduler.h>

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 {
public:
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);

// Called on every packet.
void OnPacketReceived(void* arg);

IrPacket BroadcastIr(IrData ir_data);
int prob_f(int);

void BroadcastIr();
};

} // namespace ir
Expand Down

0 comments on commit 1ff2a73

Please sign in to comment.