Skip to content

Commit

Permalink
Respect real time mode / upload mode
Browse files Browse the repository at this point in the history
Real time mode has to be set before position data is sent via BLE
  • Loading branch information
gkalab committed Aug 11, 2023
1 parent 31ae408 commit cf32372
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
34 changes: 19 additions & 15 deletions main/adapter/lib/ChessnutConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,24 @@ static std::vector<uint8_t> dateTime() {
}

void ChessnutConverter::process(const std::array<eboard::StoneId, 64>& board) {
std::array<uint8_t, 38> converted{};
converted[0] = 0x01;
converted[1] = 0x24;
int j = 33;
for (int i = 0; i < 64; i += 2) {
converted[j] = setUpperNibble(converted[j], stoneToChessnutStone(board[i]));
converted[j] = setLowerNibble(converted[j], stoneToChessnutStone(board[i + 1]));
j--;
if (realTimeMode) {
std::array<uint8_t, 38> converted{};
converted[0] = 0x01;
converted[1] = 0x24;
int j = 33;
for (int i = 0; i < 64; i += 2) {
converted[j] = setUpperNibble(converted[j], stoneToChessnutStone(board[i]));
converted[j] = setLowerNibble(converted[j], stoneToChessnutStone(board[i + 1]));
j--;
}
// add time
std::vector<uint8_t> convertedSeconds = dateTime();
converted[34] = convertedSeconds[0];
converted[35] = convertedSeconds[1];
converted[36] = convertedSeconds[2];
converted[37] = convertedSeconds[3];
boardCallback(converted.data(), 38);
}
// add time
std::vector<uint8_t> convertedSeconds = dateTime();
converted[34] = convertedSeconds[0];
converted[35] = convertedSeconds[1];
converted[36] = convertedSeconds[2];
converted[37] = convertedSeconds[3];
boardCallback(converted.data(), 38);
}

uint8_t ChessnutConverter::setLowerNibble(uint8_t orig, uint8_t nibble) {
Expand Down Expand Up @@ -80,9 +82,11 @@ std::vector<uint8_t> eboard::ChessnutConverter::chessnutToCertaboCommand(uint8_t
std::vector<uint8_t> received(&data[0], &data[data_len]);
if (received.size() >= 3 && received[0] == 0x21 && received[1] == 0x01 && received[2] == 0x00) { // real time mode
infoCallback(ack.data(), ack.size());
realTimeMode = true;
} else if (received.size() >= 3 && received[0] == 0x21 && received[1] == 0x01 &&
received[2] == 0x01) { // upload mode
infoCallback(ack.data(), ack.size());
realTimeMode = false;
} else if (received.size() >= 3 && received[0] == 0x29 && received[1] == 0x01 &&
received[2] == 0x00) { // battery status
auto result = std::vector<uint8_t>{0x2a, 0x02, 0x64, 0x00}; // battery full, not loading
Expand Down
1 change: 1 addition & 0 deletions main/adapter/lib/ChessnutConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class ChessnutConverter {

ConverterCallbackFunction boardCallback;
ConverterCallbackFunction infoCallback;
bool realTimeMode = false;
};

} // namespace eboard
4 changes: 4 additions & 0 deletions main/adapter/test/ChessnutAdapterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class ChessnutAdapterTest : public ::testing::Test {
// toBle
toBleData = std::vector<uint8_t>(&data[0], &data[data_len]);
});
// init with real time mode
std::vector<uint8_t> realTimeMode{0x21, 0x01, 0x00};
adapter->fromBle(&realTimeMode.front(), realTimeMode.size());
toBleData.clear();
}

void givenCalibrationDataIsReceived() {
Expand Down
1 change: 1 addition & 0 deletions main/adapter/test/ChessnutConverterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ChessnutConverterTest : public ::testing::Test {
[this](uint8_t* data, size_t data_len) {
convertedInfo = std::vector<uint8_t>(&data[0], &data[data_len]);
});
whenChessnutToCertaboCommandIsCalledWith({0x21, 0x01, 0x00}); // enable real-time mode
}

void whenConvertingBoard(std::array<eboard::StoneId, 64> const& board) {
Expand Down

0 comments on commit cf32372

Please sign in to comment.