44The official nRF Connect Power Profiler was used as a reference: https://github.com/NordicSemiconductor/pc-nrfconnect-ppk
55"""
66
7- import serial
87import time
8+ import serial
99import struct
1010import logging
1111
12- import multiprocessing
1312import queue
13+ import multiprocessing
1414
1515class PPK2_Command ():
1616 """Serial command opcodes"""
@@ -393,13 +393,14 @@ def run(self):
393393 # calculate stats
394394 s += len (d )
395395 dt = tm_now - t
396- if dt >= 1.0 :
396+ if dt >= 0.1 :
397397 if self .print_stats :
398- print (s , dt )
398+ print (f"Samples: { s } , delta time: { dt } " )
399399 self ._stats = (s , dt )
400400 s = 0
401401 t = tm_now
402- time .sleep (0.002 )
402+
403+ time .sleep (0.0001 )
403404
404405 # process would hang on join() if there's data in the buffer after the measurement is done
405406 while True :
@@ -413,7 +414,7 @@ def get_data(self):
413414 count = 0
414415 while True :
415416 try :
416- ret += self ._buffer_q .get (timeout = 0.2 ) # get_nowait sometimes skips a chunk for some reason
417+ ret += self ._buffer_q .get (timeout = 0.01 ) # get_nowait sometimes skips a chunk for some reason
417418 count += 1
418419 except queue .Empty :
419420 break
@@ -435,31 +436,41 @@ def __init__(self, port, buffer_seconds=10):
435436 self ._quit_evt = multiprocessing .Event ()
436437 self ._buffer_seconds = buffer_seconds
437438
438- # stop measurement in case it was already started
439+ def __del__ (self ):
440+ """Destructor"""
439441 PPK2_API .stop_measuring (self )
442+ self ._quit_evt .clear ()
443+ self ._quit_evt = None
444+ del self ._quit_evt
445+ if self ._fetcher is not None :
446+ self ._fetcher .join ()
447+ self ._fetcher = None
448+ del self ._fetcher
440449
441450 def start_measuring (self ):
442451 # discard the data in the buffer
452+ self .stop_measuring ()
443453 while self .get_data ()!= b'' :
444454 pass
445455
446456 PPK2_API .start_measuring (self )
457+ self ._quit_evt .clear ()
447458 if self ._fetcher is not None :
448- # fetcher already started
449459 return
450- self . _quit_evt . clear ()
460+
451461 self ._fetcher = PPK_Fetch (self , self ._quit_evt , self ._buffer_seconds )
452462 self ._fetcher .start ()
453463
454464 def stop_measuring (self ):
455465 PPK2_API .stop_measuring (self )
456466 PPK2_API .get_data (self ) # flush the serial buffer (to prevent unicode error on next command)
457467 self ._quit_evt .set ()
458- self ._fetcher .join () # join() will block if the queue isn't empty
468+ if self ._fetcher is not None :
469+ self ._fetcher .join () # join() will block if the queue isn't empty
470+ self ._fetcher = None
459471
460472 def get_data (self ):
461473 try :
462474 return self ._fetcher .get_data ()
463475 except (TypeError , AttributeError ):
464476 return b''
465-
0 commit comments