Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assistance Modifying Code
#1
my question here

I have these two script working perfectly on my Raspberry pi and Arduino respectively.

I am trying to add in two features to the code at this point.

  1. Add a skip button that is tied to the Arduino and skips through the songs in the queue of the py code
  2. Add a "play series of mp3 clips while the RPI is in an idle or inactive state
Where would be the first place to look to get this going, can someone possible help me code this?



Thank you!


#!/usr/bin/env python import glob import json import os import random import select import serial import socket import subprocess import sys import time import traceback SERIAL = (glob.glob('/dev/ttyUSB*') + glob.glob('/dev/ttyACM*') + [''])[0] PORT = 55555 LETTERS = "ABCDEFGHJKLMNPQRSTUV" NUMBERS = range(1,9) CONFIG = '/var/jukebox/config.json' DevNull = open('/dev/null','rw') config = {} def readConfig(): global config config = { 'dup': False, 'rpt': False, 'rnd': False, 'lim': 0, } try: conf = json.loads(open(CONFIG).read()) for k,v in config.items(): config[k] = type(v)(conf[k]) except: pass readConfig() try: ser = serial.Serial(SERIAL) ser.setBaudrate(9600) except: ser = None traceback.print_exc() Q = [] p = None filename = None def play(filename): p = subprocess.Popen(('play', filename), stdout = DevNull, stdin = DevNull, stderr = DevNull) return p def playGlob(filenameGlob): filenames = glob.glob(filenameGlob) if (len(filenames) == 1): filename = filenames[0] return (filename, play(filename)) return None class AtomicFile(object): def __init__(self, filename, mode = 'w'): self.filename = filename self.new = filename + ".new" self.w = open(self.new, mode) def __enter__(self, *extra): #print(extra) return self.w def __exit__(self, *extra): #print(extra) self.w.close() os.rename(self.new, self.filename) def process(data, address): global p global Q if not data: return print(data, address) if (data == 'Stop'): if p: p.kill() return if (data == 'Flush'): Q = [] return if (data == 'Shutdown'): os.system("sudo /sbin/poweroff") return if (data == 'Play'): return try: data = data.split() if (len(data) != 2): return (letter, number) = data assert(letter in LETTERS) number = int(number) assert(number in NUMBERS) Q.append((letter, number, time.time(), address)) except: traceback.print_exc() def dump(data): with AtomicFile('/dev/shm/jukebox.json') as w: w.write(json.dumps(data, indent=4, sort_keys=1)) # Create a UDP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Bind the socket to the port sock.bind(('0.0.0.0', PORT)) if ser: rs = (sock, ser) else: rs = (sock,) while True: readConfig() print(config) if config['lim']: Q = Q[:config['lim']] if config['rnd']: random.shuffle(Q) (r,w,x) = select.select(rs,(),(),1) try: if sock in r: (data, address) = sock.recvfrom(4096) process(data, address) elif ser in r: try: data = ser.readline().strip() except: rs = (sock,) process(data, SERIAL) except: pass if p: if p.poll() is not None: print(p.wait()) p = None filename = None else: print(filename) if Q: if p: print(Q) else: (letter, number, epoch, address) = Q.pop(0) if config['rpt']: Q.append((letter, number, epoch, address)) if not config['dup']: while Q: (l, n, e, a) = Q[0] if (l == letter) and (n == number): Q.pop(0) else: break p = playGlob("/var/jukebox/%s/%d/*" % (letter, number)) if p: (filename, p) = p elif p: print(filename) else: print('.') dump({'current': filename, 'length' : len(Q), 'queue' : Q, })
Reply
#2
  • A lot of parasitic empty lines
  • Un-indented blocks
Please, make your code presentable - I don't think you'll find anyone willing to read 400 lines of mess
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
I agree with volcano63, chuco61 - can you edit your post please so that your code is indented? I can then try on my RapsberryPi / Arduino for you..
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code Assistance needed in saving the file MithunT 0 1,725 Oct-09-2022, 03:50 PM
Last Post: MithunT
  Modifying code cheburashka 1 2,347 Dec-13-2021, 01:01 PM
Last Post: Kebap
  Assistance with running a few lines of code at an EXACT time nethatar 5 5,830 Feb-24-2021, 10:43 PM
Last Post: nilamo
  Python Code Assistance bfpa40 2 3,371 Oct-24-2018, 01:40 AM
Last Post: bfpa40
  i need assistance on the output of a particular code sirvinprogramming 3 4,892 May-19-2018, 03:37 PM
Last Post: buran
  Need assistance with this code aka2d7 4 5,041 Jan-11-2018, 12:40 AM
Last Post: aka2d7
  win32 package: self-modifying source code??? Orthoducks 3 6,066 Jan-13-2017, 09:29 AM
Last Post: buran

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.