@@ -189,11 +189,10 @@ 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 ):
193+ # print(f"Value: {value}")
194+ # print(f"Meas: {meas}")
193195 masked_value = (value & meas ["mask" ]) >> meas ["pos" ]
194- if meas ["pos" ] == 24 :
195- if masked_value == 255 :
196- masked_value = - 1
197196 return masked_value
198197
199198 def _handle_raw_data (self , adc_value ):
@@ -205,10 +204,10 @@ def _handle_raw_data(self, adc_value):
205204 bits = self ._get_masked_value (adc_value , self .MEAS_LOGIC )
206205 analog_value = self .get_adc_result (
207206 current_measurement_range , adc_result ) * 10 ** 6
208- return analog_value
207+ return analog_value , bits
209208 except Exception as e :
210209 print ("Measurement outside of range!" )
211- return None
210+ return None , None
212211
213212 @staticmethod
214213 def list_devices ():
@@ -327,6 +326,26 @@ def _digital_to_analog(self, adc_value):
327326 """Convert discrete value to analog value"""
328327 return int .from_bytes (adc_value , byteorder = "little" , signed = False ) # convert reading to analog value
329328
329+ def digital_channels (self , bits ):
330+ """
331+ Convert raw digital data to digital channels.
332+
333+ Returns a 2d matrix with 8 rows (one for each channel). Each row contains HIGH and LOW values for the selected channel.
334+ """
335+
336+ # Prepare 2d matrix with 8 rows (one for each channel)
337+ digital_channels = [[], [], [], [], [], [], [], []]
338+ for sample in bits :
339+ digital_channels [0 ].append ((sample & 1 ) >> 0 )
340+ digital_channels [1 ].append ((sample & 2 ) >> 1 )
341+ digital_channels [2 ].append ((sample & 4 ) >> 2 )
342+ digital_channels [3 ].append ((sample & 8 ) >> 3 )
343+ digital_channels [4 ].append ((sample & 16 ) >> 4 )
344+ digital_channels [5 ].append ((sample & 32 ) >> 5 )
345+ digital_channels [6 ].append ((sample & 64 ) >> 6 )
346+ digital_channels [7 ].append ((sample & 128 ) >> 7 )
347+ return digital_channels
348+
330349 def get_samples (self , buf ):
331350 """
332351 Returns list of samples read in one sampling period.
@@ -338,28 +357,35 @@ def get_samples(self, buf):
338357 sample_size = 4 # one analog value is 4 bytes in size
339358 offset = self .remainder ["len" ]
340359 samples = []
360+ raw_digital_output = []
341361
342362 first_reading = (
343363 self .remainder ["sequence" ] + buf [0 :sample_size - offset ])[:4 ]
344364 adc_val = self ._digital_to_analog (first_reading )
345- measurement = self ._handle_raw_data (adc_val )
365+ measurement , bits = self ._handle_raw_data (adc_val )
346366 if measurement is not None :
347367 samples .append (measurement )
368+ if bits is not None :
369+ raw_digital_output .append (bits )
348370
349371 offset = sample_size - offset
350372
351373 while offset <= len (buf ) - sample_size :
352374 next_val = buf [offset :offset + sample_size ]
353375 offset += sample_size
354376 adc_val = self ._digital_to_analog (next_val )
355- measurement = self ._handle_raw_data (adc_val )
377+ measurement , bits = self ._handle_raw_data (adc_val )
356378 if measurement is not None :
357379 samples .append (measurement )
380+ if bits is not None :
381+ raw_digital_output .append (bits )
358382
359383 self .remainder ["sequence" ] = buf [offset :len (buf )]
360384 self .remainder ["len" ] = len (buf )- offset
361385
362- return samples # return list of samples, handle those lists in PPK2 API wrapper
386+ # return list of samples and raw digital outputs
387+ # handle those lists in PPK2 API wrapper
388+ return samples , raw_digital_output
363389
364390
365391class PPK_Fetch (threading .Thread ):
0 commit comments