1

micro:bit wireless radio BLE using Python

I want to send over radio a member of the Image collection (Image.HEART). I know how to send strings and a custom image but not a member of the Image collection.

I want receiver's message_in string to be used directly by display.show (or maybe an intermediate variable to modify). I don't want to test the received string for every possible member of the Image collection by using if/else or a dictionary.

I've tried ideas in code below but all fail. I appreciate your help.

# micro:bit radio: Send an image from Image collection
from microbit import *
import radio
radio.config(group=1)
radio.on()
while True:
    if button_a.is_pressed():
        radio.send(Image.HEART) # ?????
#        radio.send(index(Image.HEART)) # ?????
#        radio.send(str(Image.HEART)) # ?????
#        radio.send('Image.HEART') # ?????
#        radio.send('HEART') # ?????

    message_in = radio.receive()
    if message_in != None:
        display.show(message_in) #show heart
        # and other tries at syntax for argument
Playing with GAS
  • 565
  • 3
  • 10
  • 18

2 Answers2

1

This feels rather "hacky" and brittle, and I am happy to delete it if a better method shows up, but one way that works is like this.

If you run this (link to docs):

repr(Image.HEART)

you'll get this:

"Image('09090:99999:99999:09990:00900:')"

If you look at the documentation for Image class (link to docs), you'll see you can create a new Image from that string. So, my suggestion for the moment is to do this:

# Get a string corresponding to Image.HEART
s = repr(Image.HEART)[7:-3]

... TRANSMIT ...

# Convert received string back into Image
I = Image(received)

I guess this is a slightly less brittle way of picking up digits and colons from the repr output, but it's still ugly:

s = ""
for char in repr(Image.SAD):
    if char in '0123456789:': s += char
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

The way Mark has suggested works well and allows for any image to be sent. I've put it in a function to make it easier for me to experiment with.

from microbit import *
import radio
radio.config(group=1)
radio.on()

def tx_value(image_to_send):
    return ''.join([x for x in str(image_to_send) if x in '0123456789:'])

while True:
    if button_a.is_pressed() and button_b.is_pressed():
        radio.send(tx_value(Image('97531:97531:97531:97531:97531')))
    elif button_a.is_pressed():
        radio.send(tx_value(Image.DUCK))
    elif button_b.is_pressed():
        radio.send(tx_value(Image.HEART))
    sleep(.25)
    
    message_in = radio.receive()
    if message_in != None:
        display.show(Image(message_in))

The other approach is to have a dictionary of images and just transmit the dictionary key:

from microbit import *
import radio
radio.config(group=1)
radio.on()

IMAGES = {'duck': Image.DUCK,
          'heart': Image.HEART,
          'fade': Image('97531:97531:97531:97531:97531')}

while True:
    if button_a.is_pressed() and button_b.is_pressed():
        radio.send('fade')
    elif button_a.is_pressed():
        radio.send('duck')
    elif button_b.is_pressed():
        radio.send('heart')
    sleep(.25)
    
    message_in = radio.receive()
    if message_in != None:
        display.show(IMAGES[message_in])

This requires for the dictionary to be defined the same on both micro:bits

ukBaz
  • 6,985
  • 2
  • 8
  • 31