As the title says... Chat on...

User avatar
By mickles
#54352 Thanks marcelstoer, that's exactly the kind of perspective I was after, I just didn't know how to do it. I didn't realize you could pass a function as an argument in lua, but that makes sense. The bme280 module has a sensor-reading function with a callback that works in a similar way.

In the end, passing it a callback function that returned the value worked, something like this:
Code: Select allfunction querySensorThenTrigger(callback_func)
 querySensor()
  tmr.alarm(0, 100, tmr.ALARM_SINGLE, function(callback_func)
    val = querySensor()
    callback_func(val)
  end)
end


and to use:
Code: Select allquerySensorThenTrigger(function()
    print(val)
end)



marcelstoer wrote:
mickles wrote:Anyone have any neat ideas on how to hold off returning a value until the sensor conversion is complete?


I'm not sure I fully understand but I have the feeling you're looking at this from the "wrong" perspective. Lua/NodeMCU is asynchronous and event-driven (reactive if you like buzz words). So, rather than "holding off returning until X" you need to define a callback function to "execute after X happened".
Something like the following may be more promising:

Code: Select allfunction querySensorThenTrigger(callback_func)
  querySensor()
  tmr.alarm(0, 100, tmr.ALARM_SINGLE, function(callback_func)
    querySensor()
    callback_func()
  end)
end