Skip to content

Commit

Permalink
test(OpenGloves): update old alpha-encoding unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
leon0399 committed Feb 8, 2024
1 parent 809f14e commit bb5de28
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 465 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
os: [ ubuntu-latest ]
target:
- bhaptics_tactsuit_x16
- bhaptics_tactsuit_x16_pca9685
Expand All @@ -32,10 +32,10 @@ jobs:
- bhaptics_tactal
- bhaptics_tactvisor
- bhaptics_tactglove_right
battery_flag: [SENSESHIFT_BATTERY_ENABLED=true]
serial_plotter_flag: [SENSESHIFT_SERIAL_PLOTTER=false]
nimble_flag: [SENSESHIFT_BLE_USE_NIMBLE=false]
coverage: [false]
battery_flag: [ SENSESHIFT_BATTERY_ENABLED=true ]
serial_plotter_flag: [ SENSESHIFT_SERIAL_PLOTTER=false ]
nimble_flag: [ SENSESHIFT_BLE_USE_NIMBLE=false ]
coverage: [ false ]

include:
# Extra tests for x40, as it uses the most features
Expand Down Expand Up @@ -165,7 +165,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
os: [ ubuntu-latest ]
target:
- lucidgloves-prototype3
- lucidgloves-prototype4
Expand All @@ -176,20 +176,20 @@ jobs:
comm_flag:
- COMMUNICATION_PROTOCOL=OPENGLOVES_COMM_SERIAL
- COMMUNICATION_PROTOCOL=OPENGLOVES_COMM_BTSERIAL
coverage: [false]
coverage: [ false ]

include:
- os: ubuntu-latest
target: lucidgloves-prototype4-ffb
curl_calibration_flag: CALIBRATION_CURL="::SenseShift::Calibration::MinMaxCalibrator<uint16_t, 0, ANALOG_MAX>"
curl_calibration_flag: CALIBRATION_CURL="new ::SenseShift::Input::Calibration::MinMaxCalibrator<float>()"
coverage: true
- os: ubuntu-latest
target: lucidgloves-prototype4-ffb
curl_calibration_flag: CALIBRATION_CURL="::SenseShift::Calibration::CenterPointDeviationCalibrator<uint16_t, 20, 270, 0, ANALOG_MAX>"
curl_calibration_flag: CALIBRATION_CURL="new ::SenseShift::Input::Calibration::CenterPointDeviationCalibrator<float>(0.66F, 0.005F)"
coverage: true
- os: ubuntu-latest
target: lucidgloves-prototype4-ffb
curl_calibration_flag: CALIBRATION_CURL="::SenseShift::Calibration::FixedCenterPointDeviationCalibrator<uint16_t, 20, 270, 0, ANALOG_MAX>"
curl_calibration_flag: CALIBRATION_CURL="new ::SenseShift::Input::Calibration::FixedCenterPointDeviationCalibrator<float>(0.66F, 0.005F)"
coverage: true
- os: ubuntu-latest
target: indexer-csf
Expand Down
103 changes: 75 additions & 28 deletions lib/opengloves/opengloves/opengloves.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
#include "opengloves/opengloves.hpp"

#include <cmath>
#include <cstdint>
#include <cstdio>
#include <variant>

#ifdef OG_ENCODE_FAST
template<typename Tp = int, typename Tu = float>
auto ifloor(Tu x) -> Tp
{
return (Tp) x - (x < (Tp) x);
}

template<typename Tp = int, typename Tu = float>
auto iceil(Tu x) -> Tp
{
return (Tp) x + (x > (Tp) x);
}
#endif

namespace og {

auto AlphaEncoder::encode_input(const InputData& input, char* buffer, size_t length) const -> size_t
Expand Down Expand Up @@ -32,52 +47,84 @@ namespace og {
const auto peripheral = std::get<InputPeripheralData>(input);
auto written = 0;

const auto& curl = peripheral.curl.fingers;
const auto& curls = peripheral.curl.fingers;
const auto& splays = peripheral.splay.fingers;
#ifdef OG_ENCODE_FAST
for (auto i = 0; i < curl.size(); i++) {
const auto& finger = curl[i];
for (auto i = 0; i < curls.size(); i++) {
const auto& finger = curls[i];
const auto& finger_curl = finger.curl_total;

written +=
snprintf(buffer + written, length - written, "%c%.0f", 'A' + i, finger_curl * MAX_ANALOG_VALUE);
written += snprintf(
buffer + written,
length - written,
"%c%u",
'A' + i,
ifloor<std::uint8_t, float>(finger_curl * MAX_ANALOG_VALUE)
);
}
#else
for (auto i = 0; i < curl.size(); i++) {
const auto& finger = curl[i];
const auto& joints = finger.curl;
for (auto i = 0; i < curls.size(); i++) {
const auto& finger_curl = curls[i];
const auto& finger_splay = splays[i];
const auto finger_alpha_key = 'A' + i;

written += snprintf(
buffer + written,
length - written,
"%c%.0f",
finger_alpha_key,
std::floor(finger_curl.curl_total * MAX_ANALOG_VALUE)
);

if (finger_splay > 0.0F) {
written += snprintf(
buffer + written,
length - written,
"(%cB)%.0f",
finger_alpha_key,
std::floor(finger_splay * MAX_ANALOG_VALUE)
);
}

for (auto j = 0; j < joints.size(); j++) {
const auto& joints = finger_curl.curl;
for (auto j = 1; j < joints.size(); j++) {
const auto& joint = joints[j];
const auto joint_alpha_key = 'A' + j;

// skip if joint is 0.0, except total_curl (it is probably not enabled)
if (joint == 0.0F && j != 0) {
if (joint == 0.0F) {
continue;
}

const auto& jointKey = AlphaEncoder::FINGER_CURL_JOINT_ALPHA_KEY[i][j];

written +=
snprintf(buffer + written, length - written, "%s%.0f", jointKey.data(), joint * MAX_ANALOG_VALUE);
written += snprintf(
buffer + written,
length - written,
"(%cA%c)%.0f",
finger_alpha_key,
joint_alpha_key,
std::floor(joint * MAX_ANALOG_VALUE)
);
}
}

const auto& splay = peripheral.splay.fingers;
for (auto i = 0; i < splay.size(); i++) {
const auto& finger = splay[i];

if (finger == 0.0F) {
continue;
}

#endif
if (peripheral.joystick.x != 0.0F) {
written += snprintf(
buffer + written,
length - written,
"(%cB)%.0f",
AlphaEncoder::FINGER_ALPHA_KEY[i],
finger * MAX_ANALOG_VALUE
"F%.0f",
std::floor(peripheral.joystick.x * MAX_ANALOG_VALUE)
);
}
#endif
if (peripheral.joystick.y != 0.0F) {
written += snprintf(
buffer + written,
length - written,
"G%.0f",
std::floor(peripheral.joystick.y * MAX_ANALOG_VALUE)
);
}
if (peripheral.joystick.press) {
written += snprintf(buffer + written, length - written, "H");
}

const auto& buttons = peripheral.buttons;
for (auto i = 0; i < buttons.size(); i++) {
Expand Down
2 changes: 1 addition & 1 deletion lib/opengloves/opengloves/opengloves.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ namespace og {
inline static constexpr const char* INFO_DEVICE_TYPE_KEY = "(ZG)";
inline static constexpr const char* INFO_HAND_KEY = "(ZH)";

inline static constexpr const uint16_t MAX_ANALOG_VALUE = 4096;
inline static constexpr const uint16_t MAX_ANALOG_VALUE = 4095;

#ifdef OG_USE_FROZEN
inline static constexpr const auto ALPHA_KEYS_TO_COMMAND = frozen::make_map<frozen::string, Command>
Expand Down
159 changes: 0 additions & 159 deletions test/test_opengloves/main.cpp

This file was deleted.

Loading

0 comments on commit bb5de28

Please sign in to comment.