Page 1 of 1
Writing A Sensor Library/Driver
Posted: Tue Dec 15, 2020 6:19 am
by RedDirt
If one were so inclined to take a stab at writing the code for a library to work with a particular sensor, where would you start? I'd like to especially learn how to port a library that is available for Arduino but not yet for Micropython.
Where would one start on this journed? What information would I need to be looking for?
Re: Writing A Sensor Library/Driver
Posted: Tue Dec 15, 2020 6:37 pm
by cgglzpy
There is a nice talk about that:
An Introduction to Hardware Drivers in Micro Python
To sum up:
- Understand the datasheet
- Know how to write/read to/from the sensor registers (it varies a bit depending on the protocol used I2C/SPI/UART)
- Also understanding bitwise operations, binary/hexadecimal numbering system and how this representations works in python will help a lot
Ex:
Code: Select all
>>> bin(42)
'0b101010'
>>> hex(42)
'0x2a'
>>> 0b101010
42
>>> 0x2a
42
>>>
Re: Writing A Sensor Library/Driver
Posted: Wed Dec 16, 2020 8:34 am
by RedDirt
Outstanding! Thank you cqqlzpy.
cgglzpy wrote: ↑Tue Dec 15, 2020 6:37 pm
There is a nice talk about that:
An Introduction to Hardware Drivers in Micro Python
To sum up:
- Understand the datasheet
- Know how to write/read to/from the sensor registers (it varies a bit depending on the protocol used I2C/SPI/UART)
- Also understanding bitwise operations, binary/hexadecimal numbering system and how this representations works in python will help a lot
Ex:
Code: Select all
>>> bin(42)
'0b101010'
>>> hex(42)
'0x2a'
>>> 0b101010
42
>>> 0x2a
42
>>>
Re: Writing A Sensor Library/Driver
Posted: Tue Dec 22, 2020 12:23 pm
by RedDirt
Since making this post I have been trying to find some simple device that uses I2C that I could attempt to write a library for, for the experience and the experience of it. However, I am coming up short. Does anyone have some ideas? I need to start with something relatively simple, not something like the BME680 or something insane like that. Something I can buy the sensor for and get the datasheet for and start working through the process of how to actually write the library and test it out.
Re: Writing A Sensor Library/Driver
Posted: Tue Dec 22, 2020 1:26 pm
by shaoziyang
Re: Writing A Sensor Library/Driver
Posted: Tue Dec 22, 2020 2:04 pm
by RedDirt
You're my hero. How/Where did you learn to do that?
Re: Writing A Sensor Library/Driver
Posted: Wed Dec 23, 2020 9:11 am
by shaoziyang
It's not difficult, just try more, and more discussion.
Re: Writing A Sensor Library/Driver
Posted: Wed Dec 23, 2020 3:27 pm
by RedDirt
This is rather long, and I have not read through the whole thing as of yet, but it appears to be a good document on porting/writing libraries for sensors.
https://www.digikey.fi/fi/maker/project ... 1725f28b1b