Uncategorized

MicroPython ssd1306.py v0.1

tl 

MicroPython ssd1306.py v0.1

Sun. 4/4/2021

by tl fong01

ssd1306.py listing tlfong01 2021apr04hkt1626

https://penzu.com/p/0bcf4e5b

# Reference: 
# 1. GitHub
#    https://github.com/micropython/micropython/blob/master/drivers/display/ssd1306.py
# 2. Tom’s Hardware Tutorial – 
#    How to Use an OLED Display With Raspberry Pi Pico – Les Pounder 2021feb28
#    https://www.tomshardware.com/how-to/oled-display-raspberry-pi-pico
# 3. How to install
#   Rpi pico micropython ssd1306 OSError: 5 – Zeno 2021apr04
#   https://raspberrypi.stackexchange.com/questions/123791/rpi-pico-micropython-ssd1306-

#     oserror-5/123795#123795
#   Appendix B – Installing Rpi Pico MicroPython OLED SSD1306 Library – Tom’s Hardware
#   (6) How to Use an OLED Display With Raspberry Pi Pico – Les Pounder, Tom’s Hardware, 2021feb28
#   Click on Tools > Manage Packages to open Thonny’s package manager for Python libraries.
#   Type “ssd1306” in the search bar and click “Search on PyPI”.
#   Click on “micropython-ssd1306” in the returned results and then click on Install.

#     This will copy the library to a folder, lib on the Pico.
#   Click Close to return to the main interface.
# 4. Where is file ssd1306.py located
#    Thonny IDE /lib

# ************************************************************************************************

# MicroPython SSD1306 OLED driver, I2C and SPI interfaces

from micropython import const
import framebuf

# register definitions
SET_CONTRAST = const(0x81)
SET_ENTIRE_ON = const(0xA4)
SET_NORM_INV = const(0xA6)
SET_DISP = const(0xAE)
SET_MEM_ADDR = const(0x20)
SET_COL_ADDR = const(0x21)
SET_PAGE_ADDR = const(0x22)
SET_DISP_START_LINE = const(0x40)
SET_SEG_REMAP = const(0xA0)
SET_MUX_RATIO = const(0xA8)
SET_COM_OUT_DIR = const(0xC0)
SET_DISP_OFFSET = const(0xD3)
SET_COM_PIN_CFG = const(0xDA)
SET_DISP_CLK_DIV = const(0xD5)
SET_PRECHARGE = const(0xD9)
SET_VCOM_DESEL = const(0xDB)
SET_CHARGE_PUMP = const(0x8D)

# Subclassing FrameBuffer provides support for graphics primitives
# http://docs.micropython.org/en/latest/pyboard/library/framebuf.html
 

class SSD1306(framebuf.FrameBuffer):
    def __init__(self, width, height, external_vcc):
        self.width = width
        self.height = height
        self.external_vcc = external_vcc
        self.pages = self.height // 8
        self.buffer = bytearray(self.pages * self.width)
        super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB)
        self.init_display()

    def init_display(self):
        for cmd in (
            SET_DISP | 0x00,  # off
            # address setting
            SET_MEM_ADDR,
            0x00,  # horizontal
            # resolution and layout
            SET_DISP_START_LINE | 0x00,
            SET_SEG_REMAP | 0x01,  # column addr 127 mapped to SEG0
            SET_MUX_RATIO,
            self.height – 1,
            SET_COM_OUT_DIR | 0x08,  # scan from COM[N] to COM0
            SET_DISP_OFFSET,
            0x00,
            SET_COM_PIN_CFG,
            0x02 if self.width > 2 * self.height else 0x12,
            # timing and driving scheme
            SET_DISP_CLK_DIV,
            0x80,
            SET_PRECHARGE,
            0x22 if self.external_vcc else 0xF1,
            SET_VCOM_DESEL,
            0x30,  # 0.83*Vcc
            # display
            SET_CONTRAST,
            0xFF,  # maximum
            SET_ENTIRE_ON,  # output follows RAM contents
            SET_NORM_INV,  # not inverted
            # charge pump
            SET_CHARGE_PUMP,
            0x10 if self.external_vcc else 0x14,
            SET_DISP | 0x01,
        ):  # on
            self.write_cmd(cmd)
        self.fill(0)
        self.show()

    def poweroff(self):
        self.write_cmd(SET_DISP | 0x00)

    def poweron(self):
        self.write_cmd(SET_DISP | 0x01)

    def contrast(self, contrast):
        self.write_cmd(SET_CONTRAST)
        self.write_cmd(contrast)

    def invert(self, invert):
        self.write_cmd(SET_NORM_INV | (invert & 1))

    def show(self):
        x0 = 0
        x1 = self.width – 1
        if self.width == 64:
            # displays with width of 64 pixels are shifted by 32
            x0 += 32
            x1 += 32
        self.write_cmd(SET_COL_ADDR)
        self.write_cmd(x0)
        self.write_cmd(x1)
        self.write_cmd(SET_PAGE_ADDR)
        self.write_cmd(0)
        self.write_cmd(self.pages – 1)
        self.write_data(self.buffer)


class SSD1306_I2C(SSD1306):
    def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False):
        self.i2c = i2c
        self.addr = addr
        self.temp = bytearray(2)
        self.write_list = [b”\x40″, None]  # Co=0, D/C#=1
        super().__init__(width, height, external_vcc)

    def write_cmd(self, cmd):
        self.temp[0] = 0x80  # Co=1, D/C#=0
        self.temp[1] = cmd
        self.i2c.writeto(self.addr, self.temp)

    def write_data(self, buf):
        self.write_list[1] = buf
        self.i2c.writevto(self.addr, self.write_list)


class SSD1306_SPI(SSD1306):
    def __init__(self, width, height, spi, dc, res, cs, external_vcc=False):
        self.rate = 10 * 1024 * 1024
        dc.init(dc.OUT, value=0)
        res.init(res.OUT, value=0)
        cs.init(cs.OUT, value=1)
        self.spi = spi
        self.dc = dc
        self.res = res
        self.cs = cs
        import time

        self.res(1)
        time.sleep_ms(1)
        self.res(0)
        time.sleep_ms(10)
        self.res(1)
        super().__init__(width, height, external_vcc)

    def write_cmd(self, cmd):
        self.spi.init(baudrate=self.rate, polarity=0, phase=0)
        self.cs(1)
        self.dc(0)
        self.cs(0)
        self.spi.write(bytearray([cmd]))
        self.cs(1)

    def write_data(self, buf):
        self.spi.init(baudrate=self.rate, polarity=0, phase=0)
        self.cs(1)
        self.dc(1)
        self.cs(0)
        self.spi.write(buf)
        self.cs(1)

.END

==========================================================================

Pico Scan I2C Busses v0.1  tlfong01  2021apr04hkt1459

https://penzu.com/p/0bcf4e5b

# pcf8574_v01_2021apr0403.py
# *** iox_pcf8571_v34 tlfong01 2021mar01hkt1616
# Thonny 3.3.3 MicroPython (Raspberry Pi Pico)
# Chinese Windows 10
# References – https://github.com/raspberrypi/pico-micropython-examples

#import machine
from machine import Pin, Timer
import time

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

# Contents
#   Part A – I2C Bus Config/Setup/Test
#   Part B – PCF8571 8-Bit I/O Extender Config/Setup/Test
#   

# *** I2C ***

# *** I2C Bus Dictionary

i2cBusDict = {
    ‘0’ : { ‘BusNum’     : 0,
            ‘NickName’   : ‘Amy’,
            ‘SdaPinNum’  : 0,
            ‘SclPinNum’  : 1,
            ‘Frequency’  : ‘100 kHz’,
          },
    ‘1’ : { ‘BusNum’     : 1,
            ‘NickName’   : ‘Betty’,
            ‘SdaPinNum’  : 2,
            ‘SclPinNum’  : 3,
            ‘Frequency’  : ‘400 kHz’,
          },          
    }                  

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

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

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

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

# *** Test Functions ***

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

def testSetupScanI2cBusList():
    print(‘Begin testSetupScanI2cBusList(), …’)    
    i2cBusList = setupI2cBusList([0, 1])    
    for i2cBus in i2cBusList:
        index = i2cBusList.index(i2cBus)
        print(‘  I2C Bus Num =’, index, ‘ ‘, end = ”)
        ljustNickName = padString(i2cBusDict[str(i2cBusList.index(i2cBus))][‘NickName’], 8)
        print(‘NickName =’, 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 =”)         
        printI2cDeviceList(i2cBus)
    print(‘End   testSetupI2cBusList().\n’)        
    return 

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

# *** PCF8574 8-bit IO Extender ***

# *** IoX Config ***

i2cDevDict = {
    ‘0’ : { ‘I2cDevNum’     : 0,
            ‘I2cBusNum’     : 0,
            ‘I2cDevAddr’    : 0x23,
            ‘DevName’       : ‘PCF8574 #1’,
            ‘DevNickName’   : ‘Connie’
          },
    ‘1’ : { ‘I2cDevNum’     : 1,
            ‘I2cBusNum’     : 1,
            ‘I2cDevAddr’    : 0x24,
            ‘DevName’       : ‘PCF8574 #2’,
            ‘DevNickName’   : ‘Daisy’
          },
    ‘2’ : { ‘I2cDevNum’     : 2,
            ‘I2cBusNum’     : 0,
            ‘I2cDevAddr’    : 0x27,
            ‘DevName’       : ‘LCD2004 #1’,
            ‘DevNickName’   : ‘Emily’
          },
    ‘3’ : { ‘I2cDevNum’     : 3,
            ‘I2cBusNum’     : 1,
            ‘I2cDevAddr’    : 0x22,
            ‘DevName’       : ‘LCD1602 #1’,
            ‘DevNickName’   : ‘Fanny’
          },     
    }

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

# *** IoX Functions ***

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

# *** Iox Test Functions ***

def testIoxDevices():
    printIoxDevices()
    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

def printIoxDevicesOnI2cBusList(i2cBusList): 
    for i2cBus in i2cBusList:
        index = i2cBusList.index(i2cBus)        
        print(‘      I2C Bus Num =’, index, ‘ ‘, end = ”)        
        printI2cDeviceList(i2cBus)
    return

# *** Basic Tests ***

def pcf8574BasicTest01():
    print(‘>>>>>>>>>> Begin testPcf8574(), … >>>>>>>>>>\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   testPcf8574(). >>>>>>>>>>\n’)    
    return

# *** Write/Read I2C Device ***

# *** Write Functions ***

def writeI2cBusDevAddrOneByte(i2cBus, i2cDevAddr, writeByte):
    i2cBus.writeto(i2cDevAddr, bytes([writeByte]))
    return

def writeI2cDevNumOneByte(i2cBusList, i2cDevNum, writeByte):
    i2cBusNum  = i2cDevDict[str(i2cDevNum)][‘I2cBusNum’]
    i2cBus     = i2cBusList[i2cBusNum]
    i2cDevAddr = i2cDevDict[str(i2cDevNum)][‘I2cDevAddr’]
    writeI2cBusDevAddrOneByte(i2cBus, i2cDevAddr, writeByte)
    return

def blinkI2cDevNumByte1Byte2(i2cBusList, i2cDevNum, byte1, byte2, byteTime1, \
                             byteTime2, totalCount):
    i2cBusNum  = i2cDevDict[str(i2cDevNum)][‘I2cBusNum’]
    i2cBus     = i2cBusList[i2cBusNum]
    i2cDevAddr = i2cDevDict[str(i2cDevNum)][‘I2cDevAddr’]
    
    for count in range(totalCount):
        writeI2cBusDevAddrOneByte(i2cBus, i2cDevAddr, byte1)
        time.sleep(byteTime1)
        writeI2cBusDevAddrOneByte(i2cBus, i2cDevAddr, byte2)
        time.sleep(byteTime2)
    writeI2cBusDevAddrOneByte(i2cBus, i2cDevAddr, 0x00)
    #writeI2cBusDevAddrOneByte(i2cBus, i2cDevAddr, 0xff)      
    return

def scanI2cDevNumOneByte(i2cBusList, i2cDevNum, scanByte, scanTime, totalCount):
    i2cBusNum  = i2cDevDict[str(i2cDevNum)][‘I2cBusNum’]
    i2cBus     = i2cBusList[i2cBusNum]
    i2cDevAddr = i2cDevDict[str(i2cDevNum)][‘I2cDevAddr’]
    
    maskByte = 0x01
    for byteCount in range(totalCount):
        for bitCount in range(8):
            newMaskByte = maskByte << bitCount
            newByte = scanByte & newMaskByte
            writeI2cBusDevAddrOneByte(i2cBus, i2cDevAddr, newByte)
            time.sleep(scanTime)
    writeI2cBusDevAddrOneByte(i2cBus, i2cDevAddr, 0x00)    
    return

# *** Read Functions ***

# *** Write/Read Tests ***

def pcf8574WriteBlinkTest01():
    print(‘>>>>>>>>>> Begin   pcf8574WriteTest01(), … >>>>>>>>>>\n’)     
    
    print(‘  *** Set up I2C bus list [i2cBus0, i2cBus1] ***\n’)
    i2cBusList = setupI2cBusList([0, 1])    
    
    print(‘  *** Scan and print I2C devices I2C bus list [i2cBus0, i2cBus1] ***’)
    scanAndPrintI2cBusList(i2cBusList)
        
    #print(‘\n *** Read pcf8574 input pins – to test later ***’)
    #i2cDevNum = 0    
    #readI2cDevNumOneByte(i2cBusList, i2cDevNum)
        #i2cBus = i2cBusList[0]
    #i2cDevAddr = 0x23
    #readByteArray = []
    #readByteArray = i2cBus.readfrom(i2cDevAddr, 1)
    #i2cBus.writeto_mem(76, 6, b’456′)
    #i2cBus.readfrom_mem(76, 6, 4)

    print(‘\n  *** Toggle/Blink pcf8574 output pins/LEDs ***’) 
   
    #print(‘dp 01’)  
    i2cBusList[0].writeto(0x27, bytes([0x55]))
    i2cBusList[0].writeto(0x23, bytes([0x55]))
    i2cBusList[1].writeto(0x22, bytes([0x55]))      
    i2cBusList[1].writeto(0x24, bytes([0x55]))
    
    #print(‘dp 02’)
    writeI2cBusDevAddrOneByte(i2cBusList[0], 0x23, 0xff)    
    writeI2cBusDevAddrOneByte(i2cBusList[1], 0x24, 0xff)
    time.sleep(1)
    writeI2cBusDevAddrOneByte(i2cBusList[0], 0x23, 0x00)    
    writeI2cBusDevAddrOneByte(i2cBusList[1], 0x24, 0x00)
    
    #print(‘dp03’) 
    writeI2cDevNumOneByte(i2cBusList, i2cDevNum = 0, writeByte = 0x55)
    writeI2cDevNumOneByte(i2cBusList, i2cDevNum = 1, writeByte = 0xaa)
    
    #print(‘dp04 blink’)
    byte1 = 0x55
    byte2 = 0xaa
    byte1Time = 0.5
    byte2Time = 0.5
    totalCount = 2
    
    i2cDevNum = 0    
    blinkI2cDevNumByte1Byte2(i2cBusList, i2cDevNum, byte1, byte2, byte1Time, \
                             byte2Time, totalCount)
    i2cDevNum = 1 
    blinkI2cDevNumByte1Byte2(i2cBusList, i2cDevNum, byte1, byte2, byte1Time, \
                             byte2Time, totalCount)

    print(‘\n>>>>>>>>>> End   pcf8574WriteTest01().>>>>>>>>>>’) 
    return

def pcf8574WriteBitTest01():
    print(‘\n>>>>>>>>>> Begin pcf8574WriteBitTest01(). >>>>>>>>>>’)
    
    print(‘  *** Set up I2C bus list [i2cBus0, i2cBus1] ***\n’)
    i2cBusList = setupI2cBusList([0, 1])    
    
    print(‘  *** I2c Device Dict ***’)    
    printI2cDevDict()
    
    print(‘  *** Scan and print I2C devices I2C bus list [i2cBus0, i2cBus1] ***’)
    scanAndPrintI2cBusList(i2cBusList)
    
    print(‘\n  *** Wrtie to i2cDevNum0, i2cDevNum1 ***’)
    writeI2cDevNumOneByte(i2cBusList, i2cDevNum = 0, writeByte = 0x55)
    writeI2cDevNumOneByte(i2cBusList, i2cDevNum = 1, writeByte = 0xaa)
    time.sleep(0.5)
    
    print(‘\n  *** scan bit i2cDevNum0 ***’)
    writeI2cDevNumOneByte(i2cBusList, i2cDevNum = 0, writeByte = 0x00)
    writeI2cDevNumOneByte(i2cBusList, i2cDevNum = 1, writeByte = 0x00)
    time.sleep(0.5)

    i2cDevNum = 0   
    #oldByte = 0x00
    newByte = 0x01
    maskByte = 0x00
    
    for i in range(8):
        #maskByte = newByte
        #newByte = newByte << 1
        writeI2cDevNumOneByteOrMaskByte(i2cBusList, i2cDevNum, newByte, maskByte)
        
        #newByte = newByte | oldByte
        #writeByte = newByte
        #writeI2cDevNumOneByte(i2cBusList, i2cDevNum, writeByte)
        time.sleep(0.5)
        maskByte = newByte | maskByte
        newByte = newByte << 1        
        
    print(‘\n>>>>>>>>>> End   pcf8574WriteBitTest01(). >>>>>>>>>>’) 
    return

def writeI2cDevNumOneByteOrMaskByte(i2cBusList, i2cDevNum, writeByte, maskByte):
    writeByte = writeByte | maskByte
    writeI2cDevNumOneByte(i2cBusList, i2cDevNum, writeByte)
    return

def writeI2cDevNumOneByte(i2cBusList, i2cDevNum, writeByte):
    i2cBusNum  = i2cDevDict[str(i2cDevNum)][‘I2cBusNum’]
    i2cBus     = i2cBusList[i2cBusNum]
    i2cDevAddr = i2cDevDict[str(i2cDevNum)][‘I2cDevAddr’]
    writeI2cBusDevAddrOneByte(i2cBus, i2cDevAddr, writeByte)
    return

def writeI2cDevNumOneByteOrMaskByte(i2cBusList, i2cDevNum, writeByte, maskByte):
    writeByte = writeByte | maskByte
    writeI2cDevNumOneByte(i2cBusList, i2cDevNum, writeByte)
    return

#def setBitHigh(i2cBusList, i2cDevNum, writeByte, bitPosition):

# *** Motor Driver MX1908 Functions ***

i2cMotorDict = {
    ‘0’ : {‘I2cDevNum’    : 0,
           ‘MotorName’    : ‘Gigi’,
           ‘Action’       : {‘MoveForward’  : 0b01,
                             ‘MoveBackward’ : 0b10,
                             ‘Stop’         : 0b00,
                            }
          },
    ‘1’ : {‘I2cDevNum’    : 1,
           ‘MotorName’    : ‘Helen’,
           ‘Action’       : {‘MoveForward’  : 0b01,
                             ‘MoveBackward’ : 0b10,
                             ‘Stop’         : 0b00,
                            }           
          },
    }

i2cDevNameDict = {
    ‘Gigi’  : {‘I2cDevNum’  : 0,
               ‘DeviceName’ : ‘Motor’},
    ‘Helen’ : {‘I2cDevNum’  : 1,
               ‘DeviceName’ : ‘Motor’},   
    }

def writeI2cMotorOneByte(i2cBusList, motorName, actionName):
    i2cDevNum = i2cDevNameDict[motorName][‘I2cDevNum’]
    writeByte = i2cMotorDict[str(i2cDevNum)][‘Action’][actionName]
    writeI2cDevNumOneByte(i2cBusList, i2cDevNum, writeByte)
    print(‘dp1’, ‘i2cDevNum =’, i2cDevNum, ‘actionName =’, actionName, ‘writeByte =’, hex(writeByte))
    return

def pcf8574MotorTest01():
    print(‘\n>>>>>>>>>> Begin pcf8574MotorTest01(). >>>>>>>>>>’)
    
    print(‘  *** Set up I2C bus list [i2cBus0, i2cBus1] ***\n’)
    i2cBusList = setupI2cBusList([0, 1])    
    
    print(‘  *** I2c Device Dict ***’)    
    printI2cDevDict()
    
    print(‘  *** Scan and print I2C devices I2C bus list [i2cBus0, i2cBus1] ***’)
    scanAndPrintI2cBusList(i2cBusList)
    
    print(‘\n  *** Write Motor Test ***’)
    #writeI2cMotorOneByte(i2cBusList, ‘Gigi’, ‘MoveForward’)
    
    i2cDevNum = 0
    motorByte = 0b10
    writeI2cDevNumOneByte(i2cBusList, i2cDevNum, motorByte)
    time.sleep(2)
    
    motorByte = 0b01
    writeI2cDevNumOneByte(i2cBusList, i2cDevNum, motorByte)    
    time.sleep(2)
    
    motorByte = 0b00
    writeI2cDevNumOneByte(i2cBusList, i2cDevNum, motorByte)    
    time.sleep(2) 
    
    print(‘\n>>>>>>>>>> End   pcf8574MotorTest01(). >>>>>>>>>>’)    
    return

# *** test PCF8457 IOX Devices ***

#pcf8574BasicTest01()
#pcf8574WriteBlinkTest01()
#pcf8574WriteBitTest01()
#pcf8574MotorTest01()

# *** Main ***

# *** Scan I2C Bus ***

def scanI2cBuses():
    print(‘Begin scanI2cBuses(), tlfong01  2021apr03hkt1431, …’)
    pcf8574BasicTest01()
    print(‘End   scanI2cBuses(), tlfong01  2021apr03hkt1431, …’)    
    return

scanI2cBuses()

# *** End ***

# *** Sample Output tlfong01  2021apr04hkt1435 ***

”’
>>> %Run -c $EDITOR_CONTENT
Begin scanI2cBuses(), tlfong01  2021apr03hkt1431, …
>>>>>>>>>> Begin testPcf8574(), … >>>>>>>>>>

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

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

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

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

>>>>>>>>>> End   testPcf8574(). >>>>>>>>>>

End   scanI2cBuses(), tlfong01  2021apr03hkt1431, …
>>>
”’

##################################################################################################################
”’
MicroPython v1.14 on 2021-02-02; Raspberry Pi Pico with RP2040
Type “help()” for more information.
>>> %Run -c $EDITOR_CONTENT
Begin scanI2cBuses(), tlfong01  2021apr03hkt1431, …
>>>>>>>>>> Begin testPcf8574(), … >>>>>>>>>>

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

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

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

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

>>>>>>>>>> End   testPcf8574(). >>>>>>>>>>

End   scanI2cBuses(), tlfong01  2021apr03hkt1431, …
>>> 
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
MicroPython v1.14 on 2021-02-02; Raspberry Pi Pico with RP2040
Type “help()” for more information.
>>> %Run -c $EDITOR_CONTENT
Begin scanI2cBuses(), tlfong01  2021apr03hkt1431, …
>>>>>>>>>> Begin testPcf8574(), … >>>>>>>>>>

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

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

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

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

>>>>>>>>>> End   testPcf8574(). >>>>>>>>>>

End   scanI2cBuses(), tlfong01  2021apr03hkt1431, …
>>> 
”’

Penzu

Journals

Diaries

Copyright Penzu Inc. 2021×

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.