Works on multiple platforms

Placed RasPi specific code into Try, Except bolcks. Code now runs under Windows, Non Pi Linux and on the Pi without modification.
This commit is contained in:
Dan-in-CA 2013-10-02 15:08:52 -07:00
parent 9cab706e10
commit 2ca0a8837c
1 changed files with 24 additions and 16 deletions

40
ospi.py
View File

@ -1,4 +1,4 @@
###!/usr/bin/python
#!/usr/bin/python
import re, os, json, time, base64, thread # standard Python modules
import web # the Web.py module. See webpy.org (Enables the OpenSprinkler web interface)
import gv # 'global vars' An empty module, used for storing vars (as attributes), that need to be 'global' across threads and between functions and classes.
@ -61,7 +61,7 @@ def clear_mm():
for i in range(gv.sd['nst']):
gv.rs.append([0,0,0,0])
gv.srvals = [0]*(gv.sd['nst'])
## set_output()
set_output()
return
def CPU_temperature():
@ -160,7 +160,7 @@ def stop_onrain():
continue
elif not all(v == 0 for v in gv.rs[sid]):
gv.srvals[sid] = [0]
## set_output()
set_output()
gv.ps[sid] = [0,0]
#gv.sbits = [0] * (gv.sd['nbrd'] +1)
gv.rs[sid] = [0,0,0,0]
@ -168,7 +168,7 @@ def stop_onrain():
def stop_stations():
gv.srvals = [0]*(gv.sd['nst'])
## set_output()
set_output()
gv.ps = []
for i in range(gv.sd['nst']):
gv.ps.append([0,0])
@ -222,7 +222,7 @@ def main_loop(): # Runs in a seperate thread
if gv.srvals[sid]: # if this station is on
if gv.now >= gv.rs[sid][1]: # check if time is up
gv.srvals[sid] = 0
## set_output()
set_output()
gv.sbits[b] = gv.sbits[b]&~2**s
if gv.sd['mas']-1 != sid: # if not master, fill out log
gv.ps[sid] = [0,0]
@ -237,7 +237,7 @@ def main_loop(): # Runs in a seperate thread
if gv.now >= gv.rs[sid][0] and gv.now < gv.rs[sid][1]:
if gv.sd['mas']-1 != sid: # if not master
gv.srvals[sid] = 1 # station is turned on
## set_output()
set_output()
gv.sbits[b] = gv.sbits[b]|1<<s # Set display to on
gv.ps[sid][0] = gv.rs[sid][3]
gv.ps[sid][1] = gv.rs[sid][2]+1 ### testing display
@ -249,7 +249,7 @@ def main_loop(): # Runs in a seperate thread
elif gv.sd['mas'] == sid+1:
gv.sbits[b] = gv.sbits[b]|1<<sid #(gv.sd['mas'] - 1)
gv.srvals[masid] = 1
## set_output()
set_output()
for s in range(gv.sd['nst']):
if gv.rs[s][1]: # if any station is scheduled
@ -270,7 +270,7 @@ def main_loop(): # Runs in a seperate thread
if not program_running:
gv.srvals = [0]*(gv.sd['nst'])
## set_output()
set_output()
gv.sbits = [0] * (gv.sd['nbrd'] +1)
gv.ps = []
for i in range(gv.sd['nst']):
@ -454,12 +454,17 @@ pin_sr_noe = 11
pin_sr_lat = 15
def enableShiftRegisterOutput():
## try:
GPIO.output(pin_sr_noe, GPIO.LOW)
try:
GPIO.output(pin_sr_noe, GPIO.LOW)
except NameError:
pass
def disableShiftRegisterOutput():
GPIO.output(pin_sr_noe, GPIO.HIGH)
try:
GPIO.output(pin_sr_noe, GPIO.HIGH)
except NameError:
pass
try:
GPIO.cleanup()
#### setup GPIO pins to interface with shift register ####
@ -469,8 +474,11 @@ try:
disableShiftRegisterOutput()
GPIO.setup(pin_sr_dat, GPIO.OUT)
GPIO.setup(pin_sr_lat, GPIO.OUT)
except NameError:
pass
def setShiftRegister(srvals):
def setShiftRegister(srvals):
try:
GPIO.output(pin_sr_clk, GPIO.LOW)
GPIO.output(pin_sr_lat, GPIO.LOW)
for s in range(gv.sd['nst']):
@ -478,8 +486,8 @@ try:
GPIO.output(pin_sr_dat, srvals[gv.sd['nst']-1-s])
GPIO.output(pin_sr_clk, GPIO.HIGH)
GPIO.output(pin_sr_lat, GPIO.HIGH)
except NameError:
pass
except NameError:
pass
##################
@ -529,7 +537,7 @@ class change_values:
qdict['en'] = '1' #default
elif qdict.has_key('en') and qdict['en'] == '0':
gv.srvals = [0]*(gv.sd['nst']) # turn off all stations
## set_output()
set_output()
if qdict.has_key('mm') and qdict['mm'] == '0': clear_mm() #self.clear_mm()
if qdict.has_key('rd') and qdict['rd'] != '0':
gv.sd['rdst'] = (gv.now+(int(qdict['rd'])*3600))
@ -538,7 +546,7 @@ class change_values:
if qdict.has_key('rbt') and qdict['rbt'] == '1':
jsave(gv.sd, 'sd')
gv.srvals = [0]*(gv.sd['nst'])
## set_output()
set_output()
os.system('reboot')
raise web.seeother('/')
for key in qdict.keys():