Python bindings (ctypes)

393 views
Skip to first unread message

Matthew Baker

unread,
Oct 20, 2013, 3:38:11 AM10/20/13
to bcm...@googlegroups.com
Hi there,

I recently threw together some python bindings to bcm2835 that I thought I'd share with the group. The bindings have been constructed in pure python using ctypes (http://docs.python.org/library/ctypes.html) which provides functionality to dynamically load shared libraries and, hence, requires bcm2835 to be built as a shared library object. You can find the bindings here https://github.com/mubeta06/py-libbcm2835 with instructions on installation and usage. The advantage of ctypes is it give a relatively "straight through" approach to the underlying c code. As an example I ported the example spi.c file as per below.

from _bcm2835 import *

if not bcm2835_init():
        return

    bcm2835_spi_begin()
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST) # The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0) # The default
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536) # The default
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0) # The default
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW) # the default

    # Send a byte to the slave and simultaneously read a byte back from the slave
    # If you tie MISO to MOSI, you should read back what was sent
    data = bcm2835_spi_transfer(0x23);
    print "Read from SPI: %02X\n" % data

    bcm2835_spi_end()
    bcm2835_close()

If there is any questions I'd be happy to field them.

Cheers,

matb

Mike McCauley

unread,
Oct 22, 2013, 5:40:42 PM10/22/13
to bcm...@googlegroups.com
Hi Matthew,

thanks for your contribution.
I have added a link to your module to the bcm2835 documentation for the next
release.

Cheers.
--
Mike McCauley VK4AMM mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com
Phone +61 7 5598-7474 Fax +61 7 5598-7070

Matthew Baker

unread,
Oct 27, 2013, 3:04:57 PM10/27/13
to bcm...@googlegroups.com
Hi Mike,

Thanks for the inclusion. On another note seeing as though the bindings require a shared library object would you accept a patch that sees the autotools configure file made more contemporary so both static and shared libs can be built via the build system?

Cheers,

matb

Mike McCauley

unread,
Oct 27, 2013, 4:17:06 PM10/27/13
to bcm...@googlegroups.com
Sure.

Cheers.

DGC

unread,
Nov 9, 2013, 1:04:31 AM11/9/13
to bcm...@googlegroups.com
Matt, I'm trying to use your library and I'm having problems.  I've cloned the git repository and run "sudo python ./setup.py install".  It seems to have installed correctly; the following files were installed:

/usr/local/lib/python2.7/dist-packages/libbcm2835/_bcm2835.py
/usr/local/lib/python2.7/dist-packages/libbcm2835/_bcm2835.pyc

but I get the following error when I try to include the module:

  File "therm.py", line 2, in <module>
    from _bcm2835 import *
ImportError: No module named _bcm2835

Any ideas what I'm doing wrong?

Thanks,
DG

DGC

unread,
Nov 10, 2013, 10:19:30 PM11/10/13
to bcm...@googlegroups.com
In case anybody is following, I got this to work by copying libbcm2835/_bcm2835.py into my project directory.  I wasn't expecting to have to do this but I'm a Python neophyte so maybe I'm doing something wrong.

ahlawy red

unread,
Jun 21, 2016, 10:04:31 AM6/21/16
to bcm2835

Hi Matthew,
thanks for your help.
but what if i wanna to specify a certain channel to read from
how can i do it using _bcm2835 ??

Dan H

unread,
May 9, 2017, 1:16:09 AM5/9/17
to bcm2835
Hi Mike/Matthew

The bcm2835_init() call works when using the C libraries.
However, when using the python (3.4) libraries it refuses to initialize. It returns 1 instead of 0.
Also the spi.py test returns 0 instead of 35 (hex23).

The problem may be the Waveshare AD/DA HAT that I'm using. However ads1256_test.c from Waveshare works and it goes through bcm2835_init()
I'm using version 1.52 of the lib on Raspi 2.0 with the latest Jessie (nice wall paper) and I have rebuilt al libs that I could find. 
It seems that the libbcm2835.so must be made with gcc and copied to /usr/local/lib

Please help if you can.

Regards
Dan

Mike McCauley

unread,
May 10, 2017, 8:15:16 AM5/10/17
to bcm...@googlegroups.com
Hi. I'm traveling at the moment and won't get to look at this for a few weeks

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "bcm2835" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bcm2835+u...@googlegroups.com.
To post to this group, send email to bcm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bcm2835/62341a9c-9b58-4f20-bdae-c99b58f08911%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages