33The PPK2 uses Serial communication.
44The official nRF Connect Power Profiler was used as a reference: https://github.com/NordicSemiconductor/pc-nrfconnect-ppk
55"""
6- import threading
6+
77import time
88import serial
99import struct
1010import logging
1111import os
1212import queue
13-
13+ import threading
1414
1515class PPK2_Command ():
1616 """Serial command opcodes"""
@@ -243,7 +243,7 @@ def stop_measuring(self):
243243 self ._write_serial ((PPK2_Command .AVERAGE_STOP , ))
244244
245245 def set_source_voltage (self , mV ):
246- """Inits device - based on observation only REGULATOR_SET is the command.
246+ """Inits device - based on observation only REGULATOR_SET is the command.
247247 The other two values correspond to the voltage level.
248248
249249 800mV is the lowest setting - [3,32] - the values then increase linearly
@@ -290,7 +290,7 @@ def get_adc_result(self, current_range, adc_value):
290290 self .rolling_avg = adc
291291 else :
292292 self .rolling_avg = self .spike_filter_alpha * adc + (1 - self .spike_filter_alpha ) * self .rolling_avg
293-
293+
294294 if self .rolling_avg4 is None :
295295 self .rolling_avg4 = adc
296296 else :
@@ -313,7 +313,7 @@ def get_adc_result(self, current_range, adc_value):
313313 adc = self .rolling_avg4
314314 else :
315315 adc = self .rolling_avg
316-
316+
317317 self .after_spike -= 1
318318
319319 self .prev_range = current_range
@@ -360,7 +360,7 @@ def get_samples(self, buf):
360360
361361class PPK_Fetch (threading .Thread ):
362362 '''
363- Background process for polling the data in multi-threading variant
363+ Background process for polling the data in multi-threaded variant
364364 '''
365365 def __init__ (self , ppk2 , quit_evt , buffer_len_s = 10 , buffer_chunk_s = 0.5 ):
366366 super ().__init__ ()
@@ -397,7 +397,7 @@ def run(self):
397397 self ._buffer_q .get ()
398398 local_buffer = local_buffer [self ._buffer_chunk :]
399399 self ._last_timestamp = tm_now
400- #print(len(d), len(local_buffer), self._buffer_q.qsize())
400+ # print(len(d), len(local_buffer), self._buffer_q.qsize())
401401
402402 # calculate stats
403403 s += len (d )
@@ -423,7 +423,7 @@ def get_data(self):
423423 count = 0
424424 while True :
425425 try :
426- ret += self ._buffer_q .get (timeout = 0.01 ) # get_nowait sometimes skips a chunk for some reason
426+ ret += self ._buffer_q .get (timeout = 0.001 ) # get_nowait sometimes skips a chunk for some reason
427427 count += 1
428428 except queue .Empty :
429429 break
@@ -435,15 +435,18 @@ class PPK2_MP(PPK2_API):
435435 Multiprocessing variant of the object. The interface is the same as for the regular one except it spawns
436436 a background process on start_measuring()
437437 '''
438- def __init__ (self , port , buffer_seconds = 10 ):
438+ def __init__ (self , port , buffer_max_size_seconds = 10 , buffer_chunk_seconds = 0.1 ):
439439 '''
440440 port - port where PPK2 is connected
441- buffer_seconds - how many seconds of data to keep in the buffer
441+ buffer_max_size_seconds - how many seconds of data to keep in the buffer
442+ buffer_chunk_seconds - how many seconds of data to put in the queue at once
442443 '''
443444 super ().__init__ (port )
445+
444446 self ._fetcher = None
445447 self ._quit_evt = threading .Event ()
446- self ._buffer_seconds = buffer_seconds
448+ self ._buffer_max_size_seconds = buffer_max_size_seconds
449+ self ._buffer_chunk_seconds = buffer_chunk_seconds
447450
448451 def __del__ (self ):
449452 """Destructor"""
@@ -466,8 +469,8 @@ def start_measuring(self):
466469 self ._quit_evt .clear ()
467470 if self ._fetcher is not None :
468471 return
469-
470- self ._fetcher = PPK_Fetch (self , self ._quit_evt , self ._buffer_seconds )
472+
473+ self ._fetcher = PPK_Fetch (self , self ._quit_evt , self ._buffer_max_size_seconds , self . _buffer_chunk_seconds )
471474 self ._fetcher .start ()
472475
473476 def stop_measuring (self ):
0 commit comments