Skip to content

Commit

Permalink
Make frequency configurable without recompilation
Browse files Browse the repository at this point in the history
  • Loading branch information
datacompboy committed Apr 10, 2024
1 parent 2da656a commit fb73419
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 10 deletions.
10 changes: 9 additions & 1 deletion Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ config APP_FEET_ROTATION

choice
prompt "Frequency of KAT Sensors refreshment"
default APP_KAT_FREQ_100
default APP_KAT_FREQ_PARAM

config APP_KAT_FREQ_PARAM
bool "Use configurable refresh rate 50..133 Hz"

config APP_KAT_FREQ_133
bool "Use 133Hz refresh rate (unstable)"
Expand All @@ -44,6 +47,9 @@ config APP_KAT_FREQ_114
config APP_KAT_FREQ_100
bool "Use 100Hz refresh rate"

config APP_KAT_FREQ_86
bool "Use ~86Hz refresh rate (like standard receiver)"

config APP_KAT_FREQ_80
bool "Use 80Hz refresh rate"

Expand All @@ -62,9 +68,11 @@ endchoice
# with at least 2 intervals required for each sensor to update.
config APP_KAT_CONN_INTERVAL
int
default 9 if APP_KAT_FREQ_PARAM # default param == default receiver
default 6 if APP_KAT_FREQ_133
default 7 if APP_KAT_FREQ_114
default 8 if APP_KAT_FREQ_100
default 9 if APP_KAT_FREQ_86
default 10 if APP_KAT_FREQ_80
default 12 if APP_KAT_FREQ_67
default 14 if APP_KAT_FREQ_57
Expand Down
3 changes: 2 additions & 1 deletion prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ CONFIG_SERIAL=n
# CONFIG_APP_KEEP_CONNECTIONS=n
# CONFIG_APP_DEBUG_BOOT_CONNECTIONS=n

CONFIG_APP_KAT_FREQ_133=y
CONFIG_APP_KAT_FREQ_PARAM=y
# CONFIG_APP_KAT_FREQ_133=y
# CONFIG_APP_KAT_FREQ_114=y
# CONFIG_APP_KAT_FREQ_100=y

Expand Down
2 changes: 2 additions & 0 deletions scripts/config/get-freq.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
powershell.exe -ExecutionPolicy Bypass -File y-get-frequency.ps1
pause
2 changes: 2 additions & 0 deletions scripts/config/set-freq-100hz.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
powershell.exe -ExecutionPolicy Bypass -File y-set-frequency.ps1 --Frequency 100
pause
2 changes: 2 additions & 0 deletions scripts/config/set-freq-133hz.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
powershell.exe -ExecutionPolicy Bypass -File y-set-frequency.ps1 --Frequency 133
pause
2 changes: 2 additions & 0 deletions scripts/config/set-freq-88hz.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
powershell.exe -ExecutionPolicy Bypass -File y-set-frequency.ps1 --Frequency 88
pause
84 changes: 84 additions & 0 deletions scripts/config/y-get-frequency.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Script configuration
$katPath = "C:\Program Files (x86)\KAT Gateway\"

# Load Gateway's communication helper library
if (!(Test-Path $katPath -PathType Container)) {
throw "KAT Gateway expected to be installed in '${katPath}'"
}

if (Get-Process -name "KAT Gateway" -ErrorAction SilentlyContinue) {
throw "Please stop KAT Gateway."
}

Add-Type -Path "C:\Program Files (x86)\KAT Gateway\IBizLibrary.dll"

#
# Detect known Receiver version
#
[IBizLibrary.ComUtility]::KatDevice='walk_c2'
if (Test-Path([IBizLibrary.ComUtility]::Device_Config_File_Walk_C2_Path)) {
Write-Host "Gateway with configuration for Kat Walk C2/C2+ detected."
} else {
[IBizLibrary.ComUtility]::KatDevice='walk_c2_core'
if (Test-Path([IBizLibrary.ComUtility]::Device_Config_File_Walk_C2_Path)) {
Write-Host "Gateway with configuration for Kat Walk C2 Core detected."
} else {
throw "Not found gateway configuration for neither Kat Walk C2/C2+ nor C2 Core."
}
}
[IBizLibrary.Configs].GetMethod('C2ReceiverPairingInfoRead').invoke($null, $null)
if ($null -eq [IBizLibrary.KATSDKInterfaceHelper]::receiverPairingInfoSave.ReceiverSN) {
throw "Gateway configuration for the treadmill is not found."
}

#
# Check the connected device.
#
[IBizLibrary.KATSDKInterfaceHelper].GetMethod('GetDeviceConnectionStatus').invoke($null, $null)

$platforms = [IBizLibrary.KATSDKInterfaceHelper]::walk_c2_Count + [IBizLibrary.KATSDKInterfaceHelper]::walk_c2_core_Count

if ($platforms -lt 1) {
throw "Please connect the receiver USB dongle."
}

if ($platforms -gt 1) {
throw "Please connect only the receiver USB dongle without the original treadmill cable."
}

if ([IBizLibrary.KATSDKInterfaceHelper]::walk_c2_Count -eq 1 -and [IBizLibrary.ComUtility]::KatDevice -ne 'walk_c2') {
throw "The dongle flashed for Kat Walk C2/C2+, but gateway configuration is not."
}
if ([IBizLibrary.KATSDKInterfaceHelper]::walk_c2_core_Count -eq 1 -and [IBizLibrary.ComUtility]::KatDevice -ne 'walk_c2_core') {
throw "The dongle flashed for Kat Walk C2 Core, but gateway configuration is not."
}

$dev = New-Object IBizLibrary.KATSDKInterfaceHelper+KATModels
for($devNo=0; $devNo -lt [IBizLibrary.KATSDKInterfaceHelper]::deviceCount; $devNo ++) {
[IBizLibrary.KATSDKInterfaceHelper]::GetDevicesDesc([ref]$dev, $devNo)
if ($dev.deviceType -eq 1) {
break;
}
}

if ($dev.device -ne "nRF KAT-VR Receiver") {
throw "Please connect 'nRF KAT-VR Receiver' dongle, not the original treadmill ("+$dev.device+")"
}

#
#
#

# Request correction floats
[byte[]]$ans = New-Object byte[] 32
[byte[]]$command = 0x20,0x1f,0x55,0xAA,0x00,0x00,0xDC,0xF0 + $ans
[IBizLibrary.KATSDKInterfaceHelper]::SendHIDCommand($dev.serialNumber, $command, 32, $ans, 32)

if ($ans[5] -ne 0xDC -and $ans[6] -ne 0xF0) {
throw "No answer from device, please try again"
}

$Freq = [System.BitConverter]::ToInt16($ans, 7)

Write-Host "Receiver set up to " $Freq " Hz"

89 changes: 89 additions & 0 deletions scripts/config/y-set-frequency.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
param (
[int16]$Frequency = 86
)

# Script configuration
$katPath = "C:\Program Files (x86)\KAT Gateway\"

# Load Gateway's communication helper library
if (!(Test-Path $katPath -PathType Container)) {
throw "KAT Gateway expected to be installed in '${katPath}'"
}

if (Get-Process -name "KAT Gateway" -ErrorAction SilentlyContinue) {
throw "Please stop KAT Gateway."
}

Add-Type -Path "C:\Program Files (x86)\KAT Gateway\IBizLibrary.dll"

#
# Detect known Receiver version
#
[IBizLibrary.ComUtility]::KatDevice='walk_c2'
if (Test-Path([IBizLibrary.ComUtility]::Device_Config_File_Walk_C2_Path)) {
Write-Host "Gateway with configuration for Kat Walk C2/C2+ detected."
} else {
[IBizLibrary.ComUtility]::KatDevice='walk_c2_core'
if (Test-Path([IBizLibrary.ComUtility]::Device_Config_File_Walk_C2_Path)) {
Write-Host "Gateway with configuration for Kat Walk C2 Core detected."
} else {
throw "Not found gateway configuration for neither Kat Walk C2/C2+ nor C2 Core."
}
}
[IBizLibrary.Configs].GetMethod('C2ReceiverPairingInfoRead').invoke($null, $null)
if ($null -eq [IBizLibrary.KATSDKInterfaceHelper]::receiverPairingInfoSave.ReceiverSN) {
throw "Gateway configuration for the treadmill is not found."
}

#
# Check the connected device.
#
[IBizLibrary.KATSDKInterfaceHelper].GetMethod('GetDeviceConnectionStatus').invoke($null, $null)

$platforms = [IBizLibrary.KATSDKInterfaceHelper]::walk_c2_Count + [IBizLibrary.KATSDKInterfaceHelper]::walk_c2_core_Count

if ($platforms -lt 1) {
throw "Please connect the receiver USB dongle."
}

if ($platforms -gt 1) {
throw "Please connect only the receiver USB dongle without the original treadmill cable."
}

if ([IBizLibrary.KATSDKInterfaceHelper]::walk_c2_Count -eq 1 -and [IBizLibrary.ComUtility]::KatDevice -ne 'walk_c2') {
throw "The dongle flashed for Kat Walk C2/C2+, but gateway configuration is not."
}
if ([IBizLibrary.KATSDKInterfaceHelper]::walk_c2_core_Count -eq 1 -and [IBizLibrary.ComUtility]::KatDevice -ne 'walk_c2_core') {
throw "The dongle flashed for Kat Walk C2 Core, but gateway configuration is not."
}

$dev = New-Object IBizLibrary.KATSDKInterfaceHelper+KATModels
for($devNo=0; $devNo -lt [IBizLibrary.KATSDKInterfaceHelper]::deviceCount; $devNo ++) {
[IBizLibrary.KATSDKInterfaceHelper]::GetDevicesDesc([ref]$dev, $devNo)
if ($dev.deviceType -eq 1) {
break;
}
}

if ($dev.device -ne "nRF KAT-VR Receiver") {
throw "Please connect 'nRF KAT-VR Receiver' dongle, not the original treadmill ("+$dev.device+")"
}

#
#
#

$FreqArr = [System.BitConverter]::GetBytes($Frequency)

# Request correction floats
[byte[]]$ans = New-Object byte[] 32
[byte[]]$command = 0x20,0x1f,0x55,0xAA,0x00,0x00,0xDD,0xF0 + $FreqArr + $ans
[IBizLibrary.KATSDKInterfaceHelper]::SendHIDCommand($dev.serialNumber, $command, 32, $ans, 32)

if ($ans[5] -ne 0xDD -and $ans[6] -ne 0xF0) {
throw "No answer from device, please try again"
}

$Freq = [System.BitConverter]::ToInt16($ans, 7)

Write-Host "Receiver configuration updated to " $Freq " Hz"
19 changes: 17 additions & 2 deletions src/kat_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,23 @@ static const struct bt_conn_le_create_param btConnCreateParam = BT_CONN_LE_CREAT

// Note: btConnParam and btConnParamUpdate should differ by only for timeout argument.
#define KAT_BT_CONN_PARAM(timeout) BT_LE_CONN_PARAM_INIT(CONFIG_APP_KAT_CONN_INTERVAL, CONFIG_APP_KAT_CONN_INTERVAL, 5, timeout)
static const struct bt_le_conn_param btConnParam = KAT_BT_CONN_PARAM(500);
static const struct bt_le_conn_param btConnParamUpdate = KAT_BT_CONN_PARAM(100);
static struct bt_le_conn_param btConnParam = KAT_BT_CONN_PARAM(500);
static struct bt_le_conn_param btConnParamUpdate = KAT_BT_CONN_PARAM(100);

#ifdef CONFIG_APP_KAT_FREQ_PARAM
short KatBleUpdateFrequency = 86;
void kat_ble_update_freq_param()
{
short interval = 800 / KatBleUpdateFrequency;
interval = CLAMP(interval, 6, 16);
KatBleUpdateFrequency = 800 / interval;
btConnParam.interval_max = interval;
btConnParam.interval_min = interval;
btConnParamUpdate.interval_max = interval;
btConnParamUpdate.interval_min = interval;
printk("Set connection interval to %d (%d Hz update rate)\n", interval, KatBleUpdateFrequency);
}
#endif

ASYNC_FUNC(kat_ble_connect_next, K_MSEC(100))
{
Expand Down
6 changes: 6 additions & 0 deletions src/kat_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
* BLE subsystem management API and settings.
*/

// Dynamic setting settings update frequency
#ifdef CONFIG_APP_KAT_FREQ_PARAM
extern short KatBleUpdateFrequency;
void kat_ble_update_freq_param();
#endif

// Number of paired bluetooth sensors
extern int NumKatBleDevices;

Expand Down
5 changes: 5 additions & 0 deletions src/kat_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,9 @@ void kat_settings_async_save(void);
void _do_async_##name(struct k_work *)


// Enable configuration subsystem if any of the dynamic params enabled
#if defined(CONFIG_APP_FEET_ROTATION) || defined(CONFIG_APP_KAT_FREQ_PARAM)
#define __KAT_CONFIG_COMMANDS__
#endif

#endif // __KAT_MAIN_H__
37 changes: 31 additions & 6 deletions src/kat_usb_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,21 @@ typedef __PACKED_UNION
// 5 unused bytes
}
ansfoot;
#ifdef __KAT_CONFIG_COMMANDS__
__PACKED_STRUCT
{
uint8_t nrfSettingId;
__PACKED_STRUCT
__PACKED_UNION
{
float left, right;
} footCorrection;
__PACKED_STRUCT
{
float left, right;
} footCorrection;
short updateFreq;
};
}
ansnrfsetting;
#endif
};
}
data;
Expand Down Expand Up @@ -121,20 +127,23 @@ enum tKatUsbCommand
cCloseVibration = 0xA0,
cSetOutput = 0xA1, // LED & Vibration

#if defined(CONFIG_APP_FEET_ROTATION)
#if defined(__KAT_CONFIG_COMMANDS__)
// Non-KAT protocol extension command to tweak extended settings.
cNRFSettingGet = 0xDC,
cNRFSettingSet = 0xDD,
#endif
};

#if defined(CONFIG_APP_FEET_ROTATION)
#if defined(__KAT_CONFIG_COMMANDS__)
/* nRF settings */
enum tKatExtendedUsbParam
{
#ifdef CONFIG_APP_FEET_ROTATION
csFootAngles = 0xFA,
#endif
#ifdef CONFIG_APP_KAT_FREQ_PARAM
csUpdateFreq = 0xF0,
#endif
};
#endif

Expand Down Expand Up @@ -251,19 +260,35 @@ bool kat_usb_handle_request(uint8_t *buf, int size)
// Noop, we don't have control for LED of vibration here.
return false;

#if defined(CONFIG_APP_FEET_ROTATION)
#if defined(__KAT_CONFIG_COMMANDS__)
case cNRFSettingSet:
switch(pack->data.ansnrfsetting.nrfSettingId) {

#ifdef CONFIG_APP_KAT_FREQ_PARAM
case csUpdateFreq:
KatBleUpdateFrequency = pack->data.ansnrfsetting.updateFreq;
kat_ble_update_freq_param();
break;
#endif

#ifdef CONFIG_APP_FEET_ROTATION
case csFootAngles:
KatCorrectLeft = pack->data.ansnrfsetting.footCorrection.left;
KatCorrectRight = pack->data.ansnrfsetting.footCorrection.right;
break;
#endif
}
kat_settings_async_save();
[[fallthrough]];
case cNRFSettingGet:
switch(pack->data.ansnrfsetting.nrfSettingId) {

#ifdef CONFIG_APP_KAT_FREQ_PARAM
case csUpdateFreq:
pack->data.ansnrfsetting.updateFreq = KatBleUpdateFrequency;
return true;
#endif

#ifdef CONFIG_APP_FEET_ROTATION
case csFootAngles:
pack->data.ansnrfsetting.footCorrection.left = KatCorrectLeft;
Expand Down
8 changes: 8 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ int kat_settings_set_cb(const char *key, size_t len, settings_read_cb read_cb, v
#ifdef CONFIG_APP_FEET_ROTATION
READ_SIMPLE_SETTING("correctLeft", KatCorrectLeft, 0.0f);
READ_SIMPLE_SETTING("correctRight", KatCorrectRight, 0.0f);
#endif
#ifdef CONFIG_APP_KAT_FREQ_PARAM
READ_SIMPLE_SETTING("updateFreq", KatBleUpdateFrequency, (kat_ble_update_freq_param(), 86));
#endif
READ_SIMPLE_SETTING("devCnt", NumKatBleDevices, 0);

Expand Down Expand Up @@ -101,6 +104,11 @@ int kat_settings_export_cb(int(*export_func)(const char *name, const void *val,
if (ret < 0) return ret;
#endif

#ifdef CONFIG_APP_KAT_FREQ_PARAM
ret = export_func("katrc/updateFreq", &KatBleUpdateFrequency, sizeof(KatBleUpdateFrequency));
if (ret < 0) return ret;
#endif

ret = export_func("katrc/serial", &katUsbSerial, sizeof(katUsbSerial));
if (ret < 0) return ret;

Expand Down

0 comments on commit fb73419

Please sign in to comment.