OP-BTS Device Script Interpreter Guide: Syntax

Principle introduction

Demo show

Demo 01 Light up

import PikaStdLib
import machine

mem = PikaStdLib.MemChecker()
io1 = machine.GPIO()
time = machine.Time()

io1.setPin('PA8')
io1.setMode('out')
io1.enable()
io1.low()

print('hello pikascript')
print('mem.max:')
mem.max()
print('mem.now:')
mem.now()

while True:
    io1.low()
    time.sleep_ms(500)
    io1.high()
    time.sleep_ms(500)
    

Look at the script, it’s all Python3 standard syntax.

Demo 02 Serial port test


Open a serial port and try to read two characters

This is the output result.


# Syntax support

Support for a subset of python3 standard syntax.

#### object support

|Syntax|Runtime|Shell|
|--|--|--|
|Module Definition |-|-|
|Module import |√|√|
|Class Definition |√|√|√|
|class inheritance |√|√|√|
|method definition |√|√|√|
|method overloading |√|√|√|
|method invocation |√|√|
|parameter definition |√|√|√|
|parameter assignment |√|√|√|
|object creation |√|√|√|
|object destruction |√|√|√|
|object nesting |√|√|√|
|control flow |√|√|√|
{.table .table-striped}

#### Operator

| + | - | * | / | == | > | < | >= | <= | % | ** | // | != | & | >> | << | and | or | not | in | += | -= | *= | /= |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|
{.table .table-striped}

#### Control flow

| Syntax | State |
| --- | --- |
| if | √ |
| while | √ |
| for in [list] | √ | for in range(a, b)
| for in range(a, b) | √ | for in [list] | √ | for in range(a, b) | √ |
| for in [dict] | √ | for in range(a, b) | √
| if elif else | √ | for break/continue | √
| for break/continue | √ | while break/continue | √
| while break/continue | √ |
{.table .table-striped}

#### Module
| Syntax | Python Module |
| --- | --- |
| import [module] | √ |
| import [module] as | √ |
| from [module] import [class/function>]| √ |
| from [module] import [class/function>] as | √ |
| from [module] import * | - |
{.table .table-striped}

#### List/Dict

| Syntax | State |
| --- | --- |
| l = list() | √ |
| l = [a, b, c] | √ |
| d = dict() | √ |
| d = {'a':x, 'b':y, 'c':z} | √ |
{.table .table-striped}

#### Exception

| Syntax | State |
| --- | --- |
|try:| √ |
|except:| √ |
|except [Exception]:| - |
|except [Exception] as [err]: | - |
|except: ... 
|raise:| √ |
|raise [Exception]:| - |finally:| -
|finally:| - |
{.table .table-striped}

#### Slice

| Syntax | str | bytes | list |
| --- | --- | --- | --- |
| test[i] | √ | √ | √ |
| test[a : b] | √ | √ | √ | √ 
| test[a :] | √ | √ | √ | √
{.table .table-striped}

#### Other keywords/Syntax

| yield | is | comprehensions |
| --- | --- | --- |
| - - | √ | - |
{.table .table-striped}