Hi
Think I have sorted it.
Part of the problem was needing to specifically flush the file write, because it is in a polling loop function and the other was sorting out the file path
(I just did a ~/ in the example, but that won't work with C only bash)
Code:
component paramsaver;
pin in float invalue1 "Receives value1 to update to";
pin out float outvalue1 "Sends value1";
pin in float invalue2 "Receives value2 to update to";
pin out float outvalue2 "Sends value2";
pin in float invalue3 "Receives value3 to update to";
pin out float outvalue3 "Sends value3";
pin in bit readtrigger = 0 "signal to read from file";
pin in bit writetrigger = 0 "signal to write to file";
option singleton yes;
option userspace yes;
author "ArcEye <arceyeATmgwareDOTcoDOTuk>";
license "GPL";
;;
#include <stdio.h> /* Standard input/output definitions */
#include <stdlib.h>
#include <stdint.h> /* Standard types */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <signal.h>
#include <pwd.h>
int done = 0;
void adios(int sig) { done = 1; }
void user_mainloop(void)
{
char line[80];
char filepath[80];
float val1, val2, val3;
int read, write;
struct passwd *pw = getpwuid(getuid());
const char *homedir = pw->pw_dir;
sprintf(filepath, "%s/linuxcnc/param.sav", homedir);
read = write = 0;
signal(SIGINT, adios);
signal(SIGTERM, adios);
while(!done)
{
usleep(250000);
FOR_ALL_INSTS()
{
if(!readtrigger && read) read = 0;
if(!writetrigger && write) write = 0;
if(readtrigger && !read)
// reads the file and sets the out pins to the values read
// connect to the spinbox param-pin which will update the widget display
{
FILE *fp1;
fp1 = fopen(filepath, "r");
fgets(line, sizeof(line), fp1);
// only write to pins if sscanf was sucessful
if (sscanf(line, "%f %f %f ", &val1, &val2, &val3) != 1)
outvalue1 = val1; outvalue2 = val2; outvalue3 = val3;
fclose(fp1);
read = 1;
}
else if(writetrigger && ! write)
// reads the values from the in pins and write to file
// connect to the spinbox out pin
{
FILE *fp2;
fp2 = fopen(filepath, "w");
fprintf(fp2, "%lf %lf %lf\n", invalue1, invalue2, invalue3);
fflush(fp2);
fclose(fp2);
write = 1;
}
}
}
exit(0);
}
and proof of concept running in a sim
The input pins receive what you want to write to file and are connected to the spinbox hal pin so as to reflect the current value
The output pins receive the saved values from file and are connected to the spinbox.N.param_pin so as to update the spinbox from file
Note that the amended pyvcp_widgets param_pins and the corresponding paramsaver.output pins will remain at the last values they were set at until set again
They are checked internally in pyvcp_widget.py against the last value that
they were set to, not the last value of the spinbox widget.
The connections for the pyvcp panel in the screenshot were as below, which demonstrates
Code:
net 1p pyvcp.spinbox.0.param_pin <= paramsaver.outvalue1
net 2p pyvcp.spinbox0 => paramsaver.invalue1
net 3p pyvcp.spinbox.1.param_pin <= paramsaver.outvalue2
net 4p pyvcp.spinbox1 => paramsaver.invalue2
net 5p pyvcp.spinbox.2.param_pin <= paramsaver.outvalue3
net 6p pyvcp.spinbox2 => paramsaver.invalue3
net 7p pyvcp.load => paramsaver.readtrigger
net 8p pyvcp.write => paramsaver.writetrigger
Hopefully will do what you want
regards