using proto enum for types
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
import serial
|
||||
import struct
|
||||
import time
|
||||
from google.protobuf.internal import encoder
|
||||
from google.protobuf.internal import decoder
|
||||
import sys
|
||||
from google.protobuf.message import DecodeError
|
||||
from serial.tools import list_ports
|
||||
from crccheck.crc import Crc32
|
||||
from crccheck.crc import Crc
|
||||
|
||||
from firmware_pb2 import FirmwareStart, FirmwarePackage, FirmwarePackageAck, FirmwareDone
|
||||
from firmware_pb2 import FirmwareStart, FirmwarePackage, FirmwarePackageAck, FirmwareDone, UsbPackageType
|
||||
|
||||
|
||||
Crc32 = Crc(32,0x04C11DB7,0xffffffff)
|
||||
|
||||
def load_firmware(filename):
|
||||
# Open the file in binary mode
|
||||
@@ -24,15 +24,16 @@ def load_firmware(filename):
|
||||
return crc, size, packages
|
||||
|
||||
|
||||
def make_header(typeid: int, length: int) -> bytearray:
|
||||
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
|
||||
check = (length & 0xFF) + ((length >> 8) & 0xFF) + (typeid & 0xFF) + ((typeid >> 8) & 0xFF)
|
||||
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 :int, data: bytearray, serial: serial.Serial):
|
||||
def send_package(typeid : UsbPackageType, data: bytearray, serial: serial.Serial):
|
||||
head = make_header(typeid, len(data))
|
||||
package = head + data
|
||||
serial.write(package)
|
||||
@@ -55,7 +56,7 @@ def receive_ack(serial):
|
||||
print('Incomplete message')
|
||||
return None
|
||||
|
||||
if typeid != 0xf04:
|
||||
if typeid != UsbPackageType.FIRMWAREPACKAGEACK:
|
||||
return None
|
||||
|
||||
# Parse the message
|
||||
@@ -69,6 +70,8 @@ def receive_ack(serial):
|
||||
return ack
|
||||
|
||||
FILENAME = 'firmware.bin'
|
||||
if len(sys.argv) > 1:
|
||||
FILENAME = sys.argv[1]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -99,8 +102,8 @@ if __name__ == "__main__":
|
||||
|
||||
# Send the FirmwareStart message
|
||||
print(start)
|
||||
#send_package(0xf01, start.SerializeToString(), ser)
|
||||
time.sleep(1) # wait for the device to process the message
|
||||
send_package(UsbPackageType.FIRMWARESTART, start.SerializeToString(), ser)
|
||||
#time.sleep(1) # wait for the device to process the message
|
||||
|
||||
# Send the firmware packages
|
||||
for (i,pack_data) in enumerate(packages):
|
||||
@@ -113,8 +116,7 @@ if __name__ == "__main__":
|
||||
# Send the FirmwarePackage message
|
||||
print(package)
|
||||
print(hex(package.crc_pac))
|
||||
exit(0)
|
||||
#send_package(0xf02, package.SerializeToString(), ser)
|
||||
send_package(UsbPackageType.FIRMWAREPACKAGE, package.SerializeToString(), ser)
|
||||
|
||||
|
||||
# Wait for the FirmwarePackageAck message
|
||||
@@ -122,7 +124,7 @@ if __name__ == "__main__":
|
||||
print(ack)
|
||||
if not ack.ack:
|
||||
print(f'Package {i} not acknowledged')
|
||||
break
|
||||
exit(-1)
|
||||
|
||||
|
||||
# Send the FirmwareDone message
|
||||
@@ -131,4 +133,4 @@ if __name__ == "__main__":
|
||||
done.crc_fw = start.crc_fw
|
||||
done.device_id = start.device_id
|
||||
print(done)
|
||||
send_package(0xf03, done.SerializeToString(), ser)
|
||||
send_package(UsbPackageType.FIRMWAREDONE, done.SerializeToString(), ser)
|
||||
|
||||
Reference in New Issue
Block a user