Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from gpiozero import Button as realButton#import button from the Pi GPIO library
- import time # import time functions
- import os #imports OS library for Shutdown control
- import RPi.GPIO as GPIO
- import alsaaudio
- from time import perf_counter, sleep
- from tkinter import *
- from tkinter.ttk import *
- import queue
- def button_callback(channel):
- print("Button was pushed!")
- def pushedPower():
- time.sleep(0.1) # wait for the hold time we want.
- if stopButton.is_pressed: #check if the user let go of the button
- time.sleep(0.5)
- if stopButton.is_pressed:
- GPIO.output(18,GPIO.LOW)
- os.system("shutdown now -h") #shut down the Pi -h is or -r will reset
- #print("shutdown")
- else:
- #print("restart")
- GPIO.output(18,GPIO.LOW)
- os.system("shutdown now -r") #shut down the Pi -h is or -r will reset
- #else:
- #print("cancel")
- def pushedVolumeUp():
- print("VolUp")
- change = 0
- start = perf_counter()
- while alsaaudio.Mixer().getvolume()[0] < 100 and upButton.is_pressed:
- change = round(((perf_counter() - start)*4)/3 + 1)
- newVolume = alsaaudio.Mixer().getvolume()[0] + change
- if (newVolume > 100):
- newVolume = 100
- alsaaudio.Mixer().setvolume(newVolume)
- window.after(0, updateBar(newVolume))
- def pushedVolumeDown():
- print("VolDown")
- change = 0
- start = perf_counter()
- while alsaaudio.Mixer().getvolume()[0] > 0 and downButton.is_pressed:
- change = round(((perf_counter() - start)*4)/3 + 1)
- newVolume = alsaaudio.Mixer().getvolume()[0] - change
- if (newVolume < 0):
- newVolume = 0
- alsaaudio.Mixer().setvolume(newVolume)
- window.after(0, updateBar(newVolume))
- def updateBar(change):
- if not window.winfo_viewable():
- window.deiconify()
- progress['value'] = change
- def hideBar():
- if not upButton.is_pressed and not downButton.is_pressed:
- window.withdraw()
- def releasedVolume():
- window.after(1000, hideBar)
- stopButton = realButton(3) # defines the button as an object and chooses GPIO 26
- stopButton.when_pressed = pushedPower
- upButton = realButton(6)
- downButton = realButton(5)
- upButton.when_pressed = pushedVolumeUp
- downButton.when_pressed = pushedVolumeDown
- upButton.when_released = releasedVolume
- downButton.when_released = releasedVolume
- GPIO.setup(18,GPIO.OUT)
- print("LED on")
- GPIO.output(18,GPIO.HIGH)
- window = Tk()
- window.overrideredirect(1)
- height = window.winfo_screenheight()
- # getting screen's width in pixels
- width = window.winfo_screenwidth()
- barWidth = width/3
- barXpos = (int)((width/2)-barWidth/2)
- barYpos = (int)(height-100)
- window.geometry("+"+str(barXpos)+"+"+str(barYpos))
- progress = Progressbar(window, orient = HORIZONTAL,
- length = barWidth, mode = 'determinate')
- progress.pack(pady = 0)
- #window.wait_visibility(window)
- #window.wm_attributes('-alpha', 0.8)
- progress['value'] = alsaaudio.Mixer().getvolume()[0]
- window.update_idletasks()
- window.withdraw()
- progress.pack(pady = 0)
- window.mainloop()
- GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
- while True:
- GPIO.wait_for_edge(10,GPIO.RISING)
- #pause()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement