Skip to content
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

Outfile #1

Open
adamssson opened this issue Jul 30, 2024 · 6 comments
Open

Outfile #1

adamssson opened this issue Jul 30, 2024 · 6 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@adamssson
Copy link

You don't mention in documentation how to define outfile. It's also not visible in comments.

@tspspi
Copy link
Owner

tspspi commented Jul 30, 2024

@adamssson what do you mean with outfile?

@adamssson
Copy link
Author

adamssson commented Jul 30, 2024

@tspspi Sorry - this term saw in your logpressure.py - line14. I meant external file where all measures are stored.
Don't get it how to define file and location. Without this your script running into error.

@tspspi
Copy link
Owner

tspspi commented Jul 31, 2024

@adamssson Now I get it - I haven't looked into the examples and wondered. I'm going to update this / move to argparse in the example when there is some spare time. Currently this is only a really crude implementation that captures the measurement values till the program is interrupted via Keyboard Interrupt and then in the end writes everything into a numpy npz file where one can just specify the filename. Maybe I'm going to rework the whole usage sample at some point - I've just written this in a hurry to show how one can use the library / since I needed to monitor pressure of a system over night.

@adamssson
Copy link
Author

adamssson commented Jul 31, 2024

Thanks this will be helpful.
In the mean time would you be so kind and let me know how to link pybpg400 to external script to display pressure values. Specifically which variable I should link to get pressure value on screen?
Don't need storing data or graphs, just indication of pressure.
This question my looks silly but I'm 2 weeks in programming and building app displaying pressure with Tkinter.

@tspspi
Copy link
Owner

tspspi commented Jul 31, 2024

This question is not silly - but doing this with this library in a GUI is not so straight forward as doing in a simple loop. This is because this "library" is written in an imperative and non event based style. To use it in a GUI application you'd need some kind of timer that queries the get_pressure() function periodically or query it from your event loop. Maybe I have some time in the next view days hacking something together that shows how this works as a complete example. As some starters for how my personal approach would look like:

For TK

To execute a task periodically you could use tk's after:

def fetch_pressure(root,pressureString):
	current_pressure = pg.get_pressure()
	root.after(1000, fetch_pressure)
	pressureString.set(f"{current_pressure} mbar")

And before starting you main loop register the task to run after a second:

pressureString = StringVar()
pressureString.set("--- mbar")

pressureLabel = Label(root, textvariable = pressureString)

root.after(1000, fetch_pressure, root, pressureString)
root.mainloop()

Using the CLI

For now a very simple way of pressure indication that utilizes a CLI but not a GUI that I can imagine would just look like the following:

fom labdevices.pressuregauge import PressureGaugeUnit
from bpg400.bpg400 import BGP400_RS232

import sys
from time import sleep
from time import time

DEVNAME="/dev/cuaU0"

with BGP400_RS232(DEVNAME) as pg:
	pg.set_unit(PressureGaugeUnit.MBAR)
	
	while pg.get_pressure() is None:
		sleep(0.1)

	try:
		while True:
			pres = pg.get_pressure()
			ts = time()
			print(f"{ts}: {pres} mbar")
			sleep(1)
	except KeyboardInterrupt:
		pass

Matplotlib

If you really only need a very quick visualization you could also utilize a way of using matplotlib and just reopening the window all the time. This is then about the same as the CLI example just closing the plot all over again (not complete code, just a sample):

def close_event():
	plt.close()

while True:
	fig, ax = plt.subplots(...)
	
	# Do plotting

	# Here we start a timer that  closes the plot after 30 seconds and then displays everything.
	# Then the loop starts over again ...

	tmr = fig.canvas.new_timer(interval=30000)
	tmr.add_callback(close_event)
	tmr.start()
	plt.show()

Using FreeSimpleGUI and Matplotlib

What I usually do when I really want to visualize in a very quick way is embedding matplotlib in freesimplegui since one doesnt have to deal with most of the GUI stuff there - I've got a small project at GitHub that does something similar but received information via MQTT - when using such an approach one would put the query somewhere into the event loop or trigger a timer periodically (this would be the loop at line 121 in the main program there)

@adamssson
Copy link
Author

Thanks! You are Star!

@tspspi tspspi self-assigned this Jul 31, 2024
@tspspi tspspi added the documentation Improvements or additions to documentation label Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants