Uncategorized

sysutilv26.py tlfong01 2021mar13hkt1704

# sysutilv26.py tlfong01 2021mar13hkt1704
# Piromoni Tiny2040

# *** Contents ***
# 01 Import

# *** Import  ***
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

ButtonReleased = True
ButtonPressed  = False

RepeatTimes    = 2 # default repeat twice

# ========== ========== ========== ========== ========== ========== ========== ========== 

# *** 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

# ========== ========== ========== ========== ========== ========== ========== ==========

# *** DS18B20 Temperature Sensor ***

# *** DS18B20 Config ***

import onewire, ds18x20

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

# *** Functions ***

def testReadTemperature():
    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(), ...
>>>
'''
# ========== ========== ========== ========== ========== ========== ========== ==========

# *** 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 ***

# *** Read DS18B20 temperature sensor twice ***

#readBootSelectButton()

# *** Main ***

#testOnBoardLedsV01()
#testMechBuzzerV01()

testReadTemperature()

# 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 )

Google photo

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

Twitter picture

You are commenting using your Twitter 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.