Device Script Guide: Device Control


opbt Device

Module structure

class device:

	def SetOutputMode(self,mode: int) -> int: ...
	def GetOutputMode(self) -> int: ...
	def UartConfig(self, baudrate: int, parity: str, DataBit: int, StopBit: int): ...
	def TransparentModeOn(self): ...
	def TransparentModeOff(self): ...
	def IecAutoOn(self): ...
	def IecAutoOff(self): ...
	def UartSendByte(self, ch: int): ...
	def UartGetByte(self) -> int: ...
	def UartSendString(self, buf: str, len: int): ...
	def UartSendStringNoWait(self, buf: str, len: int): ...
	def BtUartSendByte(self, ch: int): ...
	def BtUartGetByte(self) -> int: ...
	def BtUartSendString(self, buf: str, len: int): ...
	def BtUartSendStringNoWait(self, buf: str, len: int): ...
	def BtUartSendBytes(self, buf: bytes, len: int): ...
	def LedSet(self, leds: int, mode: int): ...
	def LedBlink(self, leds: int, cnt: int, duty: int, time:int): ...
	def GetKeyStatus(self) -> int: ...
	def TmosSystemProcess(self): ...
	def LcdClear(self): ...
	def LcdTextColor(self,fc: int,bc: int): ...
	def LcdTextXY(self, x: int,y:int): ...
	def LcdPrint(self,str_p: str): ...
	def LcdFill(self, xsta: int,ysta: int,xend: int,yend: int,color: int): ...

更多函数持续更新中…

opbt.device.SetOutputMode()

Overview

设置脚本显示输出的设备,程序运行时首先要设置这个输出的方式,例如print函数的输出

Syntax

opbt.device.SetOutputMode(mode)

Parameters

  • mode : 这是0-3的数值,表示脚本中输出信息的设备
    • 0 : 自动选择默认的输出设备
    • 1 : 蓝牙端
    • 2 : 光电头端
    • 3 : 屏幕显示器

Return

返回当前设置的值(0-3)

Sample

在LCD屏幕上显示 Hello World!

>>> opbt.device.SetOutputMode(3)
>>> print("Hello World!")

opbt.device.GetOutputMode()

Overview

得到当前显示输出的设备

Syntax

opbt.device.GetOutputMode()

Parameters

Return

  • 返回当前设置的值(0-3)
    • 0 : 自动选择默认的输出设备
    • 1 : 蓝牙端
    • 2 : 光电头端
    • 3 : 屏幕显示器

Sample

>>> a=opbt.device.GetOutputMode()
>>> print("OutputDevice=",a)
OutputDevice= 0

opbt.device.TransparentModeOn()

Overview

设置OPBT设备进入透明传输方式

Syntax

opbt.device.TransparentModeOn()

Parameters

Return

Sample

>>> opbt.device.TransparentModeOn()

opbt.device.TransparentModeOff()

Overview

设置OPBT设备退出透明传输方式

Syntax

opbt.device.TransparentModeOff()

Parameters

Return

Sample

>>> opbt.device.TransparentModeOff()

opbt.device.IecAutoOn()

Overview

设置OPBT设备进入Iec自动波特率切换模式,支持IEC65026的方式C和E

Syntax

opbt.device.IecAutoOn()

Parameters

Return

Sample

>>> opbt.device.IecAutoOn()

opbt.device.IecAutoOff()

Overview

设置OPBT设备退出Iec自动波特率切换模式

Syntax

opbt.device.IecAutoOff()

Parameters

Return

Sample

>>> opbt.device.IecAutoOff()

opbt.device.UartConfig()

Overview

设置光电头端的串口通讯参数

Syntax

UartConfig(baudrate,parity,databit,stopbit)

Parameters

  • baudrate 波特率值从300-57600,intger -典型值:300,600,1200,2400,4800,9600,19200,38400,57600等
  • parity 校验值, string
    • “N” : None 无校验位
    • “E” : Even 偶校验位
    • “O” : Odd 奇检验位
    • 默认为无校验位
  • databit 数据位长度(5-8),数值型
    • 5 : 数据位为5位
    • 6 : 数据位为6位
    • 7 : 数据位为7位
    • 8 : 数据位为8位
    • 默认数据位为8位
  • stopbit 停止位(1-2),数值型
    • 1 : 停止位为1
    • 2 : 停止位为2
    • 默认停止位为1

Return

无返回值

Sample

设置串口波特率为9600bps,无校验位,数据位长度为8,停止位为1

>>> opbt.device.UartConfig(9600,"N",8,1)

opbt.device.UartSendByte()

Overview

用设置好的串口参数发送一个字节数据出去

Syntax

opbt.device.UartSendByte(ch)

Parameters

  • ch : intger, 待发送的数据的字节代码

Return

Sample

发送数据"123"

>>> opbt.device.UartConfig(9600,"N",8,1)
>>> opbt.device.UartSendByte(49)
>>> opbt.device.UartSendByte(50)
>>> opbt.device.UartSendByte(51)

opbt.device.UartGetByte()

Overview

从串口取一个字节数据

Syntax

opbt.device.UartGetByte()

Parameters

Return

  • 返回整数型值
    • -1 : 没有接收到数据
    • 其它数值 : 字节代码

Sample

>>> opbt.device.UartConfig(9600,"N",8,1)
>>> ch=opbt.device.UartGetByte()
>>> print(ch)
-1

opbt.device.UartSendString()

Overview

用设置好的串口参数发送指定长度的字符串出去,并等待最后一个数据发送完成才返回

Syntax

opbt.device.UartSendString(string,length)

Parameters

  • string : 字符串,待发送的字符串数据
  • length : 整型值,发送数据的长度

Return

Sample

发送数据"123"

>>> opbt.device.UartConfig(9600,"N",8,1)
>>> opbt.device.UartSendString("123",3)

opbt.device.UartSendStringNoWait()

Overview

用设置好的串口参数发送指定长度的字符串出去,数据发完马上返回,这个时候如果更改串口参数,则有可能破坏之前仍在缓冲区未发送的数据

Syntax

opbt.device.UartSendStringNoWait(string,length)

Parameters

  • string : 字符串,待发送的字符串数据
  • length : 整型值,发送数据的长度

Return

Sample

发送数据"123"

>>> opbt.device.UartConfig(9600,"N",8,1)
>>> opbt.device.UartSendStringNoWait("123",3)

opbt.device.BtUartSendByte()

Overview

通过蓝牙串口发送一个字节数据

Syntax

opbt.device.BtUartSendByte(ch)

Parameters

  • ch : 整型值,待发送的数据的字节代码

Return

Sample

通过蓝牙串口发送数据"123"

#!pika
opbt.device.SetOutputMode(1)  # 设置输出到蓝牙端
opbt.device.BtUartSendByte(49)  # 发送字符 '1'
opbt.device.BtUartSendByte(50)  # 发送字符 '2'
opbt.device.BtUartSendByte(51)  # 发送字符 '3'
#!pika

opbt.device.BtUartGetByte()

Overview

从蓝牙串口读取一个字节数据

Syntax

opbt.device.BtUartGetByte()

Parameters

Return

  • 返回整型值
    • -1 : 没有接收到数据
    • 其它数值 : 字节代码

Sample

从蓝牙串口读取数据

#!pika
opbt.device.SetOutputMode(1)  # 设置输出到蓝牙端
ch = opbt.device.BtUartGetByte()
if ch != -1:
    print("Received:", ch)
else:
    print("No data received")
#!pika

opbt.device.BtUartSendString()

Overview

通过蓝牙串口发送指定长度的字符串,并等待最后一个数据发送完成才返回(阻塞方式)

Syntax

opbt.device.BtUartSendString(string,length)

Parameters

  • string : 字符串,待发送的字符串数据
  • length : 整型值,发送数据的长度

Return

Sample

通过蓝牙串口发送数据"Hello"

#!pika
opbt.device.SetOutputMode(1)  # 设置输出到蓝牙端
opbt.device.BtUartSendString("Hello", 5)
#!pika

opbt.device.BtUartSendStringNoWait()

Overview

通过蓝牙串口发送指定长度的字符串,数据发完马上返回(非阻塞方式)

Syntax

opbt.device.BtUartSendStringNoWait(string,length)

Parameters

  • string : 字符串,待发送的字符串数据
  • length : 整型值,发送数据的长度

Return

Sample

通过蓝牙串口非阻塞方式发送数据"Hello"

#!pika
opbt.device.SetOutputMode(1)  # 设置输出到蓝牙端
opbt.device.BtUartSendStringNoWait("Hello", 5)
# 立即返回,不等待发送完成
#!pika

opbt.device.BtUartSendBytes()

Overview

通过蓝牙串口发送字节数据,跳过协议头,直接发送原始字节数据

Syntax

opbt.device.BtUartSendBytes(buf,length)

Parameters

  • buf : bytes类型,待发送的字节数据
  • length : 整型值,发送数据的长度

Return

Sample

通过蓝牙串口发送字节数据

#!pika
opbt.device.SetOutputMode(1)  # 设置输出到蓝牙端
data = b'\x01\x02\x03\x04\x05'
opbt.device.BtUartSendBytes(data, len(data))
#!pika

opbt.device.LedSet()

Overview

Turn ON/OFF/TOGGLE given LEDs

Syntax

opbt.device.LedSet(leds,mode)

Parameters

  • leds : intger, bit mask value of leds to be turned ON/OFF/TOGGLE
    • 0x01 : 屏幕左边的绿色指示灯
    • 0x02 : 屏幕左边的红色指示灯
    • 0x04 : 屏幕左边的蓝色指示灯
    • 0x08 : 屏幕右边的绿色指示灯
    • 0x10 : 屏幕右边的红色指示灯
    • 0x20 : 屏幕右边的蓝色指示灯
    • 0x40 : 手电筒
  • mode : 整型值,TOGGLE, ON, OFF
    • 0 : Off
    • 1 : On
    • 2 : blink 等效于ledBlink(leds,1,5,1000)
    • 4 : flash 等效于ledBlink(leds,50,5,1000)
    • 8 : toggle

Return

Sample

让左边的红色和右边的蓝色指示灯亮

>>> opbt.device.LedSet(0xff,0)  # 先关闭所有指示灯
>>> opbt.device.LedSet(0x02|0x20,1)

Overview

Blink the leds, 需要TmosSystemProcess支持

Syntax

opbt.device.LedBlink(leds,cnt,duty,time)

Parameters

  • leds : 整数值,bit mask value of leds to be turned ON/OFF/TOGGLE
    • 0x01 : 屏幕左边的绿色指示灯
    • 0x02 : 屏幕左边的红色指示灯
    • 0x04 : 屏幕左边的蓝色指示灯
    • 0x08 : 屏幕右边的绿色指示灯
    • 0x10 : 屏幕右边的红色指示灯
    • 0x20 : 屏幕右边的蓝色指示灯
    • 0x40 : 手电筒
  • cnt : 整数值, number of blinks
  • duty : 整数值, the percentage in each period where the led will be on
  • time : 整数值, length of each cycle in milliseconds

Return

Sample

让左边的红色灯闪烁

opbt.device.LedSet(0xff,0)
opbt.device.LedBlink(0x02,10,50,300)
for i in range(500):  
    opbt.device.SystemProcess()
opbt.device.LedSet(0xff,0)

opbt.device.GetKeyStatus()

Overview

Read the current value of a key

Syntax

opbt.device.GetKeyStatus()

Parameters

Return

  • current keys status
    • bit 1 : =1 按下右键
    • bit 2 : =1 按下左键

Sample

>>> k=opbt.device.GetKeyStatus()
>>> print(k)
0

opbt.device.SystemProcess()

Overview

opbt系统处理函数,需要不断在主函数中运行

Syntax

opbt.device.SystemProcess()

Parameters

Return

Sample

opbt.device.LedSet(0xff,0)
opbt.device.LedBlink(0x02,10,50,300)
for i in range(500):  
    opbt.device.SystemProcess()
opbt.device.LedSet(0xff,0)

opbt.device.LcdClear()

Overview

LCD屏幕清屏

Syntax

opbt.device.LcdClear()

Parameters

Return

Sample

opbt.device.LcdClear()

opbt.device.LcdTextColor()

Overview

设置LCD屏幕文字显示的颜色

Syntax

opbt.device.LcdTextColor(fc,bc)

Parameters

  • fc : 整数值,前景色
  • bc : 整数值,背景色
  • 颜色值
    • 0xFFFF : White
    • 0x0000 : Black
    • 0x001F : Blue
    • 0xF800 : Red
    • 0x07E0 : Green
    • 0xFFE0 : Yellow
    • 0xFD20 : Orange
    • 0xBC40 : Brown
    • 0x8430 : Gray
    • 0xF81F : BRED
    • 0xFFE0 : GRED
    • 0x07FF : GBLUE
    • 0xF81F : MAGENTA
    • 0x7FFF : CYAN
    • 0xFC07 : BRRED
    • 0x01CF : DRAKBLUE
    • 0x7D7C : LIGHTBLUE
    • 0x5458 : GRAYBLUE
    • 0x841F : LIGHTGREEN
    • 0xC618 : LGRAY
    • 0xA651 : LGRAYBLUE
    • 0x2B12 : LBBLUE

Return

Sample

设置屏幕文字颜色为红色,文字背景为黑色

opbt.device.LcdClear()
opbt.device.LcdTextColor(0xf800,0x0000)
opbt.device.LcdPrint("Hello World!")

opbt.device.LcdTextXY()

Overview

设置LCD屏幕文字显示的坐标

Syntax

opbt.device.LcdTextXY(x,y)

Parameters

  • x : 整数值,范围是0-19
  • y : 整数值,范围是0-4

Return

Sample

opbt.device.LcdClear()
opbt.device.LcdTextColor(0xf800,0x0000)
opbt.device.LcdTextXY(5,2)
opbt.device.LcdPrint("Hello World!")

opbt.device.LcdPrint()

Overview

在LCD屏幕显示字符串

Syntax

opbt.device.LcdPrint(strp)

Parameters

  • strp : 字符串, 待显示的字符串

Return

Sample

opbt.device.LcdClear()
opbt.device.LcdTextColor(0xf800,0x0000)
opbt.device.LcdTextXY(5,2)
opbt.device.LcdPrint("Hello World!")

opbt.device.LcdFill()

Overview

在矩形框内填充指定的颜色

Syntax

opbt.device.LcdFill(xsta,ysta,xend,yend,color)

Parameters

  • xsta : 整数值,左上角x坐标,范围是0-159
  • ysta : 整数值,左上角y坐标,范围是0-79
  • xend : 整数值,右上角x坐标,范围是0-159
  • yend : 整数值,右上角y坐标,范围是0-79
  • color : 整数值,颜色值参考LcdTextColor()

Return

Sample

opbt.device.LcdClear()
opbt.device.LcdFill(10,20,50,60,0xffff)