@@ -97,140 +97,104 @@ def metadata(self, key, value):
9797 if key == srd .SRD_CONF_SAMPLERATE :
9898 self .samplerate = value
9999
100- # def handle_bits(self, result):
101- # for thing in results:
102- # bit, ss, es = thing
103-
104- # if len(bits) == 16:
105- # dshot_value = int(reduce(lambda a, b: (a << 1) | b, self.bits[:11]))
106- # telem_request = self.bits[11]
107- # received_crc = int(reduce(lambda a, b: (a << 1) | b, self.bits[12:]))
100+ def handle_bits (self , results ):
101+ #ss, es, bit
102+ #print(results)
103+ bits = [result [2 ] for result in results ]
104+ # print(bits)
105+
106+
107+ if len (bits ) == 16 :
108+ dshot_value = int (reduce (lambda a , b : (a << 1 ) | b , bits [:11 ]))
109+ telem_request = bits [11 ]
110+ received_crc = int (reduce (lambda a , b : (a << 1 ) | b , bits [12 :]))
108111
109- # value_tocrc = int(reduce(lambda a, b: (a << 1) | b, self. bits[:12]))
112+ value_tocrc = int (reduce (lambda a , b : (a << 1 ) | b , bits [:12 ]))
110113
111- # calculated_crc = ((~(value_tocrc ^ (value_tocrc >> 4) ^ (value_tocrc >> 8))) & 0x0F)
114+ calculated_crc = int ((~ (value_tocrc ^ (value_tocrc >> 4 ) ^ (value_tocrc >> 8 )))& 0x0F )
112115
113- # crc_ok = True if value_tocrc == calculated_crc else False
116+ if received_crc == calculated_crc :
117+ crc_ok = True
118+ else :
119+ crc_ok = False
114120
115- # # TODO: Align this correctly
116- # crc_startsample = samplenum-(self.actual_period*5)
121+ # TODO: Align this correctly
122+ crc_startsample = results [ 12 ][ 0 ]
117123
118- # # Split annotation based on value type
119- # if dshot_value < 48:
120- # # Command
121- # self.put(self.ss_packet , crc_startsample, self.out_ann,
122- # [1, ['%04d' % dshot_value]])
123- # else:
124- # # Throttle
125- # self.put(self.ss_packet , crc_startsample, self.out_ann,
126- # [2, ['%04d' % dshot_value]])
127-
128- # self.put(crc_startsample, samplenum , self.out_ann, [3, ['Calc CRC: '+('%04d' % calculated_crc)+' TXed CRC:'+('%04d' % received_crc)]])
129- # if not crc_ok:
130- # self.put(crc_startsample, samplenum , self.out_ann,
131- # [4, ['CRC INVALID']])
124+ # Split annotation based on value type
125+ if dshot_value < 48 :
126+ # Command
127+ self .put (results [ 0 ][ 0 ] , crc_startsample , self .out_ann ,
128+ [1 , ['%04d' % dshot_value ]])
129+ else :
130+ # Throttle
131+ self .put (results [ 0 ][ 0 ] , crc_startsample , self .out_ann ,
132+ [2 , ['%04d' % dshot_value ]])
133+
134+ self .put (crc_startsample , results [ 15 ][ 1 ] , self .out_ann , [3 , ['Calc CRC: ' + ('%04d' % calculated_crc )+ ' TXed CRC:' + ('%04d' % received_crc )]])
135+ if not crc_ok :
136+ self .put (crc_startsample , results [ 15 ][ 1 ] , self .out_ann ,
137+ [4 , ['CRC INVALID' ]])
132138
133139
134- # self.bits = []
135- # self.ss_packet = None
136- # else:
137- # self.put(self.es, self.samplenum, self.out_ann,
138- # [1, ['ERROR: INVALID PACKET LENGTH', 'ERR', 'E']])
140+ self .bits = []
141+ self .ss_packet = None
142+ else :
143+ return
144+ # self.put(results[0][0], results[-1::1][1], self.out_ann,
145+ # [1, ['ERROR: INVALID PACKET LENGTH', 'ERR', 'E']])
139146
140147
141148 def handle_bit (self , ss , es , nb_ss ):
142149
143- period = nb_ss - es
150+ period = nb_ss - ss
144151 duty = es - ss
145152 # Ideal duty for T0H: 33%, T1H: 66%.
146153 bit_ = (duty / period ) > 0.5
147154
148155 self .put (ss , nb_ss , self .out_ann ,
149156 [0 , ['%d' % bit_ ]])
150- return ss ,nb_ss ,bit_
151-
152- # def handle_notbit(self):
153- # self.inreset = False
154- # self.es = self.samplenum
155-
156- # def check_reset(self):
157- # # Decode last bit value.
158- # tH = (self.es - self.ss) / self.samplerate
159-
160- # # High if greater than half the period
161- # bit_ = True if tH >= (self.dshot_period/2) else False
162-
163- # self.bits.append(bit_)
164- # self.handle_bits(self.es)
157+ return [ss ,nb_ss ,bit_ ]
165158
166- # self.put(self.ss, self.es, self.out_ann, [0, ['%d' % bit_]])
167- # # self.put(self.es, self.samplenum, self.out_ann,
168- # # [1, ['RESET', 'RST', 'R']])
169-
170- # self.inreset = True
171- # self.bits = []
172- # self.ss_packet = None
173- # self.ss = None
159+
174160 def decode (self ):
175161 if not self .samplerate :
176162 raise SamplerateError ('Cannot decode without samplerate.' )
177163
178164 results = []
179165 while True :
180- # TODO: Come up with more appropriate self.wait() conditions.
181- # (pin,) = self.wait()
182-
183- # if self.oldpin is None:
184- # self.oldpin = pin
185- # continue
186-
187- # Check idle condition if longer is greater than 2x max period
188- # TODO: Confirm this with spec
189166 if not self .bidirectional :
190-
191-
192167 pins = self .wait ([{0 : 'r' },{0 : 'f' },{'skip' :self .samples_toreset }])
193- if self .currbit_ss and self .currbit_es and self .matched [2 ]:
194- # Have seen start and end of a potential bit but no further change within 3 periods
195- results += self .handle_bit (self .currbit_ss ,self .currbit_es ,(self .currbit_ss + self .samples_pp ))
196- self .currbit_ss = None
197- self .currbit_es = None
198-
199- if self .matched [0 ] and self .currbit_ss is None :
200- # Start of bit
201- self .currbit_ss = self .samplenum
202- elif self .matched [1 ] and self .currbit_es is None :
203- # End of bit
204- self .currbit_es = self .samplenum
205-
206- elif self .matched [0 ]:
207- # Have complete bit, can handle bit now
208- results += self .handle_bit (self .currbit_ss ,self .currbit_es ,self .samplenum )
209- self .currbit_ss = self .samplenum
210- self .currbit_es = None
211- # print(results)
212- #print(self.samplerate*self.dshot_period*2)
213- # Start of next bit
214-
215- # if not self.inreset and not pin and self.es is not None and \
216- # self.ss is not None and \
217- # (self.samplenum - self.es) / self.samplerate > self.dshot_period*2:
218- # self.check_reset()
219- # if not self.oldpin and pin:
220- # # Rising edge
221- # self.handle_bit()
222- # elif self.oldpin and not pin:
223- # # Falling edge
224- # self.handle_notbit()
225- # else:
226- # if self.oldpin and not pin:
227- # # Falling edge.
228- # self.handle_bit()
229- # elif not self.oldpin and pin:
230- # # Rising edge.
231- # self.handle_notbit()
232-
233- # self.oldpin = pin
168+ else :
169+ pins = self .wait ([{0 : 'f' },{0 : 'r' },{'skip' :self .samples_toreset }])
170+
171+ if self .currbit_ss and self .currbit_es and self .matched [2 ]:
172+ # Assume end of packet if have seen start and end of a potential bit but no further change within 3 periods
173+ # TODO: Confirm wait period this works with spec
174+ results += [self .handle_bit (self .currbit_ss ,self .currbit_es ,(self .currbit_ss + self .samples_pp ))]
175+ self .currbit_ss = None
176+ self .currbit_es = None
177+
178+ # Pass results to decoder
179+ self .handle_bits (results )
180+ results = []
181+
182+
183+ if self .matched [0 ] and not self .currbit_ss and not self .currbit_es :
184+ # Start of bit
185+ self .currbit_ss = self .samplenum
186+ elif self .matched [1 ] and self .currbit_ss and not self .currbit_es :
187+ # End of bit
188+ self .currbit_es = self .samplenum
189+ elif self .matched [0 ] and self .currbit_es and self .currbit_ss :
190+ # Have complete bit, can handle bit now
191+ result = [self .handle_bit (self .currbit_ss ,self .currbit_es ,self .samplenum )]
192+ # print(result)
193+ results += result
194+ self .currbit_ss = self .samplenum
195+ self .currbit_es = None
196+
197+
234198
235199
236200
0 commit comments