Python Forum
Adafruits Neotrellis interrupt with RAsp and Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adafruits Neotrellis interrupt with RAsp and Python
#1
Hi.

Im new to python and electronics. Im trying to use interrupt on a NEotrellis Board. It did worked with Arduino (with the sample code from Adafruit), but I need it to work on raspberry pi and Adafruits sample code doesnt include it ( it says it does work with python)

SO, Im trying this.

import RPi.GPIO as GPIO
BUTTON_GPIO = 16
if __name__ == '__main__':
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    while True:
        GPIO.wait_for_edge(BUTTON_GPIO, GPIO.FALLING)
        print("Button pressed!")
But cant make it work. Does anyone know if it could work?
Reply
#2
Download the Neotrellis library from github: https://github.com/adafruit/Adafruit_Cir...NeoTrellis
if you haven't already done so.
Look a the program in examples named neotrellis_simpletest.py

It captures interrupts as events which is what you want to do.

polling for level changes eats up huge amounts of processor time, and can miss interrupts.

the library can be installed with pip3 install adafruit-circuitpython-neotrellis

this line: trellis.callbacks[i] = blink set's the event to call the blink function.

Note that in the sample code, it appears that this is done in for loop for i in range(16):

you don't want to do this, as they are setting all GPIO pins to execute the blink function.
Instead, you want to use BUTTON_GPIO trellis.callbacks[BUTTON_GPIO] = my_interrupt_func

and then write what you want to execute in a new function (you write) named def my_interrupt_func(event):

Since you are not using class, make sure you define this function prior to setting event.
Reply
#3
Thank you for the answer.

I had already installed the Adafruit library and was using the simpletest.py.

This is my first proyect with python. Im a bit confused.


What I need to be executed with interrupt is the trellis.sync() function wich in the simpletest.py is in the While loop.

Im trying to make it work with only 1 Neotrellis button/led board, but my proyect consist of 32 boards.
It takes 17 ms to read each board, so, if trellis.sync() is continuosly reading in the While loop I cant smoothly read the data coming from serial.

Here is implemented in Arduino


void loop() {

  if(!digitalRead(Interrupt_PIN)){

   trellis.read();
}
I try you suggestion but I get the error :

Error:
trellis.callbacks[BUTTON_GPIO]=My_func IndexError: list assigment index out of range
Here is my code


import time
import RPi.GPIO as GPIO
from board import SCL, SDA
import busio
from adafruit_neotrellis.neotrellis import NeoTrellis

BUTTON_GPIO = 16

GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    

i2c_bus = busio.I2C(SCL, SDA)
trellis = NeoTrellis(i2c_bus)

OFF = (0, 0, 0)
CYAN = (0, 255, 255)



def My_func(event):
   print("test", GPIO.input(16))
  
trellis.callbacks[BUTTON_GPIO]=My_func




def blink(event):
    if event.edge == NeoTrellis.EDGE_RISING:
        trellis.pixels[event.number] = CYAN
    elif event.edge == NeoTrellis.EDGE_FALLING:
        trellis.pixels[event.number] = OFF
for i in range(16):
    trellis.activate_key(i, NeoTrellis.EDGE_RISING)
    trellis.activate_key(i, NeoTrellis.EDGE_FALLING)
    trellis.callbacks[i] = blink
    trellis.pixels[i] =CYAN 
    time.sleep(0.05)
for i in range(16):
    trellis.pixels[i] = OFF
    time.sleep(0.05)



while True:

#    GPIO.wait_for_edge(BUTTON_GPIO, GPIO.FALLING)
#    print("test")
 
  

    # call the sync function call any triggered callbacks
    trellis.sync()
    # the trellis can only be read every 17 millisecons or so
    time.sleep(0.02)
IS it possible to read the boards (trellis.sync()) outside of the While loop altogether ? That would be my dream solution.
Reply
#4
I'm sure it's possible, just a bit more work. If you look at the module details you'll see that it creates a NeoTrellis class. The methods here seem to default to using callbacks, but you could easily create a method that just returns the raw keypress status.

It appears to just be reusing the adafruit_seesaw.keypad module and using the KeyEvent method. That returns a bytearray with a value for each key.
Reply
#5
this particular section addresses multiple board connection: https://learn.adafruit.com/adafruit-neot...3002805-16

each board must have some sort of address in order to identify itself.
when you write your interrupt event, make sure that you not only identify the GPIO port , but also the board ID
so as to differentiate where interrupt is coming from.

I have worked with multiple boards on embedded applications when I was working full time but that's some time ago. Also using either microchips pic processors, or TI MSP-430 processors.
The logic is quite similar for i2c connections.

One bit of advise: Before you get too far into your project, convert code to classes, and use an IDE (I suggest VSCode (not to get confused with Virtual Studio)).

Here's what your original code would look like as a class (with interrupt routine) .. Since I don't have the board in question, this is untested:
import RPi.GPIO as GPIO


BUTTON_GPIO = 16


class Neotrellis:
    def __init__(self):
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        self.setup_callbacks()
    
    def process_button_16(self, event):
        print(f"Button 16 pressed.")
    
    def setup_callbacks(self):
        # Add other GPIO pins (and correcpondinh event handlers) as required.
        trellis.callbacks[BUTTON_GPIO] = process_button_16


def main():
    nt = Neotrellis()


if __name__ == '__main__':
    main()
you can write individual handlers for various interrupts, or you can interpret the event attribute to dispatch
from one interrupt method.

Hope this is helpful
Reply
#6
Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Enabling interrupt on Adafruits button/led board Moris526 0 350 Apr-30-2021, 03:29 PM
Last Post: Moris526
  python delay without interrupt the whole code Nick_tkinter 4 674 Feb-22-2021, 10:51 PM
Last Post: nilamo
  Interrupt for Adafruits Neotrellis button/led board Moris526 0 428 Dec-28-2020, 05:42 AM
Last Post: Moris526
  input interrupt Nickd12 1 663 Dec-09-2020, 05:01 PM
Last Post: Gribouillis
  python ibeacon rasp pi localization joefreedy 13 2,571 Feb-12-2019, 08:52 PM
Last Post: joefreedy
  Rasp Pi: "lp: not allowed to print" when using a python script montypython76 1 1,845 Jun-13-2017, 09:10 AM
Last Post: DeaD_EyE
  Interrupt/Break Function sdprelude 2 3,772 Feb-17-2017, 12:19 AM
Last Post: Ofnuts
  interrupt serial port trainee1 9 12,828 Feb-15-2017, 08:19 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020