OP-BTS Device Examples
This document summarizes common OPBTPython script examples for OP-BTS devices: LED, serial, LCD, keys, transparent transmission, IEC auto baudrate, security unlock, and comprehensive applications.
Module Introduction
The opbt module provides device hardware access interface. No need to import other modules before calling opbt.device (subject to firmware). Built-in example:
#!pika
import opbt
opbt.device.SetOutputMode(1) # Output to Bluetooth
print('Hello')
#!pika
1. LED Control Examples
1.1 Light Specific LED
#!pika
import opbt
# Turn off all LEDs first
opbt.device.LedSet(0xff, 0)
# Light up top-left green, top-right blue
opbt.device.LedSet(0x02 | 0x20, 1)
#!pika
1.2 LED Blinking
#!pika
import opbt
opbt.device.LedSet(0xff, 0)
opbt.device.LedBlink(0x02, 10, 50, 300) # Top-left green blink 10 times
for i in range(500):
opbt.device.SystemProcess()
opbt.device.LedSet(0xff, 0)
#!pika
Note: LedBlink requires calling SystemProcess() periodically in loop to see blinking.
2. Serial Communication Examples
2.1 Configure and Send String
#!pika
import opbt
opbt.device.UartConfig(9600, "N", 8, 1)
opbt.device.UartSendString("Hello", 5)
#!pika
2.2 Send Single Byte
#!pika
import opbt
opbt.device.UartConfig(9600, "N", 8, 1)
opbt.device.UartSendByte(65) # 'A'
opbt.device.UartSendByte(66) # 'B'
#!pika
2.3 Read One Byte
#!pika
import opbt
opbt.device.UartConfig(9600, "N", 8, 1)
ch = opbt.device.UartGetByte()
if ch >= 0:
print(ch)
#!pika
3. LCD Display Examples
3.1 Clear Screen and Display Text
#!pika
import opbt
opbt.device.SetOutputMode(3) # Output to LCD
opbt.device.LcdClear()
opbt.device.LcdTextColor(0xF800, 0x0000) # Red text on black background
opbt.device.LcdTextXY(0, 0)
opbt.device.LcdPrint("Hello OPBTPython!")
#!pika
3.2 Specify Position and Color
#!pika
import opbt
opbt.device.LcdClear()
opbt.device.LcdTextColor(0x07E0, 0x0000) # Green
opbt.device.LcdTextXY(5, 2)
opbt.device.LcdPrint("Line 3")
#!pika
3.3 Fill Rectangle
#!pika
import opbt
opbt.device.LcdClear()
opbt.device.LcdFill(10, 20, 50, 60, 0xFFFF) # White rectangle
#!pika
4. Key Detection Examples
#!pika
import opbt
opbt.device.SetOutputMode(1) # Output to Bluetooth for observation
while True:
k = opbt.device.GetKeyStatus()
if k & 1:
print("Right key")
if k & 2:
print("Middle key")
opbt.device.Sleep(100)
#!pika
Note: Bit 1 represents right key, bit 2 represents middle key (subject to device definition).
5. Transparent Transmission Mode Example
#!pika
import opbt
opbt.device.TransparentModeOn() # Enter transparent transmission
# Device now transparently transmits data between optical probe and Bluetooth, script can do other logic
# .
opbt.device.TransparentModeOff() # Exit transparent transmission
#!pika
6. IEC Auto Baudrate Example
#!pika
import opbt
opbt.device.IecAutoOn() # Enable IEC auto baudrate (for IEC62056-21 mode C/E)
# Device can automatically switch baudrate when communicating with meters
# .
opbt.device.IecAutoOff() # Disable
#!pika
7. Comprehensive Application Example
Following example: Set output to Bluetooth, configure serial, control LED, poll keys and display on LCD.
#!pika
import opbt
opbt.device.SetOutputMode(1)
opbt.device.UartConfig(9600, "N", 8, 1)
opbt.device.LedSet(0xff, 0)
opbt.device.LcdClear()
opbt.device.LcdTextColor(0x07E0, 0x0000)
opbt.device.LcdTextXY(0, 0)
opbt.device.LcdPrint("Ready")
count = 0
for i in range(200):
opbt.device.SystemProcess()
k = opbt.device.GetKeyStatus()
if k != 0:
count += 1
opbt.device.LedSet(0x02, 1)
opbt.device.LcdTextXY(0, 1)
opbt.device.LcdPrint("Key " + str(count))
opbt.device.Sleep(50)
opbt.device.LedSet(0xff, 0)
#!pika
8. Security unlock examples
Requires firmware 4.84 or later.
When the device has security enabled (SecurityEnable), scripts can call CheckUnlock() to verify unlock status before sensitive operations; if not unlocked, the LCD password input screen is shown.
#!pika
import opbt
if opbt.device.CheckUnlock() != 1:
print("Unlock canceled")
else:
print("Unlocked, proceed")
#!pika
To query whether the device is currently unlocked only (no UI):
#!pika
import opbt
if opbt.device.IsUnlocked():
print("Already unlocked")
else:
print("Locked")
#!pika
Notes
- Before using LCD or
print, it’s recommended toSetOutputModefirst, otherwise output may not be visible. LedBlinkrequires callingSystemProcess()in loop to take effect.- Serial parameters must match the peer; supported baudrate range see device documentation.
Related Links
- Main Manual: OPBTPython Script Manual
- Device API description please refer to
ScriptDoc/cn/cn_module-opbt-device.mdin project (or device commands/developer documentation).