StreamDock Base Class
All control classes for specific device models must inherit from the StreamDock base class. The StreamDock base class provides the following methods:
Device Control Methods
Open Device
def open(self):
"""
Opens device connection and starts reading thread
"""
self.transport.open(bytes(self.path,'utf-8'))
self._setup_reader(self._read)
Initialize Device
def init(self):
"""
Initializes the device, including waking screen, setting brightness, clearing all icons and refreshing display
"""
self.wakeScreen()
self.set_brightness(100)
self.clearAllIcon()
self.refresh()
Close Device
def close(self):
"""
Closes device connection and clears all display
"""
self.disconnected()
# self.transport.close()
Disconnect and Clear All Display
def disconnected(self):
"""
Disconnects and clears all display
"""
self.transport.disconnected()
Screen Control Methods
Wake Screen
def wakeScreen(self):
"""
Wakes up device screen
"""
self.transport.wakeScreen()
Refresh Device Display
def refresh(self):
"""
Refreshes device display
"""
self.transport.refresh()
Set Brightness
@abstractmethod
def set_brightness(self, percent):
"""
Sets device screen brightness
Parameters:
percent (int): Brightness percentage (0-100)
Returns:
int: Operation result status code
"""
pass
Key Control Methods
Set Key Icon
@abstractmethod
def set_key_image(self, key, image):
"""
Sets icon for specified key
Parameters:
key (int): Key number
image (str): Image file path
Returns:
int: Operation result status code
"""
pass
Clear Specific Key Icon
def clearIcon(self, index):
"""
Clears icon for specified key
Parameters:
index (int): Key number
Returns:
int: Operation result status code, returns -1 if key is out of range
"""
origin = index
index = self.key(index)
if index not in range(1, 16):
print(f"key '{origin}' out of range. you should set (1 ~ 15)")
return -1
self.transport.clearKey(index)
Clear All Key Icons
def clearAllIcon(self):
"""
Clears all key icons
Returns:
int: Operation result status code
"""
self.transport.clearAllKeys()
Touchscreen Control Methods
Set Touchscreen Image
@abstractmethod
def set_touchscreen_image(self, image):
"""
Sets touchscreen background image
Parameters:
image (str): Image file path
Returns:
int: Operation result status code
"""
pass
Device Information Methods
Get Device Path
def getPath(self):
"""
Gets device path
Returns:
str: Device path
"""
return self.path
Get Device Serial Number
@abstractmethod
def get_serial_number(self, length):
"""
Gets device serial number
Parameters:
length (int): Data length to read
Returns:
bytes: Device serial number data
"""
pass
Get Device ID
def id(self):
"""
Gets physical identifier of the device
Returns:
str: Device identifier
"""
return self.getPath()
Data Reading Methods
Get Device Feedback Information
def read(self):
"""
Reads device feedback information
Returns:
bytes: Data returned by device
"""
data = self.transport.read_(13)
return data
Continuously Monitor Device Feedback (Recommended to use in separate thread)
def whileread(self):
"""
Continuously monitors device feedback information, recommended to use in separate thread
"""
while 1:
try:
data = self.read()
if data != None and len(data) >= 11:
if (data[:3].decode('utf-8', errors='ignore') == "ACK" and data[5:7].decode('utf-8', errors='ignore')):
if data[10] == 0x01 and data[9] > 0x00 and data[9] <= 0x0f:
if (self.KEY_MAP):
print("按键{}".format(KEY_MAPPING[data[9]]) + "被按下")
else:
print("按键{}".format(data[9]) + "被按下")
elif data[10] == 0x00 and data[9] > 0x00 and data[9] <= 0x0f:
if (self.KEY_MAP):
print("按键{}".format(KEY_MAPPING[data[9]]) + "抬起")
else:
print("按键{}".format(data[9]) + "抬起")
except Exception as e:
print("发生错误:")
traceback.print_exc()
break
Event Callback Methods
Set Key Callback
def set_key_callback(self, callback):
"""
Sets key state change callback function
Parameters:
callback (function): Callback function that receives parameters (device, key, state)
"""
self.key_callback = callback
Set Async Key Callback
def set_key_callback_async(self, async_callback, loop=None):
"""
Sets async key state change callback function
Parameters:
async_callback (function): Async callback function
loop (optional): asyncio event loop
"""
import asyncio
loop = loop or asyncio.get_event_loop()
def callback(*args):
asyncio.run_coroutine_threadsafe(async_callback(*args), loop)
self.set_key_callback(callback)
Set Touchscreen Callback
def set_touchscreen_callback(self, callback):
"""
Sets touchscreen interaction callback function
Parameters:
callback (function): Callback function
"""
self.touchscreen_callback = callback
Set Async Touchscreen Callback
def set_touchscreen_callback_async(self, async_callback, loop=None):
"""
Sets async touchscreen interaction callback function
Parameters:
async_callback (function): Async callback function
loop (optional): asyncio event loop
"""
import asyncio
loop = loop or asyncio.get_event_loop()
def callback(*args):
asyncio.run_coroutine_threadsafe(async_callback(*args), loop)
self.set_touchscreen_callback(callback)
Image Format Methods
Get Key Image Format
@abstractmethod
def key_image_format(self):
"""
Gets key image format information
Returns:
dict: Dictionary containing image format information
"""
pass
Get Touchscreen Image Format
@abstractmethod
def touchscreen_image_format(self):
"""
Gets touchscreen image format information
Returns:
dict: Dictionary containing image format information
"""
pass
Key Mapping
Key Mapping Conversion
def key(self, k):
"""
Converts key number based on device configuration
Parameters:
k (int): Original key number
Returns:
int: Converted key number
"""
if (self.KEY_MAP):
return KEY_MAPPING[k]
else:
return k
Device Properties
Each StreamDock device has the following properties:
KEY_COUNT: Number of device keysKEY_COLS: Number of key columnsKEY_ROWS: Number of key rowsKEY_PIXEL_WIDTH: Key image width (pixels)KEY_PIXEL_HEIGHT: Key image height (pixels)KEY_IMAGE_FORMAT: Key image formatKEY_FLIP: Key image flip settings (horizontal, vertical)KEY_ROTATION: Key image rotation angleKEY_MAP: Whether to use key mappingTOUCHSCREEN_PIXEL_WIDTH: Touchscreen width (pixels)TOUCHSCREEN_PIXEL_HEIGHT: Touchscreen height (pixels)TOUCHSCREEN_IMAGE_FORMAT: Touchscreen image formatTOUCHSCREEN_FLIP: Touchscreen image flip settingsTOUCHSCREEN_ROTATION: Touchscreen image rotation angleDIAL_COUNT: Number of dialsDECK_TYPE: Device typeDECK_VISUAL: Whether visual feedback is supportedDECK_TOUCH: Whether touch is supportedfeature_option: Device feature options (FeatrueOption instance)
FeatrueOption Class
The FeatrueOption class is used to describe device extended feature characteristics:
class FeatrueOption:
def __init__(self):
self.hasRGBLed = False # Whether RGB LED is supported
self.ledCounts = 0 # Number of LEDs
self.supportConfig = False # Whether configuration is supported
LED Control Methods
Set LED Brightness
def set_led_brightness(self, percent):
"""
Sets device LED brightness
Parameters:
percent (int): Brightness percentage (0-100)
Returns:
int: Operation result status code, returns None if device doesn't support LED
"""
if self.feature_option.hasRGBLed:
return self.transport.set_led_brightness(percent)
Set LED Color
def set_led_color(self, r, g, b):
"""
Sets device LED color
Parameters:
r (int): Red component (0-255)
g (int): Green component (0-255)
b (int): Blue component (0-255)
Returns:
int: Operation result status code, returns None if device doesn't support LED
"""
if self.feature_option.hasRGBLed:
return self.transport.set_led_color(self.feature_option.ledCounts, r, g, b)
Reset LED Effect
def reset_led_effect(self):
"""
Resets device LED effect to default state
Returns:
int: Operation result status code, returns None if device doesn't support LED
"""
if self.feature_option.hasRGBLed:
return self.transport.reset_led_color()
