Skip to content

Commit

Permalink
Basic functions (Successfully Build)
Browse files Browse the repository at this point in the history
  • Loading branch information
john0312 committed Jul 25, 2024
1 parent 1ff2a73 commit 63b5a02
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
47 changes: 25 additions & 22 deletions fw/Core/Hitcon/Logic/IrController.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "IrController.h"
#include "game.h"
#include <Logic/IrController.h>
#include <Logic/game.h>
#include <Service/IrService.h>
#include <stdlib.h>
#include <time.h>

using namespace hitcon::service::sched;

namespace hitcon {
namespace ir {

Expand All @@ -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<GamePacket*>(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<IrPacket*>(arg);
callback_col = packet->size;
callback_data = packet->data;
IrData* data = reinterpret_cast<IrData*>(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.
Expand All @@ -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;
}

Expand Down
36 changes: 25 additions & 11 deletions fw/Core/Hitcon/Logic/IrController.h
Original file line number Diff line number Diff line change
@@ -1,40 +1,54 @@
#ifndef LOGIC_IRCONTROLLER_DOT_H_
#define LOGIC_IRCONTROLLER_DOT_H_

#include <stddef.h>
#include <stdint.h>

#include <Logic/game.h>
#include <Logic/IrLogic.h>
#include <Logic/game.h>
#include <Service/IrService.h>
#include <Service/Sched/PeriodicTask.h>
#include <Service/Sched/Scheduler.h>
#include <stddef.h>
#include <stdint.h>

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);
Expand All @@ -44,7 +58,7 @@ class IrController {

int prob_f(int);

void BroadcastIr();
void BroadcastIr(void* unused);
};

} // namespace ir
Expand Down

0 comments on commit 63b5a02

Please sign in to comment.