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

Unit test #1

Merged
merged 5 commits into from
Oct 20, 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: 38 additions & 0 deletions .github/workflows/unit_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Unit Tests

on:
push:
branches:
- unit_test
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v2

- name: Install Dependencies
run: |
sudo apt-get update
sudo apt-get install -y build-essential cmake g++ # Install build tools
sudo apt-get install -y libgtest-dev # Install Google Test
sudo apt-get install -y libhidapi-dev
cd /usr/src/gtest && sudo cmake . && sudo make # Build Google Test library

- name: Build Project
run: |
mkdir build
cd build
cmake ..
cmake --build .

- name: Run Tests
run: |
cd build
ctest

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
.vscode/
32 changes: 32 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,38 @@ target_link_libraries(g29_text_example
${HIDAPI_LIBRARIES}
)


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

# Enable testing
enable_testing()

# Add the test executable
add_executable(G29Test
test/G29Test.cpp
)

target_link_libraries(G29Test
PRIVATE
G29
${HIDAPI_LIBRARIES}
gtest_main
gmock_main
)

# Include the Google Test module
include(GoogleTest)

# Discover tests
gtest_discover_tests(G29Test)
# Install rules
# install(TARGETS G29 g29_example
# RUNTIME DESTINATION bin
Expand Down
49 changes: 27 additions & 22 deletions src/G29.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "G29.hpp"
#include <iostream>
#include <algorithm>

G29::G29() {
if (hid_init() != 0) {
Expand Down Expand Up @@ -132,35 +133,36 @@ uint8_t G29::calculateSteering(uint8_t start, uint8_t end) const {

std::string G29::updateButtonState(const std::vector<uint8_t>& byteArray) {
if (byteArray.size() < 16) return "";
//buttonState.clear();

buttonState["X"] = (byteArray[0] == 0x18);
buttonState["Square"] = (byteArray[0] == 0x28);
buttonState["Triangle"] = (byteArray[0] == 0x88);
buttonState["Circle"] = (byteArray[0] == 0x48);
// Correct the conditions and ensure they are distinct
buttonState["X"] = (byteArray[0] & 0x18) == 0x18;
buttonState["Square"] = (byteArray[0] & 0x28) == 0x28;
buttonState["Triangle"] = (byteArray[0] & 0x88) == 0x88;
buttonState["Circle"] = (byteArray[0] & 0x48) == 0x48;

buttonState["L2"] = (byteArray[1] == 0x08);
buttonState["R2"] = (byteArray[1] == 0x04);
buttonState["L3"] = (byteArray[1] == 0x80);
buttonState["R3"] = (byteArray[1] == 0x40);
buttonState["L2"] = (byteArray[1] & 0x08) == 0x08;
buttonState["R2"] = (byteArray[1] & 0x04) == 0x04;
buttonState["L3"] = (byteArray[1] & 0x80) == 0x80;
buttonState["R3"] = (byteArray[1] & 0x40) == 0x40;

buttonState["DPadUp"] = (byteArray[0] == 0x00);
buttonState["DPadDown"] = (byteArray[0] == 0x04);
buttonState["DPadLeft"] = (byteArray[0] == 0x06);
buttonState["DPadRight"] = (byteArray[0] == 0x02);
buttonState["DPadUp"] = (byteArray[0] & 0x0F) == 0x00; // Mask with 0x0F for directional checks
buttonState["DPadDown"] = (byteArray[0] & 0x0F) == 0x04;
buttonState["DPadLeft"] = (byteArray[0] & 0x0F) == 0x06;
buttonState["DPadRight"] = (byteArray[0] & 0x0F) == 0x02;

buttonState["RotaryDialPress"] = (byteArray[3] == 0x08);
buttonState["RotaryDialPress"] = (byteArray[3] & 0x08) == 0x08;

buttonState["PlusButton"] = (byteArray[2] == 0x80);
buttonState["MinusButton"] = (byteArray[3] == 0x01);
buttonState["PlusButton"] = (byteArray[2] & 0x80) == 0x80;
buttonState["MinusButton"] = (byteArray[3] & 0x01) == 0x01;

buttonState["LeftPaddle"] = (byteArray[1] == 0x02);
buttonState["RightPaddle"] = (byteArray[1] == 0x01);
buttonState["LeftPaddle"] = (byteArray[1] & 0x02) == 0x02;
buttonState["RightPaddle"] = (byteArray[1] & 0x01) == 0x01;

buttonState["Share"] = (byteArray[1] == 0x10);
buttonState["Options"] = (byteArray[1] == 0x20);
buttonState["PS"] = (byteArray[3] == 0x10);


buttonState["Share"] = (byteArray[1] & 0x10) == 0x10;
buttonState["Options"] = (byteArray[1] & 0x20) == 0x20;
buttonState["PS"] = (byteArray[3] & 0x10) == 0x10;
// std::cout << "Button pressed: " << getPressedButton() << std::endl;

if (byteArray[0] == 0x18) return "X";
if (byteArray[0] == 0x28) return "Square";
Expand Down Expand Up @@ -192,6 +194,9 @@ std::string G29::updateButtonState(const std::vector<uint8_t>& byteArray) {
return "";
}




bool G29::isButtonPressed(const std::string& button) const {
auto it = buttonState.find(button);
if (it != buttonState.end()) {
Expand Down
18 changes: 11 additions & 7 deletions src/G29.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ class G29 {
*/
std::string getPressedButton();

/**
* @brief Updates the state of all buttons based on raw input data.
*
* @param byteArray The raw input data from the device.
* @return The name of the first pressed button found, or an empty string if no button is pressed.
*/
std::string updateButtonState(const std::vector<uint8_t>& byteArray);



private:
hid_device* device; ///< Pointer to the HID device.
std::vector<uint8_t> cache; ///< Buffer for storing raw input data.
Expand All @@ -136,11 +146,5 @@ class G29 {
*/
uint8_t calculateSteering(uint8_t start, uint8_t end) const;

/**
* @brief Updates the state of all buttons based on raw input data.
*
* @param byteArray The raw input data from the device.
* @return The name of the first pressed button found, or an empty string if no button is pressed.
*/
std::string updateButtonState(const std::vector<uint8_t>& byteArray);

};
Loading
Loading