Due to high demand, expect some shipping delays at this time - orders may not ship for up to 2-3 business days.
0

GROVE definition of i2c
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

GROVE definition of i2c

by blakebr on Sun Jan 23, 2022 1:32 pm

Good Morning,

I have tried several ways to do the following and failed miserably.
This does not work.
Code: Select all | TOGGLE FULL SIZE
GROVE4 = (board.GP7, board.GP6)
i2c = io.I2C(GROVE4)

I must use individual:
Code: Select all | TOGGLE FULL SIZE
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 | TOGGLE FULL SIZE
        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

blakebr
 
Posts: 276
Joined: Tue Apr 17, 2012 6:23 pm

Re: GROVE definition of i2c

by dastels on Sun Jan 23, 2022 2:02 pm

Something like this will probably do the job:
Code: Select all | TOGGLE FULL SIZE
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

dastels
 
Posts: 9782
Joined: Tue Oct 20, 2015 3:22 pm

Re: GROVE definition of i2c

by blakebr on Sun Jan 23, 2022 3:11 pm

Dave,

Your code worked. Thank you.

This is my selection code.
Code: Select all | TOGGLE FULL SIZE
# 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 | TOGGLE FULL SIZE
        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

blakebr
 
Posts: 276
Joined: Tue Apr 17, 2012 6:23 pm

Re: GROVE definition of i2c

by dastels on Sun Jan 23, 2022 4:17 pm

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 | TOGGLE FULL SIZE
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 | TOGGLE FULL SIZE
i2c = io.I2C(pins[grove][0], pins[grove][1])
GR = "GR-{0}".format(grove)

Dave

dastels
 
Posts: 9782
Joined: Tue Oct 20, 2015 3:22 pm

Re: GROVE definition of i2c

by blakebr on Sun Jan 23, 2022 5:31 pm

Dave,

That is so much more elegant. Thank you.

Bruce

blakebr
 
Posts: 276
Joined: Tue Apr 17, 2012 6:23 pm

Re: GROVE definition of i2c

by dastels on Sun Jan 23, 2022 6:15 pm

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

dastels
 
Posts: 9782
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.