Skip to content

Commit 2e4d713

Browse files
committed
Moved comments to top of variable instead of being on the same line
1 parent aabbfd9 commit 2e4d713

File tree

5 files changed

+85
-69
lines changed

5 files changed

+85
-69
lines changed

.github/workflows/ruff.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ jobs:
66
runs-on: ubuntu-latest
77
steps:
88
- uses: actions/checkout@v4
9-
- uses: astral-sh/ruff-action@v2
9+
- uses: astral-sh/ruff-action@v2
10+

example.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
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
@@ -46,9 +49,10 @@
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):
@@ -65,8 +69,8 @@
6569
# Print last 10 values of each channel
6670
print(ch[-10:])
6771
print()
68-
time.sleep(
69-
0.01
70-
) # 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)
7175

7276
ppk2_test.stop_measuring()

example_mp.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@
3232
"""
3333
Source mode example
3434
"""
35-
ppk2_test.use_source_meter() # set source meter mode
36-
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()
3741

38-
ppk2_test.start_measuring() # start measuring
3942
# measurements are a constant stream of bytes
4043
# the number of measurements in one sampling period depends on the wait between serial reads
4144
# it appears the maximum number of bytes received is 1024
@@ -57,13 +60,16 @@
5760

5861
time.sleep(0.001)
5962

60-
ppk2_test.toggle_DUT_power("OFF") # disable DUT power
63+
64+
# disable DUT power
65+
ppk2_test.toggle_DUT_power("OFF")
6166
ppk2_test.stop_measuring()
6267

6368
"""
6469
Ampere mode example
6570
"""
66-
ppk2_test.use_ampere_meter() # set ampere meter mode
71+
# set ampere meter mode
72+
ppk2_test.use_ampere_meter()
6773

6874
ppk2_test.start_measuring()
6975
while True:
@@ -80,8 +86,8 @@
8086
# Print last 10 values of each channel
8187
print(ch[-10:])
8288
print()
83-
time.sleep(
84-
0.001
85-
) # 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)
8692

8793
ppk2_test.stop_measuring()

src/power_profiler.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ def __init__(self, serial_port=None, source_voltage_mV=3300, filename=None):
2828
self.ppk2 = PPK2_API(serial_port)
2929

3030
try:
31-
ret = (
32-
self.ppk2.get_modifiers()
33-
) # 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()
3433
print(f"Initialized ppk2 api: {ret}")
3534
except Exception as e:
3635
print(f"Error initializing power profiler: {e}")
@@ -47,7 +46,8 @@ def __init__(self, serial_port=None, source_voltage_mV=3300, filename=None):
4746

4847
self.source_voltage_mV = source_voltage_mV
4948

50-
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)
5151

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

@@ -130,14 +130,15 @@ def disable_power(self):
130130
def measurement_loop(self):
131131
"""Endless measurement loop will run in a thread"""
132132
while True and not self.stop:
133-
if self.measuring: # read data if currently measuring
133+
# read data if currently measuring
134+
if self.measuring:
134135
read_data = self.ppk2.get_data()
135136
if read_data != b"":
136137
samples = self.ppk2.get_samples(read_data)
137-
self.current_measurements += (
138-
samples # can easily sum lists, will append individual data
139-
)
140-
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)
141142

142143
def _average_samples(self, list, window_size):
143144
"""Average samples based on window size"""
@@ -152,17 +153,22 @@ def _average_samples(self, list, window_size):
152153

153154
def start_measuring(self):
154155
"""Start measuring"""
155-
if not self.measuring: # toggle measuring flag only if currently not measuring
156-
self.current_measurements = [] # reset current measurements
157-
self.measuring = True # set internal flag
158-
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()
159164
self.measurement_start_time = time.time()
160165

161166
def stop_measuring(self):
162167
"""Stop measuring and return average of period"""
163168
self.measurement_stop_time = time.time()
164169
self.measuring = False
165-
self.ppk2.stop_measuring() # send command to ppk2
170+
# send command to ppk2
171+
self.ppk2.stop_measuring()
166172

167173
# samples_average = self._average_samples(self.current_measurements, 1000)
168174
if self.filename is not None:
@@ -182,32 +188,33 @@ def get_average_current_mA(self):
182188
if len(self.current_measurements) == 0:
183189
return 0
184190

191+
# measurements are in microamperes, divide by 1000
185192
average_current_mA = (
186193
sum(self.current_measurements) / len(self.current_measurements)
187-
) / 1000 # measurements are in microamperes, divide by 1000
194+
) / 1000
188195
return average_current_mA
189196

190197
def get_average_power_consumption_mWh(self):
191198
"""Return average power consumption of last measurement in mWh"""
192199
average_current_mA = self.get_average_current_mA()
193-
average_power_mW = (
194-
(self.source_voltage_mV / 1000) * average_current_mA
195-
) # divide by 1000 as source voltage is in millivolts - this gives us milliwatts
196-
measurement_duration_h = (
197-
self.get_measurement_duration_s() / 3600
198-
) # 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
199204
average_consumption_mWh = average_power_mW * measurement_duration_h
200205
return average_consumption_mWh
201206

202207
def get_average_charge_mC(self):
203208
"""Returns average charge in milli coulomb"""
204209
average_current_mA = self.get_average_current_mA()
205-
measurement_duration_s = self.get_measurement_duration_s() # in seconds
210+
# in seconds
211+
measurement_duration_s = self.get_measurement_duration_s()
206212
return average_current_mA * measurement_duration_s
207213

208214
def get_measurement_duration_s(self):
209215
"""Returns duration of measurement"""
216+
# measurement duration in seconds
210217
measurement_duration_s = (
211218
self.measurement_stop_time - self.measurement_start_time
212-
) # measurement duration in seconds
219+
)
213220
return measurement_duration_s

src/ppk2_api/ppk2_api.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ def _write_serial(self, cmd_tuple):
124124
def _twos_comp(self, val):
125125
"""Compute the 2's complement of int32 value"""
126126
if (val & (1 << (32 - 1))) != 0:
127-
val = val - (1 << 32) # compute negative value
127+
# compute negative value
128+
val = val - (1 << 32)
128129
return val
129130

130131
def _convert_source_voltage(self, mV):
@@ -141,11 +142,13 @@ def _convert_source_voltage(self, mV):
141142
# get difference to baseline (the baseline is 800mV but the initial offset is 32)
142143
diff_to_baseline = mV - self.vdd_low + offset
143144
base_b_1 = 3
144-
base_b_2 = 0 # is actually 32 - compensated with above offset
145+
# is actually 32 - compensated with above offset
146+
base_b_2 = 0
145147

146148
# get the number of times we have to increase the first byte of the command
147149
ratio = int(diff_to_baseline / 256)
148-
remainder = diff_to_baseline % 256 # get the remainder for byte 2
150+
# get the remainder for byte 2
151+
remainder = diff_to_baseline % 256
149152

150153
set_b_1 = base_b_1 + ratio
151154
set_b_2 = base_b_2 + remainder
@@ -201,9 +204,10 @@ def _get_masked_value(self, value, meas, is_bits=False):
201204
def _handle_raw_data(self, adc_value):
202205
"""Convert raw value to analog value"""
203206
try:
207+
# 5 is the number of parameters
204208
current_measurement_range = min(
205209
self._get_masked_value(adc_value, self.MEAS_RANGE), 4
206-
) # 5 is the number of parameters
210+
)
207211
adc_result = self._get_masked_value(adc_value, self.MEAS_ADC) * 4
208212
bits = self._get_masked_value(adc_value, self.MEAS_LOGIC)
209213
analog_value = (
@@ -273,28 +277,26 @@ def set_source_voltage(self, mV):
273277
def toggle_DUT_power(self, state):
274278
"""Toggle DUT power based on parameter"""
275279
if state == "ON":
280+
# 12,1
276281
self._write_serial(
277282
(PPK2_Command.DEVICE_RUNNING_SET, PPK2_Command.TRIGGER_SET)
278-
) # 12,1
283+
)
279284

280285
if state == "OFF":
281-
self._write_serial(
282-
(PPK2_Command.DEVICE_RUNNING_SET, PPK2_Command.NO_OP)
283-
) # 12,0
286+
# 12,0
287+
self._write_serial((PPK2_Command.DEVICE_RUNNING_SET, PPK2_Command.NO_OP))
284288

285289
def use_ampere_meter(self):
286290
"""Configure device to use ampere meter"""
287291
self.mode = PPK2_Modes.AMPERE_MODE
288-
self._write_serial(
289-
(PPK2_Command.SET_POWER_MODE, PPK2_Command.TRIGGER_SET)
290-
) # 17,1
292+
# 17,1
293+
self._write_serial((PPK2_Command.SET_POWER_MODE, PPK2_Command.TRIGGER_SET))
291294

292295
def use_source_meter(self):
293296
"""Configure device to use source meter"""
294297
self.mode = PPK2_Modes.SOURCE_MODE
295-
self._write_serial(
296-
(PPK2_Command.SET_POWER_MODE, PPK2_Command.AVG_NUM_SET)
297-
) # 17,2
298+
# 17,2
299+
self._write_serial((PPK2_Command.SET_POWER_MODE, PPK2_Command.AVG_NUM_SET))
298300

299301
def get_adc_result(self, current_range, adc_value):
300302
"""Get result of adc conversion"""
@@ -359,9 +361,8 @@ def get_adc_result(self, current_range, adc_value):
359361

360362
def _digital_to_analog(self, adc_value):
361363
"""Convert discrete value to analog value"""
362-
return int.from_bytes(
363-
adc_value, byteorder="little", signed=False
364-
) # convert reading to analog value
364+
# convert reading to analog value
365+
return int.from_bytes(adc_value, byteorder="little", signed=False)
365366

366367
def digital_channels(self, bits):
367368
"""
@@ -390,8 +391,8 @@ def get_samples(self, buf):
390391
Manipulation of samples is left to the user.
391392
See example for more info.
392393
"""
393-
394-
sample_size = 4 # one analog value is 4 bytes in size
394+
# one analog value is 4 bytes in size
395+
sample_size = 4
395396
offset = self.remainder["len"]
396397
samples = []
397398
raw_digital_output = []
@@ -438,12 +439,10 @@ def __init__(self, ppk2, quit_evt, buffer_len_s=10, buffer_chunk_s=0.5):
438439
self._stats = (None, None)
439440
self._last_timestamp = 0
440441

441-
self._buffer_max_len = int(
442-
buffer_len_s * 100000 * 4
443-
) # 100k 4-byte samples per second
444-
self._buffer_chunk = int(
445-
buffer_chunk_s * 100000 * 4
446-
) # put in the queue in chunks of 0.5s
442+
# 100k 4-byte samples per second
443+
self._buffer_max_len = int(buffer_len_s * 100000 * 4)
444+
# put in the queue in chunks of 0.5s
445+
self._buffer_chunk = int(buffer_chunk_s * 100000 * 4)
447446

448447
# round buffers to a whole sample
449448
if self._buffer_max_len % 4 != 0:
@@ -495,9 +494,8 @@ def get_data(self):
495494
count = 0
496495
while True:
497496
try:
498-
ret += self._buffer_q.get(
499-
timeout=0.001
500-
) # get_nowait sometimes skips a chunk for some reason
497+
# get_nowait sometimes skips a chunk for some reason
498+
ret += self._buffer_q.get(timeout=0.001)
501499
count += 1
502500
except queue.Empty:
503501
break

0 commit comments

Comments
 (0)