Advertisement
Guest User

Python Pi Reboot

a guest
Jan 21st, 2022
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. from gpiozero import Button as realButton#import button from the Pi GPIO library
  2. import time # import time functions
  3. import os #imports OS library for Shutdown control
  4. import RPi.GPIO as GPIO
  5. import alsaaudio
  6. from time import perf_counter, sleep
  7. from tkinter import *
  8. from tkinter.ttk import *
  9. import queue
  10.  
  11. def button_callback(channel):
  12.     print("Button was pushed!")
  13. def pushedPower():
  14.     time.sleep(0.1) # wait for the hold time we want.
  15.     if stopButton.is_pressed: #check if the user let go of the button
  16.         time.sleep(0.5)
  17.         if stopButton.is_pressed:
  18.             GPIO.output(18,GPIO.LOW)
  19.             os.system("shutdown now -h") #shut down the Pi -h is or -r will reset
  20.             #print("shutdown")
  21.         else:
  22.             #print("restart")
  23.             GPIO.output(18,GPIO.LOW)
  24.             os.system("shutdown now -r") #shut down the Pi -h is or -r will reset
  25.     #else:
  26.         #print("cancel")
  27.  
  28. def pushedVolumeUp():
  29.     print("VolUp")
  30.     change = 0
  31.     start = perf_counter()
  32.     while alsaaudio.Mixer().getvolume()[0] < 100 and upButton.is_pressed:
  33.         change = round(((perf_counter() - start)*4)/3 + 1)
  34.         newVolume = alsaaudio.Mixer().getvolume()[0] + change
  35.         if (newVolume > 100):
  36.             newVolume = 100
  37.         alsaaudio.Mixer().setvolume(newVolume)
  38.         window.after(0, updateBar(newVolume))
  39. def pushedVolumeDown():
  40.     print("VolDown")
  41.     change = 0
  42.     start = perf_counter()
  43.     while alsaaudio.Mixer().getvolume()[0] > 0 and downButton.is_pressed:
  44.         change = round(((perf_counter() - start)*4)/3 + 1)
  45.         newVolume = alsaaudio.Mixer().getvolume()[0] - change
  46.         if (newVolume < 0):
  47.             newVolume = 0
  48.         alsaaudio.Mixer().setvolume(newVolume)
  49.         window.after(0, updateBar(newVolume))
  50. def updateBar(change):
  51.     if not window.winfo_viewable():
  52.         window.deiconify()
  53.     progress['value'] = change
  54.    
  55. def hideBar():
  56.     if not upButton.is_pressed and not downButton.is_pressed:
  57.         window.withdraw()
  58.  
  59. def releasedVolume():
  60.         window.after(1000, hideBar)
  61.  
  62. stopButton = realButton(3) # defines the button as an object and chooses GPIO 26
  63. stopButton.when_pressed = pushedPower
  64.  
  65. upButton = realButton(6)
  66. downButton = realButton(5)
  67. upButton.when_pressed = pushedVolumeUp
  68. downButton.when_pressed = pushedVolumeDown
  69. upButton.when_released = releasedVolume
  70. downButton.when_released = releasedVolume
  71.  
  72. GPIO.setup(18,GPIO.OUT)
  73. print("LED on")
  74. GPIO.output(18,GPIO.HIGH)
  75.  
  76.  
  77. window = Tk()
  78. window.overrideredirect(1)
  79. height = window.winfo_screenheight()
  80.  
  81. # getting screen's width in pixels
  82. width = window.winfo_screenwidth()
  83.  
  84. barWidth = width/3
  85.  
  86. barXpos = (int)((width/2)-barWidth/2)
  87.  
  88. barYpos = (int)(height-100)
  89.  
  90. window.geometry("+"+str(barXpos)+"+"+str(barYpos))
  91. progress = Progressbar(window, orient = HORIZONTAL,
  92.               length = barWidth, mode = 'determinate')
  93.  
  94. progress.pack(pady = 0)
  95.  
  96. #window.wait_visibility(window)
  97.  
  98. #window.wm_attributes('-alpha', 0.8)
  99.  
  100. progress['value'] = alsaaudio.Mixer().getvolume()[0]
  101. window.update_idletasks()
  102. window.withdraw()
  103. progress.pack(pady = 0)
  104.  
  105. window.mainloop()
  106.  
  107. GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  108. while True:
  109.     GPIO.wait_for_edge(10,GPIO.RISING)
  110.  
  111. #pause()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement