The Blog: Python Code Libraries Updates

12 days ago

We are pleased to announce that we have a new and updated Python library available to use with our Raspberry Pi expansion boards.  The new version 2.0 update includes several major changes which can be found in the changelog.md in the root of the github repository.

Since we launched the python library in 2014 it has grown organically with regular updates to the features and new boards and demos added.  The result of all those updates is that the structure of the library had become untidy as the demos were in the same folders as the board classes and helper functions.  The classes and demos were also not PEP8 compliant and the naming conventions were not consistent across the code.  

For several years we have maintained two separate versions of the Python library for Python 2 and Python 3 releases.  This meant that any change to one library would need to be duplicated which added extra work and the potential for bugs to creep into the code. 

With these issues in mind we decided that it was time to restructure the library and bring everything up to the current Python PEP8 standards.

The new Python library is now compatible with Python 2 and 3 allowing you to develop your applications in either version from a single code base.  

The demo files have been moved into a demos subfolder in each of the class folders making the library more organised.  This change will make it easier for us to add more demos and tutorials for our expansion boards without adding clutter to the class folders.

We have renamed the class files removing the ABE_ from all the names so for example ABE_ADCPi is now ADCPi.  We have also merged the ABE_Helper.py into the main classes so you no longer need to create an instance of the helper class and pass it into the board class.  These changes should reduce the amount of code needed to use our expansion boards.  

To give you an example with the previous version of the Python library you would need the following code to initialise an ADC Pi object:

from ABE_ADCPi import ADCPi
from ABE_helpers import ABEHelpers

i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
adc = ADCPi(bus, 0x68, 0x69, 12) 

With the new 2.0 update, this has been reduced to:

from ADCPi import ADCPi

adc = ADCPi(0x68, 0x69, 12) 

All the function names are still the same so if you want to use the 2.0 release with your current project the code changes you need to make will be minimal.

As well as reducing the amount of code you need to write we have also gone through all the classes optimising the functions and adding more error protection.  In the previous release, errors were printed to the console.  With 2.0 we have removed all the print calls and changed the code to raise an exception when an error occurs.  This should make it easier for you to manage exceptions and deal with them accordingly.  We have also added extra input validation so the functions should raise a ValueError if it gets an unexpected value.

On the ADCPi and ADCDifferential Pi classes we have made several optimisations to reduce the number of function calls needed during each ADC sample.  While this won’t have any major impact to the sample rate which is still limited by the ADC it does mean your Raspberry Pi will use less CPU cycles, improving application performance and power usage.

With the set_dac_raw() function in the ADCDACPi and ExpanderPi classes we have completely rewritten the way it calculates the values before sending them to the DAC which should give you a speed improvement of around 48%.  On a Raspberry Pi 2 using the previous version of the Python library you could generate a 6KHz square wave from the DAC output, this has been increased to over 10KHz on the updated version.  

The other major change which has been requested from several of our customers is the library can now be installed into the Python library path on your Raspberry Pi by using PIP or running the new setup.py file in the root of the library.

For python 2.7 you can install the library with:

sudo python2.7 -m pip install git+https://github.com/abelectronicsuk/ABElectronics_Python_Libraries.git

For python 3.4 use:

sudo python3.4 -m pip install git+https://github.com/abelectronicsuk/ABElectronics_Python_Libraries.git

If you want to download the library and install it you can use git to clone the library onto your computer:

git clone https://github.com/abelectronicsuk/ABElectronics_Python_Libraries.git

To install it, navigate to the ABElectronics_Python_Libraries folder and run:

For Python 2.7:

sudo python setup.py install

For Python 3.4:

sudo python3 setup.py install 

The new updated library is available from https://github.com/abelectronicsuk/ABElectronics_Python_Libraries and we have demo code samples for each of the supported expansion boards in the demo folders for each board.

The older ABElectronics_Python3_Libraries will no longer be updated so please use the new ABElectronics_Python_Libraries instead for your Python 3 projects.

Previous versions of the Python libraries can be found at https://github.com/abelectronicsuk/Archive

We have Python, Arduino, C, C++, Node.js and Windows 10 IOT libraries available for many of our expansion boards.  You can download all of the libraries from github at:

https://github.com/abelectronicsuk/ or click on the logos below for your selected programming language,

Download Python 2 and 3 code samples

Node.js code samples

C code samples C++ code samples

Download Windows 10 IOT code samples

Download Arduino code samples


Back to the Blog.

Shop

Support

Products added to basket

Items

Total (inc shipping)

CheckoutView Basket

Free Shipping on all orders over £30

Click to contact us.
X

Have any Questions?

Send us any questions about this page or product and we will reply within 24 hours.

Submit your Message

X

Have any Questions?

Thank you for contacting us.

We will respond to your enquiry in the next 24 hours.

You can find answers to many questions and additional product support in our Knowledge Base and the Community Forum.