Uncategorized

ssd1306 program

# scan_i2c_bus_v13.py

# Notes:
#   1. This program has the following functions:
#      1.1 setup one I2C bus
#      1.2 setup a list of 1 or 2 I2C buses, with bus number 0 or 1
#      1.3 setup and scan one or a list of I2C buses and print out the I2C device addresses in hex

# *** Print/Debug Functions ***

def printIntListInHex(intList):
    for integer in intList:
        print(hex(integer), ' ', end = '')
    #print('')        
    return

def padString(string, paddedStringLength):
    leftOver = paddedStringLength - len(string)
    string = string + (' ' * leftOver)
    return string 

# *** I2C Bus Config ***

# *** I2C Bus Dictionary ***
# Notes:
#   1. Two I2C buses can be set up
#   2. The two buses are numbered 0, 1Ў@ 
#   3. The GPIO pins used for the two buses are: 
#        Bus Num 0, SDA GPIO pin = 0, SCL GPIO pin = 1
#        Bus Num 1, SDA GPIO pin = 2, SCL GPIO pin = 3

i2cBusDict = {
    '0' : { 'BusNum'     : 0,
            'NickName'   : 'Alex',
            'SdaPinNum'  : 0,
            'SclPinNum'  : 1,
            'Frequency'  : '100 kHz',
          },
    '1' : { 'BusNum'     : 1,
            'NickName'   : 'Ben',
            'SdaPinNum'  : 2,
            'SclPinNum'  : 3,
            'Frequency'  : '400 kHz',
          },  
    }                  

# I2C Bus Frequency Dictionary ***
# Notes:
#   1. Each I2C bus frequency can be set to 100k¦Л 400kHz, or 1 MHz

frequencyDict = {
    '100 kHz' :  100000,
    '400 kHz' :  400000,
    '1 MHz'   : 1000000,
    }

i2cDevDict = {
    '0' : { 'I2cDevNum'     : 0,
            'I2cBusNum'     : 0,
            'I2cDevAddr'    : 0x23,
            'DevName'       : 'PCF8574 #1',
            'DevNickName'   : 'Amy'
          },
    '1' : { 'I2cDevNum'     : 1,
            'I2cBusNum'     : 1,
            'I2cDevAddr'    : 0x24,
            'DevName'       : 'PCF8574 #2',
            'DevNickName'   : 'Betty'
          },
    '2' : { 'I2cDevNum'     : 2,
            'I2cBusNum'     : 0,
            'I2cDevAddr'    : 0x27,
            'DevName'       : 'LCD2004 #1',
            'DevNickName'   : 'Connie'
          },
    '3' : { 'I2cDevNum'     : 3,
            'I2cBusNum'     : 1,
            'I2cDevAddr'    : 0x22,
            'DevName'       : 'LCD1602 #1',
            'DevNickName'   : 'Daisy'
          },
    '4' : { 'I2cDevNum'     : 3,
            'I2cBusNum'     : 0,
            'I2cDevAddr'    : 0x99,
            'DevName'       : 'SSD1306 #1',
            'DevNickName'   : 'Emily'
          },    
    '5' : { 'I2cDevNum'     : 3,
            'I2cBusNum'     : 1,
            'I2cDevAddr'    : 0x99,
            'DevName'       : 'SSD1806 #2',
            'DevNickName'   : 'Fanny'
          },    
    }

# *** I2C Setup One Bus Function ***
# Notes:
#   1. setupI2CBus(i2cBusNum) sets up either I2C Bus Number 0, or I2C Bus Number 1
#   2. example function call: setupI2cBus(0) sets up I2C number 0
#   3. returns one I2c Bus object

def setupI2cBus(i2cBusNum):
    sdaPinNum = i2cBusDict[str(i2cBusNum)]['SdaPinNum'] 
    sclPinNum = i2cBusDict[str(i2cBusNum)]['SclPinNum']
    sdaPin    = machine.Pin(sdaPinNum)    
    sclPin    = machine.Pin(sclPinNum)
    frequency = frequencyDict[i2cBusDict[str(i2cBusNum)]['Frequency']]     
    i2cBus = machine.I2C(i2cBusNum, sda = sdaPin, scl = sclPin, freq = frequency)
    return i2cBus

# *** I2C Setup A List of One or Two Bus Function ***
# Notes:
#   1. setupI2CBusList(i2cBusNumList) sets up a list of one or two I2C buses
#   2. example function call: setupI2cBus([0, 1]) sets up a list of two I2C buses numbered 0, 1
#   3. returns a list of one or two I2C bus objects

def setupI2cBusList(i2cBusNumList):
    i2cBusList = []
    for i2cBusNum in i2cBusNumList:        
       i2cBus = setupI2cBus(i2cBusNum)
       i2cBusList.append(i2cBus)   
    return i2cBusList

# *** Print I2C Bus and Device Functions ***

def printI2cDevDict():
    dictLength = len(i2cDevDict)
    i2cDevNumList = [i2cDevNum for i2cDevNum in range(dictLength)]
    
    for i2cDevNum in i2cDevNumList:
        print('      i2cDevNum =', i2cDevNum, ' ', end = '')
        
        ljustDeviceName = padString(i2cDevDict[str(i2cDevNum)]['DevName'], 8)
        print(' DeviceName =', ljustDeviceName, end ='')        
        
        ljustNickName = padString(i2cDevDict[str(i2cDevNum)]['DevNickName'], 8)
        print('  NickName =', ljustNickName, end ='')
        
        print('  I2cBusNum =', i2cDevDict[str(i2cDevNum)]['I2cBusNum'],end ='')
        print('  I2cDevAddr =', hex(i2cDevDict[str(i2cDevNum)]['I2cDevAddr']),end ='')         
        
        print('')
    print('')        
    return

def printI2cDevInfo(i2cDevNum):
    print('      i2cDevNum =', i2cDevNum, ' ', end = '')
        
    ljustDeviceName = padString(i2cDevDict[str(i2cDevNum)]['DevName'], 8)
    print(' DeviceName =', ljustDeviceName, end ='')        
        
    ljustNickName = padString(i2cDevDict[str(i2cDevNum)]['DevNickName'], 8)
    print('  NickName =', ljustNickName, end ='')
        
    print('  I2cBusNum =', i2cDevDict[str(i2cDevNum)]['I2cBusNum'],end ='')
    print('  I2cDevAddr =', hex(i2cDevDict[str(i2cDevNum)]['I2cDevAddr']),end ='')         
        
    print('')
    return

def printI2cBusListConfig(i2cBusList):
    #print('\n        I2C Bus List Config, ... ')
    for i2cBus in i2cBusList:
        index = i2cBusList.index(i2cBus)        
        print('      I2C Bus Num =', index, ' ', end = '')
        ljustNickName = padString(i2cBusDict[str(i2cBusList.index(i2cBus))]['NickName'], 8)
        print('Bus nick name =', ljustNickName, end ='')       
        print('Frequency =', i2cBusDict[str(i2cBusList.index(i2cBus))]['Frequency'], ' ', end ='')
        print('sdaPin =', i2cBusDict[str(i2cBusList.index(i2cBus))]['SdaPinNum'], ' ', end ='')
        print('sclPin =', i2cBusDict[str(i2cBusList.index(i2cBus))]['SclPinNum'], ' ', end ='')
        print('')
    return

# *** Print Bus and Device Functions ***

def testSetupI2cBusList():
    print('Begin testSetupI2cBusList(), ...')
    i2cBustList = setupI2cBusList([0, 1])
    print('End   testSetupI2cBusList().\n')
    return i2cBustList

# *** Example function call ***
# testSetupI2cBusList() # Sets up two buses, numbered 0, 1

# *** 
def scanAndPrintI2cBusList(i2cBusList):
    for i2cBus in i2cBusList:
        index = i2cBusList.index(i2cBus)        
        print('      I2C Bus Num =', index, ' ', end = '')
        i2cDeviceList = i2cBus.scan()
        print('I2cDeviceList = ', end = '')
        printIntListInHex(i2cDeviceList)
        print('')
    return

# *** I2C Setup A List of One or Two Bus and scan the devices on the buses ***
# Notes:
#   1. testSetupScanI2cBusList(i2cBusNumList) sets up and scans a list of one or two I2c buses
#   2. example function call: setupI2cBus([0, 1]) sets up a list of I2C buses, scans and prints the devices addresses in hex

def testSetupScanPrintI2cBusListV01():
    print('>>>>>>>>>> Begin testScanI2cBusList(), ... >>>>>>>>>>\n')  

    print('  *** Set up I2C bus list [i2cBus0, i2cBus1] ***\n')
    i2cBusList = setupI2cBusList([0, 1])
    
    print('  *** I2C Bus List Config ***')     
    printI2cBusListConfig(i2cBusList)
    print('')     
    
    print('  *** I2c Device Dict ***')    
    printI2cDevDict()
    
    print('  *** Scan and print I2C devices I2C bus list [i2cBus0, i2cBus1] ***')
    scanAndPrintI2cBusList(i2cBusList)
    print('\n>>>>>>>>>> End   testScanI2cBusList(). >>>>>>>>>>\n')    
    return

# *** Main Tests ***

#testSetupI2cBusList() # Sets up two buses, numbered 0, 1
testSetupScanPrintI2cBusListV01()

# *** End ***

'''
>>> %Run -c $EDITOR_CONTENT
>>>>>>>>>> Begin testScanI2cBusList(), ... >>>>>>>>>>

  *** Set up I2C bus list [i2cBus0, i2cBus1] ***

  *** I2C Bus List Config ***
      I2C Bus Num = 0  Bus nick name = Alex    Frequency = 100 kHz  sdaPin = 0  sclPin = 1  
      I2C Bus Num = 1  Bus nick name = Ben     Frequency = 400 kHz  sdaPin = 2  sclPin = 3  

  *** I2c Device Dict ***
      i2cDevNum = 0   DeviceName = PCF8574 #1  NickName = Amy       I2cBusNum = 0  I2cDevAddr = 0x23
      i2cDevNum = 1   DeviceName = PCF8574 #2  NickName = Betty     I2cBusNum = 1  I2cDevAddr = 0x24
      i2cDevNum = 2   DeviceName = LCD2004 #1  NickName = Connie    I2cBusNum = 0  I2cDevAddr = 0x27
      i2cDevNum = 3   DeviceName = LCD1602 #1  NickName = Daisy     I2cBusNum = 1  I2cDevAddr = 0x22
      i2cDevNum = 4   DeviceName = SSD1803 #1  NickName = Emily     I2cBusNum = 0  I2cDevAddr = 0x99
      i2cDevNum = 5   DeviceName = SSD1803 #2  NickName = Fanny     I2cBusNum = 1  I2cDevAddr = 0x99

  *** Scan and print I2C devices I2C bus list [i2cBus0, i2cBus1] ***
      I2C Bus Num = 0  I2cDeviceList = 0x23  
      I2C Bus Num = 1  I2cDeviceList = 0x24  

>>>>>>>>>> End   testScanI2cBusList(). >>>>>>>>>>

>>>
'''


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.