Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
gkalab committed Aug 10, 2023
1 parent 8edba43 commit 9595a6b
Show file tree
Hide file tree
Showing 44 changed files with 4,925 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
BasedOnStyle: LLVM
IndentWidth: 4
---
Language: Cpp
# Force pointers to the type for C++.
DerivePointerAlignment: false
PointerAlignment: Left
ColumnLimit: 120
AllowShortLambdasOnASingleLine: Empty
AllowShortFunctionsOnASingleLine: Empty
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vscode
.devcontainer
build
managed_components
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# For more information about build system see
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(cer2nut)
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
# cer2nut
Certabo to Chessnut adapter using an ESP32-S3 development board

Cer2nut adapts the protocol of a Certabo chess e-board to the Chessnut protocol used in the Chessnut Air and Chessnut Pro e-boards.

The adapter allows software written to support the Chessnut e-boards to be used with a Certabo e-board.
Not all software written for the Chessnut protocol may work with this adapter and there is no guarantee that this adapter will work in the future.

Hardware requirements
---------------------

This software is only compatible with development boards having a ESP32-S3-WROOM-1 chip and a USB OTG port on board.

Software requirements
---------------------

ESP-IDF >= 5.1 https://idf.espressif.com/

Connection
----------

**Use at Own Risk.** The Software is licensed for free under the GNU general public license version 3 and “as is” and
may not operate properly, be in final form or fully functional, and may have errors, bugs, design flaws, and other defects.
Use of the software is at your own risk, the author is not responsible for any damage to your equipment when using this software!

After flashing the firmware, connect the USB OTG port to the Certabo E-Board and the second USB port to the power supply.

![Alt text](connection_diagram.png?raw=true "Connection diagram")
Binary file added connection_diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions dependencies.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
dependencies:
espressif/usb_host_cdc_acm:
component_hash: 29ab411dcacf824f4f7aa4256a60801cdcc2ecd6afecd9c829219f493e9972f9
source:
service_url: https://api.components.espressif.com/
type: service
version: 1.0.4
idf:
component_hash: null
source:
type: idf
version: 5.1.0
manifest_hash: 807dd8ead555fefc8048b460d32a2a0b531af640994a4fb2343bbea46ad81470
target: esp32s3
version: 1.0.0
8 changes: 8 additions & 0 deletions main/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BasedOnStyle: LLVM
IndentWidth: 4
---
Language: Cpp
# Force pointers to the type for C++.
DerivePointerAlignment: false
PointerAlignment: Left
ColumnLimit: 120
11 changes: 11 additions & 0 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
idf_component_register(SRCS "vcpusb.cpp" "bleuart.cpp" "cer2nut.cpp" "cp210x_usb.cpp"
"adapter/lib/CalibrationSquare.cpp"
"adapter/lib/CertaboBoardMessageParser.cpp"
"adapter/lib/CertaboCalibrator.cpp"
"adapter/lib/CertaboParser.cpp"
"adapter/lib/CertaboPiece.cpp"
"adapter/lib/CertaboLedControl.cpp"
"adapter/lib/ChessnutAdapter.cpp"
"adapter/lib/ChessnutConverter.cpp"
INCLUDE_DIRS ".")
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
2 changes: 2 additions & 0 deletions main/adapter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
cmake-build-*
29 changes: 29 additions & 0 deletions main/adapter/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.2)
project(cer2nutlib)

# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)

include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# uncomment to enable profiling
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pg")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3")

file(GLOB_RECURSE LIB_SOURCE_FILES lib/*.cpp)

include_directories(lib)

file(GLOB_RECURSE TEST_SOURCE_FILES test/*.cpp)
enable_testing()
add_executable(unittests ${LIB_SOURCE_FILES} ${TEST_SOURCE_FILES})
target_link_libraries(unittests GTest::gmock_main)
include(GoogleTest)
gtest_discover_tests(unittests)
20 changes: 20 additions & 0 deletions main/adapter/lib/BoardTranslator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <vector>

#include "CertaboPiece.h"

namespace eboard {

class BoardTranslator {
public:
BoardTranslator() = default;
virtual ~BoardTranslator() = default;

public:
virtual void hasPieceRecognition(bool canRecognize) = 0;

virtual void translate(std::vector<CertaboPiece> board) = 0;
};

} // namespace eboard
100 changes: 100 additions & 0 deletions main/adapter/lib/CalibrationSquare.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <functional>
#include <map>
#include <vector>

#include "CalibrationSquare.h"
#include "ChessData.h"

eboard::CalibrationSquare::CalibrationSquare(int square)
: square(square), piece(CertaboPiece(PieceId{255, 255, 255, 255, 255})) {}

bool eboard::CalibrationSquare::isCalibrated() {
return piece.getId() != PieceId{255, 255, 255, 255, 255};
}

static int toSquare(int b) {
int row = 7 - (b / 8);
int col = b % 8;
return row * 8 + col;
}

void eboard::CalibrationSquare::calibratePiece(std::vector<std::vector<CertaboPiece>>& receivedBoards,
CalibrationCompleteForSquareFunction const& completeForSquareFunction) {
std::map<CertaboPiece, int> pieceCount;
for (auto& board : receivedBoards) {
CertaboPiece& pc = board[square];
pieceCount[pc]++;
}
for (auto& entry : pieceCount) {
int count = entry.second;
auto& pc = const_cast<CertaboPiece&>(entry.first);
if (count > receivedBoards.size() / 2 && pieceOrExtraQueenSquare(pc)) {
piece = CertaboPiece(pc.getId());
if (pc.getId() != PieceId{}) {
completeForSquareFunction(toSquare(square));
//printf("calibration complete for square %d\n", toSquare(square));
}
}
}
}

bool eboard::CalibrationSquare::pieceOrExtraQueenSquare(CertaboPiece& pc) const {
return pc.getId() != PieceId{} || square == 19 || square == 43;
}

int eboard::CalibrationSquare::noStoneOr(int stone) {
if (piece.getId() != PieceId{}) {
return stone;
} else {
return ChessData::NO_STONE;
}
}

int eboard::CalibrationSquare::getStone() {
if (square > 7 && square < 16) {
return ChessData::BLACK_PAWN;
}
if (square > 47 && square < 56) {
return ChessData::WHITE_PAWN;
}
switch (square) {
case 0:
case 7:
return noStoneOr(ChessData::BLACK_ROOK);
case 1:
case 6:
return noStoneOr(ChessData::BLACK_KNIGHT);
case 2:
case 5:
return noStoneOr(ChessData::BLACK_BISHOP);
case 3:
case 19: // black extra queen square
return noStoneOr(ChessData::BLACK_QUEEN);
case 4:
return noStoneOr(ChessData::BLACK_KING);
case 56:
case 63:
return noStoneOr(ChessData::WHITE_ROOK);
case 57:
case 62:
return noStoneOr(ChessData::WHITE_KNIGHT);
case 58:
case 61:
return noStoneOr(ChessData::WHITE_BISHOP);
case 59:
case 43: // white extra queen square
return noStoneOr(ChessData::WHITE_QUEEN);
case 60:
return noStoneOr(ChessData::WHITE_KING);
default:
return ChessData::NO_STONE;
}
}

eboard::CertaboPiece eboard::CalibrationSquare::getPiece() {
return piece;
}

int eboard::CalibrationSquare::getSquare() const {
return square;
}
32 changes: 32 additions & 0 deletions main/adapter/lib/CalibrationSquare.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <functional>
#include <vector>

#include "CertaboPiece.h"

namespace eboard {

using CalibrationCompleteForSquareFunction = std::function<void(int square)>;

class CalibrationSquare {
public:
explicit CalibrationSquare(int square);

bool isCalibrated();
void calibratePiece(std::vector<std::vector<CertaboPiece>>& receivedBoards,
CalibrationCompleteForSquareFunction const& completeForSquareFunction);
int getStone();
int getSquare() const;

eboard::CertaboPiece getPiece();

private:
bool pieceOrExtraQueenSquare(CertaboPiece& pc) const;
int noStoneOr(int stone);

int square;
CertaboPiece piece;
};

} // namespace eboard
67 changes: 67 additions & 0 deletions main/adapter/lib/CertaboBoardMessageParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <algorithm>
#include <unordered_map>

#include "CertaboBoardMessageParser.h"
#include "ChessData.h"

using eboard::CertaboBoardMessageParser;

CertaboBoardMessageParser::CertaboBoardMessageParser(Stones stones, CallbackFunction callbackFunction)
: parser(CertaboParser(*this)), stones(std::move(stones)), callback(std::move(callbackFunction)) {}

void CertaboBoardMessageParser::parse(uint8_t* msg, size_t data_len) {
parser.parse(msg, data_len);
}

void eboard::CertaboBoardMessageParser::hasPieceRecognition(bool canRecognize) {
pieceRecognition = canRecognize;
}

void eboard::CertaboBoardMessageParser::translate(std::vector<CertaboPiece> board) {
std::array<StoneId, 64> newBoard = {};
int i = 0;
for (auto& piece : board) {
int square = toSquare(i);
if (stones.find(piece) != stones.end()) {
newBoard[square] = stones[piece];
} else {
newBoard[square] = ChessData::NO_STONE;
}
i++;
}
averageLastBoards(newBoard);
}

void CertaboBoardMessageParser::averageLastBoards(std::array<StoneId, 64> newBoard) {
while (boardHistory.size() > 2) {
boardHistory.pop_front();
}
boardHistory.push_back(newBoard);
std::array<StoneId, 64> avg_board{};
for (int i = 0; i < 64; i++) {
std::vector<StoneId> counter;
for (const auto& board : boardHistory) {
counter.push_back(board[i]);
}
avg_board[i] = mostFrequent(counter);
}
callback(avg_board);
}

using MapEntry = std::pair<uint8_t, std::size_t>;
int CertaboBoardMessageParser::mostFrequent(std::vector<StoneId>& entries) {
std::unordered_map<StoneId, std::size_t> freqMap;
std::for_each(entries.begin(), entries.end(), [&](StoneId& elem) {
freqMap[elem]++;
});
auto result = std::max_element(freqMap.begin(), freqMap.end(), [](MapEntry left, MapEntry right) {
return left.second < right.second;
});
return result->first;
}

int CertaboBoardMessageParser::toSquare(int index) {
int row = 7 - (index / 8);
int col = index % 8;
return row * 8 + col;
}
Loading

0 comments on commit 9595a6b

Please sign in to comment.