Setting initval of a spinbox in hal

More
21 Apr 2013 19:43 - 21 Apr 2013 19:44 #32959 by mariusl
Hi All,
Is there a way to set the <initval> of a spinbox from HAL? I need to read a value from the .INI file and then set the spinbox for that value.

Regards
Marius


www.bluearccnc.com

Last edit: 21 Apr 2013 19:44 by mariusl.

Please Log in or Create an account to join the conversation.

More
21 Apr 2013 23:20 #32966 by ArcEye
Hi

Quick answer - No

The value is set at either 100 or <initval> in the .xml file if set.
By the time the pins are available, the panel has been created from the .xml file

Long answer - Anything is possible, if you want to do it.

Have a look at /usr/share/pyshared/pyvcp_widgets.py
The code for the spinbox widget starts at line 763

You could rewrite the widget so that it creates a param called initparam and updates when this changes.
class pyvcp_spinbox(Spinbox):
    """ (control) controls a float, also shown as text 
        reacts to the mouse wheel 
        <spinbox>
            [ <halpin>"my-spinbox"</halpin> ]
            [ <min_>55</min_> ]   sets the minimum value to 55
            [ <max_>123</max_> ]  sets the maximum value to 123
            [ <initval>100</initval> ]  sets intial value to 100  TJP 12 04 2007
        </spinbox>
    """
    # FIXME: scale resolution when shift/ctrl/alt is held down?
    n=0
    def __init__(self,master,pycomp,halpin=None, halparam=None,
                    min_=0,max_=100,initval=0,resolution=1,format="2.1f",**kw):
        self.v = DoubleVar()
        if 'increment' not in kw: kw['increment'] = resolution
        if 'from' not in kw: kw['from'] = min_
        if 'to' not in kw: kw['to'] = max_
        if 'format' not in kw: kw['format'] = "%" + format
        kw['command'] = self.command
        Spinbox.__init__(self,master,textvariable=self.v,**kw)
        
        if halpin == None:
            halpin = "spinbox."+str(pyvcp_spinbox.n)
            pyvcp_spinbox.n += 1
        self.halpin=halpin
        
        if halparam == None:
            halparam = "spinbox.initparam"
        self.halparam=halparam
        
        if initval < min_:
            self.value=min_
        elif initval > max_:
            self.value=max_
        else:
            self.value=initval
        self.oldvalue=min_
        self.init=self.value
        self.oldinit=self.init
        self.format = "%(b)"+format
        self.max_=max_
        self.min_=min_
        self.resolution=resolution
        self.v.set( str( self.format  % {'b':self.value} ) )
        pycomp.newpin(halpin, HAL_FLOAT, HAL_OUT)
        pycomp.newpin(halparam, HAL_FLOAT, HAL_IN)
        self.bind('<Button-4>',self.wheel_up)
        self.bind('<Button-5>',self.wheel_down)
        pycomp[self.halparam] = self.init

    def command(self):
        self.value = self.v.get()

    def update(self,pycomp):
        pycomp[self.halpin] = self.value
        if self.value != self.oldvalue:
            self.v.set( str( self.format  % {'b':self.value} ) )
            self.oldvalue=self.value
        self.init = pycomp[self.halparam]
        if self.init != self.oldinit:
            self.v.set( str( self.format  % {'b':self.init} ) )
            self.oldinit=self.init    
            self.value=self.init
          
    def wheel_up(self,event):
        self.value += self.resolution
        if self.value > self.max_:
            self.value = self.max_
          
     
    def wheel_down(self,event):
        self.value -= self.resolution
        if self.value < self.min_:
            self.value = self.min_
          


# -------------------------------------------

And here it is working - ignore the other stuff, I just added it to a sim to test it




This is not polished, I am not a python programmer, but it works.

(you need to delete /usr/lib/python2.6/dist-packages/pyvcp_widgets.pyc and insert this code into pyvcp_widgets.py or it will run the old compiled code)

regards
Attachments:
The following user(s) said Thank You: AndrewL

Please Log in or Create an account to join the conversation.

More
22 Apr 2013 00:26 #32968 by mariusl
Damnnnn!! some guys are clever.
Thanks a lot man I will test this out and include in my screen.
I think this should be standard so that one can set values instead of hard coding them into the screen. I will use these on my plasma setup.

Regards
Marius


www.bluearccnc.com

Please Log in or Create an account to join the conversation.

More
22 Apr 2013 01:09 - 22 Apr 2013 03:41 #32971 by mariusl
Arceye, I commented out the spinbox and added your new code. Then I deleted the pyc file and loaded my controller. This is the error that comes up. I am no good at python so I dont even know here to start looking.

Error constructing spinbox({'max_': 99, 'format': '3.0f', 'initval': 54, 'width': '3', 'min_': 1, 'halpin': 'Spin-TravelHeight', 'resolution': 1, 'justify': 'right'}):
Duplicate item name 'spinbox.initparam'

I also found more than one instance of the file.

./usr/share/pyshared/pyvcp_widgets.py
./usr/lib/pymodules/python2.6/pyvcp_widgets.py
./usr/lib/python2.6/dist-packages/pyvcp_widgets.pyc

P.S. I think it might work with one only spinbox. I have several. Can you test it with more than one please?

Regards
Marius


www.bluearccnc.com

Last edit: 22 Apr 2013 03:41 by mariusl.

Please Log in or Create an account to join the conversation.

More
22 Apr 2013 13:41 #32979 by ArcEye
Hi

Yes the error is because you have more than one spinbox
I will change it so that the instances get numbered
This code was just a first draft

Regards the other copies of the file, I had these too.

I deleted all of them and created a symlink to the .py file in /usr/share
at each location

I found it was only the .pyc in python2.6 that regenerated

I,ll get back later

regards

Please Log in or Create an account to join the conversation.

More
22 Apr 2013 15:23 - 22 Apr 2013 15:28 #32980 by ArcEye
Hi

Have amended the code slightly so that the initparams number the same as the spinboxes

If you name your spinboxes with a suffixed number starting at 0, as I did, it is easy to see which initparam matches which spinbox

The new code is
class pyvcp_spinbox(Spinbox):
    """ (control) controls a float, also shown as text 
        reacts to the mouse wheel 
        <spinbox>
            [ <halpin>"my-spinbox"</halpin> ]
            [ <min_>55</min_> ]   sets the minimum value to 55
            [ <max_>123</max_> ]  sets the maximum value to 123
            [ <initval>100</initval> ]  sets intial value to 100  TJP 12 04 2007
        </spinbox>
    """
    # FIXME: scale resolution when shift/ctrl/alt is held down?
    n=0
    def __init__(self,master,pycomp,halpin=None, halparam=None,
                    min_=0,max_=100,initval=0,resolution=1,format="2.1f",**kw):
        self.v = DoubleVar()
        if 'increment' not in kw: kw['increment'] = resolution
        if 'from' not in kw: kw['from'] = min_
        if 'to' not in kw: kw['to'] = max_
        if 'format' not in kw: kw['format'] = "%" + format
        kw['command'] = self.command
        Spinbox.__init__(self,master,textvariable=self.v,**kw)
        
        if halpin == None:
            halpin = "spinbox."+str(pyvcp_spinbox.n)
            
        self.halpin=halpin
        
        if halparam == None:
            halparam = "spinbox.initparam."+ str(pyvcp_spinbox.n)
        self.halparam=halparam
        pyvcp_spinbox.n += 1
        
        if initval < min_:
            self.value=min_
        elif initval > max_:
            self.value=max_
        else:
            self.value=initval
        self.oldvalue=min_
        self.init=self.value
        self.oldinit=self.init
        self.format = "%(b)"+format
        self.max_=max_
        self.min_=min_
        self.resolution=resolution
        self.v.set( str( self.format  % {'b':self.value} ) )
        pycomp.newpin(halpin, HAL_FLOAT, HAL_OUT)
        pycomp.newpin(halparam, HAL_FLOAT, HAL_IN)
        self.bind('<Button-4>',self.wheel_up)
        self.bind('<Button-5>',self.wheel_down)
        pycomp[self.halparam] = self.init

    def command(self):
        self.value = self.v.get()

    def update(self,pycomp):
        pycomp[self.halpin] = self.value
        if self.value != self.oldvalue:
            self.v.set( str( self.format  % {'b':self.value} ) )
            self.oldvalue=self.value
        self.init = pycomp[self.halparam]
        if self.init != self.oldinit:
            self.v.set( str( self.format  % {'b':self.init} ) )
            self.oldinit=self.init    
            self.value=self.init
          
    def wheel_up(self,event):
        self.value += self.resolution
        if self.value > self.max_:
            self.value = self.max_
          
     
    def wheel_down(self,event):
        self.value -= self.resolution
        if self.value < self.min_:
            self.value = self.min_
          


# -------------------------------------------

and 'proof of life'




Don't forget to delete all the .pyc files, replace the various .py files with symlinks to /usr/share/pyshared/pyvcp_widgets.py and then you won't get into difficulties with old code being used.

regards
Attachments:
Last edit: 22 Apr 2013 15:28 by ArcEye.
The following user(s) said Thank You: mariusl

Please Log in or Create an account to join the conversation.

More
22 Apr 2013 15:28 #32981 by mariusl
Awesome stuff man. I suggest that you submit this as a featured change. It is very handy.

Just to make sure I dont screw this up.

This is where the real code sits:
./usr/share/pyshared/pyvcp_widgets.py

These two are replaced by a symlink:
./usr/lib/pymodules/python2.6/pyvcp_widgets.py
./usr/lib/python2.6/dist-packages/pyvcp_widgets.pyc

Regards
Marius


www.bluearccnc.com

Please Log in or Create an account to join the conversation.

More
22 Apr 2013 15:36 #32982 by ArcEye
Almost

Just delete all the .pyc (python compiled) libs, those are generated automatically from the new .py file next time it is run

You need to symlink the /usr/share/pyshared/pyvcp_widgets.py to everywhere it occurred, having deleted the old .py file first of course
I actually had it in /usr/lib/python2.5 as well, but you probably don't have that

regards

Please Log in or Create an account to join the conversation.

More
22 Apr 2013 15:48 #32983 by mariusl
It works like a charm. Thanks a lot ArcEye.

Regards
Marius


www.bluearccnc.com

Please Log in or Create an account to join the conversation.

More
23 Apr 2013 18:21 - 23 Apr 2013 21:49 #33050 by ArcEye
Hi

If you or anyone else wants to, this patch will change the spinbox, scale and dial widgets, so that there is a parameter pin which allows the initial setting through HAL.
EDIT: It is not just for initialisation, it can be used to change the value displayed and output directly.

The methodology and naming is the same as I did for the spinbox, if you number your widgets with a suffix starting at 0, they will match the relevant initparams.



I don't know how generally useful this is, so don't think I will bother submitting for inclusion in the release.

regards

(rename to params.patch)


File Attachment:

File Name: params-patch.txt
File Size:6 KB
Attachments:
Last edit: 23 Apr 2013 21:49 by ArcEye.

Please Log in or Create an account to join the conversation.

Time to create page: 0.147 seconds
Powered by Kunena Forum