# 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](https://www.yuque.com/attachments/yuque/0/2020/zip/1432972/1600221900298-00115433-2988-472c-938c-09802ee59c35.zip?from=https%3A%2F%2Fwww.yuque.com%2Fkittenbot%2Fhardwares%2Fmeowbit-micropython)

```
# 顯示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](http://cdn.kittenbot.cn/freqToTone.txt)
* 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)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sharinghub-eng.kittenbot.hk/main-controllers/meowbit/meowbit-programming-tutorial/meowbit-micropython-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
