Skip to content

Commit 65e7dc0

Browse files
committed
ruff run and added github action to check formatting
1 parent a686f1c commit 65e7dc0

File tree

6 files changed

+224
-128
lines changed

6 files changed

+224
-128
lines changed

.github/workflows/ruff.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: Ruff
2+
on: [ push, pull_request ]
3+
jobs:
4+
ruff:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v4
8+
- uses: astral-sh/ruff-action@v3
9+

example.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
21
"""
32
Basic usage of PPK2 Python API.
43
The basic ampere mode sequence is:
54
1. read modifiers
65
2. set ampere mode
76
3. read stream of data
87
"""
8+
99
import time
1010
from ppk2_api.ppk2_api import PPK2_API
1111

@@ -22,17 +22,20 @@
2222
ppk2_test.get_modifiers()
2323
ppk2_test.set_source_voltage(3300)
2424

25-
ppk2_test.use_source_meter() # set source meter mode
26-
ppk2_test.toggle_DUT_power("ON") # enable DUT power
25+
# set source meter mode
26+
ppk2_test.use_source_meter()
27+
# enable DUT power
28+
ppk2_test.toggle_DUT_power("ON")
29+
# start measuring
30+
ppk2_test.start_measuring()
2731

28-
ppk2_test.start_measuring() # start measuring
2932
# measurements are a constant stream of bytes
3033
# the number of measurements in one sampling period depends on the wait between serial reads
3134
# it appears the maximum number of bytes received is 1024
3235
# the sampling rate of the PPK2 is 100 samples per millisecond
3336
for i in range(0, 1000):
3437
read_data = ppk2_test.get_data()
35-
if read_data != b'':
38+
if read_data != b"":
3639
samples, raw_digital = ppk2_test.get_samples(read_data)
3740
print(f"Average of {len(samples)} samples is: {sum(samples)/len(samples)}uA")
3841

@@ -46,14 +49,15 @@
4649
print()
4750
time.sleep(0.01)
4851

49-
ppk2_test.toggle_DUT_power("OFF") # disable DUT power
50-
51-
ppk2_test.use_ampere_meter() # set ampere meter mode
52+
# disable DUT power
53+
ppk2_test.toggle_DUT_power("OFF")
54+
# set ampere meter mode
55+
ppk2_test.use_ampere_meter()
5256

5357
ppk2_test.start_measuring()
5458
for i in range(0, 1000):
5559
read_data = ppk2_test.get_data()
56-
if read_data != b'':
60+
if read_data != b"":
5761
samples, raw_digital = ppk2_test.get_samples(read_data)
5862
print(f"Average of {len(samples)} samples is: {sum(samples)/len(samples)}uA")
5963

@@ -65,6 +69,8 @@
6569
# Print last 10 values of each channel
6670
print(ch[-10:])
6771
print()
68-
time.sleep(0.01) # lower time between sampling -> less samples read in one sampling period
72+
73+
# lower time between sampling -> less samples read in one sampling period
74+
time.sleep(0.01)
6975

7076
ppk2_test.stop_measuring()

example_mp.py

+25-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
21
"""
32
Basic usage of PPK2 Python API - multiprocessing version.
43
The basic ampere mode sequence is:
54
1. read modifiers
65
2. set ampere mode
76
3. read stream of data
87
"""
8+
99
import time
1010
from ppk2_api.ppk2_api import PPK2_MP as PPK2_API
1111

@@ -18,24 +18,34 @@
1818
print(f"Too many connected PPK2's: {ppk2s_connected}")
1919
exit()
2020

21-
ppk2_test = PPK2_API(ppk2_port, buffer_max_size_seconds=1, buffer_chunk_seconds=0.01, timeout=1, write_timeout=1, exclusive=True)
21+
ppk2_test = PPK2_API(
22+
ppk2_port,
23+
buffer_max_size_seconds=1,
24+
buffer_chunk_seconds=0.01,
25+
timeout=1,
26+
write_timeout=1,
27+
exclusive=True,
28+
)
2229
ppk2_test.get_modifiers()
2330
ppk2_test.set_source_voltage(3300)
2431

2532
"""
2633
Source mode example
2734
"""
28-
ppk2_test.use_source_meter() # set source meter mode
29-
ppk2_test.toggle_DUT_power("ON") # enable DUT power
35+
# set source meter mode
36+
ppk2_test.use_source_meter()
37+
# enable DUT power
38+
ppk2_test.toggle_DUT_power("ON")
39+
# start measuring
40+
ppk2_test.start_measuring()
3041

31-
ppk2_test.start_measuring() # start measuring
3242
# measurements are a constant stream of bytes
3343
# the number of measurements in one sampling period depends on the wait between serial reads
3444
# it appears the maximum number of bytes received is 1024
3545
# the sampling rate of the PPK2 is 100 samples per millisecond
3646
while True:
3747
read_data = ppk2_test.get_data()
38-
if read_data != b'':
48+
if read_data != b"":
3949
samples, raw_digital = ppk2_test.get_samples(read_data)
4050
print(f"Average of {len(samples)} samples is: {sum(samples)/len(samples)}uA")
4151

@@ -50,18 +60,21 @@
5060

5161
time.sleep(0.001)
5262

53-
ppk2_test.toggle_DUT_power("OFF") # disable DUT power
63+
64+
# disable DUT power
65+
ppk2_test.toggle_DUT_power("OFF")
5466
ppk2_test.stop_measuring()
5567

5668
"""
5769
Ampere mode example
5870
"""
59-
ppk2_test.use_ampere_meter() # set ampere meter mode
71+
# set ampere meter mode
72+
ppk2_test.use_ampere_meter()
6073

6174
ppk2_test.start_measuring()
6275
while True:
6376
read_data = ppk2_test.get_data()
64-
if read_data != b'':
77+
if read_data != b"":
6578
samples, raw_digital = ppk2_test.get_samples(read_data)
6679
print(f"Average of {len(samples)} samples is: {sum(samples)/len(samples)}uA")
6780

@@ -73,6 +86,8 @@
7386
# Print last 10 values of each channel
7487
print(ch[-10:])
7588
print()
76-
time.sleep(0.001) # lower time between sampling -> less samples read in one sampling period
89+
90+
# lower time between sampling -> less samples read in one sampling period
91+
time.sleep(0.001)
7792

7893
ppk2_test.stop_measuring()

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ def read(*names, **kwargs):
3737
"License :: OSI Approved :: GNU General Public License v2 (GPLv2)",
3838
"Operating System :: OS Independent",
3939
],
40-
)
40+
)

src/power_profiler.py

+56-31
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22
import csv
33
import datetime
44
from threading import Thread
5+
56
# import numpy as np
67
# import matplotlib.pyplot as plt
78
# import matplotlib
89
from ppk2_api.ppk2_api import PPK2_MP as PPK2_API
910

10-
class PowerProfiler():
11+
12+
class PowerProfiler:
1113
def __init__(self, serial_port=None, source_voltage_mV=3300, filename=None):
1214
"""Initialize PPK2 power profiler with serial"""
1315
self.measuring = None
1416
self.measurement_thread = None
1517
self.ppk2 = None
1618

17-
print(f"Initing power profiler")
19+
print("Initing power profiler")
1820

1921
# try:
2022
if serial_port:
@@ -26,7 +28,8 @@ def __init__(self, serial_port=None, source_voltage_mV=3300, filename=None):
2628
self.ppk2 = PPK2_API(serial_port)
2729

2830
try:
29-
ret = self.ppk2.get_modifiers() # try to read modifiers, if it fails serial port is probably not correct
31+
# try to read modifiers, if it fails serial port is probably not correct
32+
ret = self.ppk2.get_modifiers()
3033
print(f"Initialized ppk2 api: {ret}")
3134
except Exception as e:
3235
print(f"Error initializing power profiler: {e}")
@@ -35,13 +38,16 @@ def __init__(self, serial_port=None, source_voltage_mV=3300, filename=None):
3538

3639
if not ret:
3740
self.ppk2 = None
38-
raise Exception(f"Error when initing PowerProfiler with serial port {serial_port}")
41+
raise Exception(
42+
f"Error when initing PowerProfiler with serial port {serial_port}"
43+
)
3944
else:
4045
self.ppk2.use_source_meter()
4146

4247
self.source_voltage_mV = source_voltage_mV
4348

44-
self.ppk2.set_source_voltage(self.source_voltage_mV) # set to 3.3V
49+
# set to 3.3V
50+
self.ppk2.set_source_voltage(self.source_voltage_mV)
4551

4652
print(f"Set power profiler source voltage: {self.source_voltage_mV}")
4753

@@ -62,7 +68,7 @@ def __init__(self, serial_port=None, source_voltage_mV=3300, filename=None):
6268
# write to csv
6369
self.filename = filename
6470
if self.filename is not None:
65-
with open(self.filename, 'w', newline='') as file:
71+
with open(self.filename, "w", newline="") as file:
6672
writer = csv.writer(file)
6773
row = []
6874
for key in ["ts", "avg1000"]:
@@ -71,10 +77,10 @@ def __init__(self, serial_port=None, source_voltage_mV=3300, filename=None):
7177

7278
def write_csv_rows(self, samples):
7379
"""Write csv row"""
74-
with open(self.filename, 'a', newline='') as file:
80+
with open(self.filename, "a", newline="") as file:
7581
writer = csv.writer(file)
7682
for sample in samples:
77-
row = [datetime.datetime.now().strftime('%d-%m-%Y %H:%M:%S.%f'), sample]
83+
row = [datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f"), sample]
7884
writer.writerow(row)
7985

8086
def delete_power_profiler(self):
@@ -85,26 +91,26 @@ def delete_power_profiler(self):
8591
print("Deleting power profiler")
8692

8793
if self.measurement_thread:
88-
print(f"Joining measurement thread")
94+
print("Joining measurement thread")
8995
self.measurement_thread.join()
9096
self.measurement_thread = None
9197

9298
if self.ppk2:
93-
print(f"Disabling ppk2 power")
99+
print("Disabling ppk2 power")
94100
self.disable_power()
95101
del self.ppk2
96102

97-
print(f"Deleted power profiler")
103+
print("Deleted power profiler")
98104

99105
def discover_port(self):
100106
"""Discovers ppk2 serial port"""
101107
ppk2s_connected = PPK2_API.list_devices()
102-
if(len(ppk2s_connected) == 1):
108+
if len(ppk2s_connected) == 1:
103109
ppk2_port = ppk2s_connected[0]
104-
print(f'Found PPK2 at {ppk2_port}')
110+
print(f"Found PPK2 at {ppk2_port}")
105111
return ppk2_port
106112
else:
107-
print(f'Too many connected PPK2\'s: {ppk2s_connected}')
113+
print(f"Too many connected PPK2's: {ppk2s_connected}")
108114
return None
109115

110116
def enable_power(self):
@@ -124,16 +130,21 @@ def disable_power(self):
124130
def measurement_loop(self):
125131
"""Endless measurement loop will run in a thread"""
126132
while True and not self.stop:
127-
if self.measuring: # read data if currently measuring
133+
# read data if currently measuring
134+
if self.measuring:
128135
read_data = self.ppk2.get_data()
129-
if read_data != b'':
136+
if read_data != b"":
130137
samples = self.ppk2.get_samples(read_data)
131-
self.current_measurements += samples # can easily sum lists, will append individual data
132-
time.sleep(0.001) # TODO figure out correct sleep duration
138+
# can easily sum lists, will append individual data
139+
self.current_measurements += samples
140+
# TODO figure out correct sleep duration
141+
time.sleep(0.001)
133142

134143
def _average_samples(self, list, window_size):
135144
"""Average samples based on window size"""
136-
chunks = [list[val:val + window_size] for val in range(0, len(list), window_size)]
145+
chunks = [
146+
list[val : val + window_size] for val in range(0, len(list), window_size)
147+
]
137148
avgs = []
138149
for chunk in chunks:
139150
avgs.append(sum(chunk) / len(chunk))
@@ -142,19 +153,24 @@ def _average_samples(self, list, window_size):
142153

143154
def start_measuring(self):
144155
"""Start measuring"""
145-
if not self.measuring: # toggle measuring flag only if currently not measuring
146-
self.current_measurements = [] # reset current measurements
147-
self.measuring = True # set internal flag
148-
self.ppk2.start_measuring() # send command to ppk2
156+
# toggle measuring flag only if currently not measuring
157+
if not self.measuring:
158+
# reset current measurements
159+
self.current_measurements = []
160+
# set internal flag
161+
self.measuring = True
162+
# send command to ppk2
163+
self.ppk2.start_measuring()
149164
self.measurement_start_time = time.time()
150165

151166
def stop_measuring(self):
152167
"""Stop measuring and return average of period"""
153168
self.measurement_stop_time = time.time()
154169
self.measuring = False
155-
self.ppk2.stop_measuring() # send command to ppk2
170+
# send command to ppk2
171+
self.ppk2.stop_measuring()
156172

157-
#samples_average = self._average_samples(self.current_measurements, 1000)
173+
# samples_average = self._average_samples(self.current_measurements, 1000)
158174
if self.filename is not None:
159175
self.write_csv_rows(self.current_measurements)
160176

@@ -172,24 +188,33 @@ def get_average_current_mA(self):
172188
if len(self.current_measurements) == 0:
173189
return 0
174190

175-
average_current_mA = (sum(self.current_measurements) / len(self.current_measurements)) / 1000 # measurements are in microamperes, divide by 1000
191+
# measurements are in microamperes, divide by 1000
192+
average_current_mA = (
193+
sum(self.current_measurements) / len(self.current_measurements)
194+
) / 1000
176195
return average_current_mA
177196

178197
def get_average_power_consumption_mWh(self):
179198
"""Return average power consumption of last measurement in mWh"""
180199
average_current_mA = self.get_average_current_mA()
181-
average_power_mW = (self.source_voltage_mV / 1000) * average_current_mA # divide by 1000 as source voltage is in millivolts - this gives us milliwatts
182-
measurement_duration_h = self.get_measurement_duration_s() / 3600 # duration in seconds, divide by 3600 to get hours
200+
# divide by 1000 as source voltage is in millivolts - this gives us milliwatts
201+
average_power_mW = (self.source_voltage_mV / 1000) * average_current_mA
202+
# duration in seconds, divide by 3600 to get hours
203+
measurement_duration_h = self.get_measurement_duration_s() / 3600
183204
average_consumption_mWh = average_power_mW * measurement_duration_h
184205
return average_consumption_mWh
185206

186207
def get_average_charge_mC(self):
187208
"""Returns average charge in milli coulomb"""
188209
average_current_mA = self.get_average_current_mA()
189-
measurement_duration_s = self.get_measurement_duration_s() # in seconds
210+
# in seconds
211+
measurement_duration_s = self.get_measurement_duration_s()
190212
return average_current_mA * measurement_duration_s
191213

192214
def get_measurement_duration_s(self):
193215
"""Returns duration of measurement"""
194-
measurement_duration_s = (self.measurement_stop_time - self.measurement_start_time) # measurement duration in seconds
195-
return measurement_duration_s
216+
# measurement duration in seconds
217+
measurement_duration_s = (
218+
self.measurement_stop_time - self.measurement_start_time
219+
)
220+
return measurement_duration_s

0 commit comments

Comments
 (0)