<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env python
# -*- coding:UTF-8 -*-
"""
    This file will control some options of the gmoccapy plasma screen
    and demonstrats at the same time the possibilities you have introducing
    your own handler files and functions to that screen, showing the
    possibilities to modify the layout and behavior

    Copyright 2013 Norbert Schechner
    nieson@web.de

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
modified 12/16 for my table JDM
"""

import hal_glib                           # needed to make our own hal pins
import hal                                # needed to make our own hal pins
from gladevcp.persistence import IniFile  # we use this one to save the states of the widgets on shut down and restart
from gladevcp.persistence import widget_defaults
from gladevcp.persistence import select_widgets
import gtk
from gmoccapy import preferences
from gmoccapy import getiniinfo

class StatisticsClass:

    def __init__(self, halcomp, builder, useropts):
        self.builder = builder
        self.halcomp = halcomp
        self.defaults = { IniFile.vars : { 

                                           "piercecount"        : 0,

                                           "distancecut"        : 0.0,

                                           "cuttimehours"       : 0,
                                           "cuttimeminutes"     : 0,
                                           "cuttimeseconds"     : 0.0,
                                          

                                           "rapidtimehours"    : 0,
                                           "rapidtimeminutes"  : 0,
                                           "rapidtimeseconds"  : 0.0,
                                           

                                           "probetimehours"   : 0,
                                           "probetimeminutes" : 0,
                                           "probetimeseconds" : 0.0,

                                         },
                        }

        get_ini_info = getiniinfo.GetIniInfo()
        prefs = preferences.preferences(get_ini_info.get_preference_file_path())
        theme_name = prefs.getpref("gtk_theme", "Follow System Theme", str)
        if theme_name == "Follow System Theme":
            theme_name = gtk.settings_get_default().get_property("gtk-theme-name")
        gtk.settings_get_default().set_string_property("gtk-theme-name", theme_name, "")

        self.ini_filename = __name__ + ".var"
        self.ini = IniFile(self.ini_filename, self.defaults, self.builder)
        self.ini.restore_state(self)

        self.lastcutmillisecs = 0
        self.lastrapidmillisecs = 0
        self.lastprobeseconds = 0
        self.lastDTGvalue = 0
        self.motiontype = 0
        self.cutting_enable = False
        self.tempcutlen = 0
        self.cuttimemS = 0
        self.rapidtimemS = 0
        self.firsttime = True
        self. lastcutdistance = 0

        # lets make our input pins
        self.pierce_count_in = hal_glib.GPin(halcomp.newpin("Pierce-Count-In", hal.HAL_BIT, hal.HAL_IN))

        self.cut_distance_in = hal_glib.GPin(halcomp.newpin("Cut-Distance-In", hal.HAL_FLOAT, hal.HAL_IN))
        self.motiontype_in = hal_glib.GPin(halcomp.newpin("Motion-Type-In", hal.HAL_S32, hal.HAL_IN)) 
        self.cutting_enable_in = hal_glib.GPin(halcomp.newpin("Cutting-Enable-In",hal.HAL_BIT, hal.HAL_IN))

        self.cut_time_mS_in = hal_glib.GPin(halcomp.newpin("Cut-Time-mS-In", hal.HAL_U32, hal.HAL_IN))

        self.rapid_time_mS_in = hal_glib.GPin(halcomp.newpin("Rapid-Time-mS-In", hal.HAL_U32, hal.HAL_IN))

        self.probe_time_seconds_in = hal_glib.GPin(halcomp.newpin("Probe-Time-Seconds-In", hal.HAL_U32, hal.HAL_IN))

        # get all widgets and connect them

        self.pierce_count_reset = self.builder.get_object("hal_btn_reset_pierce_count")
        self.pierce_count_reset.connect("pressed", self.on_btn_pierce_count_reset)

        self.lbl_pierce_count = self.builder.get_object("lbl_pierce_count")
        self.lbl_pierce_count.set_label("%d" % self.piercecount)
 
        self.distance_cut_reset = self.builder.get_object("hal_btn_reset_cut_distance")
        self.distance_cut_reset.connect("pressed", self.on_btn_distance_cut_reset)

        self.lbl_cut_distance = self.builder.get_object("lbl_cut_distance")
        self.lbl_cut_distance.set_label("%.f" % self.distancecut)

        self.cut_time_reset = self.builder.get_object("hal_btn_reset_cut_time")
        self.cut_time_reset.connect("pressed", self.on_btn_cut_time_reset)

        self.lbl_cut_time_hours = self.builder.get_object("lbl_cut_time_hours")
        self.lbl_cut_time_minutes = self.builder.get_object("lbl_cut_time_minutes")
        self.lbl_cut_time_seconds = self.builder.get_object("lbl_cut_time_seconds")
        self.lbl_cut_time_hours.set_label("%02d" % self.cuttimehours)
        self.lbl_cut_time_minutes.set_label("%02d" % self.cuttimeminutes)
        self.lbl_cut_time_seconds.set_label("%02d" % self.cuttimeseconds)

        self.rapid_time_reset = self.builder.get_object("hal_btn_reset_rapid_time")
        self.rapid_time_reset.connect("pressed", self.on_btn_rapid_time_reset)

        self.lbl_rapid_time_hours = self.builder.get_object("lbl_rapid_time_hours")
        self.lbl_rapid_time_minutes = self.builder.get_object("lbl_rapid_time_minutes")
        self.lbl_rapid_time_seconds = self.builder.get_object("lbl_rapid_time_seconds")
        self.lbl_rapid_time_hours.set_label("%02d" % self.rapidtimehours)
        self.lbl_rapid_time_minutes.set_label("%02d" % self.rapidtimeminutes)
        self.lbl_rapid_time_seconds.set_label("%02d" % self.rapidtimeseconds)

        self.probe_time_reset = self.builder.get_object("hal_btn_reset_probe_time")
        self.probe_time_reset.connect("pressed", self.on_btn_probe_time_reset)

        self.lbl_probe_time_hours = self.builder.get_object("lbl_probe_time_hours")
        self.lbl_probe_time_minutes = self.builder.get_object("lbl_probe_time_minutes")
        self.lbl_probe_time_seconds = self.builder.get_object("lbl_probe_time_seconds")
        self.lbl_probe_time_hours.set_label("%02d" % self.probetimehours)
        self.lbl_probe_time_minutes.set_label("%02d" % self.probetimeminutes)
        self.lbl_probe_time_seconds.set_label("%02d" % self.probetimeseconds)

        self.all_statistics_reset = self.builder.get_object("hal_btn_reset_all_statistics")
        self.all_statistics_reset.connect("pressed", self.on_btn_all_statistics_reset)

        #connect and service value changes on hal input pins
	self.pierce_count_in.connect("value-changed", self._on_pierce_count_in)

	self.cut_distance_in.connect("value-changed", self._on_cut_distance_in)

        self.motiontype_in.connect("value-changed", self._on_motiontype_in)

        self.cutting_enable_in.connect("value-changed", self._on_cutting_enable_change)

	#connect and service value changes on hal input pins
        self.cut_time_mS_in.connect("value-changed", self._on_cut_time_mS_in)

        self.rapid_time_mS_in.connect("value-changed", self._on_rapid_time_mS_in)

        self.probe_time_seconds_in.connect("value-changed", self._on_probe_time_seconds_in)

    #leave these together
        self.statistics_tab = self.builder.get_object("vbox_statistics_tab")
        self.statistics_tab.connect("destroy", self._on_destroy)

    def _on_destroy(self, obj, data = None):
        self.ini.save_state(self)
   #end leave these together

    # What to do on button pres events?
    def on_btn_all_statistics_reset(self, widget, data = None):
        self.on_btn_pierce_count_reset(self.pierce_count_reset)
        self.on_btn_distance_cut_reset( self.distance_cut_reset)
        self.on_btn_cut_time_reset(self.cut_time_reset)
        self.on_btn_probe_time_reset(self.probe_time_reset)
        self.on_btn_rapid_time_reset(self.rapid_time_reset)

    def on_btn_pierce_count_reset(self, widget, data = None):
        self.piercecount = 0
	self.lbl_pierce_count.set_label("%d" % self.piercecount)
             
    def _on_pierce_count_in(self,hal_pin, data = None):
        if hal_pin.get() == 1:
             self.piercecount = self.piercecount + 1
             self.lbl_pierce_count.set_label("%d" % self.piercecount)

    def on_btn_distance_cut_reset(self, widget, data = None):
        self.distancecut = 0
	self.lbl_cut_distance.set_label("%d" % self.distancecut)
  
    def _on_motiontype_in(self,hal_pin, data = None):
        self.motiontype = hal_pin.get()
        if self.motiontype == 0 and self.firsttime == True:
#        if self.firsttime == True:
             self.distancecut = self.distancecut + self.lastcutdistance
             self.lbl_cut_distance.set_label("%.1f" % self.distancecut)
             self.firsttime = False
        else:
             if self.motiontype == 2 or self.motiontype == 3:
                  self.firsttime = True

    def _on_cutting_enable_change(self, hal_pin, data = None):
#        if hal_pin.get() == 1:
        self.cutting_enable = hal_pin.get()
             
    def _on_cut_distance_in(self,hal_pin, data = None):
#        if self.motiontype == 0 and self.firsttime == True:
#             self.distancecut = self.distancecut + hal_pin.get()
#             self.lbl_cut_distance.set_label("%.1f" % self.distancecut)
#             self.firsttime = False
#        else:
#             if self.motiontype == 2 or self.motiontype == 3:
#                  self.firsttime = True

#        if self.lastcutdistance == 0:
#             self.distancecut = self.distancecut + hal_pin.get()
#             self.lbl_cut_distance.set_label("%.1f" % self.distancecut)
        self.lastcutdistance = hal_pin.get()

    def on_btn_cut_time_reset(self, widget, data = None):
        self.cuttimehours = 0
        self.cuttimeminutes = 0
        self.cuttimeseconds = 0
        self.cuttimemS = 0
        self.lbl_cut_time_hours.set_label("%02d" % self.cuttimehours)
        self.lbl_cut_time_minutes.set_label("%02d" % self.cuttimeminutes)
        self.lbl_cut_time_seconds.set_label("%02d" % self.cuttimeseconds)

    def _on_cut_time_mS_in(self, hal_pin, data = None):
	if self.lastcutmillisecs != hal_pin.get() and (self.motiontype == 2 or self.motiontype == 3) and self.cutting_enable == True:
             self.cuttimemS = self.cuttimemS + .1
             self.cuttimeseconds = self.cuttimemS
             if self.cuttimeseconds &gt;= 60:
                  self.cuttimeseconds = 0
                  self.cuttimemS = 0
                  self.cuttimeminutes = self.cuttimeminutes + 1
                  if self.cuttimeminutes &gt;= 60:
                       self.cuttimeminutes = 0
                       self.cuttimehours = self.cuttimehours + 1
        self.lbl_cut_time_hours.set_label("%02d" % self.cuttimehours)
        self.lbl_cut_time_minutes.set_label("%02d" % self.cuttimeminutes)
        self.lbl_cut_time_seconds.set_label("%02d" % self.cuttimeseconds)
        self.lastcutmillisecs = hal_pin.get()

    def on_btn_probe_time_reset(self, widget, data = None):
        self.probetimehours = 0
        self.probetimeminutes = 0
        self.probetimeseconds = 0
        self.lbl_probe_time_hours.set_label("%02d" % self.probetimehours)
        self.lbl_probe_time_minutes.set_label("%02d" % self.probetimeminutes)
        self.lbl_probe_time_seconds.set_label("%02d" % self.probetimeseconds)

    def _on_probe_time_seconds_in(self, hal_pin, data = None):
	if self.lastprobeseconds != hal_pin.get():
             self.probetimeseconds = self.probetimeseconds + 1
             if self.probetimeseconds &gt;= 60:
                  self.probetimeseconds = 0
                  self.probetimeminutes = self.probetimeminutes + 1
                  if self.probetimeminutes &gt;= 60:
                       self.probetimeminutes = 0
                       self.probetimehours = self.probetimehours + 1
        self.lbl_probe_time_hours.set_label("%02d" % self.probetimehours)
        self.lbl_probe_time_minutes.set_label("%02d" % self.probetimeminutes)
        self.lbl_probe_time_seconds.set_label("%02d" % self.probetimeseconds)
        self.lastprobeseconds = hal_pin.get()

    def on_btn_rapid_time_reset(self, widget, data = None):
        self.rapidtimehours = 0
        self.rapidtimeminutes = 0
        self.rapidtimeseconds = 0
        self.rapidtimemS = 0
        self.lbl_rapid_time_hours.set_label("%02d" % self.rapidtimehours)
        self.lbl_rapid_time_minutes.set_label("%02d" % self.rapidtimeminutes)
        self.lbl_rapid_time_seconds.set_label("%02d" % self.rapidtimeseconds)

    def _on_rapid_time_mS_in(self, hal_pin, data = None):
	if self.lastrapidmillisecs != hal_pin.get() and self.motiontype == 1:
             self.rapidtimemS = self.rapidtimemS + .1
             self.rapidtimeseconds = self.rapidtimemS
             if self.rapidtimeseconds &gt;= 60:
                  self.rapidtimeseconds = 0
                  self.rapidtimemS = 0
                  self.rapidtimeminutes = self.rapidtimeminutes + 1
                  if self.rapidtimeminutes &gt;= 60:
                       self.rapidtimeminutes = 0
                       self.rapidtimehours = self.rapidtimehours + 1
        self.lbl_rapid_time_hours.set_label("%02d" % self.rapidtimehours)
        self.lbl_rapid_time_minutes.set_label("%02d" % self.rapidtimeminutes)
        self.lbl_rapid_time_seconds.set_label("%02d" % self.rapidtimeseconds)
        self.lastrapidmillisecs = hal_pin.get()

def get_handlers(halcomp, builder, useropts):
    return[StatisticsClass(halcomp, builder, useropts)]
</pre></body></html>