Uncategorized

sysutil29.py

# sysutilv29.py tlfong01 2021mar14hkt1547
# Piromoni Tiny2040

# *** Contents ***
# 1. Import
# 2. Logical/time/repetition variable/constant declarations
# 3. Digital Output / LED / Mechanical Buzzer
#    testOnBoardLedsV01()
#    testMechBuzzerV01()
#    testPiezoBuzzerMusicV1()
# 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

# ========== ========== ========== ========== ========== ========== ========== ========== 
# *** Piezo Buzzer ***

from machine import PWM

# *** Config ***

piezoBuzzerPinNum = 27
piezoBuzzerPin    = Pin(piezoBuzzerPinNum)
piezoBuzzer       = PWM(piezoBuzzerPin)

# *** Tones ***

tones = { "B0": 31,  "C1": 33, "CS1": 35, "D1": 37, "DS1": 39, "E1": 41, "F1": 44, "FS1": 46,
          "G1": 49, "GS1": 52, "A1": 55, "AS1": 58, "B1": 62, "C2": 65, "CS2": 69, "D2": 73,
          "DS2": 78, "E2": 82, "F2": 87, "FS2": 93, "G2": 98, "GS2": 104, "A2": 110, "AS2": 117,
          "B2": 123, "C3": 131, "CS3": 139, "D3": 147, "DS3": 156, "E3": 165, "F3": 175,
          "FS3": 185, "G3": 196, "GS3": 208, "A3": 220, "AS3": 233, "B3": 247, "C4": 262, "CS4": 277,
          "D4": 294, "DS4": 311, "E4": 330, "F4": 349, "FS4": 370, "G4": 392, "GS4": 415, "A4": 440,
          "AS4": 466, "B4": 494, "C5": 523, "CS5": 554, "D5": 587, "DS5": 622, "E5": 659, "F5": 698,
          "FS5": 740, "G5": 784, "GS5": 831, "A5": 880, "AS5": 932, "B5": 988, "C6": 1047, "CS6": 1109,
          "D6": 1175, "DS6": 1245, "E6": 1319, "F6": 1397, "FS6": 1480, "G6": 1568, "GS6": 1661,
          "A6": 1760, "AS6": 1865, "B6": 1976, "C7": 2093, "CS7": 2217, "D7": 2349, "DS7": 2489,
          "E7": 2637, "F7": 2794, "FS7": 2960, "G7": 3136, "GS7": 3322, "A7": 3520, "AS7": 3729,
          "B7": 3951, "C8": 4186, "CS8": 4435, "D8": 4699, "DS8": 4978
        }

# *** Songs ***

song01 = ["E5","G5","A5","P","E5","G5","B5","A5","P","E5","G5","A5","P","G5","E5"]

# *** Functions ***

def playtone(frequency):
    piezoBuzzer.duty_u16(1000)
    piezoBuzzer.freq(frequency)
    return

def bequiet():
    piezoBuzzer.duty_u16(0)
    return    

def playsong(mysong):
    for i in range(len(mysong)):
        if (mysong[i] == "P"):
            bequiet()
        else:
            playtone(tones[mysong[i]])
        time.sleep(0.3)
    bequiet()
    return

# *** Test Piezo Buzzer ***

def testPiezoBuzzerMusicV01():
    print('Begin playSong(), ...')
    playsong(song01)
    print('End   playSong().')
    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()
testPiezoBuzzerMusicV01()

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