Code: Select all
from gpiozero import OutputDevice
myPin = OutputDevice(3)
myPin2 = OutputDevice(4)
device1 = MyDeviceClass(myPin)
device2 = MySecondDeviceClass(myPin, myPin2)
Code: Select all
Class motorcontrol(object):
def __init__(self,OutputDevice):
myPin = OutputDevice(3)
myPin2 = OutputDevice(4)
self.device1 = MyDeviceClass(myPin)
self.device2 = MySecondDeviceClass(myPin, myPin2)
def motor_check(self):
if self.device1==1: #looks at gpio3
blah blah
# =============================
Class PressureRead(object):
def __init__(self,OutputDevice):
myPin = OutputDevice(3)
myPin2 = OutputDevice(4)
self.device1 = MyDeviceClass(myPin)
self.device2 = MySecondDeviceClass(myPin, myPin2)
def pressure_update(self):
if self.device2==1: #looks at gpio3
blah blah
Its very important and definitely different in your case. You have 2 SPI devices and an I2C device which means that you don't setup the GPIO pins in order to communicate with them. You leave that to the OS, you just access the devices made available to you by the OS. The OS's driver (actually its even lower level than the OS) manages the fact that the two different SPI devices share the same physical pin.
Code: Select all
import spidev
Class DAC_BU2502:
def __init__(self, bus, device):
self.spi = spidev.SpiDev()
self.spi.open(bus, device)
self.spi.max_speed_hz = 976000
#other class methods to configure/read
Class sense_MPL115:
def __init__(self, bus, device):
self.spi = spidev.SpiDev()
self.spi.open(bus, device)
self.spi.max_speed_hz = 976000
#other class methods to configure/read
dac = DAC_BU2502(0,0) #Open the DAC on SPI bus 0 port 0
mpl = sense_MPL115(0,1) #Open the MPL device on SPI bus 0 port 1
You haven't gotten anywhere because you think you need to bit bang the SPI and I2C devices. I have no idea why you are doing this and this is the first time you have mentioned bit banging.picandies wrote: ↑Wed Oct 24, 2018 2:31 pmWe haven't gotten anywhere. You still haven't answered the question...first the spi lines are bit-banged in each class. Also, the other pin (trigger_signal) goes to both boards of electronics to act as a trigger signal for that board's electronics. If the same pin is needed in multiple classes, how is it setup/handled?
Code: Select all
from gpiozero import OutputDevice, InputDevice
#Pins are initialised once here
cs0_pin = OutputDevice(x)
cs1_pin = OutputDevice(x)
clk_pin = OutputDevice(x)
mosi_pin = OutputDevice(x)
miso_pin = InputDevice(x)
class DAC_BU2502:
def __init__(self,cs,clk,mosi,miso):
#We tell the DAC class which pin it should use for chip select
self.cs = cs
self.clk = clk
self.mosi = mosi
self.miso = miso
class sense_MPL115:
def __init__(self,cs,clk,mosi,miso):
self.cs = cs
self.clk = clk
self.mosi = mosi
self.miso = miso
dac = DAC_BU2502(cs0_pin,clk_pin,mosi_pin,miso_pin)
mpl = sense_MPL115(cs1_pin,clk_pin,mosi_pin,miso_pin)
Code: Select all
class SPI_bit_bang:
def __init__(self,cs,clk,mosi,miso):
#store the pins here
class DAC_BU2502(SPI_bit_bang):
#This class doesn't need an init method because it inherits from its parent.
def write_data(self,data):
#use the GPIO pins here
dac = DAC_BU2502(cs0_pin,clk_pin,mosi_pin,miso_pin)
Sure we have: viewtopic.php?f=63&t=225302#p1383780We haven't gotten anywhere. You still haven't answered the question..
That makes no difference. It's a resource that should be managed in one place, by one piece of code. Be it a class, functions in a module, or whatever. The rest of your program accesses the resource though that interface. Clean and nice....first the spi lines are bit-banged in each class.
Don't do that. See above.If the same pin is needed in multiple classes, how is it setup/handled?
It is not. See my post above and others here.I'm not sure why this is focusing on spi,i2c forget them for now.
It is not. See my post above and others here.How do you define/set that up so that both classes can tell the line is high? Not sure why this is such a tough question to get an answer.
Code: Select all
...class DAC_BU2502(SPI_bit_bang):
Code: Select all
class my_io_setup():
.blah blah
set up ALL the io stuff
class readpressure(my_io_set_up):
def readPressure: ..use the io stuff from my_io_setup
def check_4_trigger
def calibratepressure:
class motor_ctrl(my_io_setup):
def set_rpms
def check_4_trigger ...similar to one above
de calibrate rpms
Why are you sounding so acidic? That why I posted the question--for some help on setting up I/o using several classes.Perhaps you need to take a python tutorial so that you understand how classes work
Code: Select all
from gpiozero import OutputDevice, InputDevice
#Pins are initialised once here, x can be what ever pin you want
#Note that the pins are only initialised once and aren't reinitialised in each class.
cs0_pin = OutputDevice(x)
cs1_pin = OutputDevice(x)
clk_pin = OutputDevice(x)
mosi_pin = OutputDevice(x)
miso_pin = InputDevice(x)
class DAC_BU2502:
def __init__(self,cs,clk,mosi,miso):
#We tell the DAC class which pin it should use for chip select
self.cs = cs
self.clk = clk
self.mosi = mosi
self.miso = miso
def config(self):
#Example - Both classes can set the same GPIO pins.
self.cs.on()
self.clk.on()
class sense_MPL115:
def __init__(self,cs,clk,mosi,miso):
self.cs = cs
self.clk = clk
self.mosi = mosi
self.miso = miso
def config(self):
#Example
self.cs.on()
self.clk.on()
#See below here. CLK, MISO, MOSI pins are shared by the instances of the two classes
#Both classes are passed a reference to a GPIO pin that has already been defined.
dac = DAC_BU2502(cs0_pin,clk_pin,mosi_pin,miso_pin)
mpl = sense_MPL115(cs1_pin,clk_pin,mosi_pin,miso_pin)