Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work in progress IrController by Lisa #30

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 40 additions & 26 deletions fw/Core/Hitcon/Logic/IrController.cc
Original file line number Diff line number Diff line change
@@ -1,55 +1,69 @@
#include <Logic/IrController.h>
#include <Logic/game.h>
#include <Service/IrService.h>
#include <stdlib.h>
#include <time.h>

namespace {

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

}
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),
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(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);
// TODO: Remove the srand and time
srand(time(NULL));
}

void IrController::OnPacketReceived(void* arg) {
IrPacket* packet = reinterpret_cast<IrPacket*>(arg);
IrData* data = reinterpret_cast<IrData*>(packet->data_);

// Handle this packet.
// 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]; }

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;
scheduler.Queue(&broadcast_task, nullptr);
}
}

void IrController::InitBroadcastService(uint8_t game_types) {
for (int i = 0; i < game_types; ++i) {
// 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;
}

} // namespace ir
Expand Down
42 changes: 31 additions & 11 deletions fw/Core/Hitcon/Logic/IrController.h
Original file line number Diff line number Diff line change
@@ -1,44 +1,64 @@
#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 {
uint8_t type;
struct GamePacket {
uint8_t col;
uint8_t data[GAME_DATA_SIZE];
score_t score;
};

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 SendIr2Game(IrPacket &packet);
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};

hitcon::service::sched::PeriodicTask routine_task;
hitcon::service::sched::Task send2game_task;
hitcon::service::sched::Task broadcast_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(void* unused);
};

} // namespace ir
Expand Down
Loading