commit 317c2b101d432a6b8f2278e508a48479a74fcca3
Author: dsorber <david.sorber@gmail.com>
Date:   Mon Apr 22 20:32:48 2013 -0400

    Adding work to rpi_client from this weekend.

diff --git a/525.743/code/bfcslib/sensor.py b/525.743/code/bfcslib/sensor.py
new file mode 100644
index 0000000..0d2facb
--- /dev/null
+++ b/525.743/code/bfcslib/sensor.py
@@ -0,0 +1,14 @@
+
+class Sensor(object):
+	""" Container abstraction for a 'sensor' """
+
+	def __init__(self, led, temp_sensor_function):
+		self.led = led
+		self.temp_sensor_function = temp_sensor_function
+
+	def get_temp(self):
+		return round(self.temp_sensor_function())
+
+	# When the "temp" attribute is accessed call the temp sensor function
+	# which will return the desired temperature reading
+	temp = property(get_temp)
\ No newline at end of file
diff --git a/525.743/code/rpi_client.py b/525.743/code/rpi_client.py
index fbbeb79..782c070 100644
--- a/525.743/code/rpi_client.py
+++ b/525.743/code/rpi_client.py
@@ -1,31 +1,37 @@
+import datetime
 import itertools
 import sys
+import time
 
 import RPIO
 
+from bfcslib.relay import Relay
+from bfcslib.sensor import Sensor
 from bfcslib.seven_seg import SevenSegmentDisplay
 from bfcslib.status_led import LED
 from bfcslib.tmp512 import TMP512
 
-
+# Global variables
 NUM_SENSORS = 4
 sensor_iterator = itertools.cycle(range(0, NUM_SENSORS))
 CURRENT_IDX = sensor_iterator.next()
 SENSORS = None
 
+THRESHOLD_TEMP = 82
+
 def button_isr(gpio_id, val):
 	global CURRENT_IDX
 	global SENSORS
 
-	# Turn off previous LED
-	SENSORS[CURRENT_IDX]['led'].off()
+	# Turn off previous sensor indicator LED
+	SENSORS[CURRENT_IDX].led.off()
 
 	# Update current index
 	# print 'CURRENT_IDX: %s' % CURRENT_IDX
 	CURRENT_IDX = sensor_iterator.next()
 
-	# Turn on new LED
-	SENSORS[CURRENT_IDX]['led'].on()
+	# Turn on new sensor indicator LED
+	SENSORS[CURRENT_IDX].led.on()
 
 def main():
 	global CURRENT_IDX
@@ -43,22 +49,29 @@ def main():
 	tmp512_1 = TMP512(0x5d)
 	tmp512_2 = TMP512(0x5c)
 
-	SENSORS = [{'led': sensor1_led, 'temp': tmp512_1.temp_sensor2},
-			   {'led': sensor2_led, 'temp': tmp512_1.temp_sensor1},
-			   {'led': sensor3_led, 'temp': tmp512_2.temp_sensor2},
-			   {'led': sensor4_led, 'temp': tmp512_2.temp_sensor1}]
+	# Create the sensor objects to encapsulate a status LED and temperature
+	# sensor channel
+	sensor1 = Sensor(sensor1_led, tmp512_1.temp_sensor2)
+	sensor2 = Sensor(sensor2_led, tmp512_1.temp_sensor1)
+	sensor3 = Sensor(sensor3_led, tmp512_2.temp_sensor2)
+	sensor4 = Sensor(sensor4_led, tmp512_2.temp_sensor1)
+
+	# Create the global list of all the sensor objects
+	SENSORS = [sensor1, sensor2, sensor3, sensor4]
 
 	# Turn on initial sensor LED
-	SENSORS[CURRENT_IDX]['led'].on()
+	SENSORS[CURRENT_IDX].led.on()
 
 	# Setup 7 seg display
-	ssdisplay = SevenSegmentDisplay()
+	ss_display = SevenSegmentDisplay()
+
+	# Setup relay
+	relay = Relay()
+	relay_state = False
 
 	# Setup pushbutton
-	# Use the BCM addressing scheme
+	# Use the BCM addressing scheme, set GPIO 22 as input
 	RPIO.setmode(RPIO.BCM)
-
-	# Setup input
 	RPIO.setup(22, RPIO.IN)
 
 	# Setup switch interrupt with debounce
@@ -72,11 +85,47 @@ def main():
 	# Main loop
 	while True:
 
+		temps = []
+
+		now = datetime.datetime.now()
+
 		# Iterate over all temperature sensors and read their temp
 		for idx in xrange(NUM_SENSORS):
-			temp = SENSORS[idx]['temp']()
+			
+			# Get the current temperature reading 
+			temp = SENSORS[idx].temp
+			temps.append(temp)
+
+			# Display the currently selected temp sensor value
 			if idx == CURRENT_IDX:
-				ssdisplay.display_number(temp)
+				ss_display.display_number(temp)
+
+		# Bit 'o debug information
+		print now.strftime('%m/%d/%Y %H:%M:%S')
+		for idx in xrange(NUM_SENSORS):
+			print '\t Sensor %d: %d' % (idx + 1, temps[idx])
+
+		# Sort all the temperature readings for easier manipulation
+		temps.sort(reverse=True)
+
+		if relay_state:
+			# Relay is on, decide if we need to turn it off
+			if temps[0] < THRESHOLD_TEMP and \
+			   temps[1] < THRESHOLD_TEMP and \
+			   temps[2] < THRESHOLD_TEMP and \
+			   temps[3] < THRESHOLD_TEMP:
+				relay_state = False
+				relay.off()
+		else:
+			# Relay is off, decide if we need to turn it on
+			if temps[0] > THRESHOLD_TEMP and temps[1] > THRESHOLD_TEMP:
+				relay_state = True
+				relay.on()
+
+		# Read once ever second
+		time.sleep(1)
+
+
 
 
 if __name__ == '__main__':
