I am trying to do a similar thing in python for rotary encoders.
When I press the button of one I want it to pause/play depending on the current state.
From this page (gist.github.com/ivesdebruycker/ … c1d28e9b9e) I got some idea of what is going on and borrowed this snippet.
[code]def on_push_state(*args):
# print(‘state’, args)
global status
status = args[0][‘status’].encode(‘ascii’, ‘ignore’)
print status
socketIO.on(‘pushState’, on_push_state)
get initial state
socketIO.emit(‘getState’, ‘’, on_push_state)[/code]
my problem appears to be the status is not being updated.
I have kludged together bits form all over the place and it is looking a little messy, but I will attach what I have right now, as embarrassing as it will be.
In full:
#!/usr/bin/env python
#
# Raspberry Pi Rotary Test Encoder Class
#
# Author : Bob Rathbone
# Site : http://www.bobrathbone.com
#
# This class uses a standard rotary encoder with push switch
#
import subprocess
import sys
import time
from rotary_class import RotaryEncoder
from socketIO_client import SocketIO, LoggingNamespace
# Define the IO elements
socketIO = SocketIO('localhost', 3000)
status = 'unknown'
# Switch definitions
LEFT_A = 17
LEFT_B = 27
LEFT_BUTTON = 10
RIGHT_A = 23
RIGHT_B = 24
RIGHT_BUTTON = 25
# This is the event callback routine to handle left knob events
def left_knob_event(event):
handle_event(event,"Left")
return
# This is the event callback routine to handle right knob events
def right_knob_event(event):
handle_event(event,"Right")
return
def on_push_state(*args):
# print('state', args)
global status
status = args[0]['status'].encode('ascii', 'ignore')
print status
socketIO.on('pushState', on_push_state)
# get initial state
socketIO.emit('getState', '', on_push_state)
# This is the event callback routine to handle events
def handle_event(event, name):
if event == RotaryEncoder.CLOCKWISE:
if name == "Right":
socketIO.emit('volume', '+');
else :
socketIO.emit('seek',5);
print name, "Clockwise event =", RotaryEncoder.CLOCKWISE
elif event == RotaryEncoder.ANTICLOCKWISE:
if name == "Right":
socketIO.emit('volume', '-');
else:
socketIO.emit('seek',-5);
print name, "Anticlockwise event =", RotaryEncoder.BUTTONDOWN
elif event == RotaryEncoder.BUTTONDOWN:
if name == "Right":
print status
if status == 'play':
socketIO.emit('pause');
else:
socketIO.emit('play');
else:
socketIO.emit('next');
print name, "Button down event =", RotaryEncoder.BUTTONDOWN
#elif event == RotaryEncoder.BUTTONUP:
# print name, "Button up event =", RotaryEncoder.BUTTONUP
return
# Define the left and right knobs
leftknob = RotaryEncoder(LEFT_A,LEFT_B,LEFT_BUTTON,left_knob_event)
rightknob = RotaryEncoder(RIGHT_A,RIGHT_B,RIGHT_BUTTON,right_knob_event)
# Wait for events
while True:
time.sleep(0.5)
# End of program
Additional:
running this from cmdline returns a result.
curl volumio.local:3000/api/v1/getState
Whereas in py the getstate appears to be returning “None” when I tried this
print socketIO.emit(‘getState’)
None
I am wondering if I shouldn’t be using socketIO.emit, but something else.
Another Update:
This time I tried adding this [code]
socketIO.on(‘pushState’, on_push_state)
get initial state
socketIO.emit(‘getState’, ‘’, on_push_state)
try:
socketIO.wait(0.1)
except KeyboardInterrupt:
pass
[/code]
Result in:
print status
pause
I can wait 0.1 seconds.
More testing to follow…