gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Sat Apr 18, 2020 4:33 pm

I tried the shutdown and it works for me.

Have you checked where and how it's connected ?

I meant it wouldn't shutdown whilst waiting for the coinslot

ikn
Posts: 67
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Sun Apr 19, 2020 3:06 pm

Thanks. I checked all the wiring and it seems I had a couple of bad connections. The shutdown button now works. I will give the code a good trial now and see how the Pi behaves.

ikn
Posts: 67
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Tue Apr 21, 2020 3:54 pm

The code seems to be working well, though I have noticed that when the player is waiting too long for a song selection after the coin button has been pressed, the cpu temperature rises from it's normal running temperature (approx 42'C when song playing)

Most of the time this wouldn't be a problem because most people would select the song straight after putting a coin in. But if someone was to dither too long or just leave it in that state then the temperature would rise, though never getting much hotter than 52'C.

I know the Pi is capable of running at more than 52'C but I would like to keep the running temperature as low as I can.

Like I said, most of the time it runs at the lower temperature when the song is playing and after it has stopped. The temperature only starts to rise when it is waiting for a song selection after the coin has been put in and the red LED2 illuminated. The temperature then drops when a song has been selected and the red LED2 goes out.

I would like to keep all button and LED functions exactly as they are now, so is there a way to keep the code as it is but only tweak it slightly so that when the coinslot button has been pressed (red LED2 illuminated) and is waiting for a song selection, it will time out if a song is not selected say within 60 seconds, after which, it will reset and turn the red LED2 off again until another coin is put in to start the song selection process again.

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Tue Apr 21, 2020 6:32 pm

try this...

Code: Select all

#!/usr/bin/env python3
import RPi.GPIO as GPIO
import glob
import subprocess
import os, signal
import time

# define GPIO pins used, first 10 for letters, second 10 for numbers
pins = (7,10,15,16,18,19,21,22,23,24,26,29,31,32,33,35,36,37,38,40)

# buttons
stop = 8
shutdown  = 11
coinslot  = 13

# LEDS
LED1 = 12
LED2 = 5

# define letters
letters = ("A","B","C","D","E","F","G","H","I","J")

# setup GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
for count in range(0,len(pins)):
    GPIO.setup(pins[count],GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(stop,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(shutdown,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(coinslot,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED1,GPIO.OUT)
GPIO.setup(LED2,GPIO.OUT)
GPIO.output(LED1,GPIO.HIGH) # turn LED1 ON
GPIO.output(LED2,GPIO.LOW)  # turn LED2 OFF

# find tracks
tracks = glob.glob("/home/pi/Music/*.mp3")
tracks.sort()

# set starting variables
X = -1
Y = -1
coin_det = 0
time_start = 0
start = 0

while True:

    # wait for a coin
    while GPIO.input(coinslot) == 1 and coin_det == 0:
        time.sleep (.25)
        if GPIO.input(shutdown) == 0:
            os.system("sudo shutdown -h now")

    # coin detected, start timer       
    if coin_det == 0:
        print ("Coin detected")
        time_start = time.time()
        GPIO.output(LED2,GPIO.HIGH) # turn LED2 ON
        coin_det = 1

    # if no activity for 60 seconds, cancel coin detected   
    if time.time() - time_start > 60 and X == -1:
        coin_det = 0
        GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
        print ("Sorry timed out, insert another coin !")
    
    time.sleep(0.1)
    if  GPIO.input(shutdown) == 0:
        print ("Goodbye!")
        os.system("sudo shutdown -h now")
        
    # check for a letter press
    if X == -1 and coin_det == 1:
        for A in range(0,int(len(pins)/2)):
            if GPIO.input(pins[A])== 0:
                X = A
                start = time.time()
                GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
                print ("Letter pressed")
                time_start = time.time()

    # no number pressed within 5 seconds of a letter, cancel letter
    if X != -1 and time.time() - start > 5:
        X = -1
        GPIO.output(LED2,GPIO.HIGH) # turn LED2 ON
        print ("Timed out waiting for a number, choose again")

    # no letter pressed within 5 seconds of a number, cancel number
    if Y != -1 and time.time() - start > 5:
        Y = -1
        GPIO.output(LED2,GPIO.HIGH) # turn LED2 ON
        print ("Timed out waiting for a letter, choose again")
            
    # check for a number press
    if Y == -1 and coin_det == 1:
        for B in range(int(len(pins)/2),len(pins)):
            if GPIO.input(pins[B])== 0:
                Y = (B - int(len(pins)/2))
                start = time.time()
                print ("Number pressed")
                GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
                time_start = time.time()
            
    # if letter AND number pressed play a song
    if X > -1 and Y > -1:
        # calculate track number from letter and number pressed
        Z = 10 * X + Y
        if Z < len(tracks):
            print ("Playing Track: ", letters[X] + str(Y+1), tracks[Z] )
            rpistr = "mplayer " + '"' + tracks[Z] + '"'
            p = subprocess.Popen(rpistr, shell=True, preexec_fn=os.setsid)
            # check if track still playing
            poll = p.poll()
            while poll == None: # track still playing
                GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
                time.sleep(1)
                if  GPIO.input(stop) == 0:
                    print ("Track stopped")
                    os.killpg(p.pid, signal.SIGTERM)
                if  GPIO.input(shutdown) == 0:
                    GPIO.output(LED1,GPIO.LOW) # turn LED1 OFF
                    print ("Goodbye!")
                    os.killpg(p.pid, signal.SIGTERM)
                    os.system("sudo shutdown -h now")
                    
                poll = p.poll()
            print ("Stopped Playing")
            coin_det = 0
            X = -1
            Y = -1
            print ("Insert another coin")
        else:
            print ("No track found")
            coin_det = 0
            X = -1
            Y = -1
            print ("Insert another coin")
            


ikn
Posts: 67
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Sat Apr 25, 2020 9:47 am

Thanks. I have given the new code a test and the Pi now stays at a stable temperature.

Though the code and all buttons function as they should, the red LED2 now goes out when the first letter or number button is presssed, it does not wait for the other button press.

Is there a way to keep this latest code and all functions exactly as they are, except slightly adjust the song selection button press so that when the letter button is pressed first , it will wait for the number button to be pressed before it turns off the red LED2 and also if the number button is pressed first it will wait for the letter button to be pressed before turning off the red LED2

If it's not practical, then no worries, the latest code we have now would be ok and I would use that.

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Sat Apr 25, 2020 10:26 am

Code: Select all

#!/usr/bin/env python3
import RPi.GPIO as GPIO
import glob
import subprocess
import os, signal
import time

# define GPIO pins used, first 10 for letters, second 10 for numbers
pins = (7,10,15,16,18,19,21,22,23,24,26,29,31,32,33,35,36,37,38,40)

# buttons
stop = 8
shutdown  = 11
coinslot  = 13

# LEDS
LED1 = 12
LED2 = 5

# define letters
letters = ("A","B","C","D","E","F","G","H","I","J")

# setup GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
for count in range(0,len(pins)):
    GPIO.setup(pins[count],GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(stop,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(shutdown,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(coinslot,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED1,GPIO.OUT)
GPIO.setup(LED2,GPIO.OUT)
GPIO.output(LED1,GPIO.HIGH) # turn LED1 ON
GPIO.output(LED2,GPIO.LOW)  # turn LED2 OFF

# find tracks
tracks = glob.glob("/home/pi/Music/*.mp3")
tracks.sort()

# set starting variables
X = -1
Y = -1
coin_det = 0
time_start = 0
start = 0

while True:

    # wait for a coin
    while GPIO.input(coinslot) == 1 and coin_det == 0:
        time.sleep (.25)
        if GPIO.input(shutdown) == 0:
            os.system("sudo shutdown -h now")

    # coin detected, start timer       
    if coin_det == 0:
        print ("Coin detected")
        time_start = time.time()
        GPIO.output(LED2,GPIO.HIGH) # turn LED2 ON
        coin_det = 1

    # if no activity for 60 seconds, cancel coin detected   
    if time.time() - time_start > 60 and X == -1:
        coin_det = 0
        GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
        print ("Sorry timed out, insert another coin !")
    
    time.sleep(0.1)
    if  GPIO.input(shutdown) == 0:
        print ("Goodbye!")
        os.system("sudo shutdown -h now")
        
    # check for a letter press
    if X == -1 and coin_det == 1:
        for A in range(0,int(len(pins)/2)):
            if GPIO.input(pins[A])== 0:
                X = A
                start = time.time()
                print ("Letter pressed")
                time_start = time.time()

    # no number pressed within 5 seconds of a letter, cancel letter
    if X != -1 and time.time() - start > 5:
        X = -1
        GPIO.output(LED2,GPIO.HIGH) # turn LED2 ON
        print ("Timed out waiting for a number, choose again")

    # no letter pressed within 5 seconds of a number, cancel number
    if Y != -1 and time.time() - start > 5:
        Y = -1
        GPIO.output(LED2,GPIO.HIGH) # turn LED2 ON
        print ("Timed out waiting for a letter, choose again")
            
    # check for a number press
    if Y == -1 and coin_det == 1:
        for B in range(int(len(pins)/2),len(pins)):
            if GPIO.input(pins[B])== 0:
                Y = (B - int(len(pins)/2))
                start = time.time()
                print ("Number pressed")
                time_start = time.time()
            
    # if letter AND number pressed play a song
    if X > -1 and Y > -1:
        GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
        # calculate track number from letter and number pressed
        Z = 10 * X + Y
        if Z < len(tracks):
            print ("Playing Track: ", letters[X] + str(Y+1), tracks[Z] )
            rpistr = "mplayer " + '"' + tracks[Z] + '"'
            p = subprocess.Popen(rpistr, shell=True, preexec_fn=os.setsid)
            # check if track still playing
            poll = p.poll()
            while poll == None: # track still playing
                GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
                time.sleep(1)
                if  GPIO.input(stop) == 0:
                    print ("Track stopped")
                    os.killpg(p.pid, signal.SIGTERM)
                if  GPIO.input(shutdown) == 0:
                    GPIO.output(LED1,GPIO.LOW) # turn LED1 OFF
                    print ("Goodbye!")
                    os.killpg(p.pid, signal.SIGTERM)
                    os.system("sudo shutdown -h now")
                    
                poll = p.poll()
            print ("Stopped Playing")
            coin_det = 0
            X = -1
            Y = -1
            print ("Insert another coin")
        else:
            print ("No track found")
            coin_det = 0
            X = -1
            Y = -1
            print ("Insert another coin")
            


ikn
Posts: 67
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Tue Apr 28, 2020 3:15 pm

Thanks. I'll use that code.

ikn
Posts: 67
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Sat Jul 25, 2020 4:47 pm

I've got everything up and running and apart from a few minor hardware issues the jukebox is working fine.

Apart from being a one song at a time functioning jukebox, I thought it would be good to make it a general music player too.

While it is fun to use it as a fully functioning jukebox, sometimes I would like to have the choice of playing all the songs one after the other in a loop without needing to keep selecting the songs individually.

I have run out of GPIO pins and want to keep all hardware as it is. So I was was wondering if it is possible to utilise the existing song selection buttons and adjust the script we already have above, so the jukebox would function as follows:

1) When button 'B' is long pressed (held down for say 5 seconds) then the songs would start playing in order from first song through to last song.
2) Then if 'C' button was pressed, it would skip forward to next song.
3) If 'A' button was pressed, it would skip back to previous song.
4) If 'Stop' button was long pressed (held down for say 5 seconds) then it would revert back to the original jukebox mode.

As always, any help would be much appreciated.

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Sun Jul 26, 2020 8:01 am

try this... It will flash LED1 to show in Continuous play mode

Code: Select all

#!/usr/bin/env python3
import RPi.GPIO as GPIO
import glob
import subprocess
import os, signal
import time
import random
from random import shuffle

# define GPIO pins used, first 10 for letters, second 10 for numbers
pins = (7,10,15,16,18,19,21,22,23,24,26,29,31,32,33,35,36,37,38,40)

# shuffle tracks played in continuous mode, set to 1 to shuffle
con_shuffle = 0

# buttons
stop = 8
shutdown  = 11
coinslot  = 13

# LEDS
LED1 = 12
LED2 = 5

# define letters
letters = ("A","B","C","D","E","F","G","H","I","J")

# setup GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
for count in range(0,len(pins)):
    GPIO.setup(pins[count],GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(stop,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(shutdown,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(coinslot,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED1,GPIO.OUT)
GPIO.setup(LED2,GPIO.OUT)
GPIO.output(LED1,GPIO.HIGH) # turn LED1 ON
GPIO.output(LED2,GPIO.LOW)  # turn LED2 OFF

# find tracks
tracks = glob.glob("/home/pi/Music/*.mp3")
tracks.sort()

# set starting variables
X = -1
Y = -1
coin_det = 0
time_start = 0
start = 0
B_pressed = 0
Con_Play = 0

while True:

    # wait for a coin
    while GPIO.input(coinslot) == 1 and coin_det == 0:
        time.sleep (.25)
        if GPIO.input(shutdown) == 0:
            os.system("sudo shutdown -h now")

    # coin detected, start timer       
    if coin_det == 0:
        print ("Coin detected")
        time_start = time.time()
        GPIO.output(LED2,GPIO.HIGH) # turn LED2 ON
        coin_det = 1

    # if no activity for 60 seconds, cancel coin detected   
    if time.time() - time_start > 60 and X == -1:
        coin_det = 0
        GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
        print ("Sorry timed out, insert another coin !")
    
    time.sleep(0.1)
    if  GPIO.input(shutdown) == 0:
        print ("Goodbye!")
        os.system("sudo shutdown -h now")
        
    # check for a letter press
    if X == -1 and coin_det == 1:
        for A in range(0,int(len(pins)/2)):
            if GPIO.input(pins[A])== 0:
                X = A
                start = time.time()
                print ("Letter pressed")
                time_start = time.time()
                if X == 1:
                    print ("B Pressed")
                    B_pressed = 1
    if GPIO.input(pins[1]) == 1 and B_pressed == 1:
        B_pressed = 0
        print ("B Released")

    # no number pressed within 5 seconds of a letter, cancel letter
    # or continuous play mode if B pressed for > 5 seconds
    if X != -1 and time.time() - start > 5:
        if B_pressed == 0:
            X = -1
            GPIO.output(LED2,GPIO.HIGH) # turn LED2 ON
            print ("Timed out waiting for a number, choose again")
        else:
            print ("B Pressed > 5 secs")
            Con_Play = 1

    # no letter pressed within 5 seconds of a number, cancel number
    if Y != -1 and time.time() - start > 5:
        Y = -1
        GPIO.output(LED2,GPIO.HIGH) # turn LED2 ON
        print ("Timed out waiting for a letter, choose again")
            
    # check for a number press
    if Y == -1 and coin_det == 1:
        for B in range(int(len(pins)/2),len(pins)):
            if GPIO.input(pins[B])== 0:
                Y = (B - int(len(pins)/2))
                start = time.time()
                print ("Number pressed")
                time_start = time.time()
            
    # if letter AND number pressed play a song
    if X > -1 and Y > -1:
        GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
        # calculate track number from letter and number pressed
        Z = 10 * X + Y
        if Z < len(tracks):
            print ("Playing Track: ", letters[X] + str(Y+1), tracks[Z] )
            rpistr = "mplayer " + '"' + tracks[Z] + '"'
            p = subprocess.Popen(rpistr, shell=True, preexec_fn=os.setsid)
            # check if track still playing
            poll = p.poll()
            while poll == None: # track still playing
                GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
                time.sleep(1)
                if  GPIO.input(stop) == 0:
                    print ("Track stopped")
                    os.killpg(p.pid, signal.SIGTERM)
                if  GPIO.input(shutdown) == 0:
                    GPIO.output(LED1,GPIO.LOW) # turn LED1 OFF
                    print ("Goodbye!")
                    os.killpg(p.pid, signal.SIGTERM)
                    os.system("sudo shutdown -h now")
                    
                poll = p.poll()
            print ("Stopped Playing")
            coin_det = 0
            X = -1
            Y = -1
            print ("Insert another coin")
        else:
            print ("No track found")
            coin_det = 0
            X = -1
            Y = -1
            print ("Insert another coin")

    # Con_Play = 1 so continuous play mode...
    if Con_Play == 1:
        # shuffle tracks, if selected.
        nums = [0] * len(tracks)
        for q in range(0,len(tracks)):
            nums[q] = q
        if con_shuffle == 1:
            print ("Shuffled tracks")
            shuffle(nums)
        
        GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
        Z = 0
        while Z < len(tracks) :
            print ("Playing Track: ",str(Z+1),"of",str(len(tracks)), tracks[nums[Z]] )
            rpistr = "mplayer " + '"' + tracks[nums[Z]] + '"'
            p = subprocess.Popen(rpistr, shell=True, preexec_fn=os.setsid)
            # check if track still playing
            poll = p.poll()
            while poll == None: # track still playing
                GPIO.output(LED2,GPIO.LOW)
                #GPIO.output(LED1,GPIO.HIGH)
                time.sleep(0.5)
                #GPIO.output(LED1,GPIO.LOW)
                time.sleep(0.5)
                if  GPIO.input(pins[2]) == 0: # Next Track (C pressed)
                    print ("Next Track...")
                    time.sleep(0.5)
                    os.killpg(p.pid, signal.SIGTERM)
                if  GPIO.input(pins[0]) == 0: # Previous track (A pressed)
                    print ("Previous Track...")
                    time.sleep(0.5)
                    os.killpg(p.pid, signal.SIGTERM)
                    Z -= 2
                    if Z < -1:
                        Z = -1
                if  GPIO.input(stop) == 0: # Exiting Continuous Play (if STOP pressed > 5 secs)
                    timer1 = time.time()
                    while GPIO.input(stop) == 0:
                        if time.time() - timer1 > 5:
                            if Con_Play == 1:
                                timer1 = time.time()
                                print ("Track stopped")
                                os.killpg(p.pid, signal.SIGTERM)
                                Con_Play == 0
                                Z = len(tracks)
                                print ("Exiting Continuous Play...")
                                GPIO.output(LED1,GPIO.LOW)
                if  GPIO.input(shutdown) == 0: # shutdown
                    GPIO.output(LED1,GPIO.LOW) 
                    print ("Goodbye!")
                    os.killpg(p.pid, signal.SIGTERM)
                    os.system("sudo shutdown -h now")
                poll = p.poll()
            Z += 1
        print ("Stopped Continuous Playing, all tracks played")
        B_pressed = 0
        Con_Play = 0
        coin_det = 0
        X = -1
        Y = -1
        print ("Insert another coin")

            
Last edited by gordon77 on Sun Jul 26, 2020 12:01 pm, edited 1 time in total.

ikn
Posts: 67
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Sun Jul 26, 2020 11:26 am

Thanks for that. I see you have edited this latest code. I tried it and the LED illumination lights go out completely when the stop button is switched back to jukebox mode (long press stop button). Also I prefer not to have the LEDs flashing.

The code you gave earlier this morning (before edit and without flashing LEDs) is more preferable. I gave it a quick try and it seemed to work well.

Though there is one extra function that would make skipping songs quicker in continuous play mode. Can it be made to work so that when the player is in continuous play mode, it will skip backwards 10 songs when 'D' buton is pressed and skip forward 10 songs when 'E' button is pressed.

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Sun Jul 26, 2020 12:01 pm

Flashing led disabled in above code.

I'll think about the 10 skips.

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Sun Jul 26, 2020 12:19 pm

Try this.. I am sure you'll let me know any errors...

Hopefully you can work out how to add more options.

Code: Select all

#!/usr/bin/env python3
import RPi.GPIO as GPIO
import glob
import subprocess
import os, signal
import time
import random
from random import shuffle

# define GPIO pins used, first 10 for letters, second 10 for numbers
pins = (7,10,15,16,18,19,21,22,23,24,26,29,31,32,33,35,36,37,38,40)

# shuffle tracks played in continuous mode, set to 1 to shuffle
con_shuffle = 0

# buttons
stop = 8
shutdown  = 11
coinslot  = 13

# LEDS
LED1 = 12
LED2 = 5

# define letters
letters = ("A","B","C","D","E","F","G","H","I","J")

# setup GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
for count in range(0,len(pins)):
    GPIO.setup(pins[count],GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(stop,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(shutdown,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(coinslot,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED1,GPIO.OUT)
GPIO.setup(LED2,GPIO.OUT)
GPIO.output(LED1,GPIO.HIGH) # turn LED1 ON
GPIO.output(LED2,GPIO.LOW)  # turn LED2 OFF

# find tracks
tracks = glob.glob("/home/pi/Music/*.mp3")
tracks.sort()

# set starting variables
X = -1
Y = -1
coin_det = 0
time_start = 0
start = 0
B_pressed = 0
Con_Play = 0

while True:

    # wait for a coin
    while GPIO.input(coinslot) == 1 and coin_det == 0:
        time.sleep (.25)
        if GPIO.input(shutdown) == 0:
            os.system("sudo shutdown -h now")

    # coin detected, start timer       
    if coin_det == 0:
        print ("Coin detected")
        time_start = time.time()
        GPIO.output(LED2,GPIO.HIGH) # turn LED2 ON
        coin_det = 1

    # if no activity for 60 seconds, cancel coin detected   
    if time.time() - time_start > 60 and X == -1:
        coin_det = 0
        GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
        print ("Sorry timed out, insert another coin !")
    
    time.sleep(0.1)
    if  GPIO.input(shutdown) == 0:
        print ("Goodbye!")
        os.system("sudo shutdown -h now")
        
    # check for a letter press
    if X == -1 and coin_det == 1:
        for A in range(0,int(len(pins)/2)):
            if GPIO.input(pins[A])== 0:
                X = A
                start = time.time()
                print ("Letter pressed")
                time_start = time.time()
                if X == 1:
                    print ("B Pressed")
                    B_pressed = 1
    if GPIO.input(pins[1]) == 1 and B_pressed == 1:
        B_pressed = 0
        print ("B Released")

    # no number pressed within 5 seconds of a letter, cancel letter
    # or continuous play mode if B pressed for > 5 seconds
    if X != -1 and time.time() - start > 5:
        if B_pressed == 0:
            X = -1
            GPIO.output(LED2,GPIO.HIGH) # turn LED2 ON
            print ("Timed out waiting for a number, choose again")
        else:
            print ("B Pressed > 5 secs")
            Con_Play = 1

    # no letter pressed within 5 seconds of a number, cancel number
    if Y != -1 and time.time() - start > 5:
        Y = -1
        GPIO.output(LED2,GPIO.HIGH) # turn LED2 ON
        print ("Timed out waiting for a letter, choose again")
            
    # check for a number press
    if Y == -1 and coin_det == 1:
        for B in range(int(len(pins)/2),len(pins)):
            if GPIO.input(pins[B])== 0:
                Y = (B - int(len(pins)/2))
                start = time.time()
                print ("Number pressed")
                time_start = time.time()
            
    # if letter AND number pressed play a song
    if X > -1 and Y > -1:
        GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
        # calculate track number from letter and number pressed
        Z = 10 * X + Y
        if Z < len(tracks):
            print ("Playing Track: ", letters[X] + str(Y+1), tracks[Z] )
            rpistr = "mplayer " + '"' + tracks[Z] + '"'
            p = subprocess.Popen(rpistr, shell=True, preexec_fn=os.setsid)
            # check if track still playing
            poll = p.poll()
            while poll == None: # track still playing
                GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
                time.sleep(1)
                if  GPIO.input(stop) == 0:
                    print ("Track stopped")
                    os.killpg(p.pid, signal.SIGTERM)
                if  GPIO.input(shutdown) == 0:
                    GPIO.output(LED1,GPIO.LOW) # turn LED1 OFF
                    print ("Goodbye!")
                    os.killpg(p.pid, signal.SIGTERM)
                    os.system("sudo shutdown -h now")
                    
                poll = p.poll()
            print ("Stopped Playing")
            coin_det = 0
            X = -1
            Y = -1
            print ("Insert another coin")
        else:
            print ("No track found")
            coin_det = 0
            X = -1
            Y = -1
            print ("Insert another coin")

    # Con_Play = 1 so continuous play mode...
    if Con_Play == 1:
        # shuffle tracks, if selected.
        nums = [0] * len(tracks)
        for q in range(0,len(tracks)):
            nums[q] = q
        if con_shuffle == 1:
            print ("Shuffled tracks")
            shuffle(nums)
        
        GPIO.output(LED2,GPIO.LOW) # turn LED2 OFF
        Z = 0
        while Z < len(tracks) :
            print ("Playing Track: ",str(Z+1),"of",str(len(tracks)), tracks[nums[Z]] )
            rpistr = "mplayer " + '"' + tracks[nums[Z]] + '"'
            p = subprocess.Popen(rpistr, shell=True, preexec_fn=os.setsid)
            # check if track still playing
            poll = p.poll()
            while poll == None: # track still playing
                GPIO.output(LED2,GPIO.LOW)
                time.sleep(0.5)
                if  GPIO.input(pins[2]) == 0: # Next Track (C pressed)
                    print ("Next Track...")
                    time.sleep(0.5)
                    os.killpg(p.pid, signal.SIGTERM)
                if  GPIO.input(pins[0]) == 0: # Previous track (A pressed)
                    print ("Previous Track...")
                    time.sleep(0.5)
                    os.killpg(p.pid, signal.SIGTERM)
                    Z -= 2
                    if Z < -1:
                        Z = -1
                if  GPIO.input(pins[4]) == 0: # 10 Track Fwd (E pressed)
                    print ("Skip 10 Track forward...")
                    time.sleep(0.5)
                    os.killpg(p.pid, signal.SIGTERM)
                    if len(tracks) - Z > 10:
                        Z += 9
                    else:
                        Z = len(tracks) - 2
                if  GPIO.input(pins[3]) == 0: # 10 Track Back (D pressed)
                    print ("Skip 10 Track backwards...")
                    time.sleep(0.5)
                    os.killpg(p.pid, signal.SIGTERM)
                    Z -= 11
                    if Z < -1:
                        Z = -1
                if  GPIO.input(stop) == 0: # Exiting Continuous Play (if STOP pressed > 5 secs)
                    timer1 = time.time()
                    while GPIO.input(stop) == 0:
                        if time.time() - timer1 > 5:
                            if Con_Play == 1:
                                timer1 = time.time()
                                print ("Track stopped")
                                os.killpg(p.pid, signal.SIGTERM)
                                Con_Play == 0
                                Z = len(tracks)
                                print ("Exiting Continuous Play...")
                                
                if  GPIO.input(shutdown) == 0: # shutdown
                    GPIO.output(LED1,GPIO.LOW) 
                    print ("Goodbye!")
                    os.killpg(p.pid, signal.SIGTERM)
                    os.system("sudo shutdown -h now")
                poll = p.poll()
            Z += 1
        print ("Stopped Continuous Playing, all tracks played")
        B_pressed = 0
        Con_Play = 0
        coin_det = 0
        X = -1
        Y = -1
        print ("Insert another coin")

            
Last edited by gordon77 on Sun Jul 26, 2020 4:40 pm, edited 1 time in total.

ikn
Posts: 67
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Sun Jul 26, 2020 4:16 pm

Thanks. Unfortunately it doesn't quite work. The 'D' and 'E' buttons work the wrong way round and for some reason the cabinet display illumination (LED1) still goes off completely when switched back to coin pay jukebox mode.

I have everything wired and soldered now, so don't want to mess around with LEDs and buttons.

The original code you gave this morning before it was edited, seems to function well and another bonus is it plays the songs in a continuous loop so I don't need to start the continuous play function over again when the last song ends.

I'm not sure if I will be able to edit it. If I can't, then I will use the code as it is and skip the 10 song skip function. :)

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Sun Jul 26, 2020 4:26 pm

I've already fixed the reversed D and E buttons, in the code above.

LED fixed.

All OK now ?

ikn
Posts: 67
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Sun Jul 26, 2020 6:41 pm

Thanks again. I gave the latest code (reversed D & E buttons and LED fixed) a quick try and it all seems to work now.

I'm not sure if you have edited it again since 4.40pm, but there is one small change I would like to make.

The songs don't play in a loop like the first unedited code you did this morning. It stops at the end of the last song and then you need to start the process again. Usually this wouldn't be a problem, there are 100 songs which play for several hours. But if I want to skip forward from the start and play for example song 95 first, it only plays the last 5 songs after that, then it stops playing at song 100.

Is it possible to edit this latest code to allow the songs to continually play in a loop.

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Sun Jul 26, 2020 6:50 pm

I'll look at it tomorrow.


ikn
Posts: 67
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Tue Jul 28, 2020 4:57 pm

Thanks. I took a peek. I see it uses my own well tested gpio pin button and led combination setup. ;)

I gave the code a try, but unfortunately not all the buttons function correctly.

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Tue Jul 28, 2020 5:22 pm

ikn wrote:
Tue Jul 28, 2020 4:57 pm
Thanks. I took a peek. I see it uses my own well tested gpio pin button and led combination setup. ;)

I gave the code a try, but unfortunately not all the buttons function correctly.
What doesn't work ? It's the same code with the latest addition to play continuously.

ikn
Posts: 67
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Tue Jul 28, 2020 6:29 pm

If you have tried the new code and it works, then there might be a fault with my buttons.

You've made several edits on the forum. It's a little difficult to keep up and remember which code did what. I'm not sure which one of your codes has been edited on github.

I have my Pi inside my music player which needs to be opened up to connect mouse , keyboard and tv in order to change codes. I put an old code back in it and all buttons work with that. When I get a chance to open up the music player, I will download the github code again and try the buttons again.

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Tue Jul 28, 2020 6:47 pm

ikn wrote:
Tue Jul 28, 2020 6:29 pm
If you have tried the new code and it works, then there might be a fault with my buttons.

You've made several edits on the forum. It's a little difficult to keep up and remember which code did what. I'm not sure which one of your codes has been edited on github.

I have my Pi inside my music player which needs to be opened up to connect mouse , keyboard and tv in order to change codes. I put an old code back in it and all buttons work with that. When I get a chance to open up the music player, I will download the github code again and try the buttons again.
The github is the last one on here modified for your request to play continuously until stopped. I also added the option to disable the coinslot in case anyone wants to use it without this. The latest version will always be on github.

I am working on a version for my use that uses only buttons for 1to 9, plus stop, shutdown etc, and can select upto 99999 tracks.

ikn
Posts: 67
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Wed Jul 29, 2020 4:19 pm

Button E only moves forward one song

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Wed Jul 29, 2020 6:33 pm

OK, a bit of code missing..

Add Z +=9 in the relevant section

Change to...

Code: Select all

 os.killpg(p.pid, signal.SIGTERM)
 Z +=9
 if Z > len(tracks):
      Z = Z - len(tracks)  
Fixed on github

ikn
Posts: 67
Joined: Sun Dec 15, 2019 1:07 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Thu Jul 30, 2020 10:41 am

I did kind of guess that the 'z +=9' bit was missing by looking at previous codes. I tried to arrange that bit of the code myself and was half way there, but couldn't quite get it to fully work.

So I used the new complete code from github and for some reason it is still not quite right. On song 92 when you press 'E' it should skip forward 10 songs to song 2, but instead skips to song 1. It's no big deal though.

The Pi is starting to get a bit warm with all the testing and is beginning to throttle, so I will stick with this new github code now and just see how it goes.

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame jukebox simultaneous GPIO button or keyboard press

Thu Jul 30, 2020 12:25 pm

fixed on github. I noticed it would skip only in steps of 9 if you held the button down, so fixed that as well.

https://github.com/Gordon999/Pi_Button_MP3Player

Return to “Python”