forked from M-Labs/nac3
1
0
Fork 0

nac3artiq: support kernel entry short form from original ARTIQ

This commit is contained in:
Sebastien Bourdeauducq 2021-11-06 18:41:59 +08:00
parent 016cbf2b90
commit 7385b91113
2 changed files with 16 additions and 11 deletions

View File

@ -12,7 +12,7 @@ class Demo:
self.led1 = TTLOut(self.core, 19) self.led1 = TTLOut(self.core, 19)
@kernel @kernel
def run_k(self): def run(self):
self.core.reset() self.core.reset()
while True: while True:
with parallel: with parallel:
@ -20,9 +20,6 @@ class Demo:
self.led1.pulse(100.*ms) self.led1.pulse(100.*ms)
self.core.delay(100.*ms) self.core.delay(100.*ms)
def run(self):
self.core.run(self.run_k)
if __name__ == "__main__": if __name__ == "__main__":
Demo().run() Demo().run()

View File

@ -1,6 +1,7 @@
from inspect import isclass, getmodule from inspect import isclass, getfullargspec, getmodule
from functools import wraps from functools import wraps
import sys import sys
from types import SimpleNamespace
from numpy import int32, int64 from numpy import int32, int64
import nac3artiq import nac3artiq
@ -37,13 +38,20 @@ def extern(function):
return function return function
def kernel(class_or_function): def kernel(function_or_method):
"""Decorates a function or method to be executed on the core device.""" """Decorates a function or method to be executed on the core device."""
register_module_of(class_or_function) register_module_of(function_or_method)
@wraps(class_or_function) argspec = getfullargspec(function_or_method)
def device_only(*args, **kwargs): if argspec.args and argspec.args[0] == "self":
raise RuntimeError("Kernels must not be called directly, use core.run(kernel_function) instead") @wraps(function_or_method)
return device_only def run_on_core(self, *args, **kwargs):
fake_method = SimpleNamespace(__self__=self, __name__=function_or_method.__name__)
self.core.run(fake_method, *args, **kwargs)
else:
@wraps(function_or_method)
def run_on_core(*args, **kwargs):
raise RuntimeError("Kernel functions need explicit core.run()")
return run_on_core
def portable(function): def portable(function):