FrSkyRC / ETHOS-Feedback-Community

Feedback & suggestions are welcomed here for ETHOS by FrSky
190 stars 85 forks source link

LUA (1.5.10): setting source parameters #4224

Closed Alex00101 closed 3 days ago

Alex00101 commented 4 months ago

It is very possible that the below is not an issue but my lack of understanding, if this the case I apologize and appreciate the clarification.

I'm referring to the following calls in [lua] Source (latest lua doc) and features introduced in 1.5.10

[lua] Source:minimum() Return the source minimum (and modify it on Vars and Sensors)
[lua] source:value(), source:protocolUnit(), source:protocolDecimals() can now modify the value / internal unit and decimals of a DIY telemetry sensor

I'm creating my custom source, using the code "template" below (I have removed irrelevant code). While source:category(CATEGORY_TELEMETRY_SENSOR), source:decimals(2), source:unit(UNIT_VOLT) calls work as expected other calls don't cause any effect e.g. source:minimum() always returns -1e9....

Appreciate any help

local SOURCE_NAME = 'My Source' local SOURCE_KEY = 'MySrc'

local function sourceInit(source) source:category(CATEGORY_TELEMETRY_SENSOR) source:decimals(2)
source:unit(UNIT_VOLT)
---- Above is working, below causes no effect
source:minimum(1) source:maximum(100) source:protocolUnit(UNIT_VOLT) source:protocolDecimals(2) end

local function sourceRead(source) end

local function sourceWrite(source) end

local function sourceWakeup(source)
source:value(101) -- works as expected end

local function sourceConfigure(source)
end

local function init() system.registerSource({key=SOURCE_KEY, name=SOURCE_NAME, init=sourceInit, wakeup=sourceWakeup, configure = sourceConfigure, read=sourceRead, write = sourceWrite }) end

return {init=init}

bsongis-frsky commented 4 months ago

Does it work? source:category(CATEGORY_TELEMETRY_SENSOR)

bsongis-frsky commented 4 months ago

I don't understand what you are trying to do :) If you register a new Source, then its category is CATEGORY_LUA, and you cannot change its category

Alex00101 commented 4 months ago

Yes. category, unit, decimals, value work. The rest (minimum, maximum, protocol , options etc) does not.

bsongis-frsky commented 4 months ago

I don't understand how category can work, would you try to print source:category() ?

bsongis-frsky commented 4 months ago

I am not sure of what you are trying to do. It seems to me that you need a DIY sernsor. If I am true, you should use model.createSensor(...) where all the methods you are mentionning should work.

Alex00101 commented 4 months ago

Sorry category is my bad. It has no effect (doesn't throw an error but has no effect, still returns category lua). I'm still able to change value, unit and decimals, and assumed I should be able to change min and max as well?

bsongis-frsky commented 4 months ago

Min / Max are not needed, as only your script changes the value, you just need to define your own Min / Max which are Lua only, and private to the Lua Source.

protocolDecimals / protocolUnit are only used with DIY / Custom sensors, where there could be value conversion. But here on Lua sources, there is no protocol, you have to implement what you need in Lua.

Alex00101 commented 4 months ago

Ok thank you. I suspected I might be doing something wrong.

To explain the intent. I wanted a "source" that would effectively work like a sensor but with relatively complex logic for calculating value. My understanding that model.createSensor dies not allow me to apply this logic (cannot hijack wakeup etc) but the source works perfectly for my purposes except that I'd like to be able to set programmaticaly at least one parameter from inside the source code on top of value, unit, decimals. Member, option, min, max whatever. The reason is to signal the caller (ie widget lua code) what "formula" the source is running. I.e. the unit is mah but the value could be mah remaining or mah spent. So option, member whatever would work.

Anyway I understand this is not a bug, happy to park it, but if you could consider this a an future enhancement or later offer me any alternative I'd appreciate that. Sorry if it was wrong time to ask.

bsongis-frsky commented 4 months ago

Perhaps a new registerSensor would be better as I understand you need units conversion I don't understand how mAh remaining / mAh spent could be configured, you would need a Widget for that?

Alex00101 commented 4 months ago

Perhaps a new system.registerSensor with the format similar to registerSource (init, wakeup...) would be ideal. Currently I set source parameters via source configure where I set calculation mode or formula as a choice (mah spent, mah remaining, etc) I just have no way to tell the source caller (ie widget) what mode the source in. If it makes sense.

github-actions[bot] commented 1 month ago

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 30 days.