ev3dev / ev3dev-lang-python

Pure python bindings for ev3dev
MIT License
425 stars 144 forks source link

Buttons in micropython #696

Closed dgustavus closed 4 years ago

dgustavus commented 4 years ago

When I test a Buttons.py program from ev3python.com that works in python3, I get the following error using micropython:

Traceback (most recent call last): File "./buttons.py", line 38, in File "ev3dev2/button.py", line 133, in process AttributeError: 'Button' object has no attribute '_state'

It seems to have something to do with the use of self._state in an assignment before it has been initialized. I am not an experienced python programmer, if that is not obvious.

Thanks for any help, Duane

WasabiFan commented 4 years ago

The version information at the top of your issue may have been cut off; is there a way you can get the full text? In particular, I'm looking for a version string like 2.0.0~beta3, for example. If 2.0.0 is the full version string, that means you're already on the latest version, which is good.

Can you post the example script you're using to give me a sample to reproduce it with?

dgustavus commented 4 years ago

On Tue, Dec 17, 2019 at 11:33 PM Kaelin Laundry notifications@github.com wrote:

The version information at the top of your issue may have been cut off; is there a way you can get the full text? In particular, I'm looking for a version string like 2.0.0~beta3, for example. If 2.0.0 is the full version string, that means you're already on the latest version, which is good.

I followed the instructions for updating ev3dev2 when I first saw the problem, so I believe it is the latest version, but at any rate, here is the output of the dpkg-query command:

+++-==============-============-============-================================= ii micropython-ev 2.0.0 all Python language bindings for ev3d ii python3-ev3dev 1.2.0 all Python language bindings for ev3d ii python3-ev3dev 2.0.0 all Python language bindings for ev3d

Can you post the example script you're using to give me a sample to

reproduce it with?

Sure; it is code I downloaded from the ev3python.com site. I have included it below. I have worked around the problem by using the other functions that do not call btn.process, but it would be cleaner to be able to use it.

Thanks for your interest and help! Duane

WasabiFan commented 4 years ago

Hmmm, interesting. I'll take a look tomorrow and see if I can reproduce. Thanks for reporting!

WasabiFan commented 4 years ago

I can confirm the following fails. Looking into it.

#!/usr/bin/env python3
from ev3dev2.button import Button
from time import sleep

btn = Button()

# Do something when state of any button changes:

def left(state):
    if state:
        print('Left button pressed')
    else:
        print('Left button released')

def right(state):  # neater use of 'if' follows:
    print('Right button pressed' if state else 'Right button released')

def up(state):
    print('Up button pressed' if state else 'Up button released')

def down(state):
    print('Down button pressed' if state else 'Down button released')

def enter(state):
    print('Enter button pressed' if state else 'Enter button released')

btn.on_left = left
btn.on_right = right
btn.on_up = up
btn.on_down = down
btn.on_enter = enter

# This loop checks button states continuously (every 0.01s). 
# If the new state differs from the old state then the appropriate
# button event handlers are called.
while True:
    btn.process()
    sleep(0.01)
WasabiFan commented 4 years ago

FIxed in https://github.com/ev3dev/ev3dev-lang-python/commit/9486f2d9958ca97e6940d2975fecd2d5b30bdfb9. This hasn't yet been released.

WasabiFan commented 4 years ago

Thanks for the report!

dgustavus commented 4 years ago

On Sat, Dec 21, 2019 at 1:04 AM Kaelin Laundry notifications@github.com wrote:

Thanks for the report!

Thank you for the quick fix! I also learned a new variation for using "if"!

Duane