Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shutter switch server and client use signal #225

Merged
merged 3 commits into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/clients/switchclient/switchclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


class switchclient(QtGui.QWidget):
SIGNALID = 219749

def __init__(self, reactor, cxn=None):
"""initializes the GUI creates the reactor
Expand All @@ -21,6 +22,7 @@ def __init__(self, reactor, cxn=None):
self.reactor = reactor
self.cxn = cxn
self.d = {}
self.chan_from_port = {}
self.connect()

@inlineCallbacks
Expand All @@ -35,6 +37,10 @@ def connect(self):
self.server = yield self.cxn.get_server('arduinottl')
self.reg = yield self.cxn.get_server('registry')

yield self.server.signal__on_switch_changed(self.SIGNALID)
yield self.server.addListener(listener=self.signal_switch_changed,
source=None, ID=self.SIGNALID)

try:
yield self.reg.cd(['', 'settings'])
self.settings = yield self.reg.dir()
Expand Down Expand Up @@ -71,6 +77,7 @@ def initializeGUI(self):
port=port, chan=chan, inverted=inverted:
self.changeState(state, port, chan, inverted))
self.d[port] = widget
self.chan_from_port[port] = chan
subLayout.addWidget(self.d[port], position[0], position[1])

self.setLayout(layout)
Expand All @@ -83,6 +90,19 @@ def changeState(self, state, port, chan, inverted):
state = not state
yield self.server.ttl_output(port, state)

@inlineCallbacks
def signal_switch_changed(self, c, signal):
port = signal[0]
state = signal[1]
chan = self.chan_from_port[port]
if port in self.d.keys():
if chan + 'shutter' in self.settings:
yield self.reg.set(chan + 'shutter', state)
inverted = self.chaninfo[chan][2]
if inverted:
state = not state
self.d[port].TTLswitch.setChecked(state)

def closeEvent(self, x):
self.reactor.stop()

Expand Down
19 changes: 18 additions & 1 deletion lib/servers/shutterandswitch/Arduinoshutterandswitchserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from labrad.types import Value
from labrad.devices import DeviceServer, DeviceWrapper
from labrad.server import setting
from labrad.server import setting, Signal
from twisted.internet.defer import inlineCallbacks, returnValue

TIMEOUT = Value(1.0, 's')
Expand Down Expand Up @@ -68,12 +68,15 @@ class ArduinoTTL(DeviceServer):
deviceName = 'ArduinoTTL'
deviceWrapper = TTLDevice

on_switch_changed = Signal(124973, 'signal: on_switch_changed', '(ib)')

@inlineCallbacks
def initServer(self):
print 'loading config info...',
self.reg = self.client.registry()
yield self.loadConfigInfo()
yield DeviceServer.initServer(self)
self.listeners = set()

@inlineCallbacks
def loadConfigInfo(self):
Expand Down Expand Up @@ -102,11 +105,24 @@ def findDevices(self):
devs += [(devName, (server, port))]
returnValue(devs)

def initContext(self, c):
self.listeners.add(c.ID)

def expireContext(self, c):
self.listeners.remove(c.ID)

def getOtherListeners(self, c):
notified = self.listeners.copy()
notified.remove(c.ID)
return notified

@setting(100, 'TTL Output', chan='i', state='b')
def ttlOutput(self, c, chan, state):
dev = self.selectDevice(c)
output = (chan << 2) | (state + 2)
yield dev.write(chr(output))
notified = self.getOtherListeners(c)
self.on_switch_changed((chan, state), notified)

@setting(200, 'TTL Read', chan='i', returns='b')
def ttlInput(self, c, chan):
Expand All @@ -129,6 +145,7 @@ def ttlInput(self, c, chan):
print status, 'Error Reading'
returnValue(False)


if __name__ == "__main__":
from labrad import util
util.runServer(ArduinoTTL())