Skip to content

Commit 01cd970

Browse files
change bit detection fully utilizing api_v3
1 parent 5bbc915 commit 01cd970

1 file changed

Lines changed: 120 additions & 89 deletions

File tree

pd.py

Lines changed: 120 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -68,138 +68,169 @@ def __init__(self):
6868

6969
def reset(self):
7070
self.samplerate = None
71-
self.oldpin = None
72-
self.ss_packet = None
73-
self.ss = None
74-
self.es = None
75-
self.bits = []
71+
# self.oldpin = None
72+
# self.ss_packet = None
73+
# self.ss = None
74+
# self.es = None
75+
# self.bits = []
7676
self.inreset = False
7777
self.bidirectional = False
7878
self.dshot_period = 3.33e-6
7979
self.actual_period = None
80+
self.halfbitwidth = None
81+
self.currbit_ss = None
82+
self.currbit_es = None
83+
self.samples_toreset = None
84+
self.samples_pp = None
8085

8186

8287
def start(self):
8388
self.bidirectional = True if self.options['bidir'] == 'True' else False
8489
self.dshot_period = self.dshot_period_lookup[self.options['dshot_rate']]
90+
self.samples_pp = int(self.samplerate*self.dshot_period)
91+
self.samples_toreset = self.samples_pp*3
92+
# self.halfbitwidth = int((self.samplerate / self.dshot_period) / 2.0)
8593
#print("start period",self.dshot_period)
8694
self.out_ann = self.register(srd.OUTPUT_ANN)
8795

8896
def metadata(self, key, value):
8997
if key == srd.SRD_CONF_SAMPLERATE:
9098
self.samplerate = value
9199

92-
def handle_bits(self, samplenum):
93-
if len(self.bits) == 16:
94-
dshot_value = int(reduce(lambda a, b: (a << 1) | b, self.bits[:11]))
95-
telem_request = self.bits[11]
96-
received_crc = int(reduce(lambda a, b: (a << 1) | b, self.bits[12:]))
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:]))
97108

98-
value_tocrc = int(reduce(lambda a, b: (a << 1) | b, self.bits[:12]))
109+
# value_tocrc = int(reduce(lambda a, b: (a << 1) | b, self.bits[:12]))
99110

100-
calculated_crc = ((~(value_tocrc ^ (value_tocrc >> 4) ^ (value_tocrc >> 8))) & 0x0F)
111+
# calculated_crc = ((~(value_tocrc ^ (value_tocrc >> 4) ^ (value_tocrc >> 8))) & 0x0F)
101112

102-
crc_ok = True if value_tocrc == calculated_crc else False
113+
# crc_ok = True if value_tocrc == calculated_crc else False
103114

104-
# TODO: Align this correctly
105-
crc_startsample = samplenum-(self.actual_period*5)
115+
# # TODO: Align this correctly
116+
# crc_startsample = samplenum-(self.actual_period*5)
106117

107-
# Split annotation based on value type
108-
if dshot_value < 48:
109-
# Command
110-
self.put(self.ss_packet, crc_startsample, self.out_ann,
111-
[1, ['%04d' % dshot_value]])
112-
else:
113-
# Throttle
114-
self.put(self.ss_packet, crc_startsample, self.out_ann,
115-
[2, ['%04d' % dshot_value]])
116-
117-
self.put(crc_startsample, samplenum, self.out_ann, [3, ['Calc CRC: '+('%04d' % calculated_crc)+' TXed CRC:'+('%04d' % received_crc)]])
118-
if not crc_ok:
119-
self.put(crc_startsample, samplenum, self.out_ann,
120-
[4, ['CRC INVALID']])
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']])
121132

122133

123-
self.bits = []
124-
self.ss_packet = None
125-
else:
126-
self.put(self.es, self.samplenum, self.out_ann,
127-
[1, ['ERROR: INVALID PACKET LENGTH', 'ERR', 'E']])
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']])
128139

129140

130-
def handle_bit(self):
131-
if self.ss and self.es:
132-
period = self.samplenum - self.ss
133-
duty = self.es - self.ss
134-
# Ideal duty for T0H: 33%, T1H: 66%.
135-
bit_ = (duty / period) > 0.5
141+
def handle_bit(self, ss, es, nb_ss):
136142

137-
self.put(self.ss, self.samplenum, self.out_ann,
138-
[0, ['%d' % bit_]])
143+
period = nb_ss - es
144+
duty = es - ss
145+
# Ideal duty for T0H: 33%, T1H: 66%.
146+
bit_ = (duty / period) > 0.5
139147

140-
self.bits.append(bit_)
141-
self.actual_period = period
142-
#self.handle_bits(self.samplenum)
143-
if self.ss_packet is None:
144-
self.ss_packet = self.samplenum
145-
self.ss = self.samplenum
148+
self.put(ss, nb_ss, self.out_ann,
149+
[0, ['%d' % bit_]])
150+
return ss,nb_ss,bit_
146151

147-
def handle_notbit(self):
148-
self.inreset = False
149-
self.es = self.samplenum
152+
# def handle_notbit(self):
153+
# self.inreset = False
154+
# self.es = self.samplenum
150155

151-
def check_reset(self):
152-
# Decode last bit value.
153-
tH = (self.es - self.ss) / self.samplerate
156+
# def check_reset(self):
157+
# # Decode last bit value.
158+
# tH = (self.es - self.ss) / self.samplerate
154159

155-
# High if greater than half the period
156-
bit_ = True if tH >= (self.dshot_period/2) else False
160+
# # High if greater than half the period
161+
# bit_ = True if tH >= (self.dshot_period/2) else False
157162

158-
self.bits.append(bit_)
159-
self.handle_bits(self.es)
163+
# self.bits.append(bit_)
164+
# self.handle_bits(self.es)
160165

161-
self.put(self.ss, self.es, self.out_ann, [0, ['%d' % bit_]])
162-
# self.put(self.es, self.samplenum, self.out_ann,
163-
# [1, ['RESET', 'RST', 'R']])
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']])
164169

165-
self.inreset = True
166-
self.bits = []
167-
self.ss_packet = None
168-
self.ss = None
170+
# self.inreset = True
171+
# self.bits = []
172+
# self.ss_packet = None
173+
# self.ss = None
169174
def decode(self):
170175
if not self.samplerate:
171176
raise SamplerateError('Cannot decode without samplerate.')
172-
177+
178+
results = []
173179
while True:
174180
# TODO: Come up with more appropriate self.wait() conditions.
175-
(pin,) = self.wait()
181+
# (pin,) = self.wait()
176182

177-
if self.oldpin is None:
178-
self.oldpin = pin
179-
continue
183+
# if self.oldpin is None:
184+
# self.oldpin = pin
185+
# continue
180186

181187
# Check idle condition if longer is greater than 2x max period
182188
# TODO: Confirm this with spec
183189
if not self.bidirectional:
184-
if not self.inreset and not pin and self.es is not None and \
185-
self.ss is not None and \
186-
(self.samplenum - self.es) / self.samplerate > self.dshot_period*2:
187-
self.check_reset()
188-
if not self.oldpin and pin:
189-
# Rising edge
190-
self.handle_bit()
191-
elif self.oldpin and not pin:
192-
# Falling edge
193-
self.handle_notbit()
194-
else:
195-
if self.oldpin and not pin:
196-
# Falling edge.
197-
self.handle_bit()
198-
elif not self.oldpin and pin:
199-
# Rising edge.
200-
self.handle_notbit()
201-
202-
self.oldpin = pin
190+
191+
192+
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
203234

204235

205236

0 commit comments

Comments
 (0)