Feb 24, 2013

Python: Call a function from outside a Class (in another Class)

Warning! I'm a noob to python and the following may not be the best way to solve the problem. It may even contain errors!

I wanted to create a python class that would contain most of the functions I needed to interface a Samsung VFD to a raspberry pi. The VFD connects to the pi using SPI. To make things a little cleaner, I wanted to initialize SPI from within my main class, called SPI_VFD in the code shown below. I also needed to call the functions spi.writebytes and spi.xfer2 (from the module spidev) from within my SPI_VFD class.

In initial testing, here's what worked for me:

import spidev

class thisspi:
    spi = spidev.SpiDev()

class SPI_VFD:
     def __init__(self, callspi):
         self.myspi = callspi
     def setspi():

#instantiate spi connection
s = thisspi()

# initialize SPI_VFD class, passing the spi instance
vfd = SPI_VFD(s)



No comments:

Post a Comment