If someone is interested this code which seems to work.
Code:
#!/usr/bin/python
import time
import hal
fname = "timelog.txt"
sumlog = "time.txt"
delta_t = 1 # seconds poll interval, hold inputs at least this long
f = open(fname,mode="a+") # a: append or create if file doesnt exist
h = hal.component("timelog")
h.newpin("active",hal.HAL_BIT,hal.HAL_IN)
h.newpin("seconds",hal.HAL_U32,hal.HAL_OUT)
h.newpin("minutes",hal.HAL_U32,hal.HAL_OUT)
h.newpin("hours",hal.HAL_U32,hal.HAL_OUT)
h.ready()
try:
f2= open(sumlog,mode="r+")
runtime=float(f2.readline())
f2.close()
except:
f2= open(sumlog,mode="w+")
runtime=0.0
f2.write(str(runtime))
#f3.flush()
f2.truncate()
f2.close()
m, s = divmod(runtime, 60)
hr, m = divmod(m, 60)
#print(hr,m,s)
h["seconds"]=s
h["minutes"]=m
h["hours"]=hr
old_start_pin_value = False
t_start = time.time()
t_begin = time.time()
msg = ("Begin %s\n" % time.ctime())
f.write(msg)
f.flush()
started = False
while True:
time.sleep(delta_t)
#print h["active"]
start_pin_value = h["active"]
if start_pin_value != old_start_pin_value:
print("changed")
if started:
started = False
print("timer off")
t_now = time.time()
msg = ("Stop: %s Elapsed: %f\n" % (time.ctime(),t_now - t_start))
f.write(msg)
f.flush()
runtime=seconds
savetime=str(seconds)
print savetime
f2= open(sumlog,mode="w+")
f2.write(savetime)
#f3.flush()
f2.truncate()
f2.close()
else:
started = True
print("timer on")
msg = ("Start: %s\n" %time.ctime())
f.write(msg)
f.flush()
t_start = time.time()
f2= open(sumlog,"r")
runtime=float(f2.readline())
f2.close()
print runtime
if start_pin_value:
seconds= runtime+(time.time() - t_start)
m, s = divmod(seconds, 60)
hr, m = divmod(m, 60)
h["seconds"]=s
h["minutes"]=m
h["hours"]=hr
old_start_pin_value = start_pin_value