virantha / bricknil

Control LEGO Bluetooth Sensors and Motors with Python
https://virantha.github.io/bricknil
Apache License 2.0
143 stars 39 forks source link

I want to have multiple functions to control the Lego hub motors not Just the run() function #14

Open wobeix opened 4 years ago

wobeix commented 4 years ago

I want to use this library in a special way, i need your help (i use the control+ hub) my main file is (main.py):

import curio
import train

if __name__ == '__main__':
    curio.run(train.main()) # this works
    curio.run(train.move()) # this doesn't 

The second file is train.py

from curio import sleep
from bricknil import attach, start
from bricknil.hub import CPlusHub
from bricknil.sensor.motor import CPlusLargeMotor

@attach(CPlusLargeMotor, name='motor_A')
@attach(CPlusLargeMotor, name='motor_B')
class Train(CPlusHub):

    async def run(self):
        await self.motor_A.set_speed(+100)
        await sleep(2)

    async def move_forward(self):
        await self.motor_A.set_speed(-100)
        await self.motor_B.set_speed(-100)
        await sleep(2)

async def system():
    train = Train('My train')

async def main():  
    start(system)

async def move():
    train = Train('My train')
    await train.move_forward()