class Analog(object): def __init__(self, link, aValue): report("Analog(aValue=%s)" % repr(aValue)) self.rValue = FloatRegister(link, aValue) def set(self, value): report("Analog.set(%s)" % repr(value)) self.rValue.write(float(value))
class Counters(object): def __init__(self, link, aSwitch, aBusy, aTime, aValues): report( "Counters(aSwitch=%s, aBusy=%s, aTime=%s, aValues=%s)" % (repr(aSwitch), repr(aBusy), repr(aTime), repr(aValues)) ) self.rSwitch = ToggleRegister(link, aSwitch) self.rBusy = ToggleRegister(link, aBusy) self.rTime = FloatRegister(link, aTime) self.rValues = [DwordRegister(link, aValue) for aValue in aValues] def stop(self): report("Counters.stop()") self.rSwitch.write(False) def start(self): report("Counters.start()") self.rSwitch.write(True) def isBusy(self): done = self.rBusy.read() report("Counters.isBusy() -> %s" % repr(done)) return done def setTime(self, time_ms): report("Counters.setTime(%s)" % repr(time_ms)) self.rTime.write(float(int(time_ms))) def getCounts(self): counts = [rValue.read() for rValue in self.rValues] report("Counters.getCounts() -> %s" % repr(counts)) return counts def measure(self, time_ms): report("Counters.measure(%s)" % repr(time_ms)) # Stop counters if running. self.stop() # Specify time to count. self.setTime(time_ms) # Sample real time. t1 = time() # Start timer and counters. self.start() # Wait until time has elapsed. while self.isBusy(): sleep(0.1) # Sample real time. t2 = time() # Calculate and report real time elapsed. taken_ms = int((t2 - t1) * 1000) report("Counters.measure(%s) took %d ms" % (repr(time_ms), taken_ms)) # Read out counter values. counts = self.getCounts() # Stop counters. self.stop() self.setTime(0) return counts