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

Implement SendAll in XBoard #132

Merged
merged 1 commit into from
Aug 17, 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
38 changes: 35 additions & 3 deletions fw/Core/Hitcon/Logic/XBoardGameController.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ void XBoardGameController::Init() {
xboard::g_xboard_logic.SetOnPacketArrive(
(callback_t)&XBoardGameController::RecvAck, this,
xboard::RecvFnId::XBOARD_GAME_CONTROLLER_ACK);
xboard::g_xboard_logic.SetOnPacketArrive(
(callback_t)&XBoardGameController::OnSendAllTrigger, this,
xboard::RecvFnId::XBOARD_GAME_CONTROLLER_SEND_ALL_TRIGGER);
scheduler.Queue(&_send_routine, nullptr);
scheduler.EnablePeriodic(&_send_routine);
}
Expand All @@ -64,8 +67,20 @@ void XBoardGameController::SendOneData() {
if (remote_buffer_left_ > 0) remote_buffer_left_--;
}

void XBoardGameController::SendExactData(int col, int row) {
hitcon::ir::GamePacket to_send;
memcpy(to_send.data, game::gameLogic.GetDataCell(col, row), game::kDataSize);
to_send.col = static_cast<uint8_t>(col);
xboard::g_xboard_logic.QueueDataForTx(
reinterpret_cast<uint8_t*>(&to_send), sizeof(to_send),
xboard::RecvFnId::XBOARD_GAME_CONTROLLER);
}

void XBoardGameController::SendAllData() {
// TODO
if (send_state == Idle) {
send_state = SendingAll;
send_all_idx_ = 0;
}
}

// private functions
Expand All @@ -74,9 +89,9 @@ bool XBoardGameController::IsBusy() { return send_state != Idle; }

void XBoardGameController::SendRoutine() {
current_cycle_++;
if (!connected_) return;
if (!connected_ && send_state != SendingAll) return;

if (current_cycle_ % 8 == 0) {
if (connected_ && current_cycle_ % 8 == 0) {
SendAck(0);
}

Expand Down Expand Up @@ -105,9 +120,26 @@ void XBoardGameController::SendRoutine() {
TryExitApp();
}
break;
case SendingAll: {
for (int i = 0; i < 2; i++) {
int col = send_all_idx_ / game::kNumRows;
int row = send_all_idx_ % game::kNumRows;
SendExactData(col, row);
send_all_idx_++;
if (send_all_idx_ >= game::kNumRows * game::kNumCols) {
send_state = Idle;
break;
}
}
break;
}
}
}

void XBoardGameController::OnSendAllTrigger(xboard::PacketCallbackArg* opkt) {
SendAllData();
}

void XBoardGameController::RecvAck(xboard::PacketCallbackArg* opkt) {
if (opkt->len != sizeof(AckPacket)) {
malformed_ackpkt_++;
Expand Down
10 changes: 7 additions & 3 deletions fw/Core/Hitcon/Logic/XBoardGameController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace hitcon {

namespace xboard_game_controller {
enum SendState { Idle, Sending, WaitAck, Acked };
enum SendState { Idle, Sending, WaitAck, Acked, SendingAll };

/*Definition of IR content.*/
struct AckPacket {
Expand Down Expand Up @@ -46,8 +46,7 @@ class XBoardGameController {
int random_send_left_;

// The current index we're sending for SendAllData().
int send_all_col_;
int send_all_row_;
int send_all_idx_;

// How many slot does the remote have available for receiving data?
int remote_buffer_left_;
Expand All @@ -74,6 +73,8 @@ class XBoardGameController {
// Sends one data with GetRandomDataForXBoardTransmission().
void SendOneData();

void SendExactData(int col, int row);

// Callback for receiving AckPacket.
void RecvAck(hitcon::service::xboard::PacketCallbackArg* opkt);

Expand All @@ -83,6 +84,9 @@ class XBoardGameController {
// remote side
void RemoteRecv(hitcon::service::xboard::PacketCallbackArg* pkt);

// Called when the other side wants all data.
void OnSendAllTrigger(hitcon::service::xboard::PacketCallbackArg* opkt);

void TryExitApp();
};

Expand Down
1 change: 1 addition & 0 deletions fw/Core/Hitcon/Logic/XBoardRecvFn.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum RecvFnId {
TETRIS_RECV_ID,
XBOARD_GAME_CONTROLLER,
XBOARD_GAME_CONTROLLER_ACK,
XBOARD_GAME_CONTROLLER_SEND_ALL_TRIGGER,
// MAX is to express the length of callback function array
MAX
};
Expand Down
Loading