BlueWave Studio forum
Volume control with rotary encoder - Printable Version

+- BlueWave Studio forum (https://bluewavestudio.io/community)
+-- Forum: Community (https://bluewavestudio.io/community/forum-85.html)
+--- Forum: How to (https://bluewavestudio.io/community/forum-69.html)
+--- Thread: Volume control with rotary encoder (/thread-1811.html)

Pages: 1 2


Volume control with rotary encoder - Mella - 07-23-2020

Hello,

After several months of using CarPi (kodi 15) in my vehicle, I was looking for a newer and more stable solution to set up in the location I created with my 3D printer


[Image: 20200720-031552.png]

I acquired an Openauto Pro license more than a year ago now, but due to lack of time I had not yet installed it in my car, now done
After several hours / days of basic configuration that I had not properly understood, due to
- The Wi-Fi connection (intermediate difficulty) It must be understood that it is the raspberry (OpenAuto Pro) that must be paired to your phone and not the opposite, for me there is only the 2.4GHz which works, 5GHz does not display OpenAuto in the connections available on my phone.
A solution, I am a buyer, thank you
Phone wireless connection (difficult) the issue was resolved by enabling the develop my phone option in addition to the developer settings in Android Auto
on this video (3:03)





USB audio converter (intermediate difficulty) Four USB ports, only one works (probably because of the converter)


[Image: convert-audio.jpg]

USB audio converter I use:


[Image: Raspberry-Pi-3-Sienoc-USB-2-0-Virtuel-7-...-Carte.jpg]
Hope this information will help the most lost
Now that everything is working perfectly, I would like to try to control the volume and the music change through rotary encoders like this


[Image: 51jt2KoWruL._AC_SX425_.jpg]

They work perfectly with CarPi, but do not work at all with Openauto Pro
I researched several hours / days, trying to see the possibilities available and to understand what was possible to do with OpenAuto with the little knowledge I have in Python programming.
I read some information here http://blog.amnuts.com/2017/01/11/rotary-volume-control-for-the-raspberry-pi/ but as said above my lack of knowledge fails me.
I also read on this Openauto-like project https://github.com/opencardev/crankshaft/wiki/Customizing-Crankshaft that GPIO triggers for OpenAuto actions was enabled by default for volume control, this is also the case for OpenAuto Pro?
By thanking you in advance for your help I wish a good day


RE: Volume control with rotary encoder - Daniel_BlueWave - 07-23-2020

In order to use Android Auto wireless connection please follow this guide:
https://bluewavestudio.io/community/showthread.php?tid=1540

Your issue with USB is not common, maybe RPi is defected?

For Volume control you have to write a script that will convert input values into keystrokes according to User Guide, Section Keyboard controls:
http://bluewavestudio.io/resources/openauto_pro_user_guide/openauto_userguide.pdf


RE: Volume control with rotary encoder - Mella - 07-23-2020

Thank you for your reply.

I followed the guide well, it was a great help and allowed me to better understand the implementation of OpenAuto.

For volume control, being a novice I really don't know where to start, wouldn't he have more information about this on the forum or elsewhere?

Or maybe someone who would take the time to explain to me

Because once the script is written, which is for me at the moment impossible for lack of knowledge, where will I have to place it to make it work and especially what would be the command executed

Will I be able to hope for a future update that could incorporate this functionality of volume control via rotary encoder?


RE: Volume control with rotary encoder - Viper - 07-24-2020

For rotary encoder functionality have a look here.


RE: Volume control with rotary encoder - Mella - 07-27-2020

Thank you for your reply

So I have to change the rotary encoder to use this script?

The encoders that I have for this project have 20 pulses unlike what you indicated to me which have 24 pulses


RE: Volume control with rotary encoder - Viper - 07-28-2020

(07-27-2020, 03:11 PM)Mella Wrote: Thank you for your reply

So I have to change the rotary encoder to use this script?

The encoders that I have for this project have 20 pulses unlike what you indicated to me which have 24 pulses
I don't think that that matters.


RE: Volume control with rotary encoder - Mella - 07-31-2020

I finally got it to work, but the KY-040 rotary encoder, quote above does not work for me

I tried to connect it by following this comparative example, but without success

[Image: 0004367_rotary-encoder-switch-breakout.png]

Using this model, reference PEC11-4215F-S24 everything works perfectly

[Image: PEC11R-4215F-S0024-BOURNS-Encoder-QTY-1pcs.jpg]

By searching on the net I found some for less than $ 2 each

Here is in detail what I did to make it work

ROTARY ENCODER INSTALLATION

[Image: SQmyHAg.png]

1) This script requires the installation of evtest and xdotool

sudo apt install evtest xdotool

2) Create a directory containing the executable

mkdir /home/pi/bin

3) Creation of the script

nano /home/pi/bin/rotary_encoder.bash

4) Copy the script

#!/bin/bash

# This script requires evtest and xdotool to be installed:
# sudo apt install evtest xdotool

# Load overlays for controlling rotary switch
# Rotary switch control is configured for GPIO 6 (pin 31) and GPIO 12 (pin 32).
sudo dtoverlay rotary-encoder pin_a=6 pin_b=12 relative_axis=1
# Pushbutton control is configured for GPIO 13 (pin 33)
sudo dtoverlay gpio-key gpio=13 label=MUTE keycode=113

sleep 2

# Get input device event names
rotaryswitch_input_device_event=$(readlink -f /dev/input/by-path/$(ls /dev/input/by-path | grep rotary))
pushbutton_input_device_event=$(readlink -f /dev/input/by-path/$(ls /dev/input/by-path | grep button))


# Rotary - volume control using OAP's global hotkey volume up and down functions
evtest $rotaryswitch_input_device_event |  \
    while read line ; do \
      if [[ $line =~ .*value\ 1.* ]]; then  \
          sh -c "xdotool key F8" /dev/null ;
      else  \
          if [[ $line =~ .*value\ -1.* ]]; then  \
              sh -c "xdotool key F7" > /dev/null ;
          fi; \
      fi; \
    done &

# Pushbutton - toggle mute / unmute
evtest $pushbutton_input_device_event |  \
    while read line ; do \
      if [[ $line =~ .*value\ 1.* ]]; then  \
          sh -c "amixer -q -D pulse sset Master toggle" > /dev/null ; \
      fi; \
    done

5) Save the script under nano

Ctrl+x + Y

6) Make the script executable

chmod 744 /home/pi/bin/rotary_encoder.bash

7) Run the script

. /home/pi/bin/rotary_encoder.bash

8) Put the path of the script file in /etc/xdg/lxsession/LXDE-pi/autostart

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

9) Add the path of the .bash

/home/pi/bin/rotary_encoder.bash

10) Save under nano

Ctrl+x + Y

11) restart

sudo reboot

Now that the first rotary encoder is working I would like to do two things

1) Activate a second encoder which would be used to browse the lists or change the song (next / previous)

2) use this style push button

[Image: sw100664-008-100.jpg]

To launch applications like in this video, any help would be welcome



Here is the .bash I am using

.zip   rotary_encoder.zip (Size: 717 bytes / Downloads: 509)

Thanks to Viper or his help and to ktb for his schematic and script

Excellent day to all


RE: OAP5 on RPi4B + AudioInjector Octo - frankpintosr - 08-01-2020

(07-31-2020, 02:22 PM)Mella Wrote: For my part I have not succeeded in making the KY-040 rotary encoder work you are talking about

I followed a comparative assembly, but without success, I speak about it a little more HERE

I got the adafruit encoder working using the scripts from Mella.  The scripts you provided work great and require no changes.  I did, however, have to change the wiring to use this type of rotary dial.  If folks are a novice like me, they may benefit from step by step instructions also.  Here is what I came up with.

[Image: attachment.php?aid=721]

[Image: attachment.php?aid=722]

#Enable rotary encoder
sudo apt -y install evtest xdotool 
Sudo nano /boot/config.txt
# Load overlays for controlling rotary switch
# Rotary switch control is configured for GPIO 6 (pin 31) and GPIO 12 (pin 32).
dtoverlay=rotary-encoder,pin_a=6,pin_b=12,relative_axis=1
# Pushbutton control is configured for GPIO 13 (pin 33)
dtoverlay=gpio-key,gpio=13,label=MUTE,keycode=113

#Volume Indicator Script
sudo mkdir /home/pi/software/
sudo mkdir /home/pi/software/vol_indicator/
sudo nano /home/pi/software/vol_indicator/vol_indicator.py
sudo wget -O  /home/pi/software/vol_indicator/vol_indicator.py https://github.com/frankpintosr/openautopro/raw/master/vol_indicator.py
sudo chmod +x /home/pi/software/vol_indicator/vol_indicator.py

#Volume knob shortcut and autostart
sudo mkdir /home/pi/.config/autostart
sudo nano /home/pi/.config/autostart/knob.desktop
sudo wget -O /home/pi/.config/autostart/knob.desktop https://github.com/frankpintosr/openautopro/raw/master/knob.desktop



RE: Volume control with rotary encoder - Mella - 08-02-2020

Perfect thanks for sharing   Smile , I keep it warm, it can always be useful

Could you share the source of this diagram, thanks

Now it would be really nice to be able to use a second rotary encoder in order to be able to navigate (next / previous music)

For the moment I take care of the integration of these wireless controllers that I have just received  Big Grin

   

More information  Guide for steering wheel remote control


RE: Volume control with rotary encoder - frankpintosr - 08-03-2020

(08-02-2020, 07:49 PM)Mella Wrote: Perfect thanks for sharing   Smile , I keep it warm, it can always be useful

Could you share the source of this diagram, thanks

Now it would be really nice to be able to use a second rotary encoder in order to be able to navigate (next / previous music)

For the moment I take care of the integration of these wireless controllers that I have just received  Big Grin



More information  Guide for steering wheel remote control

I got the RPi header breakout image from here https://www.raspberrypi.org/documentation/usage/gpio/images/GPIO-Pinout-Diagram-2.png 
The rotary dial image came from here https://th.bing.com/th/id/OIP.VhygLsDfskPk0DD0dxkK1QHaKk?pid=Api&rs=1 
The other image is just a table I made

I would like to figure out a second dial also.  But for now I am focused on creating the "faceplate" for the Pi to be mounted in my car.  Thanks again, great post!