implement fake device

This commit is contained in:
Pavol Rusnak 2011-04-23 18:36:08 +02:00
parent f37c8b64dc
commit d4c3b1d9bf
2 changed files with 78 additions and 105 deletions

View file

@ -1,5 +1,3 @@
import usb
class Device: class Device:
VENDORID = 0x1fc9 VENDORID = 0x1fc9
@ -7,55 +5,88 @@ class Device:
INSIZE = 64 INSIZE = 64
OUTSIZE = 64 OUTSIZE = 64
def __init__(self): def __init__(self, fake = False):
usbdev = usb.core.find(idVendor = self.VENDORID, idProduct = self.PRODUCTID) self.fake = fake
if usbdev == None: if not fake:
raise Exception('EduBRM device not found') import usb
try: usbdev = usb.core.find(idVendor = self.VENDORID, idProduct = self.PRODUCTID)
usbdev.detach_kernel_driver(0) if usbdev == None:
except: raise Exception('EduBRM device not found')
pass try:
usbdev.set_configuration() usbdev.detach_kernel_driver(0)
self.epo = usb.util.find_descriptor(usbdev.get_interface_altsetting(), except:
custom_match = lambda e: \ pass
usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT) usbdev.set_configuration()
self.epi = usb.util.find_descriptor(usbdev.get_interface_altsetting(), self.epo = usb.util.find_descriptor(usbdev.get_interface_altsetting(),
custom_match = lambda e: \ custom_match = lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN) usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT)
self.epi = usb.util.find_descriptor(usbdev.get_interface_altsetting(),
custom_match = lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN)
else:
print 'device init'
# sets pwm (which=1,2), (duty=16bit) # sets pwm (which=1,2), (duty=16bit)
def pwm(self, which, duty): def pwm(self, which, duty):
duty = 65535 - duty if self.fake:
self.epo.write('p' + chr(which) + chr(duty & 0xff) + chr(duty >> 8)) print 'pwm', which, duty
else:
duty = 65535 - duty
self.epo.write('p' + chr(which) + chr(duty & 0xff) + chr(duty >> 8))
# sets ddswave (wave=square,sine,saw1,saw2) # sets ddswave (wave=square,sine,saw1,saw2)
def ddswave(self, wavetype): def ddswave(self, wavetype):
self.epo.write('d' + chr(wavetype)) if self.fake:
print 'dds wave', wavetype
else:
self.epo.write('d' + chr(wavetype))
# sets ddsfreq (freq=32bit) # sets ddsfreq (freq=32bit)
def ddsfreq(self, freq): def ddsfreq(self, freq):
self.epo.write('D' + chr(freq & 0xff) + chr((freq >> 8) & 0xff) + chr((freq >> 16) & 0xff) + chr(freq >> 24)) if self.fake:
print 'dds freq', freq
else:
self.epo.write('D' + chr(freq & 0xff) + chr((freq >> 8) & 0xff) + chr((freq >> 16) & 0xff) + chr(freq >> 24))
# set opamp (which=1,2), (chan=1..6), (gain=1, 2, 4, 5, 8, 10, 16, 32) # set opamp (which=1,2), (chan=1..6), (gain=1, 2, 4, 5, 8, 10, 16, 32)
def opamp(self, which, chan, gain): def opamp(self, which, chan, gain):
self.epo.write('m' + chr(which) + chr(chan) + chr(gain)) if self.fake:
print 'opamp', which, chan, gain
else:
self.epo.write('m' + chr(which) + chr(chan) + chr(gain))
# set all switches (states=6bit) # set all switches (states=6bit)
def switches(self, states): def switches(self, states):
self.epo.write('s' + chr(states)) if self.fake:
print 'switches', states
else:
self.epo.write('s' + chr(states))
# set pins state (states=3bit) (1 = input, 0 = output) # set pins state (states=3bit) (1 = input, 0 = output)
def setpins(self, states): def setpins(self, states):
self.epo.write('P' + chr(states)) if self.fake:
print 'setpins', states
else:
self.epo.write('P' + chr(states))
# set output (which=1,2,3), (state=0,1) # set output (which=1,2,3), (state=0,1)
def setout(self, which, state): def setout(self, which, state):
self.epo.write('o' + chr(which<<1 + state)) if self.fake:
print 'setout', which, state
else:
self.epo.write('o' + chr(which<<1 + state))
# 7x AD (16 bits) + 3 x I
def read(self): def read(self):
# 7x AD (16 bits) + 3 x I if self.fake:
i = self.epi.read(self.INSIZE) from random import randint
return (i[0] + i[1]<<8, # AD0 return (randint(0,65535), # AD0
i[2] + i[3]<<8, i[4] + i[5]<<8, i[6] + i[7]<<8, # AD1 .. AD3 randint(0,65535), randint(0,65535), randint(0,65535), # AD1 .. AD3
i[9] + i[9]<<8, i[10] + i[11]<<8, i[12] + i[13]<<8, # AD4 .. AD6 randint(0,65535), randint(0,65535), randint(0,65535), # AD4 .. AD6
i[14] & 0x01, (i[14] & 0x02) >> 1, (i[14] & 0x04) >> 2) # IO1 .. IO3 randint(0,7)) # IO1 .. IO3
else:
i = self.epi.read(self.INSIZE)
return (i[0] + i[1]<<8, # AD0
i[2] + i[3]<<8, i[4] + i[5]<<8, i[6] + i[7]<<8, # AD1 .. AD3
i[9] + i[9]<<8, i[10] + i[11]<<8, i[12] + i[13]<<8, # AD4 .. AD6
i[14] & 0x01, (i[14] & 0x02) >> 1, (i[14] & 0x04) >> 2) # IO1 .. IO3

View file

@ -25,107 +25,55 @@ class ModuleDebugWidget(QWidget):
QObject.connect(self.ui.pushPin2, SIGNAL("clicked(bool)"), self.on_pins_changed) QObject.connect(self.ui.pushPin2, SIGNAL("clicked(bool)"), self.on_pins_changed)
QObject.connect(self.ui.pushPin3, SIGNAL("clicked(bool)"), self.on_pins_changed) QObject.connect(self.ui.pushPin3, SIGNAL("clicked(bool)"), self.on_pins_changed)
self.dev = Device() self.dev = Device(True)
print self.dev
@pyqtSlot(int) @pyqtSlot(int)
def on_dialPWM1_valueChanged(self, val): def on_dialPWM1_valueChanged(self, val):
self.dev.pwm(1, val) self.dev.pwm(1, val)
print 'pwm1:', val
@pyqtSlot(int) @pyqtSlot(int)
def on_dialPWM2_valueChanged(self, val): def on_dialPWM2_valueChanged(self, val):
self.dev.pwm(2, val) self.dev.pwm(2, val)
print 'pwm2:', val
@pyqtSlot(int) @pyqtSlot(int)
def on_dialAMP1_valueChanged(self, val): def on_dialAMP1_valueChanged(self, val):
# self.dev.opamp(1, val) self.dev.opamp(1, val)
print 'amp1:', val
@pyqtSlot(int) @pyqtSlot(int)
def on_dialAMP2_valueChanged(self, val): def on_dialAMP2_valueChanged(self, val):
# self.dev.opamp(2, val) self.dev.opamp(2, val)
print 'amp2:', val
@pyqtSlot(int)
def on_checkOut0_stateChanged(self, state):
# state and self.dev.setout(1<<0) or self.dev.clrout(1<<0)
print 'out0:', state and '1' or '0'
@pyqtSlot(int)
def on_checkOut1_stateChanged(self, state):
# state and self.dev.setout(1<<1) or self.dev.clrout(1<<1)
print 'out1:', state and '1' or '0'
@pyqtSlot(int)
def on_checkOut2_stateChanged(self, state):
# state and self.dev.setout(1<<2) or self.dev.clrout(1<<2)
print 'out2:', state and '1' or '0'
@pyqtSlot(int)
def on_checkOut3_stateChanged(self, state):
# state and self.dev.setout(1<<3) or self.dev.clrout(1<<3)
print 'out3:', state and '1' or '0'
@pyqtSlot(int)
def on_checkOut4_stateChanged(self, state):
# state and self.dev.setout(1<<4) or self.dev.clrout(1<<4)
print 'out4:', state and '1' or '0'
@pyqtSlot(int)
def on_checkOut5_stateChanged(self, state):
# state and self.dev.setout(1<<5) or self.dev.clrout(1<<5)
print 'out5:', state and '1' or '0'
@pyqtSlot(int)
def on_checkOut6_stateChanged(self, state):
# state and self.dev.setout(1<<6) or self.dev.clrout(1<<6)
print 'out6:', state and '1' or '0'
@pyqtSlot(int)
def on_checkOut7_stateChanged(self, state):
# state and self.dev.setout(1<<7) or self.dev.clrout(1<<7)
print 'out7:', state and '1' or '0'
@pyqtSlot(bool) @pyqtSlot(bool)
def on_radioSine_clicked(self, checked): def on_radioSine_clicked(self, checked):
# self.dev.ddswave(0) self.dev.ddswave(0)
print 'dds: sine'
@pyqtSlot(bool) @pyqtSlot(bool)
def on_radioSquare_clicked(self, checked): def on_radioSquare_clicked(self, checked):
# self.dev.ddswave(1) self.dev.ddswave(1)
print 'dds: square'
@pyqtSlot(bool) @pyqtSlot(bool)
def on_radioSaw_clicked(self, checked): def on_radioSaw_clicked(self, checked):
# self.dev.ddswave(2) self.dev.ddswave(2)
print 'dds: saw'
@pyqtSlot(bool) @pyqtSlot(bool)
def on_radioInvSaw_clicked(self, checked): def on_radioInvSaw_clicked(self, checked):
# self.dev.ddswave(3) self.dev.ddswave(3)
print 'dds: invsaw'
@pyqtSlot(int) @pyqtSlot(int)
def on_dialDDS_valueChanged(self, val): def on_dialDDS_valueChanged(self, val):
# self.dev.ddsfreq(val) self.dev.ddsfreq(val)
print 'dds:', val
@pyqtSlot() @pyqtSlot()
def on_comboAMP1_changed(self): def on_comboAMP1_changed(self):
c = self.ui.comboAMP1c.currentIndex() + 1 c = self.ui.comboAMP1c.currentIndex() + 1
g = (1,2,4,5,8,10,16,32)[self.ui.comboAMP1g.currentIndex()] g = (1,2,4,5,8,10,16,32)[self.ui.comboAMP1g.currentIndex()]
# self.dev.opamp(1, c, g) self.dev.opamp(1, c, g)
print 'amp1:', c, g
@pyqtSlot() @pyqtSlot()
def on_comboAMP2_changed(self): def on_comboAMP2_changed(self):
c = self.ui.comboAMP2c.currentIndex() + 1 c = self.ui.comboAMP2c.currentIndex() + 1
g = (1,2,4,5,8,10,16,32)[self.ui.comboAMP2g.currentIndex()] g = (1,2,4,5,8,10,16,32)[self.ui.comboAMP2g.currentIndex()]
# self.dev.opamp(2, c, g) self.dev.opamp(2, c, g)
print 'amp2:', c, g
@pyqtSlot() @pyqtSlot()
def on_switches_changed(self): def on_switches_changed(self):
@ -135,8 +83,7 @@ class ModuleDebugWidget(QWidget):
self.ui.pushSwitch4.isChecked() and 1 or 0, self.ui.pushSwitch4.isChecked() and 1 or 0,
self.ui.pushSwitch5.isChecked() and 1 or 0, self.ui.pushSwitch5.isChecked() and 1 or 0,
self.ui.pushSwitch6.isChecked() and 1 or 0) self.ui.pushSwitch6.isChecked() and 1 or 0)
# self.dev.switches(s[0] + s[1]<<1 + s[2]<<2 + s[3]<<3 + s[4]<<4 + s[5]<<5) self.dev.switches(s[0] + (s[1]<<1) + (s[2]<<2) + (s[3]<<3) + (s[4]<<4) + (s[5]<<5))
print 'switches:', s
@pyqtSlot() @pyqtSlot()
def on_pins_changed(self): def on_pins_changed(self):
@ -146,28 +93,23 @@ class ModuleDebugWidget(QWidget):
self.ui.pushOut1.setEnabled(not s[0]) self.ui.pushOut1.setEnabled(not s[0])
self.ui.pushOut2.setEnabled(not s[1]) self.ui.pushOut2.setEnabled(not s[1])
self.ui.pushOut3.setEnabled(not s[2]) self.ui.pushOut3.setEnabled(not s[2])
# self.dev.setpins(s[0] + s[1]<<1 + s[2]<<2) self.dev.setpins(s[0] + (s[1]<<1) + (s[2]<<2))
print 'pins:', s
@pyqtSlot(bool) @pyqtSlot(bool)
def on_pushOut1_clicked(self, checked): def on_pushOut1_clicked(self, checked):
# self.dev.setout(1, checked and 1 or 0) self.dev.setout(1, checked and 1 or 0)
print 'out1:', checked and 1 or 0
@pyqtSlot(bool) @pyqtSlot(bool)
def on_pushOut2_clicked(self, checked): def on_pushOut2_clicked(self, checked):
# self.dev.setout(2, checked and 1 or 0) self.dev.setout(2, checked and 1 or 0)
print 'out2:', checked and 1 or 0
@pyqtSlot(bool) @pyqtSlot(bool)
def on_pushOut3_clicked(self, checked): def on_pushOut3_clicked(self, checked):
# self.dev.setout(3, checked and 1 or 0) self.dev.setout(3, checked and 1 or 0)
print 'out3:', checked and 1 or 0
@pyqtSlot(int) @pyqtSlot(int)
def on_dialInputFreq_valueChanged(self, val): def on_dialInputFreq_valueChanged(self, val):
# TODO: change sampling rate to (val) freq print self.dev.read()
print 'inputfreq:', val
class ModuleDebug(): class ModuleDebug():