import sys, time
#sys.path.append("/home/pi/Desktop/IX_assets/ix")
###########################

def initCD2(PPin):
    import RPi.GPIO as GPIO
    #By D@CC on 2023KNov01
    #PPin-2 is PPout  (tested with PPin = 40)
    GPIO.setmode(GPIO.BOARD) #physical Board numbers
    #GPIO.setmode(GPIO.BMB)   #BroadCoM numbering eg GPIO #s
    print("PPin-2:",PPin-2)
    isBadPin= ((PPin-2==1) or (PPin-2==17))  # they are 3v3
    # Ground pins won't work either, nor 5v0 pins
    print("isBadPin:",isBadPin)
    if not isBadPin:
        GPIO.setup(PPin-2,GPIO.OUT)
        print("turn On phys pin:", PPin-2)
        GPIO.output(PPin-2, True)
    #if end

    # 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(PPin):
    import RPi.GPIO as GPIO
    #By D@CC on 2023KNov01

    #Purpose: to return True if both CDs are covered
    #HW: my device "CD2_Dongle(c)"
    #Tested with PPin=40
    GPIO.setmode(GPIO.BOARD) #physical Board numbers
    GPIO.setup(PPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    reading=GPIO.input(PPin)
    # reading is True if the CD photoCells are Dark
    # Make them dark by covering them with a finger
    if reading:                                                                                                                                                                                                    
        state=False
    else:
        state=True
    #if end
    return state
#def end

progName="Test_readCD2works.py"
PPin=40	#works as GPIO21 on Maker pHAT
#PPin=21  #for Maker pHAT
#Purpose: test my device "CD2_Dongle(c)"
#By: D@CC on 2023KNov01
#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("progName:",progName,"Phys Pin:",PPin)
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD) #physical Board numbers																																																		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 2023JOct30
# tested with PPin=40 on a Raspberrry Pi400
#Cover the CDs with a thumb to "Make it Dark"
print("uses RPi.GPIO (fails on RPi 5B)")
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).")
#                        19
print("  to phys pin 40 (as input with PUD_DOWN)")
#                    21
#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(PPin)
while True:
    print("PPin:",PPin)
    if readCD2(PPin):
        print("Dark")
    else:
        print("Light")
    #if end
    time.sleep(1)
#while end
#/Test_readCD2works.py.txt