aboutsummaryrefslogtreecommitdiff
path: root/remote_plot.c
diff options
context:
space:
mode:
authorXiao Pan <xyz@flylightning.xyz>2025-05-13 01:39:28 -0700
committerXiao Pan <xyz@flylightning.xyz>2025-05-13 01:39:28 -0700
commit19bad30bfaefdfa12784bd7900fd4eee1574a94b (patch)
treeecd7e7ccc5a79c09c1f6b7f35af65026ffe05612 /remote_plot.c
parent2721baebd9e4d6e5c869af401c509e5e74c5a9c8 (diff)
Add and edit comments
Diffstat (limited to 'remote_plot.c')
-rw-r--r--remote_plot.c25
1 files changed, 22 insertions, 3 deletions
diff --git a/remote_plot.c b/remote_plot.c
index 9962281..7e43acd 100644
--- a/remote_plot.c
+++ b/remote_plot.c
@@ -28,7 +28,10 @@
#include <time.h>
#include <unistd.h>
#include <gtk/gtk.h>
+
+// Using a linked list to hold all the voltages and temperatures data
#include "list.h"
+
#include<stdint.h> //uint32_t
// use double for PLFLT
@@ -241,6 +244,7 @@ const char *checkbutton_names[]={
"0x68f_BMS_M8_Cell_12_Thermistor"
};
+// I passed this structure's pointer to many functions for them to use, which I think is kinda like the gtk style
typedef struct {
ssh_session session;
sftp_session sftp;
@@ -268,14 +272,14 @@ typedef struct {
bool just_start;
}DATA;
+// Read data from local. Or read data from local and remote and write to local.
static gboolean read_data(gpointer user_data)
-//static gboolean sftp_read_sync(gpointer user_data)
-//int sftp_read_sync(ssh_session session, sftp_session sftp)
{
//printf("read_data begin\n");
DATA *data=user_data;
bool new_entry=false;
+ // Read data from local is specify read from local on command line, or the program just started.
if(data->local || data->just_start)
{
if(data->just_start)
@@ -320,6 +324,7 @@ static gboolean read_data(gpointer user_data)
fclose(fp);
}
}
+ // Read data from remote
else
{
int access_type;
@@ -424,6 +429,7 @@ static gboolean read_data(gpointer user_data)
//printf("after read_data check local if else\n");
+ // Get and calculate min, max, and average of voltages and temperatures. And update corresponding labels.
if(new_entry)
{
double sum, avg, vmin, vmax;
@@ -468,6 +474,7 @@ static gboolean read_data(gpointer user_data)
sprintf(str,"min/avg/max temperature: %3hhu/%.0f/%3hhu C",tmin,avg,tmax);
gtk_label_set_text(GTK_LABEL(data->temp_label),str);
new_entry=false;
+ // Update drawing area
gtk_widget_queue_draw(data->area);
}
@@ -570,6 +577,7 @@ int verify_knownhost(ssh_session session)
// https://plplot.sourceforge.net/documentation.php
// https://plplot.sourceforge.net/examples.php
// plplot example 0 and ext-cairo-test.c
+// Draw plots on gtk drawing area
static void draw_function (GtkDrawingArea *area,
cairo_t *cr,
int width,
@@ -605,6 +613,7 @@ static void draw_function (GtkDrawingArea *area,
if((active_checkbutton[i]=gtk_check_button_get_active(GTK_CHECK_BUTTON(data->checkbutton[i]))))
plot_counts++;
+ // Plplot set to use extcairo device
plsdev( "extcairo" );
{
@@ -625,6 +634,7 @@ static void draw_function (GtkDrawingArea *area,
plspal1("cmap1_gray.pal", 1);
// Initialize plplot
+ // Max 2 plots per line if more than 2 plots
if(plot_counts>2)
plstar(2,(plot_counts+1)/2);
else
@@ -694,6 +704,7 @@ static void draw_function (GtkDrawingArea *area,
plend();
}
+// Print last seconds data to command line
static void print_data (GtkWidget *widget, gpointer user_data)
{
DATA *data=user_data;
@@ -756,6 +767,8 @@ static void last_seconds_toggle_callback(GtkWidget *widget, gpointer user_data)
gtk_widget_queue_draw(data->area);
}
+// Setup all the GUI widgets. Another bettery way maybe is use a .xml file instead.
+// Connect signals between widgets and callback functions.
static void activate (GtkApplication *app, gpointer user_data)
{
DATA *data=user_data;
@@ -866,11 +879,13 @@ static void activate (GtkApplication *app, gpointer user_data)
read_data(user_data);
//printf("before g_timeout_add\n");
+ // Call read_data function every 1 second
g_timeout_add(1000,read_data,user_data);
//printf("after g_timeout_add\n");
}
// https://gitlab.gnome.org/GNOME/gtk/-/blob/main/demos/gtk-demo/main.c
+// Setup all the command line things
static int command_line (GApplication *app, GApplicationCommandLine *cmdline, gpointer user_data)
{
DATA *data=user_data;
@@ -883,6 +898,7 @@ static int command_line (GApplication *app, GApplicationCommandLine *cmdline, gp
g_variant_dict_lookup (options, "destination", "&s", &dest);
g_variant_dict_lookup (options, "local", "b", &data->local);
+ // Only try to ssh connect to the server if I did not specify local on command line
if(!(data->local))
{
// Open session and set options
@@ -912,6 +928,7 @@ static int command_line (GApplication *app, GApplicationCommandLine *cmdline, gp
// Authenticate ourselves
// https://api.libssh.org/stable/libssh_tutor_authentication.html
+ // I choose to only use public key authentication
rc = ssh_userauth_publickey_auto(data->session, NULL, NULL);
if (rc != SSH_AUTH_SUCCESS)
{
@@ -940,7 +957,6 @@ static int command_line (GApplication *app, GApplicationCommandLine *cmdline, gp
}
}
-
// https://discourse.gnome.org/t/gio-application-commandline-does-trigger-activate-signal/2988/22
// > If you pass G_APPLICATION_HANDLES_COMMAND_LINE in the flags, the
// > command-line args are passed to the primary instance and ::activate is
@@ -960,6 +976,8 @@ int main (int argc, char **argv)
GtkApplication *app;
int status;
+ // Initialize filename to $XDG_DATA_HOME/mycan.csv. If no XDG_DATA_HOME
+ // environment variable, then initialize to $HOME/.local/share/mycan.csv
{
// usually ~/.local/share
const char *dir=getenv("XDG_DATA_HOME");
@@ -975,6 +993,7 @@ int main (int argc, char **argv)
sprintf(data.filename,"%s%s",dir,str);
}
+ // Initialize linked list
InitializeList(&data.cans);
if(ListIsFull(&data.cans))
{