Skip to content

Commit bbd7680

Browse files
Matthew BeaudoinMatthew Beaudoin
authored andcommitted
Switch the PPK2_MP class to use threading to fix Issue 18. The PPK2_MP can now be used on windows and fixes a possible issue with queue sync on linux platforms
1 parent 3b4d9fd commit bbd7680

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/ppk2_api/ppk2_api.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
The PPK2 uses Serial communication.
44
The official nRF Connect Power Profiler was used as a reference: https://github.com/NordicSemiconductor/pc-nrfconnect-ppk
55
"""
6-
6+
import threading
77
import time
88
import serial
99
import struct
1010
import logging
1111
import os
1212
import queue
13-
import multiprocessing
13+
1414

1515
class 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
@@ -358,9 +358,9 @@ def get_samples(self, buf):
358358
return samples # return list of samples, handle those lists in PPK2 API wrapper
359359

360360

361-
class PPK_Fetch(multiprocessing.Process):
361+
class PPK_Fetch(threading.Thread):
362362
'''
363-
Background process for polling the data in multiprocessing variant
363+
Background process for polling the data in multi-threading variant
364364
'''
365365
def __init__(self, ppk2, quit_evt, buffer_len_s=10, buffer_chunk_s=0.5):
366366
super().__init__()
@@ -380,7 +380,7 @@ def __init__(self, ppk2, quit_evt, buffer_len_s=10, buffer_chunk_s=0.5):
380380
if self._buffer_chunk % 4 != 0:
381381
self._buffer_chunk = (self._buffer_chunk // 4) * 4
382382

383-
self._buffer_q = multiprocessing.Queue()
383+
self._buffer_q = queue.Queue()
384384

385385
def run(self):
386386
s = 0
@@ -442,7 +442,7 @@ def __init__(self, port, buffer_seconds=10):
442442
'''
443443
super().__init__(port)
444444
self._fetcher = None
445-
self._quit_evt = multiprocessing.Event()
445+
self._quit_evt = threading.Event()
446446
self._buffer_seconds = buffer_seconds
447447

448448
def __del__(self):
@@ -466,7 +466,7 @@ def start_measuring(self):
466466
self._quit_evt.clear()
467467
if self._fetcher is not None:
468468
return
469-
469+
470470
self._fetcher = PPK_Fetch(self, self._quit_evt, self._buffer_seconds)
471471
self._fetcher.start()
472472

0 commit comments

Comments
 (0)