Example #1
0
import radio
import random
##################################################

# Global variables
##################################################
# Network request
init = "CHLIB_REQUEST"
accept = "CHLIB_ACCEPT"
stop = "CHLIB_STOP"
# Default communication addresse
default_address = "0x12345678"
# Default key for cipher
key = default_address
# Initialize screen
initialize(pinReset=pin0)
clear_oled()
# Communication not established by default
com_established = False

##################################################


# Network functions
##################################################
# Send message by radio to another micro:bit
# Encrypt the message before to send
def send_msg(message):
    q = vign(message, key, 'e')
    radio.send(q)  # max size 251 bytes (octets)
Example #2
0
from ssd1306 import initialize, clear_oled
from ssd1306_px import get_px, set_px
from microbit import accelerometer, button_b, button_a, sleep, display
from random import randint
from ssd1306_text import add_text
initialize()
clear_oled()
pixels = []
# returns -2 to 2 from -1024 to +1024, empirically calculated


def tilt_scale(a):
    a = a + 1024  # range 0 to 2048
    # < 512 = 0, 512-853(512+341) = 1, 853-1194 = 2,
    # 1194-1536 = 3,  1536-2048 = 4
    for i in [[512, -2], [853, -1], [1194, 0], [1536, 1]]:
        if a < i[0]:
            return i[1]
    return 2


def get_tilt():
    x, y, z = accelerometer.get_values()
    return tilt_scale(x), tilt_scale(y), tilt_scale(z)


def pick_speed(speed):
    while not button_b.is_pressed():
        add_text(4, 1, str(speed))
        if button_a.is_pressed():
            speed = speed - 1 if speed > 1 else 9