From fc546277fa775666c3c49520becbee5913e40e57 Mon Sep 17 00:00:00 2001 From: Jeffrey Kuhn Date: Tue, 29 Dec 2020 15:54:25 -0500 Subject: [PATCH] added GPIO PWM LED Driver --- Source/BasicControls.py | 70 +++++++++++++++++++++++++++++------------ Source/PiCameraApp.py | 12 ++++--- Source/PwmLed.py | 33 +++++++++++++++++++ 3 files changed, 91 insertions(+), 24 deletions(-) create mode 100644 Source/PwmLed.py diff --git a/Source/BasicControls.py b/Source/BasicControls.py index c33498d..e01060e 100644 --- a/Source/BasicControls.py +++ b/Source/BasicControls.py @@ -24,7 +24,7 @@ import os from collections import OrderedDict -import RPi.GPIO +import RPi.GPIO as GPIO # If no RPi.GPIO, then disable the ability to toggle the camera LED RPiGPIO = True @@ -183,16 +183,17 @@ def BuildPage ( self ): self.Yzoom.grid(row=0,column=3,sticky='W',padx=5,pady=3) self.Yzoom.set(0.0) ToolTip(self.Yzoom,131) - Label(f4,text='Width:').grid(row=1,column=0,sticky='E') - self.Widthzoom = ttk.Scale(f4,from_=0.05,to=1.0,orient='horizontal') - self.Widthzoom.grid(row=1,column=1,sticky='W',padx=5,pady=3) - self.Widthzoom.set(1.0) - ToolTip(self.Widthzoom,132) - Label(f4,text='Height:').grid(row=1,column=2,sticky='E') - self.Heightzoom = ttk.Scale(f4,from_=0.05,to=1.0,orient='horizontal') - self.Heightzoom.grid(row=1,column=3,sticky='W',padx=5,pady=3) - self.Heightzoom.set(1.0) - ToolTip(self.Heightzoom,133) + + Label(f4,text='Factor:').grid(row=1,column=0,sticky='E') + self.Factorzoom = ttk.Scale(f4,from_=0.05,to=1.0,orient='horizontal') + self.Factorzoom.grid(row=1,column=1,sticky='W',padx=5,pady=3) + self.Factorzoom.set(1.0) + ToolTip(self.Factorzoom,132) + # Label(f4,text='Height:').grid(row=1,column=2,sticky='E') + # self.Heightzoom = ttk.Scale(f4,from_=0.05,to=1.0,orient='horizontal') + # self.Heightzoom.grid(row=1,column=3,sticky='W',padx=5,pady=3) + # self.Heightzoom.set(1.0) + # ToolTip(self.Heightzoom,133) # WLW THIS IS A PROBLEM image = PIL.Image.open('Assets/reset.png') #.resize((16,16)) self.resetImage = GetPhotoImage(image.resize((16,16))) @@ -205,10 +206,10 @@ def BuildPage ( self ): widget=self.Xzoom:self.Zoom(newval,widget)) self.Yzoom.config(command=lambda newval, widget=self.Yzoom:self.Zoom(newval,widget)) - self.Widthzoom.config(command=lambda newval, - widget=self.Widthzoom:self.Zoom(newval,widget)) - self.Heightzoom.config(command=lambda newval, - widget=self.Heightzoom:self.Zoom(newval,widget)) + self.Factorzoom.config(command=lambda newval, + widget=self.Factorzoom:self.Zoom(newval,widget)) + # self.Heightzoom.config(command=lambda newval, + # widget=self.Heightzoom:self.Zoom(newval,widget)) Separator(f,orient=HORIZONTAL).grid(pady=5,row=5,column=0, columnspan=3,sticky='EW') @@ -318,6 +319,9 @@ def BuildPage ( self ): variable=self.LedOn, command=self.LedOnChecked) self.LedButton.grid(row=0,column=0,sticky='NW',pady=5, columnspan=2) ToolTip(self.LedButton,msg=102) + + ### MY CONSTANT LED + Label(f,text='Flash Mode:').grid(row=1,column=0,sticky='W') b = MyStringVar('off') self.FlashModeOffRadio = MyRadio(f,'Off (Default)','off',b, @@ -337,8 +341,21 @@ def BuildPage ( self ): self.FlashModeCombo.config(state='disabled') ToolTip(self.FlashModeCombo,183) + Label(f,text='Illumination:').grid(row=2,column=0,sticky='E') + self.IllumOn = MyBooleanVar(True) + self.IllumButton = ttk.Checkbutton(f,text='On', + variable=self.IllumOn, command=self.IllumOnChecked) + self.IllumButton.grid(row=2,column=1,sticky='NW',pady=5, columnspan=1) + self.IllumBrightness = ttk.Scale(f,from_=0.0,to=100.0,orient='horizontal') + self.IllumBrightness.grid(row=2,column=2,sticky='W',padx=5,pady=3,columnspan=3) + self.IllumBrightness.set(100.0) + self.IllumBrightness.config(command=lambda newval, + widget=self.IllumBrightness:self.IllumLevel(newval,widget)) + self.FixedResolutionChanged(None) + + def Reset ( self ): # Use widget.invoke() to simulate a button/radiobutton press self.UseRadio.invoke() @@ -421,18 +438,18 @@ def UseFixedResRadios ( self ): self.cb1.config(state=states[not useFixedRes]) def Zoom ( self, newVal, scale ): self.camera.zoom = (float(self.Xzoom.get()),float(self.Yzoom.get()), - float(self.Widthzoom.get()),float(self.Heightzoom.get())) + float(self.Factorzoom.get()),float(self.Factorzoom.get())) scale.focus_set() def SetZoom ( self, x, y, w, h ): self.Xzoom.set(x) self.Yzoom.set(y) - self.Widthzoom.set(w) - self.Heightzoom.set(h) + self.Factorzoom.set(w) + # self.Factorzoom.set(h) def ZoomReset ( self ): self.Xzoom.set(0.0) self.Yzoom.set(0.0) - self.Widthzoom.set(1.0) - self.Heightzoom.set(1.0) + self.Factorzoom.set(1.0) + # self.Heightzoom.set(1.0) def AllowImageResizeAfter ( self, allowResizeAfter ): if allowResizeAfter: state = 'readonly' @@ -489,3 +506,16 @@ def FlashModeButton ( self, FlashMode ): def FlashModeChanged ( self, event ): self.camera.flash_mode = self.FlashModeCombo.get() + def IllumOnChecked ( self ): + self.setIllum( self.IllumOn.get(), self.IllumBrightness.get()) + def IllumLevel ( self, newVal, scale ): + self.setIllum( self.IllumOn.get(), self.IllumBrightness.get()) + def setIllum (self, on, bright): + brightness = self.IllumBrightness.get() + if (not on): + self.data.setBrightness(0) + else: + self.data.setBrightness(brightness) + + + diff --git a/Source/PiCameraApp.py b/Source/PiCameraApp.py index 299d214..cbd0029 100644 --- a/Source/PiCameraApp.py +++ b/Source/PiCameraApp.py @@ -36,6 +36,7 @@ from time import time, sleep from Tooltip import * from AnnotationOverlay import * +from PwmLed import * #~ try: import RPi.GPIO @@ -113,7 +114,7 @@ class PiCameraApp ( Frame ): # Some statics used elsewhere ExposureModeText = None - def __init__(self, root, camera, title): + def __init__(self, root, camera, pwmled, title): Frame.__init__(self, root) self.grid(padx=5,pady=5) @@ -123,7 +124,9 @@ def __init__(self, root, camera, title): self.camera = camera self.camera.start_preview(fullscreen=False,window=(0,0,10,10)) - + + self.pwmled = pwmled + self.title = title self.root.title(title) @@ -160,7 +163,7 @@ def __init__(self, root, camera, title): n.columnconfigure(0,weight=1) n.enable_traversal() - self.BasicControlsFrame = BasicControls(n,camera) + self.BasicControlsFrame = BasicControls(n,camera,data=pwmled) self.ExposureFrame = Exposure(n,camera) self.FinerControlFrame = FinerControl(n,camera) #self.TimelapseFrame = Timelapse(n,camera) @@ -1114,13 +1117,14 @@ def Run (): # allow changing to other modes later... camera = picamera.PiCamera(sensor_mode=1) camera.sensor_mode = 0 # go back to auto mode + pwmled = PwmLed(18, invert=True) except PiCameraError: print ( "Error creating PiCamera instance! Shutting down.\n\nPress any key..." ) raw_input() return win.minsize(1024,768) - app = PiCameraApp(win,camera,title="PiCamera") + app = PiCameraApp(win,camera,pwmled,title="PiCamera") win.mainloop() camera.close() diff --git a/Source/PwmLed.py b/Source/PwmLed.py new file mode 100644 index 0000000..ffb159b --- /dev/null +++ b/Source/PwmLed.py @@ -0,0 +1,33 @@ +import RPi.GPIO as GPIO + + +class PwmLed (): + def __init__ (self, pin, invert=True): + self.pin = pin + self.invert = invert + self.bright = 100 + + GPIO.setmode(GPIO.BCM) + GPIO.setup(self.pin, GPIO.OUT) + self.pwm = GPIO.PWM(self.pin, 2000) + duty = self.bright + if (invert): + duty = 100-duty + self.pwm.start(duty) + + def __del__ (self): + self.pwm.stop() + # # turn off the LED + # if (self.invert): + # GPIO.output(18,GPIO.HIGH) + # else: + # GPIO.output(18,GPIO.LOW) + GPIO.cleanup() + + def setBrightness (self, bright): + if (bright != self.bright): + self.bright = bright + duty = bright + if (self.invert): + duty = 100-duty + self.pwm.ChangeDutyCycle(duty)