update python toolings

This commit is contained in:
2024-02-25 00:01:52 +01:00
parent 69a01f167e
commit 6508e6a07c
5 changed files with 84 additions and 31 deletions

View File

@@ -1,7 +1,12 @@
# Specify the .proto file
set(PROTO_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../proto)
set(PROTO_FILE ${PROTO_SRC_DIR}/firmware.proto)
set(PROTO_FILE
${PROTO_SRC_DIR}/firmware.proto
${PROTO_SRC_DIR}/light.proto
${PROTO_SRC_DIR}/usb.proto
${PROTO_SRC_DIR}/cls_device.proto
)
# Specify where you want to generate the python code
set(PYTHON_OUT_DIR ${CMAKE_CURRENT_SOURCE_DIR})

View File

@@ -3,37 +3,12 @@ import struct
from google.protobuf.message import DecodeError
from serial.tools import list_ports
from firmware_pb2 import RequestDeviceList, Device, ResponseDeviceList, UsbPackageType
def make_header(typeid: UsbPackageType, length: int) -> bytearray:
struct_format = '<HHB' # '<' for little-endian, 'H' for uint16_t, 'B' for uint8_t
# Calculate the check byte as the sum of length and type
typeidint = int(typeid)
check = (length & 0xFF) + ((length >> 8) & 0xFF) + (typeidint & 0xFF) + ((typeidint >> 8) & 0xFF)
packed_data = struct.pack(struct_format, length, typeid, check)
return packed_data
def send_package(typeid : UsbPackageType, data: bytearray, serial: serial.Serial):
head = make_header(typeid, len(data))
package = head + data
serial.write(package)
from cls_device_pb2 import RequestDeviceList, Device, ResponseDeviceList
from usb_pb2 import UsbPackageType
from vcp_driver import *
if __name__ == "__main__":
stm_port = None
for port in list_ports.comports():
print(port)
if "STM32 Virtual ComPort" in port.description:
stm_port = port.device
break
if stm_port is None:
print("STM32 Virtual ComPort not found")
exit(-1)
else:
# Open the serial port
ser = serial.Serial(stm_port,baudrate=5000000)
ser = setup_connection()
# Create a RequestDeviceList message
request = RequestDeviceList()
request.msg = 1 # or whatever value you want to set

35
tools/light_test.py Normal file
View File

@@ -0,0 +1,35 @@
import serial
import struct
from google.protobuf.message import DecodeError
from serial.tools import list_ports
from light_pb2 import LightGlobalBrightness, LightTheme, LightGlobalTheme, LightThemeSettings
from usb_pb2 import UsbPackageType
from vcp_driver import *
if __name__ == "__main__":
ser = setup_connection()
# Create a message
request = LightGlobalBrightness()
request.brightness = 128
# Serialize the request to a bytearray
request_data = request.SerializeToString()
# Send the request
send_package(UsbPackageType.LIGHT_GLOBAL_BRIGHT, request_data, ser)
request = LightGlobalTheme()
request.theme = 1
request_data = request.SerializeToString()
send_package(UsbPackageType.LIGHT_GLOBAL_THEME, request_data, ser)
request = LightThemeSettings()
request.deviceId = 1
request.theme = 1
request.rgb = 0x00f000f0
request.brightness = 255
request.animation = 2
request_data = request.SerializeToString()
send_package(UsbPackageType.LIGHT_SETTING_THEME, request_data, ser)

View File

@@ -7,7 +7,8 @@ from google.protobuf.message import DecodeError
from serial.tools import list_ports
from crccheck.crc import Crc
from firmware_pb2 import FirmwareStart, FirmwareFileCheck , FirmwarePackage, FirmwarePackageAck, FirmwareDone, UsbPackageType
from firmware_pb2 import FirmwareStart, FirmwareFileCheck , FirmwarePackage, FirmwarePackageAck, FirmwareDone
from usb_pb2 import UsbPackageType
Crc32 = Crc(32,0x04C11DB7,0xffffffff)

37
tools/vcp_driver.py Normal file
View File

@@ -0,0 +1,37 @@
import serial
import struct
from usb_pb2 import UsbPackageType
from serial.tools import list_ports
def make_header(typeid: UsbPackageType, length: int) -> bytearray:
struct_format = '<HHB' # '<' for little-endian, 'H' for uint16_t, 'B' for uint8_t
# Calculate the check byte as the sum of length and type
typeidint = int(typeid)
check = (length & 0xFF) + ((length >> 8) & 0xFF) + (typeidint & 0xFF) + ((typeidint >> 8) & 0xFF)
packed_data = struct.pack(struct_format, length, typeid, check)
return packed_data
def send_package(typeid : UsbPackageType, data: bytearray, serial: serial.Serial):
head = make_header(typeid, len(data))
package = head + data
serial.write(package)
def setup_connection() -> serial.Serial:
stm_port = None
for port in list_ports.comports():
print(port)
if "STM32 Virtual ComPort" in port.description:
stm_port = port.device
break
if stm_port is None:
print("STM32 Virtual ComPort not found")
exit(-1)
else:
# Open the serial port
ser = serial.Serial(stm_port,baudrate=5000000)
return ser