adafruit / Adafruit_CircuitPython_NeoTrellis

CircuitPython library for the Adafruit RGB Trellis
MIT License
8 stars 14 forks source link

Example code is broken 'object has no such attribute' #25

Open spuder opened 1 year ago

spuder commented 1 year ago

Setup

Circuit Python 7.20230112.zip Raspberry Pi Pico

Problem

If a user attempts to follow the instructions to set this up, they will run into the following problems.

  1. The instructions call for running this code https://learn.adafruit.com/adafruit-neotrellis/circuitpython-code#circuitpython-and-python-usage-3002750
    
    import time
    from board import SCL, SDA
    import busio
    from adafruit_neotrellis.neotrellis import NeoTrellis

create the i2c object for the trellis

i2c_bus = busio.I2C(SCL, SDA)

create the trellis

trellis = NeoTrellis(i2c_bus)



Which will produce error

`ImportError: cannot import name SCL`

2. If a user attempts to use the example [from the example directory](https://github.com/adafruit/Adafruit_CircuitPython_NeoTrellis/blob/main/examples/neotrellis_simpletest.py), they will encounter this error

`AttributeError: 'module' object has no attribute 'I2C'`

As a result, most users who aren't experienced python developers will likely give up. 

## Additional Information

1. The guide for Trellis was released in September 2018
2. The website [circuitpython.com](https://circuitpython.org/libraries) did not exist at that point in time so its difficult to tell what the latest version of circuit python was. 

## Theory

I suspect that the library versions that are provided in the circuit python bundle are vastly different that the ones that the user is instructed to download

[jan 12, 2023](https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/tag/20230112)

## Solution

1. Document which versions of the library the user should install
2. Update the instructions to work with the latest library
spuder commented 1 year ago

For anyone else struggling to get this working

  1. Download the January 12th, 2023 release of Circuit Python 7.x Bundle

  2. Download version 1.3.2 of NeoTrellis Library

You no longer need to install the adafruit_bus_driver

  1. copy the adafruit_neotrellis library from the neotrellis download to the lib folder
  2. copy the adafrut_seasaw library from the bundle download to the lib foler
Screen Shot 2023-01-12 at 8 10 38 PM
  1. Save the following to code.py on the device

You will need to update board.GP3 and board.GP2 with the SCL and SDA pins on your microcontroller. Some microcontrollers have aliases like board.SDA that you can reference. Other boards (like the raspberry pi pico) don't have those aliases. You can find out available pins by entering dir(board) in the REPL.

Screen Shot 2023-01-12 at 8 03 24 PM
import time
import board
import busio
from adafruit_neotrellis.neotrellis import NeoTrellis

# To view available pins, enter REPL and type `dir(board)`
# https://i0.wp.com/peppe8o.com/wp-content/uploads/2021/05/raspberry-pi-pico-pinout.jpg?w=792&ssl=1
# SDA = GP2 = pin4
# SCL = GP3 = pin5
i2c_bus = busio.I2C(board.GP3, board.GP2)

#create the trellis
trellis = NeoTrellis(i2c_bus)
trellis.brightness = 0.5 

OFF = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)

def blink(event):
    # turn the LED on when a rising edge is detected
    if event.edge == NeoTrellis.EDGE_RISING:
        trellis.pixels[event.number] = CYAN
    # turn the LED off when a falling edge is detected
    elif event.edge == NeoTrellis.EDGE_FALLING:
        trellis.pixels[event.number] = OFF

for i in range(16):
    # activate rising edge events on all keys
    trellis.activate_key(i, NeoTrellis.EDGE_RISING)
    # activate falling edge events on all keys
    trellis.activate_key(i, NeoTrellis.EDGE_FALLING)
    # set all keys to trigger the blink callback
    trellis.callbacks[i] = blink

    # cycle the LEDs on startup
    trellis.pixels[i] = PURPLE
    time.sleep(0.05)

while True:
    trellis.sync()
    time.sleep(0.02)
tekktrik commented 1 year ago

I don't see a place in the guide that says to download any specific (or older) version of the NeoTrellis library. It may be necessary to to update the pins though as you have done, as different CircuitPython boards have different names and quirks that cause the pin names to differ in the board module. If you'd like to submit a PR mentioning that in a comment in the i2c_bus = busio.I2C(... line, please do!