2
2
3
3
# -*- coding: latin-1 -*-
4
4
5
- # Copyright 2020 Shadow Robot Company Ltd.
5
+ # Copyright 2020-2022 Shadow Robot Company Ltd.
6
6
#
7
7
# This program is free software: you can redistribute it and/or modify it
8
8
# under the terms of the GNU General Public License as published by the Free
19
19
from __future__ import absolute_import
20
20
import rospy
21
21
import sys
22
- import select
23
- from pynput import keyboard
24
22
from sr_pedal .msg import Status
25
23
from threading import Lock , Thread
26
24
25
+ # If we're in a non-X-server (e.g. CI) environment, this import will fail
26
+ try :
27
+ from pynput import keyboard
28
+ except ImportError as err :
29
+ print ("SrPedalMock could not import pynput module; likely because there is no available X server. "
30
+ "Pedal mock keyboard control disabled." )
31
+
27
32
28
33
class SrPedalMock ():
29
34
def __init__ (self , connected = True , left_pressed = False , middle_pressed = False , right_pressed = False , rate = 20 ,
@@ -41,6 +46,8 @@ def __init__(self, connected=True, left_pressed=False, middle_pressed=False, rig
41
46
self ._ctrl_pressed = False
42
47
self ._alt_pressed = False
43
48
self ._kb_listener = None
49
+ # If pynput failed to import, disable keyboard control
50
+ keyboard_control = keyboard_control and "pynput" in sys .modules
44
51
if keyboard_control :
45
52
self ._kb_listener = keyboard .Listener (on_press = self ._on_keyboard_press ,
46
53
on_release = self ._on_keyboard_release )
@@ -104,10 +111,22 @@ def set_status(self, connected=None, left_pressed=None, middle_pressed=None, rig
104
111
rospy .loginfo ("Mock right pedal now released." )
105
112
self ._lock .release ()
106
113
114
+ def toggle_status (self , status ):
115
+ if status == "connected" :
116
+ self ._status .connected = not self ._status .connected
117
+ elif status == "left" :
118
+ self ._status .left_pressed = not self ._status .left_pressed
119
+ elif status == "middle" :
120
+ self ._status .middle_pressed = not self ._status .middle_pressed
121
+ elif status == "right" :
122
+ self ._status .right_pressed = not self ._status .right_pressed
123
+ else :
124
+ rospy .logwarn (f'Cannot toggle status of unknown pedal state "{ status } ".' )
125
+
107
126
def _on_keyboard_press (self , key ):
108
127
if key == keyboard .Key .ctrl :
109
128
self ._ctrl_pressed = True
110
- if key == keyboard .Key .alt :
129
+ elif key == keyboard .Key .alt :
111
130
self ._alt_pressed = True
112
131
113
132
if self ._ctrl_pressed and self ._alt_pressed :
0 commit comments