Browse Source

Refactored a bit to include more all-seeing shit in Overseer.

pyserial_fix
Scott Petersen 11 years ago
parent
commit
f6d1d4367d
2 changed files with 49 additions and 33 deletions
  1. +39
    -25
      pyad2usb/ad2usb.py
  2. +10
    -8
      test.py

+ 39
- 25
pyad2usb/ad2usb.py View File

@@ -16,6 +16,31 @@ class Overseer(object):
on_attached = event.Event('Called when an AD2USB device has been detected.')
on_detached = event.Event('Called when an AD2USB device has been removed.')

__devices = []

@classmethod
def find_all(cls):
cls.__devices = Device.find_all()

return cls.__devices

@classmethod
def devices(cls):
return cls.__devices

@classmethod
def create(cls, device=None):
if len(cls.__devices) == 0:
raise NoDeviceError('No AD2USB devices present.')

if device is None:
device = cls.__devices[0]

vendor, product, sernum, ifcount, description = device
device = Device(serial=sernum, description=description)

return AD2USB(device)

def __init__(self, attached_event=None, detached_event=None):
self._detect_thread = Overseer.DetectThread(self)

@@ -25,17 +50,26 @@ class Overseer(object):
if detached_event:
self.on_detached += detached_event

Overseer.find_all()

self.start()

def __del__(self):
pass

def close(self):
self.stop()

def start(self):
self._detect_thread.start()

def stop(self):
self._detect_thread.stop()

def get_device(self, device=None):
return Overseer.create(device)


class DetectThread(threading.Thread):
def __init__(self, overseer):
threading.Thread.__init__(self)
@@ -53,10 +87,9 @@ class Overseer(object):

while self._running:
try:
AD2USB.find_all()

current_devices = set(AD2USB._AD2USB__devices)
Overseer.find_all()

current_devices = set(Overseer.devices())
new_devices = [d for d in current_devices if d not in last_devices]
removed_devices = [d for d in last_devices if d not in current_devices]
last_devices = current_devices
@@ -78,33 +111,14 @@ class AD2USB(object):
on_read = event.Event('Called when a line has been read from the device')
on_write = event.Event('Called when data has been written to the device')

__devices = []

@classmethod
def find_all(cls):
cls.__devices = Device.find_all()

return cls.__devices

def __init__(self):
self._device = None

AD2USB.find_all()
def __init__(self, device):
self._device = device

def __del__(self):
pass

def open(self, device=None):
if len(self.__devices) == 0:
raise NoDeviceError('No AD2USB devices present.')

if device is None:
device = self.__devices[0]

self._device = Device(serial=device[2], description=device[4])

def open(self):
self._wire_events()

self._device.open()

def close(self):


+ 10
- 8
test.py View File

@@ -37,19 +37,21 @@ try:
overseer = pyad2usb.ad2usb.Overseer(attached_event=handle_attached, detached_event=handle_detached)
#overseer.start()

"""wut = pyad2usb.ad2usb.AD2USB()
#dev = pyad2usb.ad2usb.AD2USB()
#dev = overseer.get_device()

wut.on_open += handle_open
wut.on_close += handle_close
wut.on_read += handle_read
wut.on_write += handle_write
wut.open()"""
dev = pyad2usb.ad2usb.Overseer.create()
dev.on_open += handle_open
dev.on_close += handle_close
dev.on_read += handle_read
dev.on_write += handle_write
dev.open()

while running:
time.sleep(0.1)

overseer.stop()
dev.close()
overseer.close()

#wut.close()
except Exception, err:


Loading…
Cancel
Save