Development of the sino:bit MicroPython port is still very early (as of this writing just a few weeks old!).  However here are a few example programs that demonstrate some of the current capabilities of the board.  Remember the board is fully compatible with micro:bit MicroPython code and any of its examples will run on the sino:bit.  Check out the sino:bit MicroPython GitHub examples folder for more examples as they are created too!

Drawing on the 12x12 LED Display

To access the full 12x12 LED matrix you'll want to use the sinobit.display module.  This is very similar to the microbit.display module but gives you pixel access to the 12x12 LED grid instead of a 5x5 grid like on the micro:bit.  Here's a demo of an 'arc reactor' animation that animates some squares moving into the board.  Load this as a on your board (or load it from the web editor):

# sinobit 'Arc Reactor' Animation
# This will animate a 2 pixel wide rectangle that shrinks down across the
# entire 12x12 display.  The brightness level fades away over time at each step
# for a nicer effect too.
# Author: Tony DiCola

# Import the microbit module to access its sleep delay function.
# You can use all the other parts of the microbit module too except for its
# display module (for now!).  Instead see the sinobit module below for access
# to the display.
import microbit

# Import the display submodule from the sinobit module to write to the display.
from sinobit import display

# Define some functions in Python code to draw straight lines and boxes.
def hline(x, y, width, color):
    # Horizontal line at x, y position and width pixels to the right.
    # Color is a boolean true/false for on or off pixels.
    for i in range(x, x+width):
        # The set_pixel function will set a pixel at the provided x, y position
        # to on or off with the provided true/false color value.
        # Remember you won't see the pixel LED turn on/off until write is
        # called!
        display.set_pixel(i, y, color)

def vline(x, y, height, color):
    # Vertical line at x, y position and height pixels down.
    # Color is a boolean true/false for on or off pixels.
    for i in range(y, y+height):
        display.set_pixel(x, i, color)

def box(x, y, width, height, color):
    # Draw a 1 pixel wide box with upper left corner at x, y and of the
    # specified width and height of pixels.  Color is a boolean true/false
    # for turning the pixels on or off.
    hline(x, y, width, color)
    hline(x, y+height-1, width, color)
    vline(x, y, height, color)
    vline(x+width-1, y, height, color)

# Clear the display.  Note that all the display commands just update the
# internal memory and have to be followed by a write call to update the LEDs
# with the new memory value.  This way you can make a lot of pixel changes at
# once and then write them all in one call (as opposed to seeing all the pixel
# updates as they occur).

# Main loop will run the code inside it forever:
while True:
    # We'll count from 0 to 5 to move the rectangle from a starting x, y
    # of 0, 0 down to 5, 5.
    for i in range(6):
        # First clear the display.  This is another way to do it by calling
        # fill and passing a color boolean to turn off the pixels (but you
        # could instead pass true to turn them all on!).
        # Calculate the size of the square so that it shrinks down with
        # each step.
        size = 12-2*i
        # Draw the square.
        box(i, i, size, size, True)
        # If we're past the first iteration draw a second square behind this
        # one to double the size of the box.
        if i > 0:
            box(i-1, i-1, size+2, size+2, True)
        # Finally make sure to call write on the display to push out all the
        # pixels that were set with the box drawing commands above.  This will
        # turn on and off the appropriate LEDs to draw this frame of the
        # animation.
        # Once the pixels are lit you can change their brightness (you _don't_
        # have to call write after changing brightness, it will instantly
        # update!).  There are 16 brightness levels from 0 (lowest) to 15
        # (maximum, the defaul).  We'll loop down from 15 to 0 to dim the
        # square that was just drawn and make it appear to fade away over time.
        for b in range(15, -1, -1):
            # Delay at each brightness for a longer and longer period of time.
            # This means the lower brightness values will linger and give a
            # nicer exponential decay to brightness.  The sleep function will
            # pause for a number of milliseconds, like the delay function in
            # Arduino.

Printing Text on the 12x12 LED Display

Another example of using the entire LED display is with the text and text_width functions of the sinobit.display module.  These allow you to print a string of text anywhere on the display.  For example this code will scroll a message across the display--even displaying accented Latin unicode characters!  Again save this as on your board (note you must currently use ampy to load this script as the web editor has a bug with saving files that contain advanced Unicode characters):

# Simple message scrolling demo.  Will scroll the specified message across
# the display from right edge to left edge.  Try changing the message to
# to text with Unicode Latin & Latin-1 supplement characters!
# Author: Tony DiCola
import microbit
import sinobit

MESSAGE = '¿Hablas español? Parlez-vous Français?'

x = 11
width = sinobit.display.text_width(MESSAGE)
while True:
    sinobit.display.text(x, 0, MESSAGE)
    x -= 1
    if x < -width:
        x = 11

Digital Sand

Another fun demo is a port of the 'digital sand' / 'pixeldust' animation to the sino:bit.  This is an advanced demo that shows using the accelerometer (again using the microbit module just like on a BBC micro:bit) and the full 12x12 LED matrix from the sinobit.display module.  Save this as on your board and load it with ampy (again the web editor currently doesn't support loading this large of an example).

# Digital sand demo uses the accelerometer to move sand particiles in a
# realistic way.  Tilt the board to see the sand grains tumble around and light
# up LEDs.  Based on the code created by Phil Burgess and Dave Astels, see:
# Ported to sino:bit by Tony DiCola
# The MIT License (MIT)
# Copyright (c) 2018 Tony DiCola
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
import math
import random

import microbit
import sinobit

# Configuration:
GRAINS   = 20    # Number of grains of sand
WIDTH    = 12    # Display width in pixels
HEIGHT   = 12    # Display height in pixels

# Class to represent the position of each grain.
class Grain:

    def __init__(self):
        self.x = 0
        self.y = 0
        self.vx = 0
        self.vy = 0

# Helper to find a grain at x, y within the occupied_bits list.
def index_of_xy(x, y):
    return (y >> 8)  * WIDTH + (x >> 8)

# Global state
max_x = WIDTH * 256 - 1    # Grain coordinates are 256 times the pixel
max_y = HEIGHT * 256 - 1   # coordinates to allow finer sub-pixel movements.
grains = [Grain() for _ in range(GRAINS)]
occupied_bits = [False for _ in range(WIDTH * HEIGHT)]
oldidx = 0
newidx = 0
delta = 0
newx = 0
newy = 0

# Randomly place grains to start.  Go through each grain and pick random
# positions until one is found.  Start with no initial velocity too.
for g in grains:
    placed = False
    while not placed:
        g.x = random.randint(0, max_x)
        g.y = random.randint(0, max_y)
        placed = not occupied_bits[index_of_xy(g.x, g.y)]
    occupied_bits[index_of_xy(g.x, g.y)] = True

# Main loop.
while True:
    # Draw each grain.
    for g in grains:
        x = g.x >> 8  # Convert from grain coordinates to pixel coordinates by
        y = g.y >> 8  # dividing by 256.
        sinobit.display.set_pixel(x, y, True)

    # Read accelerometer...
    f_x, f_y, f_z = microbit.accelerometer.get_values()
    # sinobit accelerometer returns values in signed -1024 to 1024 values
    # that are millig's.  We'll divide by 8 to get a value in the -127 to 127
    # range for the sand coordinates.  We invert the y axis to match the
    # current display orientation too.
    f_y *= -1                         # Invert y
    ax = f_x >> 3                     # Transform accelerometer axes
    ay = f_y >> 3                     # to grain coordinate space (divide by 8)
    az = abs(f_z) >> 6                # Random motion factor grabs a few top
                                      # bits from Z axis.
    az = 1 if (az >= 3) else (4 - az) # Clip & invert
    ax -= az                          # Subtract motion factor from X, Y
    ay -= az
    az2 = (az << 1) + 1         # Range of random motion to add back in

    # ...and apply 2D accel vector to grain velocities...
    v2 = 0                      # Velocity squared
    v = 0.0                     # Absolute velociy
    for g in grains:
        g.vx += ax + random.randint(0, az2) # A little randomness makes
        g.vy += ay + random.randint(0, az2) # tall stacks topple better!

        # Terminal velocity (in any direction) is 256 units -- equal to
        # 1 pixel -- which keeps moving grains from passing through each other
        # and other such mayhem.  Though it takes some extra math, velocity is
        # clipped as a 2D vector (not separately-limited X & Y) so that
        # diagonal movement isn't faster

        v2 = g.vx * g.vx + g.vy * g.vy
        if v2 > 65536:                    # If v^2 > 65536, then v > 256
            v = math.floor(math.sqrt(v2)) # Velocity vector magnitude
            g.vx = (g.vx // v) << 8       # Maintain heading
            g.vy = (g.vy // v) << 8       # Limit magnitude

    # ...then update position of each grain, one at a time, checking for
    # collisions and having them react.  This really seems like it shouldn't
    # work, as only one grain is considered at a time while the rest are
    # regarded as stationary.  Yet this naive algorithm, taking many not-
    # technically-quite-correct steps, and repeated quickly enough,
    # visually integrates into something that somewhat resembles physics.
    # (I'd initially tried implementing this as a bunch of concurrent and
    # "realistic" elastic collisions among circular grains, but the
    # calculations and volument of code quickly got out of hand for both
    # the tiny 8-bit AVR microcontroller and my tiny dinosaur brain.)

    for g in grains:
        newx = g.x + g.vx       # New position in grain space
        newy = g.y + g.vy
        if newx > max_x:        # If grain would go out of bounds
            newx = max_x        # keep it inside, and
            g.vx //= -2         # give a slight bounce off the wall
        elif newx < 0:
            newx = 0
            g.vx //= -2
        if newy > max_y:
            newy = max_y
            g.vy //= -2
        elif newy < 0:
            newy = 0
            g.vy //= -2

        oldidx = index_of_xy(g.x, g.y)            # prior pixel
        newidx = index_of_xy(newx, newy)          # new pixel
        if oldidx != newidx and occupied_bits[newidx]: # If grain is moving to a new pixel...
                                                       # but if that pixel is already occupied...
            delta = abs(newidx - oldidx)          # What direction when blocked?
            if delta == 1:                        # 1 pixel left or right
                newx = g.x                        # cancel x motion
                g.vx //= -2                       # and bounce X velocity (Y is ok)
                newidx = oldidx                   # no pixel change
            elif delta == WIDTH:                  # 1 pixel up or down
                newy = g.y                        # cancel Y motion
                g.vy //= -2                       # and bounce Y velocity (X is ok)
                newidx = oldidx                   # no pixel change
            else:                                 # Diagonal intersection is more tricky...
                # Try skidding along just one axis of motion if possible (start w/
                # faster axis).  Because we've already established that diagonal
                # (both-axis) motion is occurring, moving on either axis alone WILL
                # change the pixel index, no need to check that again.
                if abs(g.vx) > abs(g.vy): # x axis is faster
                    newidx = index_of_xy(newx, g.y)
                    if not occupied_bits[newidx]: # that pixel is free, take it! But...
                        newy = g.y           # cancel Y motion
                        g.vy //= -2          # and bounce Y velocity
                    else:                    # X pixel is taken, so try Y...
                        newidx = index_of_xy(g.x, newy)
                        if not occupied_bits[newidx]: # Pixel is free, take it, but first...
                            newx = g.x           # Cancel X motion
                            g.vx //= -2          # Bounce X velocity
                        else:                    # both spots are occupied
                            newx = g.x           # Cancel X & Y motion
                            newy = g.y
                            g.vx //= -2 # Bounce X & Y velocity
                            g.vy //= -2
                            newidx = oldidx # Not moving
                else:                       # y axis is faster. start there
                    newidx = index_of_xy(g.x, newy)
                    if not occupied_bits[newidx]: # Pixel's free! Take it! But...
                        newx = g.x           # Cancel X motion
                        g.vx //= -2          # Bounce X velocity
                    else:                    # Y pixel is taken, so try X...
                        newidx = index_of_xy(newx, g.y)
                        if not occupied_bits[newidx]: # Pixel is free, take it, but first...
                            newy = g.y           # cancel Y motion
                            g.vy //= -2          # and bounce Y velocity
                        else:                    # both spots are occupied
                            newx = g.x           # Cancel X & Y motion
                            newy = g.y
                            g.vx //= -2 # Bounce X & Y velocity
                            g.vy //= -2
                            newidx = oldidx # Not moving
        occupied_bits[oldidx] = False
        occupied_bits[newidx] = True
        g.x = newx
        g.y = newy

This guide was first published on Feb 23, 2018. It was last updated on Feb 23, 2018.

This page (Examples) was last updated on Feb 21, 2018.

Text editor powered by tinymce.