[Microbit-Python] error code?... :(020

Ben Nuttall ben at raspberrypi.org
Fri Sep 25 22:43:58 CEST 2015


If anyone's interested, we just added a nifty callback feature into
gpiozero (the kid friendly GPIO lib for Raspberry Pi I'm working on).

Example usage:

button = Button(2)  # gpio pin 2
led = LED(3)  # gpio pin 3

button.when_pressed = led.on
button.when_released = led.off

See the code here:
https://github.com/RPi-Distro/python-gpiozero/blob/master/gpiozero/input_devices.py

All credit to the genius that is Dave Jones.

Ben

---

Ben Nuttall
Education Developer Advocate
Raspberry Pi Foundation
www.raspberrypi.org

UK registered charity 1129409

On 25 September 2015 at 14:50, Damien George <damien.p.george at gmail.com>
wrote:

> Regarding the event queue.  This is something that's implemented in
> the underlying DAL, it's nothing to do with MicroPython.
>
> There is a message bus and events can be posted to the message bus,
> with a timestamp.  You can attach arbitrary C/C++ functions to be
> called when an event is posted.
>
> For example, with the buttons, when the button state changes then
> events are generated.  The buttons have software debouncing and also
> timing to see how long they have been pressed.  The events are:
> - button high transition
> - button low transition
> - button click
> - button long press
> - button held
>
> Events will remain on the queue until the handler is run.  Since the
> threading scheme of the DAL is cooperative your code must yield for
> the events to be processed.  This is likely to change very soon so
> that events handlers are executed preemptively (on an interrupt).
>
> Currently in the MicroPython bindings to the DAL we don't use these
> events.  We simply return the current state of the button (high or
> low) as read on the button GPIO.
>
> But probably it's a good idea to use these events so that you can
> react to a "click" without sitting in a tight polling loop.
>
> The difficult thing will be to make a clean API that doesn't require
> the user to know/understand events or preemption, and doesn't require
> them to yield or make sure the queue doesn't overflow.
>
> We could try something like:
>
> button.is_held() # simply get the state of the GPIO as it currently does
> button.was_clicked() # return true if the button was clicked since the
> last call to this function; resets the state to false
> button.was_long_clicked() # same as above, but for long click
>
> We anyway need a similar set of concepts for gestures (eg
> gesture.did_fall(), gesture.was_shaken()).
>
> Thoughts?
>
>
> On Fri, Sep 25, 2015 at 2:09 AM, Alan <alainjackson at hotmail.com> wrote:
> >
> > I've just been writing myself an event queue for button pushes etc...
> maybe
> > that's not necessary if there already is one, if it's accessible.
> >
> > Cheers,
> >
> > Alan
> > ________________________________
> > To: microbit at python.org
> > From: larry at hastings.org
> > Date: Thu, 24 Sep 2015 14:17:17 +0100
> > Subject: Re: [Microbit-Python] error code?... :(020
> >
> >
> >
> > I wish to understand this more.  Micropython has an internal,
> > hidden-from-view, event queue?  How do I examine and interact with it?
> >
> > I thought button_a.is_pressed() was polling; is it watching for button
> > events on this event queue?  Does that imply that I can get delayed /
> > buffered button presses?
> >
> > In general how do I keep the event queue from filling?
> >
> >
> > /arry
> >
> > On 09/23/2015 01:54 PM, Damien George wrote:
> >
> > Hi Alan,
> >
> > Error code 020 is "out of memory".
> >
> > The problem is as you guessed: there is an event put on the event
> > queue each time the button is pressed.  To clear this queue your code
> > needs to "yield".  You can do this by putting sleep(1) in your loop.
> >
> > This is totally unexpected behaviour and I'll work out how to fix it
> > (Ie your code should just work).
> >
> > Cheers,
> > Damien.
> >
> >
> >
> > On Wed, Sep 23, 2015 at 4:02 AM, Alan <alainjackson at hotmail.com> wrote:
> >
> > Error code update.
> >
> > I'm consistently getting the ":( 020" code after 42 button presses (of
> any
> > combination of buttons A and B).
> >
> > Is there a button click buffer that's overflowing somewhere? If there is
> I
> > can't see a method on the microbit API to clear it.
> >
> > Cheers,
> >
> > Alan
> >
> > ________________________________
> > From: alainjackson at hotmail.com
> > To: microbit at python.org
> > Date: Wed, 23 Sep 2015 00:28:51 +0000
> > Subject: [Microbit-Python] error code?... :(020
> >
> >
> > Hi,
> >
> > I wrote a small program to draw on the LED matrix, but after setting a
> few
> > LEDs on I get what looks like an error message on the LEDs:
> >
> > ":(020"
> >
> > (Frowny-face zero two zero)
> >
> > It just keeps repeating that and my program stops but there's no stack
> trace
> > on the python repl.
> >
> > Is that a built in hardware error code or something? Has anyone else seen
> > that?
> >
> > Here's my program:
> >
> > ====8<====
> >
> > from microbit import *
> >
> > index = 0
> >
> > while True:
> >     if button_a.is_pressed():
> >         x = index % 5
> >         y = int(index / 5)
> >         display.image.set_pixel_value(x,y,
> > not(display.image.get_pixel_value(x,y)))
> >
> >     if button_b.is_pressed():
> >         index = (index + 1) % 25
> >
> > _______________________________________________ Microbit mailing list
> > Microbit at python.org https://mail.python.org/mailman/listinfo/microbit
> >
> > _______________________________________________
> > Microbit mailing list
> > Microbit at python.org
> > https://mail.python.org/mailman/listinfo/microbit
> >
> > _______________________________________________
> > Microbit mailing list
> > Microbit at python.org
> > https://mail.python.org/mailman/listinfo/microbit
> >
> >
> >
> > _______________________________________________ Microbit mailing list
> > Microbit at python.org https://mail.python.org/mailman/listinfo/microbit
> >
> > _______________________________________________
> > Microbit mailing list
> > Microbit at python.org
> > https://mail.python.org/mailman/listinfo/microbit
> >
> _______________________________________________
> Microbit mailing list
> Microbit at python.org
> https://mail.python.org/mailman/listinfo/microbit
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/mailman/private/microbit/attachments/20150925/68c59136/attachment-0001.html>


More information about the Microbit mailing list