MicroPython Txting Package for micro:bit
The Micro:bit Foundation was contacted a while back by Will, a 13-year-old student, who had created a txting package for the micro:bit in MicroPython. In his message he said: "I love using Python with my micro:bit and I created a text messaging program in MicroPython". We were of course thrilled. The person who recieved Will's message wrote to the rest of the team: "A 13-year-old has written a lovely little communicator using Python and two micro:bits - something to consider for the website for an article perhaps?" There was, of course, no argument, so, eventually, here is Will's Micro:bit Txting Program.
Általános:
Press pins 0 and 1 together to cycle through the characters, press button A to add characters to the message and press button B to send the completed message. The script requires only 2 micro:bits and both can send and receive via MicroPython's radio module. However, more than 2 micro:bits can be used, just flash the same program onto multiple micro:bits and all of them can both send and receive. The actual Python code
To create the .hex file, download one of the Python code files above, drag the file into the microbit.org Python Szerkesztő ekkor és töltse le a .hex fájlt.MicroPython Code Listing with Comments
from microbit import *
import radio
import random
import microbit
#setup radio
radio.config(length=251, channel=53, power=4)
sending = 1
def Phone():
#initialise radio
radio.on()
while True:
msg = str(radio.receive())
global sending
if sending >= 2:
sending = 0
#select page
while sending == 1:
message = 0
messages = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '(', ')', '"', ':)', ':(']
global tosend
tosend = messages[message]
sentance = []
while True:
msg = str(radio.receive())
#prevents errors
if message >= int(len(messages)):
message = int(len(messages))
#scrolls message when received
if msg != 'None':
display.scroll(msg)
display.show(tosend)
#scrolls through characters
if microbit.pin0.is_touched():
message = message - 1
tosend = messages[message]
display.show(tosend, delay=200)
sleep(200)
if microbit.pin1.is_touched() and tosend != 9:
message = message + 1
tosend = messages[message]
display.show(tosend, delay=200)
sleep(200)
#adds character to list
if microbit.button_a.is_pressed():
sentance.append(tosend)
tosend = messages[message]
display.scroll(tosend, delay=50)
#sends list as string
if sending == 1 and microbit.button_b.is_pressed():
radio.send(''.join(sentance))
display.scroll(''.join(sentance))
sentance = []
break
break
Phone()
Jó szórakozást kívánunk a micro:bit-al!