refactor(xlites): moved share DSC / audio jack code into own files

This commit is contained in:
raphaelcoeffic
2025-01-17 13:30:49 +01:00
committed by Raphael Coeffic
parent a697f285de
commit c8ee302ffe
7 changed files with 139 additions and 182 deletions
@@ -30,7 +30,7 @@
#include "hal.h"
void board_trainer_init()
__WEAK void board_trainer_init()
{
#if defined(TRAINER_DETECT_GPIO)
gpio_init(TRAINER_DETECT_GPIO, GPIO_IN_PU, GPIO_PIN_SPEED_LOW);
@@ -96,7 +96,7 @@ void trainer_init_dsc_in() {}
void trainer_stop_dsc() {}
#endif
bool is_trainer_dsc_connected()
__WEAK bool is_trainer_dsc_connected()
{
#if defined(TRAINER_DETECT_GPIO)
bool set = gpio_read(TRAINER_DETECT_GPIO);
+1 -42
View File
@@ -206,47 +206,6 @@ void handleUsbConnection()
#endif // defined(STM32) && !defined(SIMU)
}
#if defined(PCBXLITES)
uint8_t jackState = SPEAKER_ACTIVE;
const char STR_JACK_HEADPHONE[] = "Headphone";
const char STR_JACK_TRAINER[] = "Trainer";
void onJackConnectMenu(const char* result)
{
if (result == STR_JACK_HEADPHONE) {
jackState = HEADPHONE_ACTIVE;
disableSpeaker();
enableHeadphone();
} else if (result == STR_JACK_TRAINER) {
jackState = TRAINER_ACTIVE;
enableTrainer();
}
}
void handleJackConnection()
{
if (jackState == SPEAKER_ACTIVE && isJackPlugged()) {
if (g_eeGeneral.jackMode == JACK_HEADPHONE_MODE) {
jackState = HEADPHONE_ACTIVE;
disableSpeaker();
enableHeadphone();
} else if (g_eeGeneral.jackMode == JACK_TRAINER_MODE) {
jackState = TRAINER_ACTIVE;
enableTrainer();
} else if (popupMenuItemsCount == 0) {
POPUP_MENU_START(onJackConnectMenu, 2, STR_JACK_HEADPHONE, STR_JACK_TRAINER);
}
} else if (jackState == SPEAKER_ACTIVE && !isJackPlugged() &&
popupMenuItemsCount > 0 && popupMenuHandler == onJackConnectMenu) {
popupMenuItemsCount = 0;
} else if (jackState != SPEAKER_ACTIVE && !isJackPlugged()) {
jackState = SPEAKER_ACTIVE;
enableSpeaker();
}
}
#endif
void checkSpeakerVolume()
{
#if defined(AUDIO)
@@ -528,7 +487,7 @@ void perMain()
handleUsbConnection();
#if defined(PCBXLITES)
#if defined(SHARED_DSC_HEADPHONE_JACK)
handleJackConnection();
#endif
+1 -17
View File
@@ -548,23 +548,7 @@ void calcConsumption()
{
}
#if defined(HEADPHONE_TRAINER_SWITCH_GPIO)
void enableHeadphone()
{
}
void enableTrainer()
{
}
void enableSpeaker()
{
}
void disableSpeaker()
{
}
#endif
void handleJackConnection() {}
int trainerModuleSbusGetByte(unsigned char*) { return 0; }
+6
View File
@@ -685,6 +685,12 @@ if(FLAVOUR STREQUAL gx12)
target_include_directories(board PRIVATE ${TARGET_SRC_DIR}/gx12)
endif()
if(FLAVOUR STREQUAL xlites)
target_sources(board PRIVATE
${TARGET_SRC_DIR}/shared_dsc_headphone.cpp
)
endif()
set(SRC ${SRC}
io/frsky_firmware_update.cpp
io/multi_firmware_update.cpp
-71
View File
@@ -279,15 +279,6 @@ void boardInit()
usbChargerInit();
#endif
#if defined(JACK_DETECT_GPIO)
initJackDetect();
#endif
initSpeakerEnable();
enableSpeaker();
initHeadphoneTrainerSwitch();
#if defined(RTCLOCK)
rtcInit(); // RTC must be initialized before rambackupRestore() is called
#endif
@@ -356,65 +347,3 @@ void boardOff()
// this function must not return!
}
#if defined(AUDIO_SPEAKER_ENABLE_GPIO)
void initSpeakerEnable()
{
gpio_init(AUDIO_SPEAKER_ENABLE_GPIO, GPIO_OUT, GPIO_PIN_SPEED_LOW);
}
void enableSpeaker()
{
gpio_set(AUDIO_SPEAKER_ENABLE_GPIO);
}
void disableSpeaker()
{
gpio_clear(AUDIO_SPEAKER_ENABLE_GPIO);
}
#endif
#if defined(HEADPHONE_TRAINER_SWITCH_GPIO)
void initHeadphoneTrainerSwitch()
{
gpio_init(HEADPHONE_TRAINER_SWITCH_GPIO, GPIO_OUT, GPIO_PIN_SPEED_LOW);
}
void enableHeadphone()
{
gpio_clear(HEADPHONE_TRAINER_SWITCH_GPIO);
}
void enableTrainer()
{
gpio_set(HEADPHONE_TRAINER_SWITCH_GPIO);
}
#endif
#if defined(JACK_DETECT_GPIO)
void initJackDetect(void)
{
gpio_init(JACK_DETECT_GPIO, GPIO_IN_PU, GPIO_PIN_SPEED_LOW);
}
bool isJackPlugged()
{
// debounce
static bool debounced_state = 0;
static bool last_state = 0;
if (gpio_read(JACK_DETECT_GPIO)) {
if (!last_state) {
debounced_state = false;
}
last_state = false;
}
else {
if (last_state) {
debounced_state = true;
}
last_state = true;
}
return debounced_state;
}
#endif
+3 -50
View File
@@ -89,31 +89,6 @@ enum {
// Trainer driver
#define SLAVE_MODE() (g_model.trainerData.mode == TRAINER_MODE_SLAVE)
#if defined(TRAINER_DETECT_GPIO)
// Trainer detect is a switch on the jack
#if defined(TRAINER_DETECT_INVERTED)
#define TRAINER_CONNECTED() (gpio_read(TRAINER_DETECT_GPIO) ? 0 : 1)
#else
#define TRAINER_CONNECTED() (gpio_read(TRAINER_DETECT_GPIO) ? 1 : 0)
#endif
#elif defined(PCBXLITES)
// Trainer is on the same connector than Headphones
enum JackState
{
SPEAKER_ACTIVE,
HEADPHONE_ACTIVE,
TRAINER_ACTIVE,
};
extern uint8_t jackState;
#define TRAINER_CONNECTED() (jackState == TRAINER_ACTIVE)
#elif defined(PCBXLITE)
// No Tainer jack on Taranis X-Lite
#define TRAINER_CONNECTED() false
#else
// Trainer detect catches PPM, detection would use more CPU
#define TRAINER_CONNECTED() true
#endif
// POTS and SLIDERS default configuration
#if defined(RADIO_BOXER)
#define XPOS_CALIB_DEFAULT {0x5, 0xd, 0x16, 0x1f, 0x28}
@@ -249,33 +224,11 @@ void debugPutc(const char c);
void audioInit();
void audioEnd();
#if defined(AUDIO_SPEAKER_ENABLE_GPIO)
void initSpeakerEnable();
void enableSpeaker();
void disableSpeaker();
#else
static inline void initSpeakerEnable() { }
static inline void enableSpeaker() { }
static inline void disableSpeaker() { }
#if defined(PCBXLITES)
#define SHARED_DSC_HEADPHONE_JACK
void handleJackConnection();
#endif
#if defined(HEADPHONE_TRAINER_SWITCH_GPIO)
void initHeadphoneTrainerSwitch();
void enableHeadphone();
void enableTrainer();
#else
static inline void initHeadphoneTrainerSwitch() { }
static inline void enableHeadphone() { }
static inline void enableTrainer() { }
#endif
#if defined(JACK_DETECT_GPIO)
void initJackDetect();
bool isJackPlugged();
#endif
void audioConsumeCurrentBuffer();
// Haptic driver
void hapticInit();
void hapticOff();
@@ -0,0 +1,126 @@
#include "hal/gpio.h"
#include "stm32_gpio.h"
#include "trainer_driver.h"
#include "hal.h"
#include "edgetx.h"
static void initSpeakerEnable()
{
gpio_init(AUDIO_SPEAKER_ENABLE_GPIO, GPIO_OUT, GPIO_PIN_SPEED_LOW);
}
static void enableSpeaker()
{
gpio_set(AUDIO_SPEAKER_ENABLE_GPIO);
}
static void disableSpeaker()
{
gpio_clear(AUDIO_SPEAKER_ENABLE_GPIO);
}
static void initHeadphoneTrainerSwitch()
{
gpio_init(HEADPHONE_TRAINER_SWITCH_GPIO, GPIO_OUT, GPIO_PIN_SPEED_LOW);
}
static void enableHeadphone()
{
gpio_clear(HEADPHONE_TRAINER_SWITCH_GPIO);
}
static void enableTrainer()
{
gpio_set(HEADPHONE_TRAINER_SWITCH_GPIO);
}
static void initJackDetect(void)
{
gpio_init(JACK_DETECT_GPIO, GPIO_IN_PU, GPIO_PIN_SPEED_LOW);
}
static bool isJackPlugged()
{
// debounce
static bool debounced_state = 0;
static bool last_state = 0;
if (gpio_read(JACK_DETECT_GPIO)) {
if (!last_state) {
debounced_state = false;
}
last_state = false;
}
else {
if (last_state) {
debounced_state = true;
}
last_state = true;
}
return debounced_state;
}
enum JackState
{
SPEAKER_ACTIVE,
HEADPHONE_ACTIVE,
TRAINER_ACTIVE,
};
uint8_t jackState = SPEAKER_ACTIVE;
const char STR_JACK_HEADPHONE[] = "Headphone";
const char STR_JACK_TRAINER[] = "Trainer";
void onJackConnectMenu(const char* result)
{
if (result == STR_JACK_HEADPHONE) {
jackState = HEADPHONE_ACTIVE;
disableSpeaker();
enableHeadphone();
} else if (result == STR_JACK_TRAINER) {
jackState = TRAINER_ACTIVE;
enableTrainer();
}
}
void handleJackConnection()
{
if (jackState == SPEAKER_ACTIVE && isJackPlugged()) {
if (g_eeGeneral.jackMode == JACK_HEADPHONE_MODE) {
jackState = HEADPHONE_ACTIVE;
disableSpeaker();
enableHeadphone();
} else if (g_eeGeneral.jackMode == JACK_TRAINER_MODE) {
jackState = TRAINER_ACTIVE;
enableTrainer();
} else if (popupMenuItemsCount == 0) {
POPUP_MENU_START(onJackConnectMenu, 2, STR_JACK_HEADPHONE, STR_JACK_TRAINER);
}
} else if (jackState == SPEAKER_ACTIVE && !isJackPlugged() &&
popupMenuItemsCount > 0 && popupMenuHandler == onJackConnectMenu) {
popupMenuItemsCount = 0;
} else if (jackState != SPEAKER_ACTIVE && !isJackPlugged()) {
jackState = SPEAKER_ACTIVE;
enableSpeaker();
}
}
// defined __WEAK in boards/generic_stm32/trainer_driver.cpp
void board_trainer_init()
{
initJackDetect();
initSpeakerEnable();
enableSpeaker();
initHeadphoneTrainerSwitch();
trainer_init();
}
// defined __WEAK in boards/generic_stm32/trainer_driver.cpp
bool is_trainer_dsc_connected()
{
return jackState == TRAINER_ACTIVE;
}