TimeManager: use units further, add now/at functionality

This commit is contained in:
Sebastien Bourdeauducq 2014-05-30 19:01:27 +02:00
parent 732412e4c9
commit 13acb57179
3 changed files with 32 additions and 19 deletions

View File

@ -44,6 +44,8 @@ class _DummyTimeManager:
enter_sequential = _not_implemented
exit = _not_implemented
take_time = _not_implemented
get_time = _not_implemented
set_time = _not_implemented
_time_manager = _DummyTimeManager()
@ -70,4 +72,10 @@ class _Parallel:
parallel = _Parallel()
def delay(duration):
_time_manager.take_time(duration.amount)
_time_manager.take_time(duration)
def now():
return _time_manager.get_time()
def at(time):
_time_manager.set_time(time)

View File

@ -19,13 +19,12 @@ class Input:
def wait_edge(self):
duration = self.prng.randrange(self.wait_min, self.wait_max)*units.ms
time.manager.event(("wait_edge", self.name, duration))
time.manager.take_time(duration.amount)
time.manager.take_time(duration)
def count_gate(self, duration):
result = self.prng.randrange(self.count_min, self.count_max)
units.check_unit(duration, units.base_s_unit)
time.manager.event(("count_gate", self.name, duration, result))
time.manager.take_time(duration.amount)
time.manager.take_time(duration)
return result
class WaveOutput:
@ -33,10 +32,8 @@ class WaveOutput:
self.name = name
def pulse(self, frequency, duration):
units.check_unit(frequency, units.base_Hz_unit)
units.check_unit(duration, units.base_s_unit)
time.manager.event(("pulse", self.name, frequency, duration))
time.manager.take_time(duration.amount)
time.manager.take_time(duration)
class VoltageOutput:
def __init__(self, name):

View File

@ -1,11 +1,12 @@
from operator import itemgetter
from artiq.language import units, experiment
from artiq.language.units import *
from artiq.language import experiment
class SequentialTimeContext:
def __init__(self, current_time):
self.current_time = current_time
self.block_duration = 0
self.block_duration = 0*ps
def take_time(self, amount):
self.current_time += amount
@ -14,23 +15,23 @@ class SequentialTimeContext:
class ParallelTimeContext:
def __init__(self, current_time):
self.current_time = current_time
self.block_duration = 0
self.block_duration = 0*ps
def take_time(self, amount):
if amount > self.block_duration:
self.block_duration = amount
self.block_duration = amount
class Manager:
def __init__(self):
self.stack = [SequentialTimeContext(0)]
self.stack = [SequentialTimeContext(0*ps)]
self.timeline = []
def enter_sequential(self):
new_context = SequentialTimeContext(self.stack[-1].current_time)
new_context = SequentialTimeContext(self.get_time())
self.stack.append(new_context)
def enter_parallel(self):
new_context = ParallelTimeContext(self.stack[-1].current_time)
new_context = ParallelTimeContext(self.get_time())
self.stack.append(new_context)
def exit(self):
@ -40,16 +41,23 @@ class Manager:
def take_time(self, duration):
self.stack[-1].take_time(duration)
def get_time(self):
return self.stack[-1].current_time
def set_time(self, t):
dt = t - self.get_time()
if dt < 0*ps:
raise ValueError("Attempted to go back in time")
self.take_time(dt)
def event(self, description):
self.timeline.append((self.stack[-1].current_time, description))
self.timeline.append((self.get_time(), description))
def format_timeline(self):
r = ""
prev_time = 0
prev_time = 0*ps
for time, description in sorted(self.timeline, key=itemgetter(0)):
t = units.Quantity(time, units.base_s_unit)
dt = units.Quantity(time-prev_time, units.base_s_unit)
r += "@{:10} (+{:10}) ".format(str(t), str(dt))
r += "@{:10} (+{:10}) ".format(str(time), str(time-prev_time))
for item in description:
r += "{:16}".format(str(item))
r += "\n"