-
Notifications
You must be signed in to change notification settings - Fork 64
Fixed issue where metadata would come with garbage #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tjoms99
wants to merge
7
commits into
IRNAS:master
Choose a base branch
from
Tjoms99:metadata_fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a489d37
Fixed issue where unicode exception would be thrown if a previous PPK…
Tjoms99 683d0c4
Added example to reproduce/showcase error and fix. This examples shou…
Tjoms99 36e145a
Empty buffer on ppk2 init, remove retries on metadata as it was rathe…
Tjoms99 bd2a0da
Add retries on connection as a bad exit can cause the serial port to …
Tjoms99 2b12d1d
Remove logging and raise exceptions instead. Calling logger.*(message…
Tjoms99 df7706f
Fixed borken api call in power_profiler.py
Tjoms99 047c306
Harden API by adding types to functions
Tjoms99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import time | ||
| import threading | ||
|
|
||
| from src.ppk2_api.ppk2_api import PPK2_API | ||
|
|
||
|
|
||
| class PowerProfiler: | ||
| conversion_factor = 1_000_000 # uA to mW | ||
|
|
||
| def __init__(self, voltage_mv=3700): | ||
| self.voltage_mv = voltage_mv | ||
| self.total_power_mW = 0 | ||
| self.total_samples = 0 | ||
|
|
||
| self.lock = threading.Lock() | ||
| self.ppk2 = None | ||
| self.sampling_thread = None | ||
| self.sampling_enabled = False | ||
|
|
||
| def _connect_ppk2(self): | ||
| ppk2s_connected = [] | ||
| #After reset, the PPK2 can take a moment to reappear on the system. We wait for it to show up for a few seconds before giving up. | ||
| for _ in range(10): | ||
| ppk2s_connected = PPK2_API.list_devices() | ||
| print(f"PPK2 devices found: {ppk2s_connected}") | ||
| if ppk2s_connected: | ||
| break | ||
| time.sleep(0.5) | ||
|
|
||
| if not ppk2s_connected: | ||
| raise ConnectionError("No PPK2 devices found!") | ||
|
|
||
| print(ppk2s_connected) | ||
| # Just select the first available PPK2 device | ||
| for ppk2_port_tuple in ppk2s_connected: | ||
| ppk2_port = ppk2_port_tuple[0] #Just get the port part of the tuple | ||
| print(f"Connecting to {ppk2_port}") | ||
| try: | ||
| self.ppk2 = PPK2_API(ppk2_port, timeout=1, write_timeout=1, exclusive=True) | ||
| self.ppk2.get_modifiers() | ||
| return | ||
| except Exception: | ||
| print(f"Failed to connect to {ppk2_port}") | ||
| self.ppk2 = None | ||
|
|
||
| raise ConnectionError("Could not initialize any detected PPK2 device.") | ||
|
|
||
|
|
||
| def _setup_ppk2(self): | ||
| self._connect_ppk2() | ||
|
|
||
| self.ppk2.set_source_voltage(self.voltage_mv) | ||
| self.ppk2.use_source_meter() | ||
| self.ppk2.toggle_DUT_power("ON") | ||
|
|
||
| self.ppk2.start_measuring() | ||
| print("Initialized Power Profiler") | ||
|
|
||
|
|
||
| def _run_sampling(self): | ||
| try: | ||
| self._setup_ppk2() | ||
| while self.sampling_enabled: | ||
| time.sleep(0.01) | ||
| read_data = self.ppk2.get_data() | ||
|
|
||
| if read_data == b"": | ||
| continue | ||
|
|
||
| samples, raw_digital = self.ppk2.get_samples(read_data) | ||
| if not samples: | ||
| continue | ||
|
|
||
| average_current_uA = sum(samples) / len(samples) | ||
| average_power_mW = (average_current_uA * self.voltage_mv) / self.conversion_factor | ||
| formatted_power = round(average_power_mW, 2) | ||
|
|
||
| with self.lock: | ||
| self.total_power_mW += formatted_power | ||
| self.total_samples += 1 | ||
| average_of_averages_mW = self.total_power_mW / self.total_samples | ||
|
|
||
| print(f"{formatted_power} mW, Avg: {average_of_averages_mW:.2f} mW") | ||
|
|
||
| except Exception as e: | ||
| self.sampling_enabled = False | ||
| print(f"An error occurred: {e}") | ||
|
|
||
| def start_sampling(self): | ||
| self.sampling_enabled = True | ||
| self.sampling_thread = threading.Thread(target=self._run_sampling, daemon=True) | ||
| self.sampling_thread.start() | ||
|
|
||
| def stop_sampling(self): | ||
| self.sampling_enabled = False | ||
| self.sampling_thread.join() | ||
|
|
||
|
|
||
| def main(): | ||
| sampler = PowerProfiler(voltage_mv=3800) | ||
| sampler.start_sampling() | ||
| input("Press Enter to exit...\n") | ||
| sampler.stop_sampling() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works with python3.10 (which is the oldest still supported) and I am a big fan of the type hints.