monome / libmonome

makes writing applications for Monomes easy.
ISC License
302 stars 158 forks source link

problems with event handling via python #25

Closed alexklaeser closed 11 years ago

alexklaeser commented 11 years ago

Hi,

I have problems with using the python bindings. When registering for events, the code below will tell me as soon as a button is pressed:

Exception TypeError: '__cinit__() takes exactly 1 positional argument (4 given)' in 'monome.handler_thunk' ignored

I tried to debug the code myself, but could not find any obvious error. I also tried to compile with the most recent cython version, however, this would not resolve the issue.

BTW, I added the following lines to the monome.pyx:

BUTTON_UP, BUTTON_DOWN, ENCODER_DELTA, ENCODER_KEY_UP, ENCODER_KEY_DOWN, TILT = range(6)
CABLE_LEFT, CABLE_BOTTOM, CABLE_RIGHT, CABLE_TOP = range(4)

Here is a sligthly modified version of simple.py that I tried to use.

import monome

device = "/dev/ttyUSB0"

def button_handler(event):
    x, y = event.x, event.y
    buttons[x][y] ^= 1

    if buttons[x][event.y]:
        m.led_on(x, y)
    else:
        m.led_off(x, y)

m = monome.Monome(device)
buttons = [[0] * m.columns] * m.rows
m.register_handler(monome.BUTTON_DOWN, button_handler)

print("simple.py running, press some buttons!")

try:
    m.event_loop()
except KeyboardInterrupt:
    m.clear()

Thanks in advance Alex

alexklaeser commented 11 years ago

It seems to be an issue with MonomeGridEvent inheriting from MonomeEvent (which only expects 1 constructor parameter). As soon as I remove the inheritance of MonomeEvent, it works.

alexklaeser commented 11 years ago

The following patch works for me:

diff --git a/bindings/python/monome.pyx b/bindings/python/monome.pyx
index af750ec..bdee2e4 100644
--- a/bindings/python/monome.pyx
+++ b/bindings/python/monome.pyx
@@ -115,6 +115,8 @@ all = [

        "Monome"]

+BUTTON_UP, BUTTON_DOWN, ENCODER_DELTA, ENCODER_KEY_UP, ENCODER_KEY_DOWN, TILT = range(6)
+CABLE_LEFT, CABLE_BOTTOM, CABLE_RIGHT, CABLE_TOP = range(4)

 MODE_NORMAL = 0
 MODE_TEST = 1
@@ -155,15 +157,12 @@ def _bitmap_data(data):
 cdef class MonomeEvent(object):
        cdef object monome

-       def __cinit__(self, object monome):
-               self.monome = monome
-
 cdef class MonomeGridEvent(MonomeEvent):
        cdef uint x, y
        cdef bool pressed

        def __cinit__(self, pressed, uint x, uint y, object monome):
-               MonomeEvent.__cinit__(monome)
+               self.monome = monome
                self.pressed = bool(pressed)
                self.x = x
                self.y = y
wrl commented 11 years ago

yeah i'm cool with this. it would be nice to handle all common stuff in MonomeEvent but Cython sometimes doesn't want to play nice.

could you submit this as a pull request?

alexklaeser commented 11 years ago

Cool. I submitted a pull request... best regards Alex