# digi_ana_io_v22 tlfong01 2021mar11hkt2238
# Piromoni Tiny2040
# *** Contents ***
# 01 Import
# 02 DigiOutput pins
# 03 DigiInput pins
# 04 AnaOutput pins
# 05 AnaInput pins
# *** Import ***
from machine import Pin
import time
# *** Logical/time/repetition variable/constant declarations ***
LowVal = 0
HighVal = 1
OnVal = LowVal # On board RGB led = Low active
OffVal = HighVal
HighTime = 0.5 # seconds
LowTime = 0.5 # seconds
OnTime = LowTime # Active Low Led on time
OffTime = HighTime # Active Low Led off time
RepeatTimes = 2 # default repeat twice
# *** Digital output/LED pin configuration ***
# Tiny2040 On baord RGB led pin num ***
OnBoardRedLedPinNum = 18
OnBoardGreenLedPinNum = 19
OnBoardBlueLedPinNum = 20
OnBoardRgbLedPinNumList = [OnBoardRedLedPinNum, OnBoardGreenLedPinNum, OnBoardBlueLedPinNum]
MechBuzzerPinNum = 28
# ========== ========== ========== ========== ========== ========== ========== ==========
# ========== ========== ========== ========== ========== ========== ========== ==========
# *** Digital ouput Led pin functions ***
def initDigiOutPinNum(digiOutPinNum, initVal):
#print('Begin initDigiOutPinNum(), ...')
digiOutPin = Pin(digiOutPinNum, Pin.OUT)
digiOutPin.value(initVal)
#print('End initDigiOutPinNum().')
return digiOutPin
def initDigiOutPinNumList(digiOutPinNumList, initVal):
#print('Begin initDigiOutPinNumList(), ...')
digiOutPinList = []
for digiOutPinNum in digiOutPinNumList:
digiOutPin = initDigiOutPinNum(digiOutPinNum, initVal)
digiOutPinList.append(digiOutPin)
#print('End initDigiOutPinNumList().')
return digiOutPinList
def toggleDigiOutPinOnce(digiOutPin, initVal, highTime, lowTime):
#print('Begin toggleDigiOutPinOnce(), ...')
#print(initVal, highTime, lowTime)
if initVal == LowVal:
digiOutPin.value(HighVal)
time.sleep(highTime)
digiOutPin.value(LowVal)
time.sleep(lowTime)
else:
digiOutPin.value(LowVal)
time.sleep(highTime)
digiOutPin.value(HighVal)
time.sleep(lowTime)
#print('End toggleDigiOutPinOnce(), ...')
return
def toggleDigiOutPinManyTimes(digiOutPin, initVal, highTime, lowTime, repeatTimes):
#print('Begin toggleDigiOutPinManyTimes(), ...')
for i in range(repeatTimes):
toggleDigiOutPinOnce(digiOutPin, initVal, highTime, lowTime)
#print('End toggleDigiOutPinManyTimes(), ...')
return
def toggleDigiOutPinList(digiOutPinList, initVal, highTime, lowTime, repeatTimes):
#print('Begin toggleDigiOutpPinList(),...')
for count in range(repeatTimes):
for digiOutPin in digiOutPinList:
toggleDigiOutPinOnce(digiOutPin, initVal, highTime, lowTime)
#print('End toggleDigiOutpPinList().')
return
# ========== ========== ========== ========== ========== ========== ========== ==========
# ========== ========== ========== ========== ========== ========== ========== ==========
# *** Digital Input Pins ***
# *** Boot/Select button pin config ***
# *** Button state varaibles and constants ***
buttonNotPressed = True
buttonPressed = False
# *** Button config ***
bootSelectPinNum = 23
bootSelectButtonPin = Pin(bootSelectPinNum, Pin.IN, Pin.PULL_DOWN)
def readBootSelectButton():
print('\nBegin readBootSelectButton(),...')
print(' Press and hold 0.5 second Button pin 14 to toggle LED pin 15, <Ctrl F2> to exit program.')
for count in range(10):
print(' count =', count, end = '')
if bootSelectButtonPin.value() == buttonNotPressed:
print(' Button not pressed, ...')
else:
#led.toggle()
print(' Button pressed, ...')
time.sleep(1)
print('End readBootSelectButton(),...')
return
# *** Old Tests ***
# *** Toggle Mech Buzzer ***
#mechBuzzerPin = initDigiOutPinNum(MechBuzzerPinNum, LowVal)
#toggleDigiOutPinOnce(mechBuzzerPin, LowVal, 0.25, 0.5)
#toggleDigiOutPinOnce(mechBuzzerPin, LowVal, 0.25, 0.5)
#toggleDigiOutPinManyTimes(mechBuzzerPin, LowVal, 0.25, 0.5, 4)
# *** Toggle On Board Leds ***
#onBoardRedLedPin = initDigiOutPinNum(OnBoardRedLedPinNum, HighVal)
#toggleDigiOutPinOnce(onBoardRedLedPin, HighVal, 0.25, 0.5)
#toggleDigiOutPinOnce(onBoardRedLedPin, HighVal, 0.25, 0.5)
#toggleDigiOutPinManyTimes(onBoardRedLedPin, LowVal, 0.25, 0.5, 4)
#onBoardRgbLedPinList = initDigiOutPinNumList(OnBoardRgbLedPinNumList, HighVal)
#toggleDigiOutPinManyTimes(onBoardRgbLedPinList[0], HighVal, 0.25, 0.5, 4)
#toggleDigiOutPinList(onBoardRgbLedPinList, HighVal, 0.25, 0.25, 4)
# *** main ***
onBoardRgbLedPinList = initDigiOutPinNumList(OnBoardRgbLedPinNumList, HighVal)
toggleDigiOutPinList(onBoardRgbLedPinList, HighVal, 0.25, 0.25, 2)
mechBuzzerPin = initDigiOutPinNum(MechBuzzerPinNum, LowVal)
toggleDigiOutPinManyTimes(mechBuzzerPin, LowVal, 0.25, 0.5, 4)
#readBootSelectButton()
# End ***
# *** Sample Output - tlfong01 2021feb10hkt2043 ***
'''
'''
# *** End of sample output ***
Categories: Uncategorized