Meowbit MicroPython API

Meowbit MicroPython API

Import Meowbit library

from meowbit import *

Meowbit String Display

Meowbit support both English and Chinese characters.

Display English

screen.text(text, x=0, y=0, ext=1, color=255)
  • Coordinates refer to top left o text.

  • ext is text size.

  • color is color, RGB is supported.

Display Chinese

screen.textCh(text, x=0, y=0, ext=1, color=255)
  • Coordinates refer to top left o text.

  • ext is text size.

  • color: RGB is supported.

Sample Program

from meowbit import *

screen.text('hello world')

screen.textCh('你好世界', x=30, y=20, ext=2, color=(0,0,255))

Meowbit Display Shapes

Screen Fill

screen.fill(color)

Fill screen with color.

  • color: RGB is supported.

Draw Pixel

screen.pixel(x,y,color)

color: RGB is supported.

Draw Line

screen.line(x1, y1, x2, y2, color)

color: RGB is supported.

Sample Program

from meowbit import *

screen.fill((100,0,100))
screen.pixel(10,10,(0,255,0))
screen.line(50,50,100,100,(0,0,255))

Draw Rectangle

screen.rect(x, y, width, height, color=255, fill=0)
  • Coordinates refer to top left.

  • color: RGB is supported.

  • fill = 1 to fill shape, fill = 0 for no fill.

Draw Circle

screen.circle(x, y, r, color=(R, G, B), fill=0)
  • Coordinates refer to center.

  • color: RGB is supported.

  • fill = 1 to fill shape, fill = 0 for no fill.

Draw Triangle

screen.triangle(x1, y1, x2, y2, x3, y3, color=255, fill=0)
  • color: RGB is supported.

  • fill = 1 to fill shape, fill = 0 for no fill.

Draw Polygon

screen.polygon(x, y, sides=3, r=10, th=3, rot=0, color=255, fill=0)
  • Coordinates refer to center.

  • Sides: no. of side

  • th: thickness of the border

  • rot: rotation angle

  • color: RGB is supported.

  • fill = 1 to fill shape, fill = 0 for no fill.

Sample Program

from meowbit import *

screen.fill((100,0,100))
screen.pixel(10,10,(0,255,0))
screen.line(50,50,100,100,(0,0,255))

Screen Refresh

screen.refresh()

Set Screen Auto Sync

screen.sync=val
  • Set to 1 to turn on scren sync, 0 to turn off

Sample Program

from meowbit import *
screen.sync=0
x=0
while 1:
    screen.fill(0)
    screen.circle(x,40,20,(255,0,0),1)
    x+=1
    screen.refresh()

Meowbit Display Picture

Show bmp

screen.loadBmp(path, x=0, y=0)
  • path: file name

  • Coordinates refer to top left

Show gif

screen.loadgif(path, x = 0, y = 0)
  • path: file name

  • Coordinates refer to top left

Sample Program

Sample Pics

# 顯示bmp
from meowbit import *
screen.loadBmp('haimian.bmp')

# 顯示gif
from meowbit import *
screen.loadBmp('haimian.gif')

Meowbit Buttons

Get Button Status

sensor.btnValue(btn)
  • btn: ’a’ , ’b’ , ’up’ , ’down’ , ’left’ , ’right’

Button Triggers

sensor.btnTrig[btn] = fn
sensor.startSchedule()
  • btn: ’a’ , ’b’ , ’up’ , ’down’ , ’left’ , ’right’

  • fn: function

  • startSchedule(): enable running in background

Sample Program

from meowbit import *
screen.sync=0
while 1:
    screen.fill(0)
    screen.text(sensor.btnValue('a'))
    screen.refresh()

from meowbit import *
import random as r
screen.fill(0)
def drawCircle():
    screen.circle(r.randint(0,160),r.randint(0,128),10,(255,0,0),1)

while 1:
    sensor.btnTrig['b']=drawCircle

from meowbit import *
import random as r
screen.fill(0)
def drawCircle():
    screen.circle(r.randint(0,160),r.randint(0,128),10,(255,0,0),1)

sensor.btnTrig['b']=drawCircle
sensor.startSchedule()

Meowbit LED

Turn On / Off

led1.on()
led2.off()

Toggle Switch

led1.toggle()

Brightness Control

led1.intensity(brightness)
  • Brightness: 0-255

Sample Program

from meowbit import *
from time import sleep

for i in range(5):
    led1.on()
    sleep(1)
    led1.off()
    sleep(1)

from meowbit import *
from time import sleep

for i in range(5):
    led2.toggle()
    sleep(1)

from meowbit import *
import time

for i in range(256):
    led1.intensity(i)
    time.sleep_ms(10)
for i in range(255, 0, -1):
    led1.intensity(i)
    time.sleep_ms(10)

Meowbit Buzzer

Buzzer Tone

buzzer.tone(freq, delay = 0.5)
  • freq: frequency, reference

  • delay: duration in seconds, -1 for infinite

Buzzer Note

buzzer.note(note, delay = 0.5)
  • note: 0~130

  • delay: duration in seconds, -1 for infinite

Buzzer Rest

buzzer.rest(rest)
  • delay: duration in seconds, -1 for infinite

Buzzer Melody

buzzer.melody(m, bpm = 120)
  • m: melody, in the format of m+octave:duration, r for rest

    • for example: “d5:1 b4:1” , “a3:2 r a3:2”

  • bpm: default 120

  • These melodies are built-in

    • CORRECT,NOTICE,ERROR

Buzzer Stop

buzzer.stop()

Sample Program

from meowbit import *

def stopBuzzer():
    buzzer.stop()
    
sensor.btnTrig['a'] = stopBuzzer
sensor.startSchedule()

buzzer.tone(262, 1)
buzzer.rest(1)
buzzer.note(60, 1)
buzzer.rest(1)
buzzer.melody("d r d4:4")
buzzer.rest(1)
buzzer.melody(CORRECT)

from meowbit import *

while 1:
    if sensor.btnValue('a'):
        buzzer.tone(240, -1)
    else:
        buzzer.stop()

Meowbit Sensors

Temperature Sensor

sensor.getTemp()

Light Sensor

sensor.getLight()

Sample Program

from meowbit import *

screen.sync = 0

while 1:
    screen.fill(0)
    lightValue = sensor.getLight()
    tempValue = sensor.getTemp()
    screen.text('Temperature:' + str(tempValue), 20, 50)
    screen.text('Brightness: ' + str(lightValue), 20, 70)
    screen.refresh()

Meowbit Accelerometer

Meowbit上有個3軸的陀螺儀,可以檢測加速度和傾斜度等的數值。

Acceleration Value

sensor.accX()
sensor.accY()
sensor.accZ()

Unit: g(m/s^2)

Gyro Value

sensor.gyroX()
sensor.gyroY()
sensor.gyroZ()

Unit: g(deg/s)

Roll Degree

sensor.roll()

Pitch Degree

sensor.pitch()

Gesture

sensor.gesture(ges)
  • ges: ‘shake’, ‘freefall’, ‘tilt_up’, ‘tilt_down’, ‘tilt_left’, ‘tilt_right’, 'face_up’, ‘face_down’

Gesture Trigger

sensor.gesTrig[ges] = fn
  • ges: ‘shake’, ‘freefall’, ‘tilt_up’, ‘tilt_down’, ‘tilt_left’, ‘tilt_right’, 'face_up’, ‘face_down’

  • fn: function

  • startSchedule(): enable trigger to run in background

Sample Program

from meowbit import *

screen.sync = 0

while 1:
    screen.fill(0)

    screen.text('acc :x/y/z', 20, 10, 1, (168, 233, 74))
    screen.text(round(sensor.accX(), 2), 10, 30)
    screen.text(round(sensor.accY(), 2), 60, 30)
    screen.text(round(sensor.accZ(), 2), 110, 30)

    screen.text('gyro :x/y/z', 10, 50, 1, (74, 233, 168))
    screen.text(round(sensor.gyroX(), 2), 10, 70)
    screen.text(round(sensor.gyroY(), 2), 60, 70)
    screen.text(round(sensor.gyroZ(), 2), 110, 70)

    screen.text('roll:' + str(round(sensor.roll())), 20, 90, 1, (233, 74, 168))
    screen.text('pitch:' + str(round(sensor.pitch())), 20, 110, 1, (233, 168, 74))

    screen.text('face_up', 100, 95, 1, (74, 168, 233))
    screen.text(sensor.gesture('face_up'), 105, 110)

    screen.refresh()

GPIO

Initiate GPIO Pin mode

pin = MeowPin(pin, mode)
  • pin: P1-P20

  • mode:

    • IN: Digital In

    • OUT: Digital Out

    • ANALOG: Analog In

    • PWM: Analog Out

Digital Read

getDigital()

Analog Read

getAnalog()

Digital Write

setDigital(val)

Analog Write

setAnalog(val)
set_pulse_width(duty)

Sample Program

from meowbit import *

screen.sync = 0
in_pin = MeowPin('P1', ANALOG)
out_pin = MeowPin('P2', OUT)

while 1:
    screen.fill(0)
    screen.text(in_pin.getAnalog())
    screen.refresh()
    if (in_pin.getAnalog() > 2000):
        out_pin.setDigital(1)
    else:
        out_pin.setDigital(0)

Last updated