import sys, time #sys.path.append("/home/pi/Desktop/IX_assets/ix") ########################### def initCD2(Pin,mode,switch): import RPi.GPIO as GPIO #By D@CC on 2023KNov26 #mode ("BCM","BOARD") is the pin number method #core ("CM4","Pi4b","Pi400") #switch ("CD2","pHAT") #Pin is the pin # either phys pin or GPIO pin #CD2 Pin is phys pin 40 (G21) #pHAT Pin is G21,G16,G20 #makCM4 Pins are G17,G22,G27 #switch (pHAT,CD2,makCM4) type of switch # pHAT:maker pHAT, CD2 by D@CC, makerCM4) #PPin-2 is PPout (tested with PPin = 40) if mode=="BOARD": GPIO.setmode(GPIO.BOARD) #physical Board numbers #if end if mode=="BCM": GPIO.setmode(GPIO.BCM) #BCM numbers #if end #GPIO.setmode(GPIO.BCM) #BroadCoM numbering eg GPIO #s print("In Pin:",Pin) if mode=="BCM": OutPin=Pin-1 GPIO.setup(OutPin,GPIO.OUT) print("turn On GPIO pin:", OutPin) GPIO.output(OutPin, True) #if end if mode=="BOARD": isBadPin= ((Pin-2==1) or (Pin-2==17)) # they are 3v3 # Ground pins won't work either, nor 5v0 pins print("isBadPin:",isBadPin) if not isBadPin: GPIO.setup(Pin-2,GPIO.OUT) print("turn On phys pin:", Pin-2) GPIO.output(Pin-2, True) #if end #ifend # on a Raspberry Pi, PullUpDown is a 10k resistor # a photoresistor photocell (CD) when lit is 100. # when dark is >10k0. return #def end initCD2(PPin) #::::::::::textpak=>readCD2.py def readCD2(Pin,mode,switch): import RPi.GPIO as GPIO #By D@CC on 2023KNov26 #mode ("BCM","BOARD") is the pin number method #core ("CM4","Pi4b","Pi400") #switch ("CD2","pHAT") #Pin is the pin # either phys pin or GPIO pin #CD2 Pin is phys pin 40 (G21) #pHAT Pin is G21,G16,G20 #makCM4 Pins are G17,G22,G27 #Purpose: to return True if both CDs are covered #HW: my device "CD2_Dongle(c)" #Tested with PPin=40 if mode=="BOARD": GPIO.setmode(GPIO.BOARD) #physical Board numbers #if end if mode=="BCM": GPIO.setmode(GPIO.BCM) #GPIO Board numbers #if end #GPIO.setup(PPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #for CD2 dongle if switch=="pHAT": GPIO.setup(Pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) #for Maker pHAT reading=GPIO.input(Pin) else: # reading is True if the CD photoCells are Dark #print("reading from Pin:",Pin,reading) # Make them dark by covering them with a finger #reading=False if mode=="BCM": GPIO.setmode(GPIO.BCM) #physical Board numbers #GPIO.setup(PPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #for CD2 dongle GPIO.setup(Pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #for CD2 on RPi 4 or Pi-400 reading=GPIO.input(Pin) # reading is True if the CD photoCells are Dark print("reading from Pin ",Pin,":",reading) # Make them dark by covering them with a finger if mode=="BOARD": GPIO.setmode(GPIO.BOARD) #physical Board numbers #GPIO.setup(PPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #for CD2 dongle GPIO.setup(Pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #for CD2 on RPi 4 or Pi-400 reading=GPIO.input(Pin) # reading is True if the CD photoCells are Dark print("reading from Pin ",Pin,":",reading) # Make them dark by covering them with a finger #if end if reading: state=False else: state=True #if end return state #def end progName="Test_readCD2wPi4B.py" #by D@CC on 2023KNov26 #mode ("BCM","BOARD") is the pin number method #core ("CM4","Pi4b","Pi400") #switch ("CD2","pHAT") #Pin is the pin # either phys pin or GPIO pin #CD2 Pin is phys pin 21 (G21) #pHAT Pin is G21,G16,G20 #makCM4 Pins are G17,G22,G27 # works for BOARD,Pi400,CD2,Pin=40 # works for BOARD,Pi4B,CD2,Pin=40 # works for BOARD,Pi4B,CD2,Pin=33 when maker pHAT is used # fails for BCM,Pi4B,CD2,Pin=21 when maker pHAT is used # works for BCM,Pi4B,pHAT,Pin=21 use SW 21 instead of CD2 mode="BOARD" #not "BCM" core="Pi4B" #not "CM4","PI4b","pI400" switch="CD2" #not "CD2" nor "pHAT" Pin=33 # is PPin 40 #PPin=21 #PPin=21 #for Maker pHAT #Purpose: test my device "CD2_Dongle(c)" #By: D@CC on 2023KNov26 #Issue: SW uses RPi.GPIO which is deprecated on RPi 5 #Warning: Ignore RuntimeWarning: . . . in use . . . #Status: HW & SW are both working #Functions Used: initCD2,readCD2 #Packages Used: Desktop/ix/ix_pkg.py #Python Packages Used: sys,time #Future: should use GPIO ZERO SW # Use Phy Pin 40 on the Pi400 # But when also using the Cytron Maker pHAT: # other devices are attached to various pins # so, use Phy Pin 21 as input (with Phy pin 19) # Closing Maker pHAT button 21 makes Phys Pin 40: # "Dark" equivalent to CD2 Dark between 38 & 40 print("WARNING: BCM numbering system must be used with computer module 4") print("progName:",progName,"Pin:",Pin) print("mode:",mode) print("core:",core) print("switch:",switch) print("Pin:",Pin) import RPi.GPIO as GPIO #GPIO.setmode(GPIO.BCM) #initCD2(GPin,mode) #GPIO.cleanup() if mode=="BCM": GPIO.setmode(GPIO.BCM) #BMB numbers #of end if mode=="BOARD": GPIO.setmode(GPIO.BOARD) #BMB numbers #of end #GPIO.setup(PPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Purpose: to sense if 2CD photocells "see" Light or Dark # using an internal pulldown resistor #By D@CC on 2023KNov26 # tested with GPin=21 on a Raspberrry CM4 #Cover the CDs with a thumb to "Make it Dark" print("always works with 2 CD in series (covering both), ") print("works with 1 CD sometimes. . . ") print("CDs from phys pin 38 (with output High).") # GPin 21 if switch=="pHAT": print(" to phys pin 40 (as input with PUD_UP for pHAT)") #if end #if core=="CM4" or core=="pi400": #deprecated if switch=="CD2": print(" to phys pin 40 (as input with PUD_DOWN for CM4 and Pi400)") #if end # GPin 20 #PPout=38 #phys pin 38 #GPIO 20 output #PPin =40 #phys pin 40 #GPIO 21 input #This also works with PPin = 37 (But not PPin=3 nor 19) # It probably works with some other adjacent pin pairs initCD2(Pin,mode,switch) while True: print("Pin:",Pin) if readCD2(Pin,mode,switch): print("Dark") else: print("Light") #if end time.sleep(1) #while end