Python Forum
Stop/continue While loop block
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stop/continue While loop block
#1
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?
Reply
#2
why have you abandoned the code presented in post: https://python-forum.io/Thread-Adafruits...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.
ndc85430 likes this post
Reply
#3
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.
Reply
#4
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).
Reply
#5
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
Reply
#6
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
Reply
#7
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=
Reply
#8
Neotrellis Model:

XML-2 E315391
Reply
#9
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?
Reply
#10
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_Cir...trellis.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...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  while loop will not stop looping TheTechRobo 5 1,041 Apr-20-2020, 01:47 PM
Last Post: TheTechRobo
  for loop stumbling block YoungGrassHopper 8 1,170 Sep-11-2019, 03:34 PM
Last Post: YoungGrassHopper
  while loop stumbling block YoungGrassHopper 5 841 Sep-09-2019, 08:36 PM
Last Post: YoungGrassHopper
  How to continue in loop until correct input received sunnyarora 10 3,508 May-04-2019, 02:37 PM
Last Post: Yoriz
  Can I Control loop with Keyboad key (start/stop) Lyperion 2 1,302 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 5,469 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