From b9219c1e11de02866e4393065062d2b9f8c360ce Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 8 Dec 2015 21:32:22 +0100 Subject: added callback mode and a simple example script --- scripts/sc-callbacks.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 scripts/sc-callbacks.py diff --git a/scripts/sc-callbacks.py b/scripts/sc-callbacks.py new file mode 100644 index 0000000..bb279e5 --- /dev/null +++ b/scripts/sc-callbacks.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python + +# The MIT License (MIT) +# +# Copyright (c) 2015 Paul Wachendorf +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +"""Steam Controller Callback Mode example""" +import sys + +from steamcontroller import SteamController, SCButtons +from steamcontroller.events import EventMapper, Pos +from steamcontroller.uinput import Keys + + +def button_pressed_callback(evm, btn, pressed): + print "Button {} was {}.".format(btn, 'pressed' if pressed else 'released') + + if btn == SCButtons.STEAM and not pressed: + print "pressing the STEAM button terminates the programm" + sys.exit() + +def touchpad_click_callback(evm, pos, pressed): + print "Tochpad {} was {}".format(pos, 'pressed' if pressed else 'released') + +def evminit(): + evm = EventMapper() + evm.setButtonCallback(SCButtons.STEAM, button_pressed_callback) + evm.setButtonCallback(SCButtons.A, button_pressed_callback) + evm.setButtonCallback(SCButtons.B, button_pressed_callback) + evm.setButtonCallback(SCButtons.X, button_pressed_callback) + evm.setButtonCallback(SCButtons.Y, button_pressed_callback) + evm.setButtonCallback(SCButtons.LB, button_pressed_callback) + evm.setButtonCallback(SCButtons.RB, button_pressed_callback) + evm.setButtonCallback(SCButtons.LT, button_pressed_callback) + evm.setButtonCallback(SCButtons.RT, button_pressed_callback) + evm.setButtonCallback(SCButtons.LGRIP, button_pressed_callback) + evm.setButtonCallback(SCButtons.RGRIP, button_pressed_callback) + evm.setButtonCallback(SCButtons.START, button_pressed_callback) + evm.setButtonCallback(SCButtons.BACK, button_pressed_callback) + evm.setPadButtonCallback(SCButtons.LPAD, touchpad_click_callback) + evm.setPadButtonCallback(SCButtons.RPAD, touchpad_click_callback) + return evm + + +if __name__ == '__main__': + evm = evminit() + sc = SteamController(callback=evm.process) + sc.run() + + -- cgit v1.2.3 From d2afb558c478d1f4d7ba6176dd1d997419037a5f Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 8 Dec 2015 22:04:53 +0100 Subject: added callback mode and a simple example script --- local | 2 +- scripts/sc-callbacks.py | 4 ++-- src/events.py | 44 +++++++++++++++++++++++++++++++++++++++----- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/local b/local index 074ccf2..b9219c1 160000 --- a/local +++ b/local @@ -1 +1 @@ -Subproject commit 074ccf2642b69173ac2c37767e15c65bff74eaf9 +Subproject commit b9219c1e11de02866e4393065062d2b9f8c360ce diff --git a/scripts/sc-callbacks.py b/scripts/sc-callbacks.py index bb279e5..4d234e8 100644 --- a/scripts/sc-callbacks.py +++ b/scripts/sc-callbacks.py @@ -55,8 +55,8 @@ def evminit(): evm.setButtonCallback(SCButtons.RGRIP, button_pressed_callback) evm.setButtonCallback(SCButtons.START, button_pressed_callback) evm.setButtonCallback(SCButtons.BACK, button_pressed_callback) - evm.setPadButtonCallback(SCButtons.LPAD, touchpad_click_callback) - evm.setPadButtonCallback(SCButtons.RPAD, touchpad_click_callback) + evm.setPadButtonCallback(Pos.LEFT, touchpad_click_callback) + evm.setPadButtonCallback(Pos.RIGHT, touchpad_click_callback) return evm diff --git a/src/events.py b/src/events.py index 7b90f95..36f059f 100644 --- a/src/events.py +++ b/src/events.py @@ -50,6 +50,7 @@ class Modes(IntEnum): GAMEPAD = 0 KEYBOARD = 1 MOUSE = 2 + CALLBACK = 3 class PadModes(IntEnum): """Different possible pads modes""" @@ -189,13 +190,16 @@ class EventMapper(object): if mode is None: continue - if btn & btn_add: - _keypressed(mode, ev) + if mode is Modes.CALLBACK: + ev(self, btn, True) + else: + _keypressed(mode, ev) elif btn & btn_rem: - _keyreleased(mode, ev) - - + if mode is Modes.CALLBACK: + ev(self, btn, False) + else: + _keyreleased(mode, ev) # Manage pads for pos in [Pos.LEFT, Pos.RIGHT]: @@ -442,6 +446,19 @@ class EventMapper(object): self._btn_map[btn] = (mode, key_event) return + def setButtonCallback(self, btn, callback): + """ + set callback function to be executed when button is clicked + callback is called with parameters self(EventMapper), btn + and pushed (boollean True -> Button pressed, False -> Button released) + + @param btn Button + @param callback Callback function + """ + + self._btn_map[btn] = (Modes.CALLBACK, callback) + + def setPadButtons(self, pos, key_events, deadzone=0.6, clicked=False): """ Set pad as buttons @@ -471,6 +488,23 @@ class EventMapper(object): self._btn_map[SCButtons.LPAD] = (None, 0) else: self._btn_map[SCButtons.RPAD] = (None, 0) + + def setPadButtonCallback(self, pos, callback, clicked=True): + """ + set callback function to be executed when Pad clicked or touched + + @param Pos pos designate left or right pad + @param callback Callback function + @param bool clicked callback on touch or on click event + """ + if not clicked: + # FIXME: add touch support + raise NotImplementedError('Touch callbacks are not supported yet') + else: + if pos == Pos.LEFT: + self._btn_map[SCButtons.LPAD] = (Modes.CALLBACK, callback) + else: + self._btn_map[SCButtons.RPAD] = (Modes.CALLBACK, callback) def setPadAxesAsButtons(self, pos, abs_events, deadzone=0.6, clicked=False, revert=True): """ -- cgit v1.2.3 From cfc44afa07eccc52d769ec2d51ff10dbed8211c7 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 8 Dec 2015 22:56:57 +0100 Subject: added callback mode for pad touch events --- local | 2 +- scripts/sc-callbacks.py | 11 +++++++---- src/events.py | 18 ++++++++++++++---- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/local b/local index b9219c1..d2afb55 160000 --- a/local +++ b/local @@ -1 +1 @@ -Subproject commit b9219c1e11de02866e4393065062d2b9f8c360ce +Subproject commit d2afb558c478d1f4d7ba6176dd1d997419037a5f diff --git a/scripts/sc-callbacks.py b/scripts/sc-callbacks.py index 4d234e8..7482faa 100644 --- a/scripts/sc-callbacks.py +++ b/scripts/sc-callbacks.py @@ -37,8 +37,11 @@ def button_pressed_callback(evm, btn, pressed): print "pressing the STEAM button terminates the programm" sys.exit() -def touchpad_click_callback(evm, pos, pressed): - print "Tochpad {} was {}".format(pos, 'pressed' if pressed else 'released') +def touchpad_click_callback(evm, pad, pressed): + print "Tochpad {} was {}".format(pad, 'pressed' if pressed else 'released') + +def touchpad_touch_callback(evm, pad, x, y): + print "Tochpad {} was touched @{},{}".format(pad, x, y) def evminit(): evm = EventMapper() @@ -55,8 +58,8 @@ def evminit(): evm.setButtonCallback(SCButtons.RGRIP, button_pressed_callback) evm.setButtonCallback(SCButtons.START, button_pressed_callback) evm.setButtonCallback(SCButtons.BACK, button_pressed_callback) - evm.setPadButtonCallback(Pos.LEFT, touchpad_click_callback) - evm.setPadButtonCallback(Pos.RIGHT, touchpad_click_callback) + evm.setPadButtonCallback(Pos.LEFT, touchpad_touch_callback) + evm.setPadButtonCallback(Pos.RIGHT, touchpad_click_callback, clicked=True) return evm diff --git a/src/events.py b/src/events.py index 36f059f..60bad6b 100644 --- a/src/events.py +++ b/src/events.py @@ -292,8 +292,15 @@ class EventMapper(object): haptic = False if sci.buttons & on_test == on_test: - dzone = self._pad_dzones[pos] + # get callback events + callbacks = [] + for evt in self._pad_evts[pos]: + if evt[0] == Modes.CALLBACK: + callbacks.append(evt) + for callback_evt in callbacks: + callback_evt[1](self, pos, xm, ym) + dzone = self._pad_dzones[pos] if len(self._pad_evts[pos]) == 4: # key or buttons tmode, tev = self._pad_evts[pos][0] @@ -489,18 +496,21 @@ class EventMapper(object): else: self._btn_map[SCButtons.RPAD] = (None, 0) - def setPadButtonCallback(self, pos, callback, clicked=True): + def setPadButtonCallback(self, pos, callback, clicked=False): """ set callback function to be executed when Pad clicked or touched + if clicked is False callback will be called with pad, xpos and ypos + else with pad and boolean is_pressed @param Pos pos designate left or right pad @param callback Callback function @param bool clicked callback on touch or on click event """ if not clicked: - # FIXME: add touch support - raise NotImplementedError('Touch callbacks are not supported yet') + self._pad_modes[pos] = PadModes.BUTTONTOUCH + self._pad_evts[pos].append((Modes.CALLBACK, callback)) else: + self._pad_modes[pos] = PadModes.BUTTONCLICK if pos == Pos.LEFT: self._btn_map[SCButtons.LPAD] = (Modes.CALLBACK, callback) else: -- cgit v1.2.3 From 4a757681ee9e04badbd273eb4789cc81648c931f Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 8 Dec 2015 22:59:00 +0100 Subject: removed local --- local | 1 - 1 file changed, 1 deletion(-) delete mode 160000 local diff --git a/local b/local deleted file mode 160000 index d2afb55..0000000 --- a/local +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d2afb558c478d1f4d7ba6176dd1d997419037a5f -- cgit v1.2.3 From e40b79c0c0fc53a595bcc68cb83cae6a18255204 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 9 Dec 2015 20:45:20 +0100 Subject: removed local --- local | 1 - 1 file changed, 1 deletion(-) delete mode 160000 local diff --git a/local b/local deleted file mode 160000 index 074ccf2..0000000 --- a/local +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 074ccf2642b69173ac2c37767e15c65bff74eaf9 -- cgit v1.2.3 From 2342e9dc5c55c3c7b2e2909f39c7d31bf4eaf12c Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 9 Dec 2015 21:10:11 +0100 Subject: added setStickPressedCallback and actalized example script --- scripts/sc-callbacks.py | 4 ++++ src/events.py | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/scripts/sc-callbacks.py b/scripts/sc-callbacks.py index 7482faa..042df21 100644 --- a/scripts/sc-callbacks.py +++ b/scripts/sc-callbacks.py @@ -43,6 +43,9 @@ def touchpad_click_callback(evm, pad, pressed): def touchpad_touch_callback(evm, pad, x, y): print "Tochpad {} was touched @{},{}".format(pad, x, y) +def stick_pressed_callback(evm): + print "Stick pressed" + def evminit(): evm = EventMapper() evm.setButtonCallback(SCButtons.STEAM, button_pressed_callback) @@ -60,6 +63,7 @@ def evminit(): evm.setButtonCallback(SCButtons.BACK, button_pressed_callback) evm.setPadButtonCallback(Pos.LEFT, touchpad_touch_callback) evm.setPadButtonCallback(Pos.RIGHT, touchpad_click_callback, clicked=True) + evm.setStickPressedCallback(stick_pressed_callback) return evm diff --git a/src/events.py b/src/events.py index 60bad6b..e3552f7 100644 --- a/src/events.py +++ b/src/events.py @@ -111,7 +111,8 @@ class EventMapper(object): self._stick_lxs = None self._stick_bys = None self._stick_rxs = None - + self._stick_pressed_callback = None + self._trig_s = [None, None] self._moved = [0, 0] @@ -435,6 +436,9 @@ class EventMapper(object): elif self._stick_rxs is not None and x <= self._stick_rxs: self._stick_rxs = None _keyreleased(rmode, rev) + if sci.buttons & SCButtons.LPAD == SCButtons.LPAD: + if self._stick_pressed_callback is not None: + self._stick_pressed_callback(self) if len(_pressed): @@ -459,8 +463,8 @@ class EventMapper(object): callback is called with parameters self(EventMapper), btn and pushed (boollean True -> Button pressed, False -> Button released) - @param btn Button - @param callback Callback function + @param btn Button + @param function callback Callback function """ self._btn_map[btn] = (Modes.CALLBACK, callback) @@ -607,3 +611,12 @@ class EventMapper(object): if self._uip[mode].keyManaged(ev): self._stick_evts.append((mode, ev)) break + + def setStickPressedCallback(self, callback): + """ + Set callback on StickPressed event. + the function will be called with EventMapper as first (and only) argument + + @param function Callback function function that is called on buton press. + """ + self._stick_pressed_callback = callback \ No newline at end of file -- cgit v1.2.3 From f5a25af5e9f635cc7db6b906090eb7c52f7a6790 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 9 Dec 2015 21:31:14 +0100 Subject: added callback fir stick movement --- scripts/sc-callbacks.py | 4 ++++ src/events.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/scripts/sc-callbacks.py b/scripts/sc-callbacks.py index 042df21..41d4078 100644 --- a/scripts/sc-callbacks.py +++ b/scripts/sc-callbacks.py @@ -46,6 +46,9 @@ def touchpad_touch_callback(evm, pad, x, y): def stick_pressed_callback(evm): print "Stick pressed" +def stick_axes_callback(evm, x, y): + print "Stick Position is {}, {}".format(x, y) + def evminit(): evm = EventMapper() evm.setButtonCallback(SCButtons.STEAM, button_pressed_callback) @@ -63,6 +66,7 @@ def evminit(): evm.setButtonCallback(SCButtons.BACK, button_pressed_callback) evm.setPadButtonCallback(Pos.LEFT, touchpad_touch_callback) evm.setPadButtonCallback(Pos.RIGHT, touchpad_click_callback, clicked=True) + evm.setStickAxesCallback(stick_axes_callback) evm.setStickPressedCallback(stick_pressed_callback) return evm diff --git a/src/events.py b/src/events.py index e3552f7..14b06a5 100644 --- a/src/events.py +++ b/src/events.py @@ -111,6 +111,7 @@ class EventMapper(object): self._stick_lxs = None self._stick_bys = None self._stick_rxs = None + self._stick_axes_callback = None self._stick_pressed_callback = None self._trig_s = [None, None] @@ -389,6 +390,9 @@ class EventMapper(object): x, y = sci.lpad_x, sci.lpad_y x_p, y_p = sci_p.lpad_x, sci_p.lpad_y + if self._stick_axes_callback is not None and (x != x_p or y != y_p): + self._stick_axes_callback(self, x, y) + if self._stick_mode == StickModes.AXIS: revert = self._stick_rev (xmode, xev), (ymode, yev) = self._stick_evts # pylint: disable=E0632 @@ -398,6 +402,7 @@ class EventMapper(object): if y != y_p: syn.add(ymode) self._uip[ymode].axisEvent(yev, y if not revert else -y) + elif self._stick_mode == StickModes.BUTTON: tmode, tev = self._stick_evts[0] @@ -594,6 +599,16 @@ class EventMapper(object): (Modes.GAMEPAD, abs_y_event)] self._stick_rev = revert + def setStickAxesCallback(self, callback): + """ + Set Callback on StickAxes Movement + the function will be called with EventMapper, pos_x, pos_y + + @param function callback the callback function + """ + self._stick_axes_callback = callback + + def setStickButtons(self, key_events): """ Set stick as buttons -- cgit v1.2.3