idasen_controller/main.py
async def move_to(client, target):
    """Move the desk to a specified height"""

    initial_height, speed = struct.unpack("<Hh", await client.read_gatt_char(UUID_HEIGHT))

    # Initialise by setting the movement direction
    direction = "UP" if target > initial_height else "DOWN"
    
    # Set up callback to run when the desk height changes. It will resend
    # movement commands until the desk has reached the target height.
    loop = asyncio.get_event_loop()
    move_done = loop.create_future()
    global count
    count = 0
    def _move_to(sender, data):
        global count
        height, speed = struct.unpack("<Hh", data)
        count = count + 1
        print("Height: {:4.0f}mm Target: {:4.0f}mm Speed: {:2.0f}mm/s".format(rawToMM(height), rawToMM(target), rawToSpeed(speed)))

       
        # Stop if we have reached the target OR
        # If you touch desk control while the script is running then movement
        # callbacks stop. The final call will have speed 0 so detect that 
        # and stop.
        if speed == 0 or has_reached_target(height, target):
            asyncio.create_task(stop(client))
            asyncio.create_task(unsubscribe(client, UUID_HEIGHT))
            try:
                move_done.set_result(True)
            except asyncio.exceptions.InvalidStateError:
                # This happens on windows, I dont know why
                pass 
        # Or resend the movement command if we have not yet reached the
        # target.
        # Each movement command seems to run the desk motors for about 1
        # second if uninterrupted and the height value is updated about 16
        # times.
        # Resending the command on the 6th update seems a good balance
        # between helping to avoid overshoots and preventing stutterinhg
        # (the motor seems to slow if no new move command has been sent)
        elif direction == "UP" and count == 6:
            asyncio.create_task(move_up(client))
            count = 0
        elif direction == "DOWN" and count == 6:
            asyncio.create_task(move_down(client))
            count = 0

    # Listen for changes to desk height and send first move command (if we are 
    # not already at the target height).
    if not has_reached_target(initial_height, target):
        await subscribe(client, UUID_HEIGHT, _move_to)
        if direction == "UP":
            asyncio.create_task(move_up(client))
        elif direction == "DOWN":
            asyncio.create_task(move_down(client))
        try:
            await asyncio.wait_for(move_done, timeout=config['movement_timeout'])
        except asyncio.TimeoutError as e:
            print('Timed out while waiting for desk')
            await unsubscribe(client, UUID_HEIGHT)
Move the desk to a specified height
Move the desk to a specified height
Move the desk to a specified height
async def move_to(client, target):
    initial_height, speed = struct.unpack("<Hh", await client.read_gatt_char(UUID_HEIGHT))
    direction = "UP" if target > initial_height else "DOWN"
    loop = asyncio.get_event_loop()
    move_done = loop.create_future()
    global count
    count = 0
    def _move_to(sender, data):
        global count
        height, speed = struct.unpack("<Hh", data)
        count = count + 1
        print("Height: {:4.0f}mm Target: {:4.0f}mm Speed: {:2.0f}mm/s".format(rawToMM(height), rawToMM(target), rawToSpeed(speed)))
        if speed == 0 or has_reached_target(height, target):
            asyncio.create_task(stop(client))
            asyncio.create_task(unsubscribe(client, UUID_HEIGHT))
            try:
                move_done.set_result(True)
            except asyncio.exceptions.InvalidStateError:
                pass 
        elif direction == "UP" and count == 6:
            asyncio.create_task(move_up(client))
            count = 0
        elif direction == "DOWN" and count == 6:
            asyncio.create_task(move_down(client))
            count = 0
    if not has_reached_target(initial_height, target):
        await subscribe(client, UUID_HEIGHT, _move_to)
        if direction == "UP":
            asyncio.create_task(move_up(client))
        elif direction == "DOWN":
            asyncio.create_task(move_down(client))
        try:
            await asyncio.wait_for(move_done, timeout=config['movement_timeout'])
        except asyncio.TimeoutError as e:
            print('Timed out while waiting for desk')
            await unsubscribe(client, UUID_HEIGHT)
async def move_to ( client , target ) : initial_height , speed = struct . unpack ( "<Hh" , await client . read_gatt_char ( UUID_HEIGHT ) ) direction = "UP" if target > initial_height else "DOWN" loop = asyncio . get_event_loop ( ) move_done = loop . create_future ( ) global count count = 0 def _move_to ( sender , data ) : global count height , speed = struct . unpack ( "<Hh" , data ) count = count + 1 print ( "Height: {:4.0f}mm Target: {:4.0f}mm Speed: {:2.0f}mm/s" . format ( rawToMM ( height ) , rawToMM ( target ) , rawToSpeed ( speed ) ) ) if speed == 0 or has_reached_target ( height , target ) : asyncio . create_task ( stop ( client ) ) asyncio . create_task ( unsubscribe ( client , UUID_HEIGHT ) ) try : move_done . set_result ( True ) except asyncio . exceptions . InvalidStateError : pass elif direction == "UP" and count == 6 : asyncio . create_task ( move_up ( client ) ) count = 0 elif direction == "DOWN" and count == 6 : asyncio . create_task ( move_down ( client ) ) count = 0 if not has_reached_target ( initial_height , target ) : await subscribe ( client , UUID_HEIGHT , _move_to ) if direction == "UP" : asyncio . create_task ( move_up ( client ) ) elif direction == "DOWN" : asyncio . create_task ( move_down ( client ) ) try : await asyncio . wait_for ( move_done , timeout = config [ 'movement_timeout' ] ) except asyncio . TimeoutError as e : print ( 'Timed out while waiting for desk' ) await unsubscribe ( client , UUID_HEIGHT )
Move the desk to a specified height
Move the desk to a specified height
"""Move the desk to a specified height""" # Initialise by setting the movement direction # Set up callback to run when the desk height changes. It will resend # movement commands until the desk has reached the target height. # Stop if we have reached the target OR # If you touch desk control while the script is running then movement # callbacks stop. The final call will have speed 0 so detect that  # and stop. # This happens on windows, I dont know why # Or resend the movement command if we have not yet reached the # target. # Each movement command seems to run the desk motors for about 1 # second if uninterrupted and the height value is updated about 16 # times. # Resending the command on the 6th update seems a good balance # between helping to avoid overshoots and preventing stutterinhg # (the motor seems to slow if no new move command has been sent) # Listen for changes to desk height and send first move command (if we are  # not already at the target height).
client
target