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

Support changing module type from LUA model.setModule() #3699

Merged
merged 1 commit into from
Jun 20, 2023

Conversation

alteman
Copy link
Contributor

@alteman alteman commented Jun 18, 2023

Currently LUA model.setModule() cannot change module type. I want to change that to enable scripts that switch between internal and external module.
User scenario 1 is using same EdgeTX model with multiple craft on multiple receiver types; currently you either need to clone the model (making modifications cumbersome, since you need to keep them in sync), or manually switch modules in the menu.
User scenario 2: this can be used to enable 'check antenna' warning for radios with removable internal module antennas - they can burn out the transmitter if the antenna isn't connected, so the user would be able to have LUA remind them to put on the antenna after the radio boots, and only enable the module (via model.setModule) once the user acknowledges the antenna is on by flipping a switch.

@pfeerick pfeerick added the lua-api Lua API related label Jun 19, 2023
@pfeerick
Copy link
Member

pfeerick commented Jun 19, 2023

Sounds good to me... given the documentation instruction of

module parameters, see model.getModule()

IMO it should let you set everything getModule returns in the table.

It seems like the type was added back in OTX 2.3.8 via opentx/opentx#7538 ... but only the read-side was implemented (as that is all that was needed for that feature).

@pfeerick
Copy link
Member

Just cobbled together a quick Lua test script based on the wizard loader, and tried it out on the Jumper TPro with an external MPM module fitted, works perfectly - i.e. switches modes for the external module, and turns on and off ;)

local MODULE_MODE = 0
local MODULE_OFF = 0
local MODULE_CRSF = 1
local MODULE_MULTI = 2
local MODULE_PPM = 3

-- Common functions
local function fieldIncDec(event, value, max)
  if event == EVT_VIRTUAL_DEC or event == EVT_VIRTUAL_DEC_REPT then
    value = (value - 1)
  elseif event == EVT_VIRTUAL_INC or event == EVT_VIRTUAL_INC_REPT then
    value = (value + max + 3)
  end
  value = (value % (max+2))
  return value
end

-- Module options menu
local function moduleTypeSurround(index)
  if index <= 1 then
    lcd.drawFilledRectangle(59*(index%2)+12, 13, 43, 23)
  else
    lcd.drawFilledRectangle(59*(index%2)+12, 34, 40, 20)
  end
end

local function drawModuleChoiceMenu()
  lcd.clear()
  lcd.drawScreenTitle("Module Mode", 0, 0)
    lcd.drawText( 20, 20, "OFF")
    lcd.drawText( 78, 20, "CRSF")
    lcd.drawText( 20, 40, "MULTI")
    lcd.drawText( 78, 40, "PPM")
  moduleTypeSurround(MODULE_MODE)
  fieldsMax = 0
end

local function moduleMenu(event)
  drawModuleChoiceMenu()
  if event == EVT_VIRTUAL_ENTER then
    if MODULE_MODE == MODULE_OFF then
      model.setModule(1, {Type=0})
      return 0
    elseif MODULE_MODE == MODULE_CRSF then
      model.setModule(1, {Type=5})
      return 0
    elseif MODULE_MODE == MODULE_MULTI then
      model.setModule(1, {Type=6})
      return 0
    elseif MODULE_MODE == MODULE_PPM then
      model.setModule(1, {Type=1})
      return 0
    end
  else
    MODULE_MODE = fieldIncDec(event, MODULE_MODE, 2)
  end
  return 0
end

-- Main
local function run(event)
  if event == nil then
    error("Cannot be run as a model script!")
  end

  if event == EVT_VIRTUAL_EXIT then
    return 2
  end

  return moduleMenu(event)
end

return { run=run }

@pfeerick pfeerick added this to the 2.9 milestone Jun 20, 2023
@pfeerick pfeerick merged commit 97deeeb into EdgeTX:main Jun 20, 2023
@BoyanHou
Copy link

nice!!!

@BoyanHou
Copy link

BoyanHou commented Jul 24, 2023

@alteman @pfeerick I was testing a very simple lua script based on this change (based on release candidate 2.9)

the logic is bascially this:

-- turn off both internal and external module
model.setModule(0, {Type=0})
model.setModule(1, {Type=0})

however seems changing the module values in this way will not trigger the UI in the model setting to sync to the latest status? E.g. when I use this script, then go to the model settings page, internal module is still showing "CRSF", while it should actually be "off".

Not sure whether it is possible to sync the UI with the values set via this function (IMO this can reduce confusion and is a better overall user experience)? If it is possible could you provide some code pointers, I can also help work on that. Thanks!

@BoyanHou
Copy link

nvm, it was because I was not using the v2.9 companion. After I updated it worked perfectly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lua-api Lua API related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants