picraftzero icon indicating copy to clipboard operation
picraftzero copied to clipboard

Support buttons

Open WayneKeenan opened this issue 7 years ago • 8 comments

Support Joypad buttons. Add buttons to the web UI

Probably using the same API as gpio zero but instead of pins use logical id. Also, use text or constant logical id's, e.g 'BTN_X' or BTN_X instead of numeric ones used for joysticks

WayneKeenan avatar Jun 22 '17 03:06 WayneKeenan

Added an example here

WayneKeenan avatar Jun 22 '17 14:06 WayneKeenan

Should this example script work or is it still a draft? I can't get it working with my rock candy joypad. I connected the controller directly to the raspi and to safari. Unfortunately no success.

thymjan avatar Jun 22 '17 20:06 thymjan

Yes, it should work for you, I tested with the same.

Do you get an error? Please send the log messages.

Did you also update picraftzero library from GitHub?

WayneKeenan avatar Jun 23 '17 04:06 WayneKeenan

I don't have to edit the example file (button definitions), do I? I updated from git. I don't receive error messages. By button press nothing happened. I started the script with "python3 button.py" pointed the focus to the web-page and pressed the buttons. Nothing happened in the log file.

thymjan avatar Jun 23 '17 04:06 thymjan

You don't have to edit the example, it should work as-is, but only when the USB dongle is plugged into the Pi. I should clarify that it wont send button presses via the web client, yet. (When I tested on Mac I was running the button.py script on my Mac, not a Pi, to check the pygame backend)

I've pushed an update that has a bit more debug:

sudo pip3 uninstall picraftzero
sudo pip3 install git+https://github.com/WayneKeenan/picraftzero

Please enable debug :

sudo nano /etc/picraftzero.cfg

Add:

[logging]
debug_enabled=yes

Please re-run this on a Pi with the RockCandy dongle attached to the Pi and post the log output, thanks.

Hopefully you will see something such as:

python3 buttons.py
2017-06-23 08:05:18,037 -      joystick.py: 47 -                 <module>() ] - INFO - Optional BlueDot library not found
2017-06-23 08:05:18,040 -      joystick.py: 76 -                 <module>() ] - INFO - Using EventDev implementation
2017-06-23 08:05:18,044 -          zero.py: 19 -                 <module>() ] - INFO - picraftzero, version=0.2.4
2017-06-23 08:05:18,052 -          zero.py: 33 -                 <module>() ] - INFO - Selected pin factory is <class 'gpiozero.pins.rpigpio.RPiGPIOPin'>
2017-06-23 08:05:19,218 -      joystick.py:120 -                 __init__() ] - INFO - Found input devices: ['/dev/input/event0']
2017-06-23 08:05:19,220 -      joystick.py:123 -                 __init__() ] - INFO - Using device: /dev/input/event0
2017-06-23 08:05:19,226 -      joystick.py:130 -                 __init__() ] - INFO - Device USB VPID: 3695:296
2017-06-23 08:05:19,230 -        config.py: 20 -             parse_config() ] - INFO - Config file search locations: ['/home/pi/pycharm.projects/PiCraft/picraftzero/resources/config/picraftzero.cfg', '/etc/picraftzero.cfg', '/home/pi/.picraftzero.cfg']
2017-06-23 08:05:19,262 -        config.py: 25 -             parse_config() ] - INFO - Merging config from these files:
2017-06-23 08:05:19,268 -        config.py: 27 -             parse_config() ] - INFO - ['/etc/picraftzero.cfg',
2017-06-23 08:05:19,271 -        config.py: 27 -             parse_config() ] - INFO -  '/home/pi/pycharm.projects/PiCraft/picraftzero/resources/config/picraftzero.cfg']
2017-06-23 08:05:19,274 -        config.py: 40 -               get_config() ] - INFO - Combined final config is:
2017-06-23 08:05:19,323 -        config.py: 42 -               get_config() ] - INFO - {'camera': {'framerate': '24',
2017-06-23 08:05:19,325 -        config.py: 42 -               get_config() ] - INFO -             'hflip': 'no',
2017-06-23 08:05:19,327 -        config.py: 42 -               get_config() ] - INFO -             'led': 'no',
2017-06-23 08:05:19,329 -        config.py: 42 -               get_config() ] - INFO -             'rotation': '0',
2017-06-23 08:05:19,331 -        config.py: 42 -               get_config() ] - INFO -             'vflip': 'yes'},
2017-06-23 08:05:19,334 -        config.py: 42 -               get_config() ] - INFO -  'hmd': {'camera_left_url': 'http://cam0.local:8080/stream/video.mjpeg',
2017-06-23 08:05:19,336 -        config.py: 42 -               get_config() ] - INFO -          'camera_mono_url': 'http://${WINDOW_LOCATION_HOSTNAME}:8080/stream/video.mjpeg',
2017-06-23 08:05:19,338 -        config.py: 42 -               get_config() ] - INFO -          'camera_right_url': 'http://cam1.local:8080/stream/video.mjpeg',
2017-06-23 08:05:19,340 -        config.py: 42 -               get_config() ] - INFO -          'camera_view_lx': '13',
2017-06-23 08:05:19,342 -        config.py: 42 -               get_config() ] - INFO -          'camera_view_ly': '40',
2017-06-23 08:05:19,344 -        config.py: 42 -               get_config() ] - INFO -          'camera_view_rx': '20',
2017-06-23 08:05:19,346 -        config.py: 42 -               get_config() ] - INFO -          'camera_view_ry': '60',
2017-06-23 08:05:19,348 -        config.py: 42 -               get_config() ] - INFO -          'iconbar_element': 'icon-bar'},
2017-06-23 08:05:19,351 -        config.py: 42 -               get_config() ] - INFO -  'inputs': {'keyboard_mapping': "'8BitDoZero'"},
2017-06-23 08:05:19,353 -        config.py: 42 -               get_config() ] - INFO -  'joystick_0': {'invert_x_axis': 'no', 'invert_y_axis': 'no'},
2017-06-23 08:05:19,355 -        config.py: 42 -               get_config() ] - INFO -  'joystick_1': {'invert_x_axis': 'no', 'invert_y_axis': 'no'},
2017-06-23 08:05:19,357 -        config.py: 42 -               get_config() ] - INFO -  'logging': {'debug_enabled': 'yes',
2017-06-23 08:05:19,359 -        config.py: 42 -               get_config() ] - INFO -              'web_debug_enabled': 'yes',
2017-06-23 08:05:19,361 -        config.py: 42 -               get_config() ] - INFO -              'websocket_logging_enabled': 'no'},
2017-06-23 08:05:19,363 -        config.py: 42 -               get_config() ] - INFO -  'pantilt': {'pan_angle_offset': '0', 'tilt_angle_offset': '0'},
2017-06-23 08:05:19,365 -        config.py: 42 -               get_config() ] - INFO -  'services_topology': {'hosts_services_mapping': "{'tiny4wd.local': "
2017-06-23 08:05:19,368 -        config.py: 42 -               get_config() ] - INFO -                                                  "['picraftzero']}, "
2017-06-23 08:05:19,370 -        config.py: 42 -               get_config() ] - INFO -                                                  "'services': {'www': "
2017-06-23 08:05:19,372 -        config.py: 42 -               get_config() ] - INFO -                                                  "['picraftzero']}}"},
2017-06-23 08:05:19,374 -        config.py: 42 -               get_config() ] - INFO -  'www': {'http_port': '8000', 'ws_port': '8001', 'ws_protocol': 'ws://'}}
2017-06-23 08:05:19,379 -       servers.py: 61 -                    start() ] - INFO - WWW dir: /home/pi/pycharm.projects/PiCraft/picraftzero/resources/www
2017-06-23 08:05:19,383 -       servers.py: 45 -                   _start() ] - INFO - Starting HTTP server on port 8000
2017-06-23 08:05:19,394 -       servers.py:113 -                   _start() ] - INFO - Starting WebSocket server on port 8001
2017-06-23 08:05:19,397 -          zero.py:155 -                 __init__() ] - INFO - Joystick(id=0, xname=rx, yname=ry, xinvert=False, yinvert=False
2017-06-23 08:05:23,680 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205123.679405, code 04, type 04, val 589837, event at 1498205123.679405, code 04, type 04, val 589837
2017-06-23 08:05:23,684 -      joystick.py:155 -                   _start() ] - DEBUG - KeyEvent, keycode=BTN_MODE, scancode=316, keystate=1
2017-06-23 08:05:23,689 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205123.679405, code 00, type 00, val 00, synchronization event at 1498205123.679405, SYN_REPORT 
2017-06-23 08:05:24,768 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205124.767396, code 04, type 04, val 589837, event at 1498205124.767396, code 04, type 04, val 589837
2017-06-23 08:05:24,772 -      joystick.py:155 -                   _start() ] - DEBUG - KeyEvent, keycode=BTN_MODE, scancode=316, keystate=0
2017-06-23 08:05:24,776 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205124.767396, code 00, type 00, val 00, synchronization event at 1498205124.767396, SYN_REPORT 
2017-06-23 08:05:25,840 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205125.839405, code 04, type 04, val 589826, event at 1498205125.839405, code 04, type 04, val 589826
2017-06-23 08:05:25,844 -      joystick.py:155 -                   _start() ] - DEBUG - KeyEvent, keycode=['BTN_B', 'BTN_EAST'], scancode=305, keystate=1
2017-06-23 08:05:25,849 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205125.839405, code 00, type 00, val 00, synchronization event at 1498205125.839405, SYN_REPORT 
2017-06-23 08:05:26,083 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205126.079475, code 04, type 04, val 589826, event at 1498205126.079475, code 04, type 04, val 589826
2017-06-23 08:05:26,088 -      joystick.py:155 -                   _start() ] - DEBUG - KeyEvent, keycode=['BTN_B', 'BTN_EAST'], scancode=305, keystate=0
2017-06-23 08:05:26,093 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205126.079475, code 00, type 00, val 00, synchronization event at 1498205126.079475, SYN_REPORT 
2017-06-23 08:05:26,984 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205126.983429, code 04, type 04, val 589826, event at 1498205126.983429, code 04, type 04, val 589826
2017-06-23 08:05:26,988 -      joystick.py:155 -                   _start() ] - DEBUG - KeyEvent, keycode=['BTN_B', 'BTN_EAST'], scancode=305, keystate=1
button1_pressed
2017-06-23 08:05:26,996 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205126.983429, code 00, type 00, val 00, synchronization event at 1498205126.983429, SYN_REPORT 
2017-06-23 08:05:27,208 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205127.207422, code 04, type 04, val 589826, event at 1498205127.207422, code 04, type 04, val 589826
2017-06-23 08:05:27,212 -      joystick.py:155 -                   _start() ] - DEBUG - KeyEvent, keycode=['BTN_B', 'BTN_EAST'], scancode=305, keystate=0
2017-06-23 08:05:27,217 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205127.207422, code 00, type 00, val 00, synchronization event at 1498205127.207422, SYN_REPORT 
2017-06-23 08:05:28,696 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205128.695451, code 04, type 04, val 589827, event at 1498205128.695451, code 04, type 04, val 589827
2017-06-23 08:05:28,700 -      joystick.py:155 -                   _start() ] - DEBUG - KeyEvent, keycode=BTN_C, scancode=306, keystate=1
2017-06-23 08:05:28,705 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205128.695451, code 00, type 00, val 00, synchronization event at 1498205128.695451, SYN_REPORT 
2017-06-23 08:05:28,888 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205128.887430, code 04, type 04, val 589827, event at 1498205128.887430, code 04, type 04, val 589827
2017-06-23 08:05:28,892 -      joystick.py:155 -                   _start() ] - DEBUG - KeyEvent, keycode=BTN_C, scancode=306, keystate=0
2017-06-23 08:05:28,897 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205128.887430, code 00, type 00, val 00, synchronization event at 1498205128.887430, SYN_REPORT 
2017-06-23 08:05:29,928 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205129.927443, code 04, type 04, val 589827, event at 1498205129.927443, code 04, type 04, val 589827
2017-06-23 08:05:29,932 -      joystick.py:155 -                   _start() ] - DEBUG - KeyEvent, keycode=BTN_C, scancode=306, keystate=1
button2_pressed
2017-06-23 08:05:29,939 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205129.927443, code 00, type 00, val 00, synchronization event at 1498205129.927443, SYN_REPORT 
2017-06-23 08:05:30,136 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205130.135442, code 04, type 04, val 589827, event at 1498205130.135442, code 04, type 04, val 589827
2017-06-23 08:05:30,140 -      joystick.py:155 -                   _start() ] - DEBUG - KeyEvent, keycode=BTN_C, scancode=306, keystate=0
2017-06-23 08:05:30,145 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205130.135442, code 00, type 00, val 00, synchronization event at 1498205130.135442, SYN_REPORT 
2017-06-23 08:05:31,584 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205131.583460, code 04, type 04, val 589825, event at 1498205131.583460, code 04, type 04, val 589825
2017-06-23 08:05:31,588 -      joystick.py:155 -                   _start() ] - DEBUG - KeyEvent, keycode=['BTN_A', 'BTN_GAMEPAD', 'BTN_SOUTH'], scancode=304, keystate=1
2017-06-23 08:05:31,593 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205131.583460, code 00, type 00, val 00, synchronization event at 1498205131.583460, SYN_REPORT 
2017-06-23 08:05:31,776 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205131.775458, code 04, type 04, val 589825, event at 1498205131.775458, code 04, type 04, val 589825
2017-06-23 08:05:31,780 -      joystick.py:155 -                   _start() ] - DEBUG - KeyEvent, keycode=['BTN_A', 'BTN_GAMEPAD', 'BTN_SOUTH'], scancode=304, keystate=0
button0_released
2017-06-23 08:05:31,789 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205131.775458, code 00, type 00, val 00, synchronization event at 1498205131.775458, SYN_REPORT 
2017-06-23 08:05:32,240 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205132.239463, code 04, type 04, val 589825, event at 1498205132.239463, code 04, type 04, val 589825
2017-06-23 08:05:32,244 -      joystick.py:155 -                   _start() ] - DEBUG - KeyEvent, keycode=['BTN_A', 'BTN_GAMEPAD', 'BTN_SOUTH'], scancode=304, keystate=1
button0_pressed
2017-06-23 08:05:32,252 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205132.239463, code 00, type 00, val 00, synchronization event at 1498205132.239463, SYN_REPORT 
2017-06-23 08:05:32,352 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205132.351466, code 04, type 04, val 589825, event at 1498205132.351466, code 04, type 04, val 589825
2017-06-23 08:05:32,356 -      joystick.py:155 -                   _start() ] - DEBUG - KeyEvent, keycode=['BTN_A', 'BTN_GAMEPAD', 'BTN_SOUTH'], scancode=304, keystate=0
button0_released
2017-06-23 08:05:32,363 -      joystick.py:161 -                   _start() ] - DEBUG - event at 1498205132.351466, code 00, type 00, val 00, synchronization event at 1498205132.351466, SYN_REPORT 

WayneKeenan avatar Jun 23 '17 08:06 WayneKeenan

I have noticed a bug where the very first button press doesn't call the when_pressed handler, will fix.

WayneKeenan avatar Jun 23 '17 08:06 WayneKeenan

Got this working now when attaching the rock candy transceiver directly to the raspberry pi. Here is the logging: button_log.txt

thymjan avatar Jun 24 '17 22:06 thymjan

great, thanks for confirming.

WayneKeenan avatar Jun 26 '17 08:06 WayneKeenan