Skip to content
Closed
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
22 changes: 19 additions & 3 deletions keyboards/nuphy/halo75_v2/ansi/ansi.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "ansi.h"
#include "usb_main.h"
#include "rf_driver.h"
#include "ble_cmd.h"


user_config_t user_config;
Expand Down Expand Up @@ -414,6 +415,9 @@ bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
return false;
}
no_act_time = 0;
if (process_record_ble_cmd(keycode, record->event.pressed)) {
return false;
}
switch (keycode) {
case RF_DFU:
if (record->event.pressed) {
Expand Down Expand Up @@ -727,14 +731,20 @@ void m_londing_eeprom_data(void)
user_config.ee_side_rgb = side_rgb;
user_config.ee_side_colour = side_colour;
f_dev_sleep_enable = true;
eeconfig_update_user_datablock(&user_config);
f_ble_cmd_enabled = false;
user_config.ee_ble_cmd_slot = LINK_BT_1;
eeconfig_update_user_datablock(&user_config);
} else {
side_mode_a = user_config.ee_side_mode_a;
side_mode_b = user_config.ee_side_mode_b;
side_light = user_config.ee_side_light;
side_speed = user_config.ee_side_speed;
side_rgb = user_config.ee_side_rgb;
side_colour = user_config.ee_side_colour;
// Sanitize ee_ble_cmd_slot in case EEPROM was written before this field existed
if (user_config.ee_ble_cmd_slot < LINK_BT_1 || user_config.ee_ble_cmd_slot > LINK_BT_3) {
user_config.ee_ble_cmd_slot = LINK_BT_1;
}
}
}

Expand All @@ -744,14 +754,20 @@ void m_londing_eeprom_data(void)
*/
void keyboard_post_init_kb(void)
{
m_gpio_init();
m_gpio_init();
m_londing_eeprom_data();
rf_uart_init();
wait_ms(500);
rf_device_init();

m_break_all_key();
m_londing_eeprom_data();
m_power_on_dial_sw_scan();

// Force it here so dev_sts_sync() sends CMD_SET_LINK(BLE slot) if enabled.
if (f_ble_cmd_enabled && dev_info.link_mode == LINK_USB) {
f_send_channel = 1;
}

keyboard_post_init_user();

rf_link_show_time = 0;
Expand Down
39 changes: 37 additions & 2 deletions keyboards/nuphy/halo75_v2/ansi/ansi.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,39 @@ enum custom_keycodes {
SIDE_HUI,
SIDE_SPI,
SIDE_SPD,

BLE_CMD_0,
BLE_CMD_1,
BLE_CMD_2,
BLE_CMD_3,
BLE_CMD_4,
BLE_CMD_5,
BLE_CMD_6,
BLE_CMD_7,
BLE_CMD_8,
BLE_CMD_9,
BLE_CMD_10,
BLE_CMD_11,
BLE_CMD_12,
BLE_CMD_13,
BLE_CMD_14,
BLE_CMD_15,
BLE_CMD_16,
BLE_CMD_17,
BLE_CMD_18,
BLE_CMD_19,
BLE_CMD_20,
BLE_CMD_21,
BLE_CMD_22,
BLE_CMD_23,
BLE_CMD_24,
BLE_CMD_25,
BLE_CMD_26,
BLE_CMD_27,
BLE_CMD_28,
BLE_CMD_29,
BLE_CMD_30,
BLE_CMD_31,
};

extern uint8_t m_sleep_led;
Expand Down Expand Up @@ -199,8 +232,10 @@ typedef struct
uint8_t ee_side_speed;
uint8_t ee_side_rgb;
uint8_t ee_side_colour;
m_8bit ee_dev_config;
m_8bit ee_dev_config;
uint8_t ee_ble_cmd_slot; // BLE slot used for BLE commands: LINK_BT_1/2/3
} user_config_t;

extern user_config_t user_config;
#define f_dev_sleep_enable user_config.ee_dev_config.bit0
#define f_dev_sleep_enable user_config.ee_dev_config.bit0
#define f_ble_cmd_enabled user_config.ee_dev_config.bit1
118 changes: 118 additions & 0 deletions keyboards/nuphy/halo75_v2/ansi/ble_cmd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include "ansi.h"
#include "ble_cmd.h"

// rf.c
extern void UART_Send_Bytes(uint8_t *Buffer, uint32_t Length);
extern uint8_t get_checksum(uint8_t *buf, uint8_t len);

// ansi.c
extern bool f_dial_sw_init_ok;
extern DEV_INFO_STRUCT dev_info;
extern user_config_t user_config;

// -------------------------------------------------------------------------
// Internal: send a consumer HID report directly via UART to the nRF module.
// Bypasses uart_send_report()'s link_mode and rf_state guards intentionally —
// we want the nRF to forward this over BLE regardless of the main link mode.
// -------------------------------------------------------------------------
static void ble_cmd_send(uint16_t usage) {
if (!f_dial_sw_init_ok) return;

uint8_t buf[7];
buf[0] = UART_HEAD; // 0x5A
buf[1] = CMD_RPT_CONSUME; // 0xE3
buf[2] = 0x01;
buf[3] = 2; // payload length
buf[4] = (uint8_t)(usage & 0xFF);
buf[5] = (uint8_t)(usage >> 8);
buf[6] = get_checksum(&buf[4], 2);
UART_Send_Bytes(buf, 7);
}

// -------------------------------------------------------------------------
// Public API
// -------------------------------------------------------------------------

bool process_record_ble_cmd(uint16_t keycode, bool pressed) {
if (keycode < BLE_CMD_0 || keycode > BLE_CMD_31) return false;

// Consume the keycode even when disabled/wrong mode to prevent passthrough
if (!f_ble_cmd_enabled) return true;
if (dev_info.link_mode != LINK_USB) return true;

uint8_t cmd_id = keycode - BLE_CMD_0;
ble_cmd_send(pressed ? (uint16_t)(BLE_CMD_BASE + cmd_id) : 0x0000);
return true;
}

uint8_t ble_cmd_sync_mode(void) {
if (f_ble_cmd_enabled && dev_info.link_mode == LINK_USB) {
return user_config.ee_ble_cmd_slot;
}
return dev_info.link_mode;
}

bool ble_cmd_accepts_sync(uint8_t mode) {
return f_ble_cmd_enabled &&
dev_info.link_mode == LINK_USB &&
mode == user_config.ee_ble_cmd_slot;
}

// -------------------------------------------------------------------------
// VIA custom value callback (only compiled when VIA_ENABLE = yes)
// -------------------------------------------------------------------------
#ifdef VIA_ENABLE
#include "via.h"

void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
// data = [ command_id, channel_id, value_id, value... ]
if (data[1] != BLE_CMD_VIA_CHANNEL) {
data[0] = id_unhandled;
return;
}

uint8_t command_id = data[0];
uint8_t value_id = data[2];

switch (command_id) {
case id_custom_get_value:
switch (value_id) {
case BLE_CMD_VIA_ENABLED:
data[3] = f_ble_cmd_enabled ? 1 : 0;
break;
case BLE_CMD_VIA_SLOT:
// VIA dropdown is 0-based: 0=BLE1, 1=BLE2, 2=BLE3
data[3] = user_config.ee_ble_cmd_slot - LINK_BT_1;
break;
default:
data[0] = id_unhandled;
break;
}
break;

case id_custom_set_value:
switch (value_id) {
case BLE_CMD_VIA_ENABLED:
f_ble_cmd_enabled = data[3] ? true : false;
break;
case BLE_CMD_VIA_SLOT:
if (data[3] <= 2) {
user_config.ee_ble_cmd_slot = LINK_BT_1 + data[3];
}
break;
default:
data[0] = id_unhandled;
break;
}
break;

case id_custom_save:
eeconfig_update_user_datablock(&user_config);
break;

default:
data[0] = id_unhandled;
break;
}
}
#endif // VIA_ENABLE
28 changes: 28 additions & 0 deletions keyboards/nuphy/halo75_v2/ansi/ble_cmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <stdint.h>
#include <stdbool.h>

// Consumer HID usage base for BLE commands.
// Values 0xF000-0xF01F are vendor-defined / unassigned in the Consumer page.
#define BLE_CMD_BASE 0xF000
#define BLE_CMD_COUNT 32

// VIA custom channel and value IDs
#define BLE_CMD_VIA_CHANNEL 6
#define BLE_CMD_VIA_ENABLED 1
#define BLE_CMD_VIA_SLOT 2

// Called from process_record_kb() before the switch statement.
// Returns true if the keycode was a BLE_CMD and was consumed.
bool process_record_ble_cmd(uint16_t keycode, bool pressed);

// Returns the link mode to report to the nRF module in CMD_RF_STS_SYSC.
// When BLE cmd is enabled and keyboard is on USB, returns the configured BLE slot
// so the nRF module stays connected over BLE.
uint8_t ble_cmd_sync_mode(void);

// Returns true if 'mode' is the BLE slot we're using for commands.
// Used in RF_Protocol_Receive to accept the nRF sync response without triggering
// the error counter that would fight the BLE connection.
bool ble_cmd_accepts_sync(uint8_t mode);
2 changes: 1 addition & 1 deletion keyboards/nuphy/halo75_v2/ansi/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define DRIVER_2_LED_TOTAL 64
#define RGB_MATRIX_LED_COUNT (DRIVER_1_LED_TOTAL + DRIVER_2_LED_TOTAL)

#define RGB_MATRIX_DEFAULT_MODE RGB_MATRIX_CUSTOM_position_mode
#define RGB_MATRIX_DEFAULT_MODE RGB_MATRIX_NONE
#define RGB_DEFAULT_COLOUR 168

#define RGB_MATRIX_SLEEP // turn off effects when suspended
Expand Down
Loading
Loading