Python Forum
Stop/continue While loop block - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Stop/continue While loop block (/thread-31764.html)

Pages: 1 2 3 4 5 6 7


Stop/continue While loop block - Moris526 - Jan-02-2021

Hi.

I have this situation


I have a While loop reading serial and polling data from a board

While True:

    if (ser.inWaiting() > 0):
          data=ser.readline()
          
    trellis.sync()
   
    time.sleep(0.02)
What I want to do, if possible, is :

When data is coming from serial, then stop reading the board (trellis.syc) , and then with some marker from serial start reading the board again.

Any help?


RE: Stop/continue While loop block - Larz60+ - Jan-02-2021

why have you abandoned the code presented in post: https://python-forum.io/Thread-Adafruits-Neotrellis-interrupt-with-RAsp-and-Python

You need to use an interrupt (event) for this type of operation.

FYI: This is very close;y to other threads you have created, and you should have posted to other thread.


RE: Stop/continue While loop block - Moris526 - Jan-02-2021

Sorry for crossposting.

The whole story short. Im a musician. I thought I could build a controller for a synth (knowing close to nothing about coding and definitely nothing about electronics).

I try with Arduino (I already had a working interrupt code), learnt C, but I needed more procesing speed. Now Im trying with Raspberry pi zero w. I learnt a lot but theres always a new obtacle.
The lastest one.
Someone said that python doesnt support interrupt. And read something like that somewhere else.
I saw a few tutorials with buttons, where the interrupt is activated by bridging interrupt to gropund, but my interrupt is on the Adafruits Neotrellis board (https://learn.adafruit.com/adafruit-neotrellis), and doesnt seem to work.

So. Here are my options.

1. I dont use interrupt. I came up with a way to stop/start the trellis.sync using ¨if¨ in the while loop.
This is the least useful option for me.

2. I try with Micropython. Someone suggested it could work.

3. I try with C++. But I dont know if the Arduino librarys would work on rasp with C++

Any encouragement word is welcomed.


RE: Stop/continue While loop block - Larz60+ - Jan-03-2021

An event is an interrupt.
Python does support interrupts, don't know who told you otherwise.
I could help more if I havd an actual Neotrellis board, but try something simple, known to work,
like flashing leds.

Then little by little start substituting leds with electronics.

I'll help as much as I can. To start, what is the part number (or model) of your Neotrellis board?
I can start reading the technical docs.

FYI - If you don't use interrupt, the only other option is to use loops.
loops are extremely time consuming (processor wise), and will usually fail intermittently when expecting signals from more that one device (you will miss signals while servicing others).


RE: Stop/continue While loop block - Moris526 - Jan-03-2021

Thank you!!

I will try to do the research myself, but would be usuful to have a guide.

I didnt know if I was wasting more time learning about events and callbacks if Python didnt supported interrupt.

A moderator of Adafruit said so. So I took it at face value https://forums.adafruit.com/viewtopic.php?f=47&t=172720

Im on the last lectures of a Python Course. I dig deeper and surely come back with questions.

Thanks again


RE: Stop/continue While loop block - Moris526 - Jan-03-2021

Different question:

I sending data from PC via serial to Rasp and then via i2c to the button/led board.

But, I just thought, is there any usb/i2c board to bypass raspberry altogether?? I mean, do all processing on the PC?

I may have wasted more money than needed


RE: Stop/continue While loop block - Larz60+ - Jan-03-2021

What is model of your neotrellis boards?

The board probably can communicate directly between devices, looks to have 12c port.
If not, any (GPIO) pin can be used for i2c communications, completely software driven, although perhaps at a slower speed.

some of these packages do that: https://pypi.org/search/?q=i2c+%2B+GPIO&o=


RE: Stop/continue While loop block - Moris526 - Jan-03-2021

Neotrellis Model:

XML-2 E315391


RE: Stop/continue While loop block - Larz60+ - Jan-03-2021

I can't find any reference to XML-2 E315391 at Adafruit.
I can find: Adafruit NeoTrellis M4 Mainboard - featuring SAMD51

Is this the device?


RE: Stop/continue While loop block - Moris526 - Jan-04-2021

No. This one.

https://learn.adafruit.com/adafruit-neotrellis?view=all


I think I can interrupt:

The object is ¨trellis¨

Here is the Class and interrupt function:

class NeoTrellis(Keypad):
    """Driver for the Adafruit NeoTrellis."""

    def __init__(self, i2c_bus, interrupt=False, addr=_NEO_TRELLIS_ADDR, drdy=None):
        super().__init__(i2c_bus, addr, drdy)
        self.interrupt_enabled = interrupt
        self.callbacks = [None] * _NEO_TRELLIS_NUM_KEYS
        self.pixels = NeoPixel(self, _NEO_TRELLIS_NEOPIX_PIN, _NEO_TRELLIS_NUM_KEYS)
Here is the full Class

https://github.com/adafruit/Adafruit_CircuitPython_NeoTrellis/blob/master/adafruit_neotrellis/neotrellis.py


I try this

While True:

     if trellis.interrupt_enabled() == True:

        print("test")

     else:
        trellis.sync()
        time.sleep(0.02)
I get the error

Error:
If trellis.interrupt_enabled() == True: TypeError: 'bool' object is not callable
Im out of ideas...