key_joystick.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 '''
3 Virtual Joystick from Keyboard
4 Bharat Tak
5 September 2016
6 '''
7 
8 import uinput, time
9 import pygame, sys, os
10 from pygame.locals import *
11 
12 pygame.init()
13 BLACK = (0,0,0)
14 WIDTH = 335
15 HEIGHT = 406
16 windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
17 windowSurface.fill(BLACK)
18 pygame.display.set_caption('Virtual RC Joystick')
19 
20 # Fill background
21 background = pygame.Surface(windowSurface.get_size())
22 background = background.convert()
23 background.fill((250, 250, 250))
24 
25 # Load image
26 dir = os.path.dirname(__file__)
27 filename = os.path.join(dir, '../media/sticks.png')
28 img = pygame.image.load(filename)
29 
30 windowSurface.blit(img,(0,0))
31 pygame.display.flip()
32 
33 class stick_state(object):
34  def __init__(self, name, stick, key_up, key_down, spring_back=True, incr_val=0.2):
35  self.name = name # The name of the stick
36  self.stick = stick # The stick on the joystick that this stick maps to
37  self.key_up = key_up # The key on the keyboard that maps to this stick increment
38  self.key_down = key_down # The key on the keyboard that maps to this stick decrement
39  self.spring_back = spring_back # Does the stick spring back to center on release?
40  self.incr_val = incr_val # The increment on keypress
41  self.min_val = 0.0 # Minimum stick value
42  self.max_val = 255.0 # Maximum stick value
43  self.active_up = False # True if up key is held pressed
44  self.active_down = False # True if down key is held pressed
45  if self.spring_back:
46  self.zero = 127.0
47  else:
48  self.zero = 0.0
49  self.val = self.zero # Stick value at initialization at zero position
50  self.emit_val = int(self.val)
51  self.display_ready = False # Whether optional display params have been set
52  self.display_height = 0 # Height on the display screen
53  self.display_width = 0 # Width on the display screen
54  self.display_hor = True # Whether the display bar is horizontal, else vertical
55  self.display_bar_g = []
56  self.display_bar_b = []
57 
58  def keypress_up(self):
59  self.active_up = True
60  if (self.val + self.incr_val) <= self.max_val:
61  self.val = self.val + self.incr_val
62  else:
63  # Saturated
64  self.val = self.max_val
65 
66  def keypress_down(self):
67  self.active_down = True
68  if (self.val - self.incr_val) >= self.min_val:
69  self.val = self.val - self.incr_val
70  else:
71  # Saturated
72  self.val = self.min_val
73 
74  def release_stick(self):
75  if not self.spring_back:
76  pass
77  else:
78  if self.val > self.zero:
79  self.val = self.val - self.incr_val*0.2
80  elif self.val < self.zero:
81  self.val = self.val + self.incr_val*0.2
82  else:
83  self.val = self.zero
84 
85  def emit(self, device):
86  # emit effeciently
87  if abs(int(round(self.val)) - int(self.emit_val)) > 0.001:
88  self.emit_val = int(round(self.val))
89  device.emit(self.stick, int(self.emit_val), syn=False)
90  if self.display_ready:
91  self.display()
92 
93  def set_display(self, offset_height, offset_width, horizontal):
94  self.display_height = offset_height
95  self.display_width = offset_width
96  self.display_hor = horizontal
97  if horizontal:
98  filename = os.path.join(dir, '../media/hg.png')
99  self.display_bar_g = pygame.image.load(filename)
100  filename = os.path.join(dir, '../media/hb.png')
101  self.display_bar_b = pygame.image.load(filename)
102  else:
103  filename = os.path.join(dir, '../media/vg.png')
104  self.display_bar_g = pygame.image.load(filename)
105  filename = os.path.join(dir, '../media/vb.png')
106  self.display_bar_b = pygame.image.load(filename)
107  self.display_ready = True
108 
109  def display(self):
110  if not self.display_ready:
111  pass
112  else:
113  # Fill the entire bar
114  for i in range(256):
115  if i <= self.emit_val:
116  # Fill green
117  if self.display_hor:
118  windowSurface.blit(self.display_bar_g,(self.display_width + i, self.display_height))
119  else:
120  windowSurface.blit(self.display_bar_g,(self.display_width, self.display_height - i))
121  else:
122  # Fill grey
123  if self.display_hor:
124  windowSurface.blit(self.display_bar_b,(self.display_width + i, self.display_height))
125  else:
126  windowSurface.blit(self.display_bar_b,(self.display_width, self.display_height - i))
127  # Render it
128  pygame.display.flip()
129 
130  def update_event(self, event):
131  if event.type == KEYUP:
132  if (event.key == self.key_up):
133  self.active_up = False
134  elif event.key == self.key_down:
135  self.active_down = False
136  elif event.type == KEYDOWN:
137  if (event.key == self.key_up):
138  self.active = True
139  self.keypress_up()
140  elif (event.key == self.key_down):
141  self.active = True
142  self.keypress_down()
143 
144  def update_stick(self, device):
145  if self.active_up:
146  self.keypress_up()
147  self.emit(device)
148  elif self.active_down:
149  self.keypress_down()
150  self.emit(device)
151  else:
152  self.release_stick()
153  self.emit(device)
154 
155 def main():
156  events = (
157  uinput.BTN_JOYSTICK,
158  uinput.ABS_X + (0, 255, 0, 0),
159  uinput.ABS_Y + (0, 255, 0, 0),
160  uinput.ABS_THROTTLE + (0, 255, 0, 0),
161  uinput.ABS_RUDDER + (0, 255, 0, 0),
162  )
163 
164  sticks = []
165 
166  # create sticks
167  roll_stick = stick_state('Roll', uinput.ABS_X, K_RIGHT, K_LEFT)
168  roll_stick.set_display(21, 39, True)
169  sticks.append(roll_stick)
170  pitch_stick = stick_state('Pitch', uinput.ABS_Y, K_UP, K_DOWN)
171  pitch_stick.set_display(328, 198, False)
172  sticks.append(pitch_stick)
173  thr_stick = stick_state('Throttle', uinput.ABS_THROTTLE, K_w, K_s, False)
174  thr_stick.set_display(328, 95, False)
175  sticks.append(thr_stick)
176  rud_stick = stick_state('Yaw', uinput.ABS_RUDDER, K_d, K_a)
177  rud_stick.set_display(360, 39, True)
178  sticks.append(rud_stick)
179 
180  with uinput.Device(events) as device:
181  while True:
182  # event handling loop
183  for event in pygame.event.get():
184  for stick in sticks:
185  stick.update_event(event)
186  for stick in sticks:
187  stick.update_stick(device)
188  time.sleep(0.0005)
189 
190 if __name__ == "__main__":
191  main()
def update_stick(self, device)
def set_display(self, offset_height, offset_width, horizontal)
Definition: key_joystick.py:93
def emit(self, device)
Definition: key_joystick.py:85
def update_event(self, event)
def __init__(self, name, stick, key_up, key_down, spring_back=True, incr_val=0.2)
Definition: key_joystick.py:34


rotors_joy_interface
Author(s): Fadri Furrer, Michael Burri, Mina Kamel, Janosch Nikolic, Markus Achtelik
autogenerated on Mon Feb 28 2022 23:39:18