[Microbit-Python] Flat API

Alan alainjackson at hotmail.com
Tue Sep 22 01:52:45 CEST 2015


Hi All!

I had a great time with all of you at pycon UK and playing with the microbit, thanks!

On the way home I was thinking a lot about the API and the REPL and 11 year old kids. I had a bit of a thought that I wanted to try out - don't shout and scream and hiss. I don't know how much you'll like it. 

Looking at the microbit module there's very few name clashes within it so I wondered what it would be like if there was a version of it that was almost completely flattened. Especially for using with the REPL. 

Here's my thinking - the hardware isn't going to change (is it?). It's got 20 pins, it's only ever going to have 20 pins, 25 LEDs, 2 buttons etc. So when you're in the REPL, wanting to see immediate results on the hardware itself, and you're in such an small, embedded environment, do you want to type:

  microbit.pins.pin0.set_digital_value(1)  

or would you rather type

  dpin0 = True  ?


Anyway, as a thought experiment, here's an idea of a flattened API. The attributes with an "=" sign means you can assign to them. 


====8<====



Attributes: 

dpin0 =
dpin1 =
.
.
dpin19 =
dpin20 =


apin0 =
apin1 =
.
.
apin19 =
apin20 =

pins.analog_period = 

pixel[0,0] =
brightness =
display_mode =
image =

button_a
button_b

force_x    #accellerometer.get_x()
force_y
force_z

heading 

compass_x
compass_y
compass_z

compass.is_calibrated
compass.is_calibrating

system_time


Methods:

shift_left()
shift_right()
shift_up()
shift_down()

write(...)      #display.print(...)
scroll(...)
animate(...)
clear()

compass.calibrate()
compass.clear_calibration()

i2c.read()
i2c.write(...)

sleep(...)
random()

reset()
panic()



====8<====

My little program that scrolls through a message using the two buttons then becomes:



from flat_microbit import *

message = "1234567890"
index = 0

while True:
    if button_a:
        index = (index - 1) % 10
    elif button_b:
        index = (index + 1) % 10
    
    write(message[index])
    sleep(50)




Cheers,

Alan

 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/mailman/private/microbit/attachments/20150921/d743ecb2/attachment.html>


More information about the Microbit mailing list