Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync drjrkuhn: added GPIO PWM LED Driver #3

Open
wants to merge 1 commit into
base: drjk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 50 additions & 20 deletions Source/BasicControls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))
Expand All @@ -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')
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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)



12 changes: 8 additions & 4 deletions Source/PiCameraApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from time import time, sleep
from Tooltip import *
from AnnotationOverlay import *
from PwmLed import *

#~ try:
import RPi.GPIO
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
33 changes: 33 additions & 0 deletions Source/PwmLed.py
Original file line number Diff line number Diff line change
@@ -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)