Sunday, April 13, 2014

Python Firmata libraries


In order to abstract the hardware with Firmata, I must build up a test loop. First I will use a mature desktop firmata host application to test the firmware, then using the tested firmware to speed up the development for Android class libraries and APK.

I am used to use Python to setup a rapid application to test the LPC812MiniKit. Simplest approach ever.


Firmata for Python


There are two Python libraies available for Firmata applications:


By manually inspecting the source code, I found pyduino is older than pyfirmata. pyduino's main file is only 10KB, the latest update timestamp is 2009. The coding style is quite close to mine, who has background of embedded systems like microcontroller firmware development.


On the other hand, pyFirmata is more complete and up to date, the latest update is taken in 2013. pyFirmata also has more advanced software skills including test suite. Additionally, it allows custom boards to be added to the project. Since LPC812MiniKit can be configured as multiple profiles, pyFirmata is much suitable for our cases.


Both libraries defined Arduino class. I refer to pyFirmata implementation to define GAPmini class or LPC812 class. Although Firmata was defined for Arduino, I want a seperate class namte to identify different platforms.




I added some board profiles in and files, including LPC812Min/LPC812Max/LPC812MixMin.


Thanks Tino (the author of pyFirmata from Dutch) for its contributions and orientations for demo Python scripts. (Basic test):


from pyfirmata import LPC812Min, util
from pyfirmata import BOARDS
import time

print '>>>> 5 second delay for firmata initial'
print '>>>> Press reset to go'
board = LPC812Min('COM27', baudrate=115200)
print '>>>>',str(board)

board.sp.setDTR(1) # set LOW
board.sp.setDTR(0) # set HIGH, LOW to High to reset

layout = BOARDS['lpc812_min']
print ">>>> Board layout"
print str(layout)
(major,minor) = board.get_firmata_version()
print ">>>> Version: %d.%d"%(major,minor)
print ">>>> Firmware name: %s"%(board.firmware)

pin17 = board.get_pin('d:17:o')

while True:
    except KeyboardInterrupt:
        print ">>>> User interrupt."

print 'Exit it.'


The basic Firmata has been tested and some modifications have been made. I will propose a pull request to Tino to merge the changes in pyFirmata.



Now I have offered more demos for LPC812MiniKit, including Digital In (as keypad), Digital Out (as LED blink), Fade (as LED fade in fade out). 

Since PWM is implemented by SCT (State Configurated Timer) in LPC812, now we can have 4 PWM assigned to any digital pins to drive multiple LEDs or a quad UAV.

In LPC812, we can only have 4bit ADC by comparator, or 10bit slow sigma-delta ADC with SCT/Comp, or external I2C ADC. If I release LPC1114 mini board for Firmata, we can have ADC as analog input.

No comments: