North (formerly Thalmic Labs), the creator of the Myo armband, was acquired by Google in June 2020. Myo sales ended in October 2018 and Myo software, hardware and SDKs are no longer available or supported. Learn more.

Always Clean Up! How to use onActiveChange

Always Clean Up! How to use onActiveChange

So the new Quick Launch Menu built into Myo Connect is pretty sweet. Easy access to Myo Keyboard, Myo Mouse, and a whole bunch of very useful applications. It also exposes a bug present in a few scripts. Specifically, related to how they clean up when their script is deactivated.

What I mean is if you have mouse control active, open the quick launch menu, and close it again, mouse control will still be active. That may be what you want, but if you are using the standard locking policy, the Myo will also be locked again, meaning you may be moving the cursor but unable to press any buttons. You could definitely have run into this bug in the script before, but it's likely to be come a lot more prevalent with such easy access to the Quick Launch Menu.

Fortunately, it's also very easy to fix. Just make proper use of onActiveChange:

-- Make sure mouse control is correctly disabled and re-enabled if the Quick Launch Menu is toggled
    wasUnlocked = false  
    wasMouseControl = false  
    function onActiveChange(isActive)  
        if (isActive == false) then
            wasUnlocked = myo.isUnlocked()
            wasMouseControl = myo.mouseControlEnabled()
        elseif wasUnlocked then

You may want to call myo.unlock("timed"), or skip the elseif and just disable mouse control. Or there could be other things you need to clean up, like sending "up" for any keyboard or mouse buttons you've only sent "down" for. Depends on the specifics of your script. Just make sure to think about it!

It's easy to do, but also easy to forget. Don't! Always clean up in onActiveChange.

You've successfully subscribed to The Lab!