Permalink
Browse files

ButtonSet declared and used

  • Loading branch information...
1 parent 6966ac7 commit 18e91b748341bb032a0b29d80e24913e1435cea7 @fontanon committed Oct 10, 2009
Showing with 63 additions and 52 deletions.
  1. +6 −6 widinput.py
  2. +6 −39 widparser/conf.py
  3. +51 −0 widparser/control.py
  4. +0 −7 widparser/parser.py
View
@@ -21,17 +21,17 @@ def is_released(btn, pressed, last_pressed):
return not btn & pressed and btn & last_pressed
def process_btn(self, wiidevice, mesg_btn, btnmap):
- for btncode, button in btnmap.items():
- #FIXME: This doesn't care about (Wiimote.A + Wiimote.B) combos
- if self.is_pressed(btncode, mesg_btn, self.last['mesg_btn']) \
- and button.press_action:
+ for button in btnmap.sensible_buttons(mesg_btn):
+ #FIXME: It can't process Wiimote.A + Wiimote.B at all!
+ if button.press_action and self.is_pressed(button.btncode,
+ mesg_btn, self.last['mesg_btn']):
midi = button.get_press_action()
data = tuple([x for x in [midi.status, midi.data1, midi.data2] \
if x])
self.midiout.sendMessage(*data)
- elif self.is_released(btncode, mesg_btn, self.last['mesg_btn']) \
- and button.release_action:
+ elif button.release_action and self.is_released(buttonbtncode,
+ mesg_btn, self.last['mesg_btn']):
midi = button.get_release_action()
data = tuple([x for x in [midi.status, midi.data1, midi.data2] \
if x])
View
@@ -1,50 +1,17 @@
import cwiid
-from control import WiimoteButton, NunchukButton
+from control import ButtonSet, WiimoteButton, NunchukButton
class Conf():
-# class ButtonMap:
-# def __init__(self):
-# self.bmap = {}
-#
-# def add(btn):
-#
-#
-# def get_action(btn_code):
-# for btn in self.bmap:
-# if btn.code = btn_code:
-# return btn.action
-# return None
-
def __init__(self):
self.rptmode = 0
- self.wiimote_bmap = {}
- self.nunchuk_bmap = {}
- #self.axis = AxisMap()
+ self.wiimote_bmap = ButtonSet()
+ self.nunchuk_bmap = ButtonSet()
+ #TODO:self.axis = AxisSet()
def add_btn(self, btn):
if isinstance(btn, WiimoteButton):
self.rptmode |= cwiid.RPT_BTN
- self.wiimote_bmap[btn.btncode] = btn
+ self.wiimote_bmap.add(btn)
elif isinstance(btn, NunchukButton):
self.rptmode |= cwiid.RPT_NUNCHUK
- self.nunchuk_bmap[btn.btncode] = btn
-
- def add_axis(self, btn, action, func=lambda x, y: x, *args):
- pass
-
- """
- #FIXME: This is useless now
- def __contains__(self, btn):
- def in_bmap (btn, bmap):
- for item in bmap:
- if item == btn:
- return True
- return False
-
- if isinstance(btn, WiimoteButton):
- return in_bmap(btn, self.wiimote_bmap.values())
- elif isinstance(btn, NunchukButton):
- return in_bmap(btn, self.nunchuk_bmap.values())
- else:
- return False
- """
+ self.nunchuk_bmap.add(btn)
View
@@ -11,6 +11,37 @@
'Nunchuk.C': cwiid.NUNCHUK_BTN_C,
}
+class ButtonSet():
+ def __init__(self):
+ self.set = {}
+
+ def add(self, btn):
+ assert isinstance(btn, Button), 'Not Button instance'
+ self.set[btn.btncode] = btn
+
+ def remove(self, btn):
+ assert isinstance(btn, Button), 'Not Button instance'
+ del self.set[btn.btncode]
+
+ def sensible_buttons(self, mesg_btn):
+ #TODO: Maybe it's better a sorted insert on ButtonSet
+ sorted_btncodes = self.set.keys()
+ sorted_btncodes.sort()
+ sorted_btncodes.reverse()
+
+ for btn in [self.set[btncode] for btncode in sorted_btncodes]:
+ if btn.btncode & mesg_btn:
+ mesg_btn -= btn.btncode
+ yield btn
+
+ def __iter__(self):
+ return self.set.values().__iter__()
+
+ def __contains__(self, btn):
+ return btn in self.set.values()
+
+ def __repr__(self):
+ return 'ButtonSet(%s)' % repr([repr(x) for x in self.set.values()])
class Button():
codes = ()
@@ -98,3 +129,23 @@ def __eq__(self, nunbtn):
if not isinstance(nunbtn, NunchukButton):
return False
return self.code == nunbtn.code
+
+if __name__ == '__main__':
+ from midimesg import Note
+
+ a = WiimoteButton('Wiimote.A')
+ a.set_press_action(Note(90))
+
+ b = WiimoteButton('Wiimote.B')
+ b.set_press_action(Note(100))
+
+ ab = WiimoteButton('Wiimote.A') + WiimoteButton('Wiimote.B')
+ ab.set_press_action(Note(120))
+
+ s = ButtonSet()
+ s.add(a)
+ s.add(b)
+ s.add(ab)
+
+ for button in s.sensible_buttons(12):
+ print button
View
@@ -118,13 +118,6 @@ def p_buttoncomb(self, p):
| button """
if len(p) == 2:
- #If button in conf then replace
- if isinstance(p[1], control.WiimoteButton):
- if p[1] in self.conf.wiimote_bmap.values():
- p[1] = self.conf.wiimote_bmap[p[1].btncode]
- elif isinstance(p[1], control.NunchukButton):
- if p[1] in self.conf.nunchuk_bmap.values():
- p[1] = self.conf.nunchuk_bmap[p[1].btncode]
p[0] = p[1]
else:
p[0] = p[2] + p[4]

0 comments on commit 18e91b7

Please sign in to comment.