[Microbit-Python] mu vs microbit.co.uk/create-code API differences

Carlos Pereira Atencio carlosperate at gmail.com
Wed Oct 19 09:16:19 EDT 2016


Now that the foundation has been announced, the http://microbit.org website
(note the change from .co.uk to .org) should contain the newest version of
the online editor . So if you go to http://python.microbit.org/editor.html
it should contain the latest micropython.

On 19 October 2016 at 13:17, Joakim Pettersson <joakimbits at gmail.com> wrote:

> Aha, that's why - thanks for the explanation!
> Agree one should not eval, but this demo is about daring using the
> parser-interpreter so it's ok!
> Note the exec in globals() later on;).
> /joakim
>
> ------ Originalmeddelande ------
> Från: "Radomir Dopieralski" <radomir at dopieralski.pl>
> Till: microbit at python.org
> Kopia: "Joakim Pettersson" <joakimbits at gmail.com>
> Skickat: 2016-10-19 14:11:13
> Ämne: Re: [Microbit-Python] mu vs microbit.co.uk/create-code API
> differences
>
>
> Hi,
>>
>> the version of MicroPython at the microbit website is outdated, because it
>> takes BBC a very long time to upgrade it. The Mu editor comes with the
>> latest
>> release, because it's in control of the developers.
>>
>> One note about your code. You *really* don't want to use eval(). I would
>> use
>> the getattr() function in its place, like this:
>>
>> a_image, b_image = getattr(Image, name) for name in (a_name, b_name)
>>
>>
>> On Wed, 19 Oct 2016 12:00:42 +0000
>> "Joakim Pettersson" <joakimbits at gmail.com> wrote:
>>
>>  Hi,
>>>
>>>  I stumped into problems with the online editor.
>>>  Which documentation should I read to figure out the API?
>>>  Why is that micropython implementation different from the one used by
>>>  the mu editor?
>>>
>>>  Here is code that works in the mu editor but fails when dowloading
>>>  from https://www.microbit.co.uk/app/# (probably on the third line but
>>>  how do I tell?). The code was running a swarm of 8 micro:bit at a
>>>  student-industry event this Monday. I used it also to pitch micro:bit
>>>  to the Swedish prime minister when he received one there:).
>>>
>>>  Thanks/Joakim
>>>
>>>  # A micro:bit virus-infectable Firefly.
>>>  # mailto:Joakim.Pettersson at join.se
>>>
>>>  from radio import send, receive, on
>>>  import random
>>>  from microbit import display, Image, button_a, button_b, sleep
>>>
>>>  flock_size = 3  # Number of fireflies required for flock behaviour
>>>  me = 2  # My lucky number
>>>  a_name, b_name = {
>>>       0: ('HEART', 'ALL_ARROWS'),
>>>       1: ('HAPPY', 'ALL_CLOCKS'),
>>>       2: ('SURPRISED', 'CONFUSED'),
>>>       3: ('SNAKE', 'PACMAN'),
>>>       4: ('BUTTERFLY', 'COW'),
>>>       5: ('FABULOUS', 'SAD'),
>>>       6: ('SILLY', 'SMILE')}[me]
>>>  a_image, b_image = (eval('Image.' + name) for name in (a_name,
>>>  b_name))
>>>
>>>
>>>  # A show function that does not wait
>>>  def show(x): display.show(x, delay=100, wait=False)
>>>  show(a_image)
>>>
>>>  # Create the "flash" animation frames. Can you work out how it's done?
>>>  flash_animation = [Image().invert()*(i/9) for i in range(9, -1, -1)]
>>>
>>>  # Timing is controlled with these global variables
>>>  time = 0  # Current time
>>>  flash_time = -1  # Time to flash LEDs
>>>  reflash_time = -1  # Time to flash others
>>>
>>>
>>>  # Define the "flash" function.
>>>  def flash():
>>>       "Display the firefly flash animation after a random short pause"
>>>       global time, flash_time, reflash_time
>>>       flash_time = time + random.randint(50, 350)
>>>       if random.randint(0, flock_size) == 0:
>>>           reflash_time = flash_time + 500
>>>
>>>
>>>  # The step function decides what to do next
>>>  def step():
>>>       global time, flash_time, reflash_time
>>>       # Button A and B changes image.
>>>       if button_a.was_pressed():
>>>           send('show(Image.%s)' % a_name)
>>>           show(a_image)
>>>           reflash_time = time  # Reflash now!
>>>       if button_b.was_pressed():
>>>           send('show(Image.%s)' % b_name)
>>>           show(b_image)
>>>           reflash_time = time  # Reflash now!
>>>       # Read any incoming messages.
>>>       try:
>>>           incoming_message = receive()
>>>       except ValueError:
>>>           pass
>>>       if incoming_message:
>>>           print(incoming_message)
>>>           if incoming_message[:3] != 'On ':
>>>               try:
>>>                   exec(incoming_message, globals(), globals())
>>>               except Exception as e:
>>>                   send("On %d: %s" % (me, e))
>>>       if time == flash_time:
>>>           show(flash_animation)
>>>       if time == reflash_time:
>>>           send('flash()')  # a-ha
>>>       sleep(1)  # Pause 1 ms
>>>       time = time + 1  # Now it's time for next step
>>>
>>>  # The radio won't work unless it's switched on.
>>>  on()
>>>
>>>  # Infinite loop
>>>  while True:
>>>       try:
>>>           step()
>>>       except Exception as e:
>>>           print(e)
>>>
>>>  # For the virus author:
>>>  # Send <20 character strings containing new code ;-)!
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>> --
>> Radomir Dopieralski
>>
>
> _______________________________________________
> Microbit mailing list
> Microbit at python.org
> https://mail.python.org/mailman/listinfo/microbit
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/microbit/attachments/20161019/2ed60c7f/attachment.html>


More information about the Microbit mailing list