@@ -189,11 +189,8 @@ def _generate_mask(self, bits, pos):
189189 mask = self ._twos_comp (mask )
190190 return {"mask" : mask , "pos" : pos }
191191
192- def _get_masked_value (self , value , meas ):
192+ def _get_masked_value (self , value , meas , is_bits = False ):
193193 masked_value = (value & meas ["mask" ]) >> meas ["pos" ]
194- if meas ["pos" ] == 24 :
195- if masked_value == 255 :
196- masked_value = - 1
197194 return masked_value
198195
199196 def _handle_raw_data (self , adc_value ):
@@ -205,10 +202,10 @@ def _handle_raw_data(self, adc_value):
205202 bits = self ._get_masked_value (adc_value , self .MEAS_LOGIC )
206203 analog_value = self .get_adc_result (
207204 current_measurement_range , adc_result ) * 10 ** 6
208- return analog_value
205+ return analog_value , bits
209206 except Exception as e :
210207 print ("Measurement outside of range!" )
211- return None
208+ return None , None
212209
213210 @staticmethod
214211 def list_devices ():
@@ -327,6 +324,26 @@ def _digital_to_analog(self, adc_value):
327324 """Convert discrete value to analog value"""
328325 return int .from_bytes (adc_value , byteorder = "little" , signed = False ) # convert reading to analog value
329326
327+ def digital_channels (self , bits ):
328+ """
329+ Convert raw digital data to digital channels.
330+
331+ Returns a 2d matrix with 8 rows (one for each channel). Each row contains HIGH and LOW values for the selected channel.
332+ """
333+
334+ # Prepare 2d matrix with 8 rows (one for each channel)
335+ digital_channels = [[], [], [], [], [], [], [], []]
336+ for sample in bits :
337+ digital_channels [0 ].append ((sample & 1 ) >> 0 )
338+ digital_channels [1 ].append ((sample & 2 ) >> 1 )
339+ digital_channels [2 ].append ((sample & 4 ) >> 2 )
340+ digital_channels [3 ].append ((sample & 8 ) >> 3 )
341+ digital_channels [4 ].append ((sample & 16 ) >> 4 )
342+ digital_channels [5 ].append ((sample & 32 ) >> 5 )
343+ digital_channels [6 ].append ((sample & 64 ) >> 6 )
344+ digital_channels [7 ].append ((sample & 128 ) >> 7 )
345+ return digital_channels
346+
330347 def get_samples (self , buf ):
331348 """
332349 Returns list of samples read in one sampling period.
@@ -338,28 +355,35 @@ def get_samples(self, buf):
338355 sample_size = 4 # one analog value is 4 bytes in size
339356 offset = self .remainder ["len" ]
340357 samples = []
358+ raw_digital_output = []
341359
342360 first_reading = (
343361 self .remainder ["sequence" ] + buf [0 :sample_size - offset ])[:4 ]
344362 adc_val = self ._digital_to_analog (first_reading )
345- measurement = self ._handle_raw_data (adc_val )
363+ measurement , bits = self ._handle_raw_data (adc_val )
346364 if measurement is not None :
347365 samples .append (measurement )
366+ if bits is not None :
367+ raw_digital_output .append (bits )
348368
349369 offset = sample_size - offset
350370
351371 while offset <= len (buf ) - sample_size :
352372 next_val = buf [offset :offset + sample_size ]
353373 offset += sample_size
354374 adc_val = self ._digital_to_analog (next_val )
355- measurement = self ._handle_raw_data (adc_val )
375+ measurement , bits = self ._handle_raw_data (adc_val )
356376 if measurement is not None :
357377 samples .append (measurement )
378+ if bits is not None :
379+ raw_digital_output .append (bits )
358380
359381 self .remainder ["sequence" ] = buf [offset :len (buf )]
360382 self .remainder ["len" ] = len (buf )- offset
361383
362- return samples # return list of samples, handle those lists in PPK2 API wrapper
384+ # return list of samples and raw digital outputs
385+ # handle those lists in PPK2 API wrapper
386+ return samples , raw_digital_output
363387
364388
365389class PPK_Fetch (threading .Thread ):
@@ -401,7 +425,6 @@ def run(self):
401425 self ._buffer_q .get ()
402426 local_buffer = local_buffer [self ._buffer_chunk :]
403427 self ._last_timestamp = tm_now
404- # print(len(d), len(local_buffer), self._buffer_q.qsize())
405428
406429 # calculate stats
407430 s += len (d )
0 commit comments