I just put my hands on SHA2017 badge. I wrote the following program and ran it in a simulator:
import ugfx
import badge
def key_pressed(key, is_pressed):
print('key_pressed(key=%r, is_pressed=%r)' % (key, is_pressed))
badge.init()
ugfx.init()
ugfx.input_init()
for key in [ugfx.JOY_LEFT, ugfx.JOY_UP, ugfx.JOY_RIGHT, ugfx.JOY_DOWN]:
ugfx.input_attach(key, (lambda is_pressed: key_pressed(key, is_pressed)))
# Uncommenting this makes all keystrokes get interpreted as JOY_UP:
# ugfx.input_attach(key, (lambda is_pressed: key_pressed(key, is_pressed)))
# But uncommenting this makes JOY_UP work as intended and doesn't affect other keys
# ugfx.input_attach(ugfx.JOY_UP, (lambda is_pressed: key_pressed(ugfx.JOY_UP, is_pressed)))
while True:
pass
If I run this, pressing any key will say that I pressed JOY_DOWN. Uncommenting the first line will make it all say I pressed JOY_UP and only uncommenting the last commented line will make all keys fire JOY_DOWN, apart from JOY_UP, which works as intended.
Is this a bug or intended behavior? It seems like MicroPython is compiling the lambda only once and ties a reference to "key", which seems odd to me.