# -*- coding: utf-8 -*- ## # Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. # copyright # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, # IN PART OR IN WHOLE, # WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # file messageBuilder.py # date 2020/04/08 # author Behrouz NematiPour # import utils import crc syncByte = 'A5' ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def toCandumpFormat(vMsg): if type(vMsg) == list: for index, value in enumerate(vMsg): vMsg[index] = ".".join(utils.partition(value, 2, False)) else: vMsg = ".".join(utils.partition(vMsg, 2, False)) return vMsg ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def toFrames(vMsg): mLen = 16 padded = utils.padding(vMsg, mLen) frames = utils.partition(padded, mLen, False) return frames ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def addCRC8(vString, vDelimiter = ""): return vString + vDelimiter + crc.calcCRC8(vString, vDelimiter) ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def buildMessage(vMsgID, vLen, *vArgs): msg = "" seq = 1 msg += utils.toU16(seq) # always used seq# 1 (for now) msg += utils.toU16(vMsgID) msg += utils.toU08(vLen) for arg in vArgs: msg += arg msg += crc.calcCRC8(msg) return syncByte + msg