From: Gene H. <ghe...@wd...> - 2014-07-03 11:40:00
|
On Wednesday 02 July 2014 14:20:57 andy pugh did opine And Gene did reply: > I use some pre-defined macros, controlled by a custom GUI to do much > of my lathe work. > The lathe (and the macros) are set up as metric. > The input data is entered using hal_spinbutton widgets (typically by > typing numbers, not by "spinning" them). > Recently I have been making a lot of imperial parts, and cutting > imperial threads. Which has meant a lot of to-and-fro to the > calculator. > > In pursuit of a lazier life I have added some extra code to the > usr/lib/pymodules/python2.6/gladevcp//hal_widgets.py file on my > machine. > Now, if I type "in" after a number then the value is immediately > replaced by the value multiplied by 25.4. > There is similar behaviour for "tpi", "mm" and "pitch". > I also trap the common imperial fractions, so the behaviour is > something like > > 3 = '3' > ' ' = '3 ' > 3 = '3 3' > / = '3 3/' > 4 = '3.75' > i = '3.75i' > n = '95.25' > > I had some discussion about the way it works, and how it ought to > work, with seb in IRC last night, and there was some disagreement > about the behaviour. > > I like it as it is. The textual "magic codes" simply prompt a fixed > arithmetic operation. Seb was of the opinion that the widget should be > aware of machine native units. > In some very simple setups there will not necessarily be any concept > of native units, and certainly there can often be no INI file to look > in, or linuxcnc.stat structure to query. > > So, I like it as it is, and it seems to me something unlikely to be > eventuated by accident, you are only going to know that the feature is > there from reading docs, which could explain the limitations. > > You still need to press "enter" to commit the new value to a HAL pin. > > Here is the code: > > class HAL_SpinButton(gtk.SpinButton, _HalWidgetBase): > __gtype_name__ = "HAL_SpinButton" > > def validate(self, *a): > data = self.get_text() > if data[-3:] == 'tpi': > self.set_value(25.4/float(data[:-3])) > elif data[-2:] == 'in': > self.set_value(25.4*float(data[:-2])) > elif data[-2:] == 'mm': > self.set_value(float(data[:-2])/25.4) > elif data[-5:] == 'pitch': > self.set_value(25.4/float(data[:-5])) > elif data[-2:] in [ '/2', '/4', '/8']: > v = data[:-2].split() > if len(v) == 2: > self.set_value(float(v[0]) + float(v[1]) / > float(data[-1:])) elif len(v) == 1: > self.set_value(float(v[0]) / float(data[-1:])) > elif data[-3:] in [ '/16', '/32', '/64']: > v = data[:-3].split() > if len(v) == 2: > self.set_value(float(v[0]) + float(v[1]) / > float(data[-2:])) elif len(v) == 1: > self.set_value(float(v[0]) / float(data[-2:])) > > def hal_update(self, *a): > data = self.get_value() > self.hal_pin_f.set(float(data)) > self.hal_pin_s.set(int(data)) > > def _hal_init(self): > self.hal_pin_f = self.hal.newpin(self.hal_name+"-f", > hal.HAL_FLOAT, hal.HAL_OUT) > self.hal_pin_s = self.hal.newpin(self.hal_name+"-s", > hal.HAL_S32, hal.HAL_OUT) > self.connect("value-changed", self.hal_update) > self.connect("changed", self.validate) > self.emit("value-changed") This will be very helpful Andy, thank you. Cheers, Gene Heskett -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) Genes Web page <http://geneslinuxbox.net:6309/gene> US V Castleman, SCOTUS, Mar 2014 is grounds for Impeaching SCOTUS |