Created
August 31, 2018 21:03
-
-
Save jan-martinek/3c5595cc1063be32da0fa87edef29bd3 to your computer and use it in GitHub Desktop.
Volume service for VLC radio based on Arduino measuring potentiometer voltage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import atexit | |
import re | |
import socket | |
import subprocess | |
import time | |
import os | |
import sys | |
from datetime import datetime | |
from UUGear import * | |
VLC_HOST = "127.0.0.1" | |
VLC_PORT = 9294 | |
LOG_LEVEL = 0 | |
PIDFILE = "/var/run/uuvolume.pid" | |
LOGFILE = "/var/log/uuvolume.log" | |
ERRFILE = "/var/log/uuvolume.err" | |
SHUTDOWN_HOLD_TIME = 3 | |
LOG_INFO = 0 | |
LOG_WARN = 1 | |
LOG_FAIL = 2 | |
LOG_DEBUG = 3 | |
def log(msg, level=LOG_INFO): | |
if level < LOG_LEVEL: return | |
sys.stdout.write(str(datetime.now())) | |
sys.stdout.write(": ") | |
sys.stdout.write(msg) | |
sys.stdout.write("\n") | |
sys.stdout.flush() | |
class VLC(): | |
def __init__(self, host="127.0.0.1", port=9294): | |
self.host = host | |
self.port = port | |
self.current_stream = None | |
self.current_state = None | |
self.connecting = False | |
self.socket = None | |
def send(self, command): | |
if self.connecting: | |
return False | |
log("Sending command: {}".format(command), level=LOG_INFO) | |
command_string = command + "\n" | |
if sys.version_info[0] >= 3: | |
command_string = command_string.encode("utf-8") | |
try: | |
self.socket.send(command_string) | |
except socket.error: | |
log("Failed to send command to VLC", level=LOG_WARN) | |
if self.connect(): | |
self.send(command) | |
def recv(self, length): | |
value = self.socket.recv(8192) | |
if sys.version_info[0] >= 3: | |
value = value.decode("utf-8") | |
return value | |
def communicate(self, command, response_length=8192): | |
self.send(command) | |
return self.recv(response_length) | |
def get_current_stream(self): | |
self.send("status") | |
status = self.recv(8192) | |
result = re.search("input:\ (.*)\ ", status) | |
state = re.search("state\ (.*)\ ", status) | |
if state is not None: | |
self.current_state = state.group(1) | |
if result is not None: | |
self.current_stream = result.group(1) | |
else: | |
self.current_stream = None | |
return self.current_stream | |
def connect(self): | |
if self.connecting: | |
return | |
self.connecting = True | |
if self.socket is not None: | |
self.socket.close() | |
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
for attempt in range(10): | |
try: | |
log("Attempting to connect to VLC on {host}:{port}".format(host=self.host, port=self.port)) | |
self.socket.connect((self.host, self.port)) | |
log("Connection successful!", level=LOG_INFO) | |
self.connecting = False | |
return True | |
except socket.error: | |
time.sleep(1) | |
log("Connection failed!", level=LOG_FAIL) | |
self.connecting = False | |
return False | |
UUGearDevice.setShowLogs(0) | |
arduino = UUGearDevice('UUGear-Arduino-4923-9864') | |
voltage = 0 | |
prevVolume = 0 | |
vlc = VLC(host=VLC_HOST, port=VLC_PORT) | |
if vlc.connect(): | |
while True: | |
measured = arduino.analogRead(3) | |
print measured | |
if int(measured) != -1: | |
voltage = 0.8 * voltage + 0.2 * float(measured) | |
volume = int(round(voltage / 5)) + 20 | |
print volume | |
if volume != prevVolume: | |
prevVolume = volume | |
vlc.communicate("volume " + str(int(volume))) | |
time.sleep(0.15) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment