New Library creation

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
amaldavid
Posts: 1
Joined: Tue Nov 07, 2017 7:07 am

New Library creation

Post by amaldavid » Tue Nov 07, 2017 7:13 am

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: New Library creation

Post by pythoncoder » Wed Nov 08, 2017 7:43 am

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.
Peter Hinch
Index to my micropython libraries.

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: New Library creation

Post by mcauser » Mon Nov 13, 2017 1:05 am

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.

Post Reply