GROVE definition of i2c

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
blakebr
 
Posts: 987
Joined: Tue Apr 17, 2012 6:23 pm

GROVE definition of i2c

Post by blakebr »

Good Morning,

I have tried several ways to do the following and failed miserably.
This does not work.

Code: Select all

GROVE4 = (board.GP7, board.GP6)
i2c = io.I2C(GROVE4)
I must use individual:

Code: Select all

i2c = io.I2C(board.GP7, board.GP6)
I am using the Maker Pi Pico with my Raspberry Pi Pico and Maker Pi RP2040.
I want to be able to set up something like this in a library then pick what I want with just one label.

Code: Select all

        GROVE1 = (board.GP1,  board.GP0); GR = "GR-1"
        GROVE2 = (board.GP3,  board.GP2); GR = "GR-2" # 1  GND
        GROVE3 = (board.GP5,  board.GP4); GR = "GR-3" # 2 +3V3
        GROVE4 = (board.GP7,  board.GP6); GR = "GR-4" # 3 Even Pin SDAn
        GROVE5 = (board.GP9,  board.GP8); GR = "GR-5" # 4 Odd  Pin SCLn
        GROVE6 = (board.GP27, board.GP26); GR = "GR-6"
How do I do what I want to do?

Bruce

User avatar
dastels
 
Posts: 15820
Joined: Tue Oct 20, 2015 3:22 pm

Re: GROVE definition of i2c

Post by dastels »

Something like this will probably do the job:

Code: Select all

i2c_pins = GROVE4 #for example... putting them in an array will probably be more useful
i2c = io.I2C(i2c_pins[0], i2c_pins[1])
Dave

User avatar
blakebr
 
Posts: 987
Joined: Tue Apr 17, 2012 6:23 pm

Re: GROVE definition of i2c

Post by blakebr »

Dave,

Your code worked. Thank you.

This is my selection code.

Code: Select all

# define and select i2c GROVE port(s)
GROVE1 = False # (board.GP1,  board.GP0)
GROVE2 = False # (board.GP3,  board.GP2) # 1  GND
GROVE3 = False # (board.GP4,  board.GP4) # 2 +3V3
GROVE4 = True  # (board.GP7,  board.GP6) # 3 Even Pin SDAn
GROVE5 = False # (board.GP9,  board.GP8) # 4 Odd  Pin SCLn
GROVE6 = False # (board.GP27, board.GP26)
This is the implementation code.

Code: Select all

        global i2c
        if   GROVE1: GROVEn = (board.GP1,  board.GP0); GR = "GR-1"
        elif GROVE2: GROVEn = (board.GP3,  board.GP2); GR = "GR-2" # 1  GND
        elif GROVE3: GROVEn = (board.GP5,  board.GP4); GR = "GR-3" # 2 +3V3
        elif GROVE4: GROVEn = (board.GP7,  board.GP6); GR = "GR-4" # 3 Even Pin SDAn
        elif GROVE5: GROVEn = (board.GP9,  board.GP8); GR = "GR-5" # 4 Odd  Pin SCLn
        elif GROVE6: GROVEn = (board.GP27, board.GP26); GR = "GR-6"
        i2c = io.I2C(GROVEn[0], GROVEn[1])
Bruce

User avatar
dastels
 
Posts: 15820
Joined: Tue Oct 20, 2015 3:22 pm

Re: GROVE definition of i2c

Post by dastels »

You could simplify things if you used an integer in place of the 6 booleans. E.g. your selection code would become:

Code: Select all

pins = [(),         # to start the pin pairs at 1 top correspond to the numbering in your code
        (board.GP1,  board.GP0), 
        (board.GP3,  board.GP2),  # 1  GND
        (board.GP5,  board.GP4),  # 2 +3V3
        (board.GP7,  board.GP6),  # 3 Even Pin SDAn
        (board.GP9,  board.GP8);, # 4 Odd  Pin SCLn
        (board.GP27, board.GP26)
       ]

grove = 4
And the implementation code would be:

Code: Select all

i2c = io.I2C(pins[grove][0], pins[grove][1])
GR = "GR-{0}".format(grove)
Dave

User avatar
blakebr
 
Posts: 987
Joined: Tue Apr 17, 2012 6:23 pm

Re: GROVE definition of i2c

Post by blakebr »

Dave,

That is so much more elegant. Thank you.

Bruce

User avatar
dastels
 
Posts: 15820
Joined: Tue Oct 20, 2015 3:22 pm

Re: GROVE definition of i2c

Post by dastels »

Simplifying/removing logic and replacing it with simple data structures will tend to make the code more elegant, error proof., and extendable. Now to add another set of pins you just need to add a tuple to the array. To reorder them you just edit the array definition. Any time you have to change code, you risk introducing an error.

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”