Uncategorized

sysutilv27.py tlfong01 2021mar13hkt2303

# sysutilv27.py tlfong01 2021mar13hkt2303
# Piromoni Tiny2040

# *** Contents ***
# 1. Import
# 2. Logical/time/repetition variable/constant declarations
# 3. Digital Output / LED / Mechanical Buzzer
#    testOnBoardLedsV01()
#    testMechBuzzerV01()
# 4. Digital Input Pins
#    testReadBootSelectButtonV01()
# 5. DS18B20 Temperature Sensor
#    testReadTemperatureV01()

# ========== ========== ========== ========== ========== ========== ========== ========== 
# *** 1. Import  ***
import time

# ========== ========== ========== ========== ========== ========== ========== ========== 
# *** 2. 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

# ========== ========== ========== ========== ========== ========== ========== ========== 
# *** 3. Digital Output / LED / Mechanical Buzzer ***

# *** Config ***
from machine import Pin

# On baord RGB led pin num ***
OnBoardRedLedPinNum     = 18
OnBoardGreenLedPinNum   = 19
OnBoardBlueLedPinNum    = 20

OnBoardRgbLedPinNumList = [OnBoardRedLedPinNum, OnBoardGreenLedPinNum, OnBoardBlueLedPinNum]

# *** Electromechanical Buzzer Pin ***
MechBuzzerPinNum        = 28

def initDigiOutPinNum(digiOutPinNum, initVal):
    # *** Setup pinnum to digi output mode and initialize pin to initVal *** 
    #print('Begin initDigiOutPinNum(), ...')
    digiOutPin = Pin(digiOutPinNum, Pin.OUT)
    digiOutPin.value(initVal)
    #print('End   initDigiOutPinNum().')
    return digiOutPin

def initDigiOutPinNumList(digiOutPinNumList, initVal):
    # *** Setup pinnum list to digi output mode and initialize pins to initVal ***
    # *** Return digiOutPinList ***
    #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):
    # *** Toggle digiOutPin once ***
    #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):
    # *** Toggle digiOutPin many times ***
    #print('Begin toggleDigiOutPinManyTimes(), ...')
    for i in range(repeatTimes):
        toggleDigiOutPinOnce(digiOutPin, initVal, highTime, lowTime)
    #print('End toggleDigiOutPinManyTimes(), ...')    
    return

def toggleDigiOutPinListManyTimes(digiOutPinList, initVal, highTime, lowTime, repeatTimes):
    # *** Toggle digiOutPin list many times ***    
    #print('Begin toggleDigiOutpPinList(),...') 
    for count in range(repeatTimes):        
        for digiOutPin in digiOutPinList:
            toggleDigiOutPinOnce(digiOutPin, initVal, highTime, lowTime)

    #print('End   toggleDigiOutpPinList().')           
    return

# *** Test On Board LEDs and Mechanical Buzzer ***
def testOnBoardLedsV01():
    onBoardRgbLedPinList = initDigiOutPinNumList(OnBoardRgbLedPinNumList, HighVal)
    toggleDigiOutPinListManyTimes(onBoardRgbLedPinList, HighVal, 0.25, 0.25, 2)
    return

def testMechBuzzerV01():
    mechBuzzerPin = initDigiOutPinNum(MechBuzzerPinNum, LowVal)
    toggleDigiOutPinManyTimes(mechBuzzerPin, LowVal, 0.1, 0.5, 2)
    return

# ========== ========== ========== ========== ========== ========== ========== ==========
# *** 4. Digital Input Pins *** 

# *** Button state varaibles and constants ***
ButtonReleased = True
ButtonPressed  = False

# *** Boot/Select button pin config ***

bootSelectButtonPinNum = 23
bootSelectButtonPin    = Pin(bootSelectButtonPinNum, Pin.IN, Pin.PULL_DOWN)

# *** Boot/Select Button Pin Functions ***
def readBootSelectButtonPin():
    print('\nBegin readBootSelectButton(),...')    
    if bootSelectButtonPin.value() == ButtonReleased:
        buttonStatus = ButtonReleased
    else:
        buttonStatus = ButtonPressed
    print('  Button status (Relesed) =', buttonStatus)    
    print('End   readBootSelectButton(),...')        
    return buttonStatus

def testReadBootSelectButtonV01():
    print('\nBegin testReadBootSelectButton(),...')    
    print('  Press and hold at least 0.5 second, <Ctrl F2> to exit program.')    
    for count in range(10):
        print('    count =', count, end = '')
        if bootSelectButtonPin.value() == ButtonReleased:
            print('  Button not released, ...')
        else:
            print('  Button     pressed, ...')
        time.sleep(1)
    print('End   testReadBootSelectButton(),...')        
    return

# *** Sample Output ***
# testReadBootSelectButtonV01()

'''
MicroPython v1.14 on 2021-03-06; Raspberry Pi Pico with RP2040

Type "help()" for more information.
>>> %Run -c $EDITOR_CONTENT

Begin testReadBootSelectButton(),...
  Press and hold at least 0.5 second, <Ctrl F2> to exit program.
    count = 0  Button not released, ...
    count = 1  Button not released, ...
    count = 2  Button not released, ...
    count = 3  Button     pressed, ...
    count = 4  Button     pressed, ...
    count = 5  Button not released, ...
    count = 6  Button     pressed, ...
    count = 7  Button not released, ...
    count = 8  Button     pressed, ...
    count = 9  Button not released, ...
End   testReadBootSelectButton(),...
>>>
'''

# ========== ========== ========== ========== ========== ========== ========== ==========
# *** DS18B20 Temperature Sensor ***

# *** DS18B20 Config ***

import onewire, ds18x20

tempSensorPinNum = 29
tempSensorPin    = machine.Pin(tempSensorPinNum)
tempSensor       = ds18x20.DS18X20(onewire.OneWire(tempSensorPin))

# *** Functions ***

def testReadTemperatureV01():
    print('Begin testReadTemperature(), ...')    
    idCodeList = tempSensor.scan()
    print('  Temp Sensor DS18B20: ', idCodeList)

    for secondCount in range(4):
        print('    Second ', secondCount, ' ', end = '')    
        tempSensor.convert_temp()
        time.sleep_ms(750)
        for idCode in idCodeList:
            print(' ', '%.2f' % tempSensor.read_temp(idCode), end ='')
            time.sleep(1)
        print('')
    print('End   testReadTemperature(), ...')         
    return

# *** Sample Output of testReadTemperature() ***
'''
>>> %Run -c $EDITOR_CONTENT
Begin testReadTemperature(), ...
  Temp Sensor DS18B20:  [bytearray(b'(\xeb8y\xa2\x16\x03S'), bytearray(b'(\xc3&y\xa2\x16\x03\x9a')]
    Second  0    24.75  26.00
    Second  1    24.75  25.94
    Second  2    24.75  25.94
    Second  3    24.75  26.00
End   testReadTemperature(), ...
>>>
'''

# ========== ========== ========== ========== ========== ========== ========== ==========
# *** Main ***

#testOnBoardLedsV01()
#testMechBuzzerV01()
#testReadBootSelectButtonV01()
testReadTemperatureV01()

# ========== ========== ========== ========== ========== ========== ========== ==========
# End ***

# ========== ========== ========== ========== ========== ========== ========== ==========
# *** Sample Output - tlfong01  2021feb10hkt2043 ***

'''

'''
# *** End of sample output ***
# ========== ========== ========== ========== ========== ========== ========== ==========

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.