-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Using belay in a class #51
Comments
so its not perfect, but currently the best available solution is probably something like:
There is two issues with decorating methods in the way you posted:
(1) Can't really be solved since this is outside of Belay's control. If If you are trying to control multiple boards, Belay supports multiple device chaining. Note that multiple device chaining is currently not supported for generators. Also, honestly, this isn't as well tested as other features. |
As I'd like to dynamically use the method I can't decorate it manually which leaves out chaining multiple devices. I think (2) might be a good solution - found a similar suggestion in a discussion. Would then look something like (untested): from belay import Device
class BelayDevice(Device):
def __init__(self, port, **kwargs):
super().__init__(port, 115200) #'/dev/cu.usbmodem141201'
@Device.task
def led_loop(self, period):
from neopixel import NeoPixel
np = NeoPixel(Pin(17), 8)
x = 0
state = False
light = (0,0,0)
while x < 6:
# np.value(state)
if state is False:
light = (255, 255, 255)
else:
light = (0,0,0)
np.fill(light)
state = not state
sleep(period)
x += 1
bel = BelayDevice('/dev/tty.SLAB_USBtoUART')
bel.led_loop(1) With no modifications to the source code I get EDIT: Updated the import and decorator name. |
something like
might be doable; since it would always have to be a |
Ah sweet! I'll wait for you to have a jab at it then! 😃 And BTW thanks for mentioning me in the |
Just some additional use case that would be possible with a class. It would allow importing the class from a different script, thus keeping the methods that are inherent to the piece of hardware you're working with in a separate place. I think it would be a great way to have a focused script. For me it could look like: import time
import belay
from experiments import BelayDevice
# Initiate device
beehive = BelayDevice('/dev/tty.SLAB_USBtoUART')
# Experiment
beehive.led_toggle()
time.sleep(5)
beehive.led_toggle()
# Finish experiment
beehive.close() |
I agree that looks much nicer; i'll see what i can do. |
@roaldarbol See #52, I got it working. See if you like the interface or if you would change anything; please leave feedback in the PR. I still need to write more documentation for it, but see example EDIT: In this PR, the previous method for multiple devices (multiple decorating) has been removed. I think the class approach is much more elegant for the user, and significantly simplifies Belay's code. |
by the way, I made
|
Wohooooo! 🥳 I'm actually building a GUI for something similar, so the example is really useful! Really nice work Brian - I'll test in the afternoon. |
I'm now trying to create a class that takes a port as input, creates the
Belay.device
in__init__
and has belay-decorated methods. However, I can't do that currently as the decorator depends ondevice
. I also tried initiating the device outside the class and using it as an argument, but that doesn't help either. Any ideas on how to go about this?For background, I'd like to be able to connect to multiple controllers and run code separately. I'll use
multiprocessing
for that, but I've already got that side of things down - I just need a way that I can easily instantiate a class multiple times.The text was updated successfully, but these errors were encountered: