-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Rewrite bme280 driver in Lua+C #3126
Comments
Being original contributor of the BME208 driver I'm registering myself in the discussion. I need to re-think your suggestions to fully understand the logic behind. |
@vsky279 I'm definitely interested, but as stated in the PR, somewhat short on time. Don't let my curiosity stop you from going for it! :D |
@nwf Nathaniel, I'm trying to figure out more in detail the way the new driver should be designed. So I try to reformulate the workflow:
Existing functions Forced readout mode should be implemented in Please comment. This does not seem to difficult to be implemented... |
Excellent! Story-boarding is a very, very good idea.
Sounds good. I'd suggest "1 string : 1 transaction" -- if there are bytes that get sent to registers 0x10 through 0x13 and then later 0x18 through 0x20, that sort of feels like two strings to me. But this is a judgement call.
If these are contiguous, one "configuration transaction" may be possible, with one string back from C. if not, maybe three. But I think this is a great general flow: Lua acting as a message-passing layer between the C driver core and the device itself.
Sounds great and is the reverse direction of message passing from step 3. :)
Multiple returns also probably works fine, so long as it's not like... dozens of them. Four is probably fine.
Additional utility functions seem perfectly reasonable; these, presumably, take the UD and a readout string as well or do they need something more or less ...?
Agreed.
I believe that's correct as well; a
I hope it is not difficult, yes! But it is, nevertheless, something of an experiment in a new (hopefully more developer-approachable and -maintainable!) architecture for NodeMCU drivers. And it will probably cause us to shake things throughout the stack, including our image builders (see #3128 for broader discussion). |
While trying to figure out what was going on in #3132, I noticed that the bme280 lua module defines the read_reg and write_reg functions. |
I think we would gladly take a |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
In an effort to decrease the project's technical debt, I've proposed (in #3124) that the C-only
bme280
be cut into a Lua layer that drives communication and a C module that does any requisite low-level fiddling. This will be similar to #2819 and may, incidentally, address #2527 correctly as well. (As you can see, between that and #2241, this module has a history of being sufficiently troublesome that issues have been worked around rather than fixed.) This rewrite will also eliminate yet another source of static allocation and artificial limitation in our driver layer. All in all, deeply worth it.I would like it very much if the result of this rewrite included a test driver, as well. Ideally
bme280
becomes an example of how all drivers should look. No pressure (ha!). ;)OK, so, what does this entail, concretely?
Looking at the existing
bme280.c
, we almost immediately see that there's a bunch of goo associated with performingi2c
transactions:bme280_i2c_addr
,r8u_n
,r8u
, andw8u
. These can all be directly transcoded to Lua. The results from thei2c
package will be Lua strings.The
bme280_data
structure holds per-device state obtained duringsetup
, which means we probably want to hold it instead in a Lua userdata object that represents the sensor. Readingsetup
, it looks like this takes 3 or 4 I2C transactions to set up, including getting the chip ID, so I think our sensor object constructor should take 2 or 3 Lua strings (DIG_T
,DIG_P
, andDIG_H
) to build itself. Since it appears that the static valuesbme280_t_fine
,bme280_h
, andbme280_hc
may outlive a particular measurement, they should perhaps join thebme280_data
in the per-device userdata object.Armed with the sensor parameters, we can look at interacting with the device:
bme280_lua_startreadout
does some i2c transactions, easily done instead in Lua, and schedules a timer, also easily done in Lua. (Can the device not raise IRQs?)Once it's time to read data,
bme280_lua_read
does some more i2c transactions and a lot of math, including storing "fine" constants between procedure calls. To continue the theme, the transactions should be done in Lua, and passed to a C routine that takes the sensor userdata object and does the requisite math. For my $0.02, the results should just land in a Lua table.The abbreviated
bme280_lua_temp
,bme280_lua_baro
, andbme280_lua_humi
functions are readily achieved by having the Lua replacement forbme280_lua_read
perform fewer transactions and call into the C function with fewer string arguments. That function should return a table with fewer keys as a result.For the purposes of PRing, I'd suggest placing code in
app/modules/bme280_math.c
for the C layerlua_modules/bme280/bme280.lua
for the Lua top-level and main driverlua_modules/bme280/bme280_test.lua
for the test script (ideally in mispec, I think? See Create unit tests in Lua #2145).The text was updated successfully, but these errors were encountered: