Python Forum
Stop/continue While loop block
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stop/continue While loop block
#31
Hey. No pressure man.

If you can make it work, great, if not, Im greatful for the atention.
Reply
#32
I meant for you to use private forum mail. I don't usually like to get it, but thought you should keep that private.
there's a button at bottom of post labelled PM (for private mail).

I removed public data (and kept a copy if needed).

Not sure that there are any restrictions with your country, but will check to make sure before I post anything in question.

Question: Do you have a m4 feather board with your keypad? This contains a powerful MCU which I intend to use (or modify existing (on board)code)
Reply
#33
Quote:Question: Do you have a m4 feather board with your keypad? This contains a powerful MCU which I intend to use (or modify existing (on board)code)

Nope.

I would like to make it work with The Rasp zero.

I have a Circuit playground express too. But Rasp is much faster
Reply
#34
This should work for the pi and/or the M4 (which by the way is plenty fast enough to run a simple 4 x 4 matrix. the pi MCU is indeed 833 times faster than the ATSAMD51J19, but the latter still runs at 120Mhz which is much more than needed for a simple 4 x 4 matrix).

load package pad4pi: https://pypi.org/project/pad4pi/1.0.0/
github: https://github.com/brettmclean/pad4pi

It should be able to handle all.
Reply
#35
Quote:load package pad4pi: https://pypi.org/project/pad4pi/1.0.0/

Does this assing each button on the pad to a different pin on rasp?

I need to use i2c. I have 32 boards. 512 buttons

or you meant just for the interrupt?

I try that with GPIO 5 both on column and row with no response
Reply
#36
I'm still learning about the interface on the neotrellis, but each board has it's own I2c connection.

I realize now, that I had the wrong idea of what was needed here. Pin 5 has nothing to do with reading the I2c output, it's a clock pin.

-----------------

Each board also needs an address (same as midi does) to identify itself on the I2C Buss which is
set by soldering the pads on the back of the board (obviously each board must have an address that is different from any other device hooked up to I2c. There is an example (for two boards) here: https://learn.adafruit.com/adafruit-neotrellis.

And this page shows tiling 4 neotrellis boards together: https://learn.adafruit.com/adafruit-neotrellis/tiling using a single I2C connection.
Tiling 32 should be similar

The connection to the pi would be just one I2c connector. (or directly to MCP2221A).

Have you done this on your board?

The only connection between the pi and the neotrellis should be an I2c connector (this thought might change after I have my hardware hooked up)

-----------------

I was going to write a decoder, which requires detection of button down, button up, and a time delay between and resampling in order to prevent key bounce. That's already been done in the hardware, the work on the pi would be different, too early to see how at this point.

-----------------

I expect something like:
  • reading the I2c
  • finding the address on in-coming packet (board number)
  • the button number should be decoded by now, I don't yet know) but if not:
    determining button that is actually pushed based on board location within the 32 board matrix.
and that's it.

I think this can be done directly to a main computer, simply by converting the I2c to USB (thus the MCP2221A General Purpose I2c to USB converter), elimination the Pi or the M4.

It's too soon for me to determine this until I have a working setup.

-----------------

Please tell me more about how you have the 32 boards hooked to each other.
Reply
#37
Yes.

I already have the 32 boards solder together, with its unique address assinged by the jumper on the back.

I already have it working via I2C, with the I2C pins on the Rasperry zero.

All of this is usingo the sample code from adafruit

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time

from board import SCL, SDA
import busio
from adafruit_neotrellis.neotrellis import NeoTrellis
from adafruit_neotrellis.multitrellis import MultiTrellis

# create the i2c object for the trellis
i2c_bus = busio.I2C(SCL, SDA)

"""create the trellis. This is for a 2x2 array of NeoTrellis boards
for a 2x1 array (2 boards connected left to right) you would use:

trelli = [
    [NeoTrellis(i2c_bus, False, addr=0x2E), NeoTrellis(i2c_bus, False, addr=0x2F)]
    ]

"""
trelli = [
    [NeoTrellis(i2c_bus, False, addr=0x2E), NeoTrellis(i2c_bus, False, addr=0x2F)],
    [NeoTrellis(i2c_bus, False, addr=0x30), NeoTrellis(i2c_bus, False, addr=0x31)],
]

trellis = MultiTrellis(trelli)

# some color definitions
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)

# this will be called when button events are received
def blink(xcoord, ycoord, edge):
    # turn the LED on when a rising edge is detected
    if edge == NeoTrellis.EDGE_RISING:
        trellis.color(xcoord, ycoord, BLUE)
    # turn the LED off when a rising edge is detected
    elif edge == NeoTrellis.EDGE_FALLING:
        trellis.color(xcoord, ycoord, OFF)


for y in range(8):
    for x in range(8):
        # activate rising edge events on all keys
        trellis.activate_key(x, y, NeoTrellis.EDGE_RISING)
        # activate falling edge events on all keys
        trellis.activate_key(x, y, NeoTrellis.EDGE_FALLING)
        trellis.set_callback(x, y, blink)
        trellis.color(x, y, PURPLE)
        time.sleep(0.05)

for y in range(8):
    for x in range(8):
        trellis.color(x, y, OFF)
        time.sleep(0.05)

while True:
    # the trellis can only be read every 17 millisecons or so
    trellis.sync()
    time.sleep(0.02)
This is the sample code for multitrellis, there is one for only one board



The only thing missing is the interrupt, so I can stop the trellis.sync at the While loop, so I can read data coming from serial.

Question: Is in theory possible to read only the data coming from the button pressed? I mean, when a press a button, the signal comes with the address data?

That would be a better solution, would take less time, instead of cheking all the boards with trellis.sync in the While loop.

Here is a weird behaviour that suggest it can be done better.

If I put a large time delay in the While loop and press many buttons before it reads again, when it reads I get all the button pressed together.
Reply
#38
The code you show above should run if you connect the i2c from neotrellis to i2c on pi zero.

from neotrellis document:
Quote:These 4x4 button pad boards are fully tile-able and communicate over I2C. With 5 address pins, you've got the ability to connect up to 32 together in any arrangement you like. With our trusty seesaw I2C-to-anything chip, you don't even need to manage the NeoPixel driving. That's right! Both the button management and LED driving is completely handled for you all over plain I2C. With both Arduino/C++ and CircuitPython/Python library support, you can use these pads with any and all microcontroller or computer boards.

unfortunately, the pi zero doesn't have an i2c connector, so you need to wire one up
there is a good tutorial here: https://learn.sparkfun.com/tutorials/ras...-pi-boards

get that wired up, connect the pi to your tiled array through I2c (wired as in https://learn.adafruit.com/adafruit-neotrellis/tiling so only one I2C on neotrellis and you should be good to go. I'll get there in the next few days (with two neotrellis boards), but you should be able to get there faster as you already have it wired up.

I'd like to see the I2C output when a single button is pressed, haven't found that yet in the docs, but I expect that there will be a board address, and either the button xy, or the board pins as a byte or word bitmap. Don't know what this looks like, and the AdaFruit docs are kind of all over the place.

With this method, you will be able to distinguish the button press from any board in your matrix.
Reply
#39
I have all set and working with respect to i2c.

I have a Board of 4x8 Neotrellis units ( I would like to send you an image but I dont know how to upload it from Pc)

If I do this

# this will be called when button events are received
def blink(xcoord, ycoord, edge):
    # turn the LED on when a rising edge is detected
    if edge == NeoTrellis.EDGE_RISING:
        print(xcoord)
        print(ycoord)
I get the coordinate number

x = 0-31
y = 0-15

But to get this, trellis.sync in the While loop reads all 32 boards each time. I was wondering if that could be avoided
Reply
#40
Quote:But to get this, trellis.sync in the While loop reads all 32 boards each time. I was wondering if that could be avoided

I2C is a serial ( asynchronous ) bus, thus only one packet can be seen (or set) at any one time. A separate packet is put on the bus, with it's board Id each time an event occurs, and then is identified and read on the receiving end of the connection. I can't explain all here, but the specification here will explain all (the last release of specs was released, April 4, 2014 so don't let the date make you think that it's outdated. And is further explained here.

Putting stuff on the bus is the work of the sending hardware and/or software.
I'm guessing the following: (I have only previous real-life job experience and know how I handled this in the past)

The logical choice for setup is:

1. Poll each board individually, ( in software ) looking for a signal. This obviously has to be done fast enough to be able to detect each key press, no matter where it originates.
The polling speed is limited by the hardware on each neotrellis board, Not by the controlling processor, no matter how fast it is, so the pi zero (or any other processor, like the M4, is more than adequate A button can only physically be pressed as fast as a human finger can move).

2. Each individual board on the neotrellis fires an interrupt as soon as [b]Any[/] key is pressed on that board. This interrupt along with the board Id and x, y id is immediatly sent over the I2c bus to the controlling processor. This is the ideal setup as you don't have to discover which board is sending the button press signal. Again the time needed to process on the controlling processor is trivial when human intervention (the button press) is involved.

External processing is necessary to handle this (with a single I2c from all boards) to MCU. see: https://learn.adafruit.com/adafruit-neot...-3002700-9.

Any code, no matter what is used, can only send one packet at a time across the I2C bus. If more than one finger is used at the same time, It would be possible to have collisions (bounce) that must be taken into consideration with either de-bounce software or hardware. I'm not ready to say what is being done on this board yet. I will get to it, but as stated before, slowly (I have to fit this into my schedule, which, although being retired, is quite full).

That's why I warn that above is still speculation.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Anyway to stop a thread and continue it? dimidgen 2 258 Mar-18-2024, 10:53 AM
Last Post: DeaD_EyE
  Button to stop while loop from another script Absolutewind 5 818 Sep-25-2023, 11:20 PM
Last Post: deanhystad
  get out of while loop and stop repeat Frankduc 11 2,866 Apr-26-2022, 10:09 PM
Last Post: deanhystad
  while loop will not stop looping TheTechRobo 5 3,646 Apr-20-2020, 01:47 PM
Last Post: TheTechRobo
  for loop stumbling block YoungGrassHopper 8 4,409 Sep-11-2019, 03:34 PM
Last Post: YoungGrassHopper
  while loop stumbling block YoungGrassHopper 5 3,231 Sep-09-2019, 08:36 PM
Last Post: YoungGrassHopper
  How to continue in loop until correct input received sunnyarora 10 9,772 May-04-2019, 02:37 PM
Last Post: Yoriz
  Can I Control loop with Keyboad key (start/stop) Lyperion 2 3,269 Jul-28-2018, 10:19 AM
Last Post: Lyperion
  build a list (add_animals) using a while loop, stop adding when an empty string is en nikhilkumar 1 8,842 Jul-17-2017, 03:29 PM
Last Post: buran

Forum Jump:

User Panel Messages

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