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

fix(lua): re-implement luaSetSerialBaudrate #4501

Merged
merged 1 commit into from
Jan 3, 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
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/view_statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ void DebugViewPage::build(FormWindow* window)
#endif

#if defined(INTERNAL_GPS)
if (hasSerialMode(UART_MODE_GPS) != -1) {
if (serialGetModePort(UART_MODE_GPS) >= 0) {
line = form->newLine(&grid);
line->padAll(2);

Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/widgets/radio_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class InternalGPSWidget: public TopBarWidget

void refresh(BitmapBuffer * dc) override
{
if (hasSerialMode(UART_MODE_GPS) != -1) {
if (serialGetModePort(UART_MODE_GPS) >= 0) {
if (gpsData.fix) {
char s[10];
sprintf(s, "%d", gpsData.numSat);
Expand Down
16 changes: 4 additions & 12 deletions radio/src/gui/gui_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ bool isSourceAvailable(int source)
if (source >= MIXSRC_FIRST_SPACEMOUSE && source <= MIXSRC_LAST_SPACEMOUSE)
return false;
#elif defined(PCBHORUS) && defined(SPACEMOUSE)
if ((hasSerialMode(UART_MODE_SPACEMOUSE) == -1) &&
if ((serialGetModePort(UART_MODE_SPACEMOUSE) < 0) &&
(source >= MIXSRC_FIRST_SPACEMOUSE && source <= MIXSRC_LAST_SPACEMOUSE))
return false;
#endif
Expand Down Expand Up @@ -424,14 +424,6 @@ bool isSwitchAvailable(int swtch, SwitchContext context)
return true;
}

int hasSerialMode(int mode)
{
for (int p = 0; p < MAX_SERIAL_PORTS; p++) {
if (serialGetMode(p) == mode) return p;
}
return -1;
}

bool isSerialModeAvailable(uint8_t port_nr, int mode)
{
#if defined(USB_SERIAL)
Expand Down Expand Up @@ -492,7 +484,7 @@ bool isSerialModeAvailable(uint8_t port_nr, int mode)
return false;
#endif

auto p = hasSerialMode(mode);
auto p = serialGetModePort(mode);
if (p >= 0 && p != port_nr) return false;
return true;
}
Expand Down Expand Up @@ -1014,7 +1006,7 @@ bool isTrainerModeAvailable(int mode)

if (mode == TRAINER_MODE_MASTER_SERIAL) {
#if defined(SBUS_TRAINER)
return hasSerialMode(UART_MODE_SBUS_TRAINER) >= 0;
return serialGetModePort(UART_MODE_SBUS_TRAINER) >= 0;
#else
return false;
#endif
Expand Down Expand Up @@ -1227,4 +1219,4 @@ uint8_t getPotType(int index)
void setPotType(int index, int value)
{
g_eeGeneral.potsConfig = bfSet<potconfig_t>(g_eeGeneral.potsConfig, value, (POT_CFG_BITS * index), POT_CFG_TYPE_BITS);
}
}
1 change: 0 additions & 1 deletion radio/src/gui/gui_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ bool isSourceAvailableInResetSpecialFunction(int index);
bool isSourceAvailableInGlobalResetSpecialFunction(int index);
bool isSwitchAvailable(int swtch, SwitchContext context);
bool isSerialModeAvailable(uint8_t port_nr, int mode);
int hasSerialMode(int mode);
bool isSwitchAvailableInLogicalSwitches(int swtch);
bool isSwitchAvailableInCustomFunctions(int swtch);
bool isSwitchAvailableInMixes(int swtch);
Expand Down
25 changes: 7 additions & 18 deletions radio/src/lua/api_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2258,24 +2258,13 @@ Set baudrate for serial port(s) affected to LUA
*/
static int luaSetSerialBaudrate(lua_State * L)
{
// #if defined(AUX_SERIAL) || defined(AUX2_SERIAL)
// unsigned int baudrate = luaL_checkunsigned(L, 1);
// #endif

// TODO: add some callbacks for serial settings
// #if defined(AUX_SERIAL)
// if (auxSerialMode == UART_MODE_LUA) {
// auxSerialStop();
// auxSerialSetup(baudrate, false);
// }
// #endif
// #if defined(AUX2_SERIAL)
// if (aux2SerialMode == UART_MODE_LUA) {
// aux2SerialStop();
// aux2SerialSetup(baudrate, false);
// }
// #endif
return 1;
int port_nr = serialGetModePort(UART_MODE_LUA);
if (port_nr < 0) return 0;

uint32_t baudrate = luaL_checkunsigned(L, 1);
serialSetBaudrate(port_nr, baudrate);

return 0;
}

/*luadoc
Expand Down
32 changes: 32 additions & 0 deletions radio/src/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,38 @@ void serialSetPower(uint8_t port_nr, bool enabled)
}
#endif

uint32_t serialGetBaudrate(uint8_t port_nr)
{
auto state = getSerialPortState(port_nr);
if (!state || !state->port || !state->usart_ctx)
return 0;

auto port = state->port;
if (!port->uart || !port->uart->getBaudrate) return 0;

return port->uart->getBaudrate(state->usart_ctx);
}

void serialSetBaudrate(uint8_t port_nr, uint32_t baudrate)
{
auto state = getSerialPortState(port_nr);
if (!state || !state->port || !state->usart_ctx)
return;

auto port = state->port;
if (!port->uart || !port->uart->setBaudrate) return;

port->uart->setBaudrate(state->usart_ctx, baudrate);
}

int serialGetModePort(int mode)
{
for (int p = 0; p < MAX_SERIAL_PORTS; p++) {
if (serialGetMode(p) == mode) return p;
}
return -1;
}

void serialInit(uint8_t port_nr, int mode)
{
auto state = getSerialPortState(port_nr);
Expand Down
7 changes: 7 additions & 0 deletions radio/src/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@
#define SERIAL_CONF_POWER_BIT 7 /* MSBit of the configuration byte */

const etx_serial_port_t* serialGetPort(uint8_t port_nr);

int serialGetMode(uint8_t port_nr);
void serialSetMode(uint8_t port_nr, int mode);

bool serialGetPower(uint8_t port_nr);
void serialSetPower(uint8_t port_nr, bool enabled);

uint32_t serialGetBaudrate(uint8_t port_nr);
void serialSetBaudrate(uint8_t port_nr, uint32_t baudrate);

int serialGetModePort(int mode);

void initSerialPorts();
void serialInit(uint8_t port_nr, int mode);
void serialStop(uint8_t port_nr);
Expand Down