|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from .scservo_def import * |
| 4 | + |
| 5 | +class GroupSyncRead: |
| 6 | + def __init__(self, ph, start_address, data_length): |
| 7 | + self.ph = ph |
| 8 | + self.start_address = start_address |
| 9 | + self.data_length = data_length |
| 10 | + |
| 11 | + self.last_result = False |
| 12 | + self.is_param_changed = False |
| 13 | + self.param = [] |
| 14 | + self.data_dict = {} |
| 15 | + |
| 16 | + self.clearParam() |
| 17 | + |
| 18 | + def makeParam(self): |
| 19 | + if not self.data_dict: # len(self.data_dict.keys()) == 0: |
| 20 | + return |
| 21 | + |
| 22 | + self.param = [] |
| 23 | + |
| 24 | + for scs_id in self.data_dict: |
| 25 | + self.param.append(scs_id) |
| 26 | + |
| 27 | + def addParam(self, scs_id): |
| 28 | + if scs_id in self.data_dict: # scs_id already exist |
| 29 | + return False |
| 30 | + |
| 31 | + self.data_dict[scs_id] = [] # [0] * self.data_length |
| 32 | + |
| 33 | + self.is_param_changed = True |
| 34 | + return True |
| 35 | + |
| 36 | + def removeParam(self, scs_id): |
| 37 | + if scs_id not in self.data_dict: # NOT exist |
| 38 | + return |
| 39 | + |
| 40 | + del self.data_dict[scs_id] |
| 41 | + |
| 42 | + self.is_param_changed = True |
| 43 | + |
| 44 | + def clearParam(self): |
| 45 | + self.data_dict.clear() |
| 46 | + |
| 47 | + def txPacket(self): |
| 48 | + if len(self.data_dict.keys()) == 0: |
| 49 | + |
| 50 | + return COMM_NOT_AVAILABLE |
| 51 | + |
| 52 | + if self.is_param_changed is True or not self.param: |
| 53 | + self.makeParam() |
| 54 | + |
| 55 | + return self.ph.syncReadTx(self.start_address, self.data_length, self.param, len(self.data_dict.keys())) |
| 56 | + |
| 57 | + def rxPacket(self): |
| 58 | + self.last_result = True |
| 59 | + |
| 60 | + result = COMM_RX_FAIL |
| 61 | + |
| 62 | + if len(self.data_dict.keys()) == 0: |
| 63 | + return COMM_NOT_AVAILABLE |
| 64 | + |
| 65 | + result, rxpacket = self.ph.syncReadRx(self.data_length, len(self.data_dict.keys())) |
| 66 | + # print(rxpacket) |
| 67 | + if len(rxpacket) >= (self.data_length+6): |
| 68 | + for scs_id in self.data_dict: |
| 69 | + self.data_dict[scs_id], result = self.readRx(rxpacket, scs_id, self.data_length) |
| 70 | + if result != COMM_SUCCESS: |
| 71 | + self.last_result = False |
| 72 | + # print(scs_id) |
| 73 | + else: |
| 74 | + self.last_result = False |
| 75 | + # print(self.last_result) |
| 76 | + return result |
| 77 | + |
| 78 | + def txRxPacket(self): |
| 79 | + result = self.txPacket() |
| 80 | + if result != COMM_SUCCESS: |
| 81 | + return result |
| 82 | + |
| 83 | + return self.rxPacket() |
| 84 | + |
| 85 | + def readRx(self, rxpacket, scs_id, data_length): |
| 86 | + # print(scs_id) |
| 87 | + # print(rxpacket) |
| 88 | + data = [] |
| 89 | + rx_length = len(rxpacket) |
| 90 | + # print(rx_length) |
| 91 | + rx_index = 0; |
| 92 | + while (rx_index+6+data_length) <= rx_length: |
| 93 | + headpacket = [0x00, 0x00, 0x00] |
| 94 | + while rx_index < rx_length: |
| 95 | + headpacket[2] = headpacket[1]; |
| 96 | + headpacket[1] = headpacket[0]; |
| 97 | + headpacket[0] = rxpacket[rx_index]; |
| 98 | + rx_index += 1 |
| 99 | + if (headpacket[2] == 0xFF) and (headpacket[1] == 0xFF) and headpacket[0] == scs_id: |
| 100 | + # print(rx_index) |
| 101 | + break |
| 102 | + # print(rx_index+3+data_length) |
| 103 | + if (rx_index+3+data_length) > rx_length: |
| 104 | + break; |
| 105 | + if rxpacket[rx_index] != (data_length+2): |
| 106 | + rx_index += 1 |
| 107 | + # print(rx_index) |
| 108 | + continue |
| 109 | + rx_index += 1 |
| 110 | + Error = rxpacket[rx_index] |
| 111 | + rx_index += 1 |
| 112 | + calSum = scs_id + (data_length+2) + Error |
| 113 | + data = [Error] |
| 114 | + data.extend(rxpacket[rx_index : rx_index+data_length]) |
| 115 | + for i in range(0, data_length): |
| 116 | + calSum += rxpacket[rx_index] |
| 117 | + rx_index += 1 |
| 118 | + calSum = ~calSum & 0xFF |
| 119 | + # print(calSum) |
| 120 | + if calSum != rxpacket[rx_index]: |
| 121 | + return None, COMM_RX_CORRUPT |
| 122 | + return data, COMM_SUCCESS |
| 123 | + # print(rx_index) |
| 124 | + return None, COMM_RX_CORRUPT |
| 125 | + |
| 126 | + def isAvailable(self, scs_id, address, data_length): |
| 127 | + #if self.last_result is False or scs_id not in self.data_dict: |
| 128 | + if scs_id not in self.data_dict: |
| 129 | + return False, 0 |
| 130 | + |
| 131 | + if (address < self.start_address) or (self.start_address + self.data_length - data_length < address): |
| 132 | + return False, 0 |
| 133 | + if not self.data_dict[scs_id]: |
| 134 | + return False, 0 |
| 135 | + if len(self.data_dict[scs_id])<(data_length+1): |
| 136 | + return False, 0 |
| 137 | + return True, self.data_dict[scs_id][0] |
| 138 | + |
| 139 | + def getData(self, scs_id, address, data_length): |
| 140 | + if data_length == 1: |
| 141 | + return self.data_dict[scs_id][address-self.start_address+1] |
| 142 | + elif data_length == 2: |
| 143 | + return self.ph.scs_makeword(self.data_dict[scs_id][address-self.start_address+1], |
| 144 | + self.data_dict[scs_id][address-self.start_address+2]) |
| 145 | + elif data_length == 4: |
| 146 | + return self.ph.scs_makedword(self.ph.scs_makeword(self.data_dict[scs_id][address-self.start_address+1], |
| 147 | + self.data_dict[scs_id][address-self.start_address+2]), |
| 148 | + self.ph.scs_makeword(self.data_dict[scs_id][address-self.start_address+3], |
| 149 | + self.data_dict[scs_id][address-self.start_address+4])) |
| 150 | + else: |
| 151 | + return 0 |
0 commit comments