How to write a custom library for a sensor in micropython?
Basically I'm trying to interface AT42QT2120 sensor using micropython in my esp8266. I already have the code up and running in arduino, i just want the same to be done in micropython.
New Library creation
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: New Library creation
There are numerous examples of user contributed drivers in this forum and in the wiki - I suggest you study some of these along with the official drivers in the drives subdirectory of the source tree.
Porting drivers from C is reasonably straightforward. The first step is to ensure the indentation is right for Python (in my opinion converting tabs to spaces). Then use search and replace to change the syntax. Start with the easy stuff like getting rid of semicolons and curly brackets. Fix function definitions and variable declarations. And so on, until it looks like Python. Aim for PEP8 code style conformance.
Then address the interesting stuff which is changing the hardware interface code to match the MicroPython libraries.
Finally test and publish
The first step is the most important. C doesn't care about indentation; Python does. So it's vital that the C code's indentation is fixed to match the curly brackets before you strip them otherwise program logic bugs will be created in the Python code.
Porting drivers from C is reasonably straightforward. The first step is to ensure the indentation is right for Python (in my opinion converting tabs to spaces). Then use search and replace to change the syntax. Start with the easy stuff like getting rid of semicolons and curly brackets. Fix function definitions and variable declarations. And so on, until it looks like Python. Aim for PEP8 code style conformance.
Then address the interesting stuff which is changing the hardware interface code to match the MicroPython libraries.
Finally test and publish
The first step is the most important. C doesn't care about indentation; Python does. So it's vital that the C code's indentation is fixed to match the curly brackets before you strip them otherwise program logic bugs will be created in the Python code.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: New Library creation
Also, search on github.com for "MicroPython" or "MicroPython Sensor" to find some examples.
If you share your project on github, we can help you with a code review.
If you share your project on github, we can help you with a code review.