Revision e376a628
Added by dsorber about 13 years ago
| 525.743/code/bfcslib/LED_cycle.py | ||
|---|---|---|
|
import itertools
|
||
|
import sys
|
||
|
|
||
|
import RPIO
|
||
|
|
||
|
from blinky_pattern import cylon, randomize, turn_on_top_down, turn_off_top_down,\
|
||
|
turn_on_bottom_up, turn_off_bottom_up
|
||
|
|
||
|
outputs = [23, 24, 25, 8, 7]
|
||
|
|
||
|
def main():
|
||
|
# Use the BCM addressing scheme
|
||
|
RPIO.setmode(RPIO.BCM)
|
||
|
|
||
|
# Setup input
|
||
|
RPIO.setup(22, RPIO.IN)
|
||
|
|
||
|
# Setup all five outputs
|
||
|
for gpio in outputs:
|
||
|
RPIO.setup(gpio, RPIO.OUT)
|
||
|
RPIO.output(gpio, False)
|
||
|
|
||
|
# Setup switch interrupt with debounce
|
||
|
RPIO.add_interrupt_callback(22, switch_callback, edge='rising',
|
||
|
pull_up_down=RPIO.PUD_DOWN, threaded_callback=True,
|
||
|
debounce_timeout_ms=150)
|
||
|
|
||
|
print 'Okay, waiting for interrupts...'
|
||
|
|
||
|
# Main (blocking) loop
|
||
|
while True:
|
||
|
RPIO.wait_for_interrupts()
|
||
|
|
||
|
def switch_callback(gpio_id, val):
|
||
|
global CURRENT
|
||
|
print 'Switch activated! %s --- %s' % (gpio_id, val)
|
||
|
|
||
|
# Turn off current LED
|
||
|
RPIO.output(CURRENT, False)
|
||
|
|
||
|
# Get next output and turn it on
|
||
|
CURRENT = out_iterator.next()
|
||
|
print CURRENT
|
||
|
RPIO.output(CURRENT, True)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
| 525.743/code/bfcslib/blinky_pattern.py | ||
|---|---|---|
|
import random
|
||
|
import sys
|
||
|
import time
|
||
|
|
||
|
import RPIO
|
||
|
|
||
|
outputs = [23, 24, 25, 8, 7]
|
||
|
|
||
|
def main():
|
||
|
# Use the BCM addressing scheme
|
||
|
RPIO.setmode(RPIO.BCM)
|
||
|
|
||
|
# Setup all five outputs
|
||
|
for RPIO in outputs:
|
||
|
RPIO.setup(RPIO, RPIO.OUT)
|
||
|
RPIO.output(RPIO, False)
|
||
|
|
||
|
while True:
|
||
|
cylon()
|
||
|
randomize()
|
||
|
|
||
|
turn_on_top_down()
|
||
|
turn_off_top_down()
|
||
|
turn_on_bottom_up()
|
||
|
turn_off_bottom_up()
|
||
|
|
||
|
|
||
|
def turn_on_top_down():
|
||
|
for RPIO in outputs:
|
||
|
RPIO.output(RPIO, True)
|
||
|
time.sleep(.25)
|
||
|
|
||
|
def turn_on_bottom_up():
|
||
|
for RPIO in reversed(outputs):
|
||
|
RPIO.output(RPIO, True)
|
||
|
time.sleep(.25)
|
||
|
|
||
|
def turn_off_top_down():
|
||
|
for RPIO in outputs:
|
||
|
RPIO.output(RPIO, False)
|
||
|
time.sleep(.25)
|
||
|
|
||
|
def turn_off_bottom_up():
|
||
|
for RPIO in reversed(outputs):
|
||
|
RPIO.output(RPIO, False)
|
||
|
time.sleep(.25)
|
||
|
|
||
|
def cylon():
|
||
|
for RPIO in outputs[:-1]:
|
||
|
RPIO.output(RPIO, True)
|
||
|
time.sleep(.08)
|
||
|
RPIO.output(RPIO, False)
|
||
|
|
||
|
for RPIO in outputs[::-1][:-1]:
|
||
|
RPIO.output(RPIO, True)
|
||
|
time.sleep(.08)
|
||
|
RPIO.output(RPIO, False)
|
||
|
|
||
|
def randomize():
|
||
|
rand_val = random.randint(0, 31)
|
||
|
# LED1 - RPIO23
|
||
|
if rand_val & 0b00001:
|
||
|
RPIO.output(23, True)
|
||
|
else:
|
||
|
RPIO.output(23, False)
|
||
|
# LED2 - RPIO24
|
||
|
if rand_val & 0b00010:
|
||
|
RPIO.output(24, True)
|
||
|
else:
|
||
|
RPIO.output(24, False)
|
||
|
# LED3 - RPIO25
|
||
|
if rand_val & 0b00100:
|
||
|
RPIO.output(25, True)
|
||
|
else:
|
||
|
RPIO.output(25, False)
|
||
|
# LED4 - RPIO8
|
||
|
if rand_val & 0b01000:
|
||
|
RPIO.output(8, True)
|
||
|
else:
|
||
|
RPIO.output(8, False)
|
||
|
# LED5 - RPIO7
|
||
|
if rand_val & 0b10000:
|
||
|
RPIO.output(7, True)
|
||
|
else:
|
||
|
RPIO.output(7, False)
|
||
|
time.sleep(0.1)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
| 525.743/code/bfcslib/button_LED.py | ||
|---|---|---|
|
import itertools
|
||
|
import sys
|
||
|
|
||
|
import RPIO
|
||
|
|
||
|
outputs = [23, 24, 25, 8, 7]
|
||
|
out_iterator = itertools.cycle(outputs)
|
||
|
CURRENT = outputs[0]
|
||
|
|
||
|
def main():
|
||
|
# Use the BCM addressing scheme
|
||
|
RPIO.setmode(RPIO.BCM)
|
||
|
|
||
|
# Setup input
|
||
|
RPIO.setup(22, RPIO.IN)
|
||
|
|
||
|
# Setup all five outputs
|
||
|
for gpio in outputs:
|
||
|
RPIO.setup(gpio, RPIO.OUT)
|
||
|
RPIO.output(gpio, False)
|
||
|
|
||
|
# Setup switch interrupt with debounce
|
||
|
RPIO.add_interrupt_callback(22, switch_callback, edge='rising',
|
||
|
pull_up_down=RPIO.PUD_DOWN, threaded_callback=True,
|
||
|
debounce_timeout_ms=150)
|
||
|
|
||
|
print 'Okay, waiting for interrupts...'
|
||
|
|
||
|
# Main (blocking) loop
|
||
|
while True:
|
||
|
RPIO.wait_for_interrupts()
|
||
|
|
||
|
def switch_callback(gpio_id, val):
|
||
|
global CURRENT
|
||
|
print 'Switch activated! %s --- %s' % (gpio_id, val)
|
||
|
|
||
|
# Turn off current LED
|
||
|
RPIO.output(CURRENT, False)
|
||
|
|
||
|
# Get next output and turn it on
|
||
|
CURRENT = out_iterator.next()
|
||
|
print CURRENT
|
||
|
RPIO.output(CURRENT, True)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
| 525.743/code/bfcslib/pushbutton.py | ||
|---|---|---|
|
import sys
|
||
|
|
||
|
import RPIO
|
||
|
|
||
|
def main():
|
||
|
# Use the BCM addressing scheme
|
||
|
RPIO.setmode(RPIO.BCM)
|
||
|
|
||
|
# Setup pushbutton input
|
||
|
RPIO.setup(22, RPIO.IN)
|
||
|
|
||
|
RPIO.add_interrupt_callback(22, switch_callback, edge='both',
|
||
|
pull_up_down=RPIO.PUD_DOWN, threaded_callback=False,
|
||
|
debounce_timeout_ms=300)
|
||
|
|
||
|
print 'Okay, waiting for interrupts...'
|
||
|
while True:
|
||
|
RPIO.wait_for_interrupts()
|
||
|
|
||
|
def switch_callback(gpio_id, val):
|
||
|
print 'Switch activated! %s --- %s' % (gpio_id, val)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
| 525.743/code/bfcslib/relay.py | ||
|---|---|---|
|
import sys
|
||
|
|
||
|
import RPIO
|
||
|
|
||
|
from status_led import LED
|
||
|
|
||
|
class Relay(object):
|
||
|
|
||
|
def __init__(self):
|
||
|
# Use the BCM addressing scheme
|
||
|
RPIO.setmode(RPIO.BCM)
|
||
|
|
||
|
# Setup output for relay
|
||
|
RPIO.setup(17, RPIO.OUT)
|
||
|
RPIO.output(17, False)
|
||
|
|
||
|
# Instantiate the relay status LED (23)
|
||
|
self.led = LED(23)
|
||
|
|
||
|
def on(self):
|
||
|
self.led.on()
|
||
|
RPIO.output(17, True)
|
||
|
|
||
|
def off(self):
|
||
|
self.led.off()
|
||
|
RPIO.output(17, False)
|
||
|
|
||
|
def main():
|
||
|
|
||
|
import time
|
||
|
|
||
|
relay = Relay()
|
||
|
print 'Quick relay unit test'
|
||
|
print 'Turning relay on...'
|
||
|
relay.on()
|
||
|
time.sleep(5)
|
||
|
print 'Okay that\'s enough of that'
|
||
|
relay.off()
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
|
|
||
| 525.743/code/bfcslib/saa1064_i2c_test.py | ||
|---|---|---|
|
import sys
|
||
|
import time
|
||
|
|
||
|
import smbus
|
||
|
|
||
|
bus = smbus.SMBus(1)
|
||
|
address = 0x38
|
||
|
|
||
|
def main():
|
||
|
status = bus.read_byte(address)
|
||
|
print 'POR Status: %d' % status
|
||
|
bus.write_byte_data(address, 0x00, 0x35)
|
||
|
|
||
|
for shift in xrange(0, 8):
|
||
|
val = (1 << shift)
|
||
|
print val
|
||
|
bus.write_byte_data(address, 0x01, val)
|
||
|
time.sleep(0.1)
|
||
|
|
||
|
for shift in xrange(0, 8):
|
||
|
val = (1 << shift)
|
||
|
print val
|
||
|
bus.write_byte_data(address, 0x03, val)
|
||
|
time.sleep(0.1)
|
||
|
|
||
|
# print 'Starting write...'
|
||
|
# bus.write_byte(address, 0x0)
|
||
|
# bus.write_byte(address, 0x3E)
|
||
|
# print 'Segment test at 9ma'
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
| 525.743/code/bfcslib/seven_seg.py | ||
|---|---|---|
|
import sys
|
||
|
import time
|
||
|
|
||
|
import smbus
|
||
|
|
||
|
SEGMENTS_OFF = 0x00
|
||
|
SEGMENT_G = 0x01
|
||
|
SEGMENT_F = 0x02
|
||
|
SEGMENT_E = 0x04
|
||
|
SEGMENT_D = 0x08
|
||
|
SEGMENT_C = 0x10
|
||
|
SEGMENT_B = 0x20
|
||
|
SEGMENT_A = 0x40
|
||
|
SEGMENT_DOT = 0x80
|
||
|
|
||
|
DIGIT_0 = SEGMENT_A | SEGMENT_B | SEGMENT_C | SEGMENT_D | SEGMENT_E | SEGMENT_F
|
||
|
DIGIT_1 = SEGMENT_B | SEGMENT_C
|
||
|
DIGIT_2 = SEGMENT_A | SEGMENT_B | SEGMENT_G | SEGMENT_E | SEGMENT_D
|
||
|
DIGIT_3 = SEGMENT_A | SEGMENT_B | SEGMENT_G | SEGMENT_C | SEGMENT_D
|
||
|
DIGIT_4 = SEGMENT_F | SEGMENT_G | SEGMENT_B | SEGMENT_C
|
||
|
DIGIT_5 = SEGMENT_A | SEGMENT_F | SEGMENT_G | SEGMENT_C | SEGMENT_D
|
||
|
DIGIT_6 = SEGMENT_A | SEGMENT_F | SEGMENT_G | SEGMENT_E | SEGMENT_D | SEGMENT_C
|
||
|
DIGIT_7 = SEGMENT_A | SEGMENT_B | SEGMENT_C
|
||
|
DIGIT_8 = SEGMENT_A | SEGMENT_B | SEGMENT_C | SEGMENT_D | SEGMENT_E | SEGMENT_F | SEGMENT_G
|
||
|
DIGIT_9 = SEGMENT_A | SEGMENT_B | SEGMENT_C | SEGMENT_D | SEGMENT_F | SEGMENT_G
|
||
|
|
||
|
class SevenSegmentDisplay(object):
|
||
|
|
||
|
digit_map = {'0': DIGIT_0,
|
||
|
'1': DIGIT_1,
|
||
|
'2': DIGIT_2,
|
||
|
'3': DIGIT_3,
|
||
|
'4': DIGIT_4,
|
||
|
'5': DIGIT_5,
|
||
|
'6': DIGIT_6,
|
||
|
'7': DIGIT_7,
|
||
|
'8': DIGIT_8,
|
||
|
'9': DIGIT_9}
|
||
|
|
||
|
def __init__(self):
|
||
|
self.bus = smbus.SMBus(1)
|
||
|
self.address = 0x38
|
||
|
# 2 digits at 9ma sink
|
||
|
self.bus.write_byte_data(self.address, 0x00, 0x35)
|
||
|
|
||
|
def segment_test(self):
|
||
|
print 'Testing segments...'
|
||
|
self.bus.write_byte_data(self.address, 0x00, 0x3E)
|
||
|
time.sleep(3)
|
||
|
self.bus.write_byte_data(self.address, 0x00, 0x35)
|
||
|
print 'Test complete'
|
||
|
|
||
|
def read_status(self):
|
||
|
return bus.read_byte(address)
|
||
|
|
||
|
def write_digit0(self, pattern):
|
||
|
self.bus.write_byte_data(self.address, 0x03, pattern)
|
||
|
|
||
|
def write_digit1(self, pattern):
|
||
|
self.bus.write_byte_data(self.address, 0x01, pattern)
|
||
|
|
||
|
def clear(self):
|
||
|
self.write_digit0(SEGMENTS_OFF)
|
||
|
self.write_digit1(SEGMENTS_OFF)
|
||
|
|
||
|
def display_number(self, number):
|
||
|
str_num = '%02d' % number
|
||
|
self.write_digit1(self.digit_map[str_num[1]])
|
||
|
self.write_digit0(self.digit_map[str_num[0]])
|
||
|
|
||
|
|
||
|
|
||
|
def main():
|
||
|
|
||
|
display = SevenSegmentDisplay()
|
||
|
|
||
|
# segment test
|
||
|
display.segment_test()
|
||
|
|
||
|
# blink segment G x20
|
||
|
for ctr in xrange(20):
|
||
|
display.clear()
|
||
|
time.sleep(0.1)
|
||
|
SEGMENT_G
|
||
|
display.write_digit1(SEGMENT_G)
|
||
|
display.write_digit0(SEGMENT_G)
|
||
|
time.sleep(0.1)
|
||
|
|
||
|
# outer snake clockwise, both
|
||
|
segs = [SEGMENT_F, SEGMENT_A, SEGMENT_B, SEGMENT_C,
|
||
|
SEGMENT_D, SEGMENT_E]
|
||
|
|
||
|
for xtr in xrange(2):
|
||
|
for segment in segs:
|
||
|
display.clear()
|
||
|
display.write_digit1(segment)
|
||
|
display.write_digit0(segment)
|
||
|
time.sleep(0.2)
|
||
|
|
||
|
# outer snake counter clockwise, both
|
||
|
segs = [SEGMENT_E, SEGMENT_D, SEGMENT_C, SEGMENT_B,
|
||
|
SEGMENT_A, SEGMENT_F]
|
||
|
|
||
|
for xtr in xrange(2):
|
||
|
for segment in segs:
|
||
|
display.clear()
|
||
|
display.write_digit1(segment)
|
||
|
display.write_digit0(segment)
|
||
|
time.sleep(0.2)
|
||
|
|
||
|
# figure 8 both (clockwise start)
|
||
|
segs = [SEGMENT_G, SEGMENT_F, SEGMENT_A, SEGMENT_B, SEGMENT_G,
|
||
|
SEGMENT_E, SEGMENT_D, SEGMENT_C]
|
||
|
|
||
|
for xtr in xrange(3):
|
||
|
for segment in segs:
|
||
|
display.clear()
|
||
|
display.write_digit1(segment)
|
||
|
display.write_digit0(segment)
|
||
|
time.sleep(0.1)
|
||
|
|
||
|
# figure 8 both (counter clockwise start)
|
||
|
segs = [SEGMENT_G, SEGMENT_E, SEGMENT_D, SEGMENT_C, SEGMENT_G,
|
||
|
SEGMENT_F, SEGMENT_A, SEGMENT_B]
|
||
|
|
||
|
for xtr in xrange(3):
|
||
|
for segment in segs:
|
||
|
display.clear()
|
||
|
display.write_digit1(segment)
|
||
|
display.write_digit0(segment)
|
||
|
time.sleep(0.1)
|
||
|
|
||
|
# count down
|
||
|
while True:
|
||
|
for num in xrange(99, -1, -1):
|
||
|
display.clear()
|
||
|
display.display_number(num)
|
||
|
time.sleep(0.3)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
| 525.743/code/bfcslib/status_led.py | ||
|---|---|---|
|
import sys
|
||
|
|
||
|
import RPIO
|
||
|
|
||
|
class LED(object):
|
||
|
|
||
|
def __init__(self, gpio_num):
|
||
|
self.gpio_num = gpio_num
|
||
|
self.state = False
|
||
|
|
||
|
# Use the BCM addressing scheme
|
||
|
RPIO.setmode(RPIO.BCM)
|
||
|
|
||
|
# Configure GPIO as output and turn off
|
||
|
RPIO.setup(self.gpio_num, RPIO.OUT)
|
||
|
RPIO.output(self.gpio_num, False)
|
||
|
|
||
|
def on(self):
|
||
|
self.state = True
|
||
|
RPIO.output(self.gpio_num, True)
|
||
|
|
||
|
def off(self):
|
||
|
self.state = False
|
||
|
RPIO.output(self.gpio_num, False)
|
||
|
|
||
|
def toggle(self):
|
||
|
self.state = not self.state
|
||
|
RPIO.output(self.gpio_num, self.state)
|
||
|
|
||
|
def main():
|
||
|
|
||
|
import time
|
||
|
|
||
|
led1 = LED(23)
|
||
|
led2 = LED(7)
|
||
|
print 'Quick status LED unit test'
|
||
|
print 'Turning on LED1'
|
||
|
led1.on()
|
||
|
time.sleep(1)
|
||
|
print 'Turning on LED2'
|
||
|
led2.on()
|
||
|
time.sleep(1)
|
||
|
print 'Turning off LED1'
|
||
|
led1.off()
|
||
|
for ctr in xrange(0, 10):
|
||
|
time.sleep(1)
|
||
|
led1.toggle()
|
||
|
led2.toggle()
|
||
|
print 'Toggle iteration %d' % (ctr + 1)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
|
|
||
| 525.743/code/bfcslib/tmp512.py | ||
|---|---|---|
|
import sys
|
||
|
import time
|
||
|
|
||
|
import smbus
|
||
|
|
||
|
class TMP512(object):
|
||
|
|
||
|
def __init__(self, address):
|
||
|
self.bus = smbus.SMBus(1)
|
||
|
self.address = address
|
||
|
|
||
|
# Shunt Measurement Configuration
|
||
|
# - turn off shunt/bus voltage measurement
|
||
|
self.write_reg(0x00, 0x3998)
|
||
|
|
||
|
# Temperature Measurement Configuration
|
||
|
# - continous conversion at 1 conversion/second
|
||
|
# - enable remote 1, remote 2 and local
|
||
|
self.write_reg(0x01, 0xBE00)
|
||
|
|
||
|
def _reverse16(self, value):
|
||
|
""" A quick hack method for reversing byte order for a 16 bit value """
|
||
|
str_val = '%04X' % value
|
||
|
new_val = str_val[2:4] + str_val[0:2]
|
||
|
return int(new_val, 16)
|
||
|
|
||
|
def read_reg(self, reg):
|
||
|
raw = self.bus.read_word_data(self.address, reg)
|
||
|
return self._reverse16(raw)
|
||
|
|
||
|
def write_reg(self, reg, data):
|
||
|
raw = self._reverse16(data)
|
||
|
self.bus.write_word_data(self.address, reg, raw)
|
||
|
|
||
|
def reset(self):
|
||
|
self.write_reg(0x00, 0xB998)
|
||
|
time.sleep(1)
|
||
|
|
||
|
def status(self):
|
||
|
return self.read_reg(0x02)
|
||
|
|
||
|
def device_id(self):
|
||
|
return '0x%04X' % self.read_reg(0x1F)
|
||
|
|
||
|
def _convert_temp(self, raw_val):
|
||
|
# Raw temp is a 13 bit number, the units are 0.0625 C
|
||
|
temp = (raw_val >> 3) * 0.0625
|
||
|
# Convert C to F
|
||
|
return (temp * 1.8) + 32
|
||
|
|
||
|
def local_temp(self):
|
||
|
return self._convert_temp(self.read_reg(0x08))
|
||
|
|
||
|
def temp_sensor1(self):
|
||
|
return self._convert_temp(self.read_reg(0x09))
|
||
|
|
||
|
def temp_sensor2(self):
|
||
|
return self._convert_temp(self.read_reg(0x0A))
|
||
|
|
||
|
|
||
|
def main():
|
||
|
import time
|
||
|
|
||
|
tmp512_1 = TMP512(0x5d)
|
||
|
tmp512_2 = TMP512(0x5c)
|
||
|
|
||
|
print 'TMP512-1 status: 0x%04X' % tmp512_1.status()
|
||
|
print 'TMP512-2 status: 0x%04X' % tmp512_2.status()
|
||
|
|
||
|
print 'TMP512-1 local temp is %d' % tmp512_1.local_temp()
|
||
|
print 'TMP512-2 local temp is %d' % tmp512_2.local_temp()
|
||
|
|
||
|
print 'TMP512-1 remote 1 temp is %d' % tmp512_1.temp_sensor1()
|
||
|
print 'TMP512-1 remote 2 temp is %d' % tmp512_1.temp_sensor2()
|
||
|
print 'TMP512-2 remote 1 temp is %d' % tmp512_2.temp_sensor1()
|
||
|
print 'TMP512-2 remote 2 temp is %d' % tmp512_2.temp_sensor2()
|
||
|
|
||
|
print '---'
|
||
|
time.sleep(1)
|
||
|
|
||
|
print 'TMP512-1 remote 1 temp is %d' % tmp512_1.temp_sensor1()
|
||
|
print 'TMP512-1 remote 2 temp is %d' % tmp512_1.temp_sensor2()
|
||
|
print 'TMP512-2 remote 1 temp is %d' % tmp512_2.temp_sensor1()
|
||
|
print 'TMP512-2 remote 2 temp is %d' % tmp512_2.temp_sensor2()
|
||
|
|
||
|
print '---'
|
||
|
time.sleep(1)
|
||
|
|
||
|
print 'TMP512-1 remote 1 temp is %d' % tmp512_1.temp_sensor1()
|
||
|
print 'TMP512-1 remote 2 temp is %d' % tmp512_1.temp_sensor2()
|
||
|
print 'TMP512-2 remote 1 temp is %d' % tmp512_2.temp_sensor1()
|
||
|
print 'TMP512-2 remote 2 temp is %d' % tmp512_2.temp_sensor2()
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
| 525.743/code/rpi_client.py | ||
|---|---|---|
|
import itertools
|
||
|
import sys
|
||
|
|
||
|
import RPIO
|
||
|
|
||
|
from bfcslib.seven_seg import SevenSegmentDisplay
|
||
|
from bfcslib.status_led import LED
|
||
|
from bfcslib.tmp512 import TMP512
|
||
|
|
||
|
|
||
|
NUM_SENSORS = 4
|
||
|
sensor_iterator = itertools.cycle(range(0, NUM_SENSORS))
|
||
|
CURRENT_IDX = sensor_iterator.next()
|
||
|
SENSORS = None
|
||
|
|
||
|
def button_isr(gpio_id, val):
|
||
|
global CURRENT_IDX
|
||
|
global SENSORS
|
||
|
|
||
|
# Turn off previous 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()
|
||
|
|
||
|
def main():
|
||
|
global CURRENT_IDX
|
||
|
global SENSORS
|
||
|
|
||
|
print 'Beer Fermentation Control System - RPI Client'
|
||
|
|
||
|
# Setup the sensor status LEDs
|
||
|
sensor1_led = LED(24)
|
||
|
sensor2_led = LED(25)
|
||
|
sensor3_led = LED(8)
|
||
|
sensor4_led = LED(7)
|
||
|
|
||
|
# Setup both TMP512's
|
||
|
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}]
|
||
|
|
||
|
# Turn on initial sensor LED
|
||
|
SENSORS[CURRENT_IDX]['led'].on()
|
||
|
|
||
|
# Setup 7 seg display
|
||
|
ssdisplay = SevenSegmentDisplay()
|
||
|
|
||
|
# Setup pushbutton
|
||
|
# Use the BCM addressing scheme
|
||
|
RPIO.setmode(RPIO.BCM)
|
||
|
|
||
|
# Setup input
|
||
|
RPIO.setup(22, RPIO.IN)
|
||
|
|
||
|
# Setup switch interrupt with debounce
|
||
|
RPIO.add_interrupt_callback(22, button_isr, edge='rising',
|
||
|
pull_up_down=RPIO.PUD_DOWN, threaded_callback=True,
|
||
|
debounce_timeout_ms=150)
|
||
|
|
||
|
# Wait for interrupts in a non-blocking fashion
|
||
|
RPIO.wait_for_interrupts(threaded=True)
|
||
|
|
||
|
# Main loop
|
||
|
while True:
|
||
|
|
||
|
# Iterate over all temperature sensors and read their temp
|
||
|
for idx in xrange(NUM_SENSORS):
|
||
|
temp = SENSORS[idx]['temp']()
|
||
|
if idx == CURRENT_IDX:
|
||
|
ssdisplay.display_number(temp)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
| 525.743/code/test scripts/7seg.py | ||
|---|---|---|
|
import sys
|
||
|
import time
|
||
|
|
||
|
import smbus
|
||
|
|
||
|
SEGMENTS_OFF = 0x00
|
||
|
SEGMENT_G = 0x01
|
||
|
SEGMENT_F = 0x02
|
||
|
SEGMENT_E = 0x04
|
||
|
SEGMENT_D = 0x08
|
||
|
SEGMENT_C = 0x10
|
||
|
SEGMENT_B = 0x20
|
||
|
SEGMENT_A = 0x40
|
||
|
SEGMENT_DOT = 0x80
|
||
|
|
||
|
DIGIT_0 = SEGMENT_A | SEGMENT_B | SEGMENT_C | SEGMENT_D | SEGMENT_E | SEGMENT_F
|
||
|
DIGIT_1 = SEGMENT_B | SEGMENT_C
|
||
|
DIGIT_2 = SEGMENT_A | SEGMENT_B | SEGMENT_G | SEGMENT_E | SEGMENT_D
|
||
|
DIGIT_3 = SEGMENT_A | SEGMENT_B | SEGMENT_G | SEGMENT_C | SEGMENT_D
|
||
|
DIGIT_4 = SEGMENT_F | SEGMENT_G | SEGMENT_B | SEGMENT_C
|
||
|
DIGIT_5 = SEGMENT_A | SEGMENT_F | SEGMENT_G | SEGMENT_C | SEGMENT_D
|
||
|
DIGIT_6 = SEGMENT_A | SEGMENT_F | SEGMENT_G | SEGMENT_E | SEGMENT_D | SEGMENT_C
|
||
|
DIGIT_7 = SEGMENT_A | SEGMENT_B | SEGMENT_C
|
||
|
DIGIT_8 = SEGMENT_A | SEGMENT_B | SEGMENT_C | SEGMENT_D | SEGMENT_E | SEGMENT_F | SEGMENT_G
|
||
|
DIGIT_9 = SEGMENT_A | SEGMENT_B | SEGMENT_C | SEGMENT_D | SEGMENT_F | SEGMENT_G
|
||
|
|
||
|
class SevenSegmentDisplay(object):
|
||
|
|
||
|
digit_map = {'0': DIGIT_0,
|
||
|
'1': DIGIT_1,
|
||
|
'2': DIGIT_2,
|
||
|
'3': DIGIT_3,
|
||
|
'4': DIGIT_4,
|
||
|
'5': DIGIT_5,
|
||
|
'6': DIGIT_6,
|
||
|
'7': DIGIT_7,
|
||
|
'8': DIGIT_8,
|
||
|
'9': DIGIT_9}
|
||
|
|
||
|
def __init__(self):
|
||
|
self.bus = smbus.SMBus(1)
|
||
|
self.address = 0x38
|
||
|
# 2 digits at 9ma sink
|
||
|
self.bus.write_byte_data(self.address, 0x00, 0x35)
|
||
|
|
||
|
def segment_test(self):
|
||
|
print 'Testing segments...'
|
||
|
self.bus.write_byte_data(self.address, 0x00, 0x3E)
|
||
|
time.sleep(3)
|
||
|
self.bus.write_byte_data(self.address, 0x00, 0x35)
|
||
|
print 'Test complete'
|
||
|
|
||
|
def read_status(self):
|
||
|
return bus.read_byte(address)
|
||
|
|
||
|
def write_digit0(self, pattern):
|
||
|
self.bus.write_byte_data(self.address, 0x03, pattern)
|
||
|
|
||
|
def write_digit1(self, pattern):
|
||
|
self.bus.write_byte_data(self.address, 0x01, pattern)
|
||
|
|
||
|
def clear(self):
|
||
|
self.write_digit0(SEGMENTS_OFF)
|
||
|
self.write_digit1(SEGMENTS_OFF)
|
||
|
|
||
|
def display_number(self, number):
|
||
|
str_num = '%02d' % number
|
||
|
self.write_digit1(self.digit_map[str_num[1]])
|
||
|
self.write_digit0(self.digit_map[str_num[0]])
|
||
|
|
||
|
|
||
|
|
||
|
def main():
|
||
|
|
||
|
display = SevenSegmentDisplay()
|
||
|
|
||
|
# segment test
|
||
|
display.segment_test()
|
||
|
|
||
|
# blink segment G x20
|
||
|
for ctr in xrange(20):
|
||
|
display.clear()
|
||
|
time.sleep(0.1)
|
||
|
SEGMENT_G
|
||
|
display.write_digit1(SEGMENT_G)
|
||
|
display.write_digit0(SEGMENT_G)
|
||
|
time.sleep(0.1)
|
||
|
|
||
|
# outer snake clockwise, both
|
||
|
segs = [SEGMENT_F, SEGMENT_A, SEGMENT_B, SEGMENT_C,
|
||
|
SEGMENT_D, SEGMENT_E]
|
||
|
|
||
|
for xtr in xrange(2):
|
||
|
for segment in segs:
|
||
|
display.clear()
|
||
|
display.write_digit1(segment)
|
||
|
display.write_digit0(segment)
|
||
|
time.sleep(0.2)
|
||
|
|
||
|
# outer snake counter clockwise, both
|
||
|
segs = [SEGMENT_E, SEGMENT_D, SEGMENT_C, SEGMENT_B,
|
||
|
SEGMENT_A, SEGMENT_F]
|
||
|
|
||
|
for xtr in xrange(2):
|
||
|
for segment in segs:
|
||
|
display.clear()
|
||
|
display.write_digit1(segment)
|
||
|
display.write_digit0(segment)
|
||
|
time.sleep(0.2)
|
||
|
|
||
|
# figure 8 both (clockwise start)
|
||
|
segs = [SEGMENT_G, SEGMENT_F, SEGMENT_A, SEGMENT_B, SEGMENT_G,
|
||
|
SEGMENT_E, SEGMENT_D, SEGMENT_C]
|
||
|
|
||
|
for xtr in xrange(3):
|
||
|
for segment in segs:
|
||
|
display.clear()
|
||
|
display.write_digit1(segment)
|
||
|
display.write_digit0(segment)
|
||
|
time.sleep(0.1)
|
||
|
|
||
|
# figure 8 both (counter clockwise start)
|
||
|
segs = [SEGMENT_G, SEGMENT_E, SEGMENT_D, SEGMENT_C, SEGMENT_G,
|
||
|
SEGMENT_F, SEGMENT_A, SEGMENT_B]
|
||
|
|
||
|
for xtr in xrange(3):
|
||
|
for segment in segs:
|
||
|
display.clear()
|
||
|
display.write_digit1(segment)
|
||
|
display.write_digit0(segment)
|
||
|
time.sleep(0.1)
|
||
|
|
||
|
# count down
|
||
|
while True:
|
||
|
for num in xrange(99, -1, -1):
|
||
|
display.clear()
|
||
|
display.display_number(num)
|
||
|
time.sleep(0.3)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
| 525.743/code/test scripts/LED_cycle.py | ||
|---|---|---|
|
import itertools
|
||
|
import sys
|
||
|
|
||
|
import RPIO
|
||
|
|
||
|
from blinky_pattern import cylon, randomize, turn_on_top_down, turn_off_top_down,\
|
||
|
turn_on_bottom_up, turn_off_bottom_up
|
||
|
|
||
|
outputs = [23, 24, 25, 8, 7]
|
||
|
|
||
|
def main():
|
||
|
# Use the BCM addressing scheme
|
||
|
RPIO.setmode(RPIO.BCM)
|
||
|
|
||
|
# Setup input
|
||
|
RPIO.setup(22, RPIO.IN)
|
||
|
|
||
|
# Setup all five outputs
|
||
|
for gpio in outputs:
|
||
|
RPIO.setup(gpio, RPIO.OUT)
|
||
|
RPIO.output(gpio, False)
|
||
|
|
||
|
# Setup switch interrupt with debounce
|
||
|
RPIO.add_interrupt_callback(22, switch_callback, edge='rising',
|
||
|
pull_up_down=RPIO.PUD_DOWN, threaded_callback=True,
|
||
|
debounce_timeout_ms=150)
|
||
|
|
||
|
print 'Okay, waiting for interrupts...'
|
||
|
|
||
|
# Main (blocking) loop
|
||
|
while True:
|
||
|
RPIO.wait_for_interrupts()
|
||
|
|
||
|
def switch_callback(gpio_id, val):
|
||
|
global CURRENT
|
||
|
print 'Switch activated! %s --- %s' % (gpio_id, val)
|
||
|
|
||
|
# Turn off current LED
|
||
|
RPIO.output(CURRENT, False)
|
||
|
|
||
|
# Get next output and turn it on
|
||
|
CURRENT = out_iterator.next()
|
||
|
print CURRENT
|
||
|
RPIO.output(CURRENT, True)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
| 525.743/code/test scripts/blinky_pattern.py | ||
|---|---|---|
|
import random
|
||
|
import sys
|
||
|
import time
|
||
|
|
||
|
import RPIO
|
||
|
|
||
|
outputs = [23, 24, 25, 8, 7]
|
||
|
|
||
|
def main():
|
||
|
# Use the BCM addressing scheme
|
||
|
RPIO.setmode(RPIO.BCM)
|
||
|
|
||
|
# Setup all five outputs
|
||
|
for RPIO in outputs:
|
||
|
RPIO.setup(RPIO, RPIO.OUT)
|
||
|
RPIO.output(RPIO, False)
|
||
|
|
||
|
while True:
|
||
|
cylon()
|
||
|
randomize()
|
||
|
|
||
|
turn_on_top_down()
|
||
|
turn_off_top_down()
|
||
|
turn_on_bottom_up()
|
||
|
turn_off_bottom_up()
|
||
|
|
||
|
|
||
|
def turn_on_top_down():
|
||
|
for RPIO in outputs:
|
||
|
RPIO.output(RPIO, True)
|
||
|
time.sleep(.25)
|
||
|
|
||
|
def turn_on_bottom_up():
|
||
|
for RPIO in reversed(outputs):
|
||
|
RPIO.output(RPIO, True)
|
||
|
time.sleep(.25)
|
||
|
|
||
|
def turn_off_top_down():
|
||
|
for RPIO in outputs:
|
||
|
RPIO.output(RPIO, False)
|
||
|
time.sleep(.25)
|
||
|
|
||
|
def turn_off_bottom_up():
|
||
|
for RPIO in reversed(outputs):
|
||
|
RPIO.output(RPIO, False)
|
||
|
time.sleep(.25)
|
||
|
|
||
|
def cylon():
|
||
|
for RPIO in outputs[:-1]:
|
||
|
RPIO.output(RPIO, True)
|
||
|
time.sleep(.08)
|
||
|
RPIO.output(RPIO, False)
|
||
|
|
||
|
for RPIO in outputs[::-1][:-1]:
|
||
|
RPIO.output(RPIO, True)
|
||
|
time.sleep(.08)
|
||
|
RPIO.output(RPIO, False)
|
||
|
|
||
|
def randomize():
|
||
|
rand_val = random.randint(0, 31)
|
||
|
# LED1 - RPIO23
|
||
|
if rand_val & 0b00001:
|
||
|
RPIO.output(23, True)
|
||
|
else:
|
||
|
RPIO.output(23, False)
|
||
|
# LED2 - RPIO24
|
||
|
if rand_val & 0b00010:
|
||
|
RPIO.output(24, True)
|
||
|
else:
|
||
|
RPIO.output(24, False)
|
||
|
# LED3 - RPIO25
|
||
|
if rand_val & 0b00100:
|
||
|
RPIO.output(25, True)
|
||
|
else:
|
||
|
RPIO.output(25, False)
|
||
|
# LED4 - RPIO8
|
||
|
if rand_val & 0b01000:
|
||
|
RPIO.output(8, True)
|
||
|
else:
|
||
|
RPIO.output(8, False)
|
||
|
# LED5 - RPIO7
|
||
|
if rand_val & 0b10000:
|
||
|
RPIO.output(7, True)
|
||
|
else:
|
||
|
RPIO.output(7, False)
|
||
|
time.sleep(0.1)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
| 525.743/code/test scripts/button_LED.py | ||
|---|---|---|
|
import itertools
|
||
|
import sys
|
||
|
|
||
|
import RPIO
|
||
|
|
||
|
outputs = [23, 24, 25, 8, 7]
|
||
|
out_iterator = itertools.cycle(outputs)
|
||
|
CURRENT = outputs[0]
|
||
|
|
||
|
def main():
|
||
|
# Use the BCM addressing scheme
|
||
|
RPIO.setmode(RPIO.BCM)
|
||
|
|
||
|
# Setup input
|
||
|
RPIO.setup(22, RPIO.IN)
|
||
|
|
||
|
# Setup all five outputs
|
||
|
for gpio in outputs:
|
||
|
RPIO.setup(gpio, RPIO.OUT)
|
||
|
RPIO.output(gpio, False)
|
||
|
|
||
|
# Setup switch interrupt with debounce
|
||
|
RPIO.add_interrupt_callback(22, switch_callback, edge='rising',
|
||
|
pull_up_down=RPIO.PUD_DOWN, threaded_callback=True,
|
||
|
debounce_timeout_ms=150)
|
||
|
|
||
|
print 'Okay, waiting for interrupts...'
|
||
|
|
||
|
# Main (blocking) loop
|
||
|
while True:
|
||
|
RPIO.wait_for_interrupts()
|
||
|
|
||
|
def switch_callback(gpio_id, val):
|
||
|
global CURRENT
|
||
|
print 'Switch activated! %s --- %s' % (gpio_id, val)
|
||
|
|
||
|
# Turn off current LED
|
||
|
RPIO.output(CURRENT, False)
|
||
|
|
||
|
# Get next output and turn it on
|
||
|
CURRENT = out_iterator.next()
|
||
|
print CURRENT
|
||
|
RPIO.output(CURRENT, True)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
| 525.743/code/test scripts/pushbutton.py | ||
|---|---|---|
|
import sys
|
||
|
|
||
|
import RPIO
|
||
|
|
||
|
def main():
|
||
|
# Use the BCM addressing scheme
|
||
|
RPIO.setmode(RPIO.BCM)
|
||
|
|
||
|
# Setup all five outputs
|
||
|
RPIO.setup(22, RPIO.IN)
|
||
|
|
||
|
RPIO.add_interrupt_callback(22, switch_callback, edge='both',
|
||
|
pull_up_down=RPIO.PUD_DOWN, threaded_callback=False,
|
||
|
debounce_timeout_ms=300)
|
||
|
|
||
|
print 'Okay, waiting for interrupts...'
|
||
|
while True:
|
||
|
RPIO.wait_for_interrupts()
|
||
|
|
||
|
def switch_callback(gpio_id, val):
|
||
|
print 'Switch activated! %s --- %s' % (gpio_id, val)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
| 525.743/code/test scripts/relay.py | ||
|---|---|---|
|
import sys
|
||
|
|
||
|
import RPIO
|
||
|
|
||
|
from status_led import LED
|
||
|
|
||
|
class Relay(object):
|
||
|
|
||
|
def __init__(self):
|
||
|
# Use the BCM addressing scheme
|
||
|
RPIO.setmode(RPIO.BCM)
|
||
|
|
||
|
# Setup output for relay
|
||
|
RPIO.setup(17, RPIO.OUT)
|
||
|
RPIO.output(17, False)
|
||
|
|
||
|
# Instantiate the relay status LED (23)
|
||
|
self.led = LED(23)
|
||
|
|
||
|
def on(self):
|
||
|
self.led.on()
|
||
|
RPIO.output(17, True)
|
||
|
|
||
|
def off(self):
|
||
|
self.led.off()
|
||
|
RPIO.output(17, False)
|
||
|
|
||
|
def main():
|
||
|
|
||
|
import time
|
||
|
|
||
|
relay = Relay()
|
||
|
print 'Quick relay unit test'
|
||
|
print 'Turning relay on...'
|
||
|
relay.on()
|
||
|
time.sleep(5)
|
||
|
print 'Okay that\'s enough of that'
|
||
|
relay.off()
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
|
|
||
| 525.743/code/test scripts/saa1064_i2c_test.py | ||
|---|---|---|
|
import sys
|
||
|
import time
|
||
|
|
||
|
import smbus
|
||
|
|
||
|
bus = smbus.SMBus(1)
|
||
|
address = 0x38
|
||
|
|
||
|
def main():
|
||
|
status = bus.read_byte(address)
|
||
|
print 'POR Status: %d' % status
|
||
|
bus.write_byte_data(address, 0x00, 0x35)
|
||
|
|
||
|
for shift in xrange(0, 8):
|
||
|
val = (1 << shift)
|
||
|
print val
|
||
|
bus.write_byte_data(address, 0x01, val)
|
||
|
time.sleep(0.1)
|
||
|
|
||
|
for shift in xrange(0, 8):
|
||
|
val = (1 << shift)
|
||
|
print val
|
||
|
bus.write_byte_data(address, 0x03, val)
|
||
|
time.sleep(0.1)
|
||
|
|
||
|
# print 'Starting write...'
|
||
|
# bus.write_byte(address, 0x0)
|
||
|
# bus.write_byte(address, 0x3E)
|
||
|
# print 'Segment test at 9ma'
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
| 525.743/code/test scripts/status_led.py | ||
|---|---|---|
|
import sys
|
||
|
|
||
|
import RPIO
|
||
|
|
||
|
class LED(object):
|
||
|
|
||
|
def __init__(self, gpio_num):
|
||
|
self.gpio_num = gpio_num
|
||
|
self.state = False
|
||
|
|
||
|
# Use the BCM addressing scheme
|
||
|
RPIO.setmode(RPIO.BCM)
|
||
|
|
||
|
# Configure GPIO as output and turn off
|
||
|
RPIO.setup(self.gpio_num, RPIO.OUT)
|
||
|
RPIO.output(self.gpio_num, False)
|
||
|
|
||
|
def on(self):
|
||
|
self.state = True
|
||
|
RPIO.output(self.gpio_num, True)
|
||
|
|
||
|
def off(self):
|
||
|
self.state = False
|
||
|
RPIO.output(self.gpio_num, False)
|
||
|
|
||
|
def toggle(self):
|
||
|
self.state = not self.state
|
||
|
RPIO.output(self.gpio_num, self.state)
|
||
|
|
||
|
def main():
|
||
|
|
||
|
import time
|
||
|
|
||
|
led1 = LED(23)
|
||
|
led2 = LED(7)
|
||
|
print 'Quick status LED unit test'
|
||
|
print 'Turning on LED1'
|
||
|
led1.on()
|
||
|
time.sleep(1)
|
||
|
print 'Turning on LED2'
|
||
|
led2.on()
|
||
|
time.sleep(1)
|
||
|
print 'Turning off LED1'
|
||
|
led1.off()
|
||
|
for ctr in xrange(0, 10):
|
||
|
time.sleep(1)
|
||
|
led1.toggle()
|
||
|
led2.toggle()
|
||
|
print 'Toggle iteration %d' % (ctr + 1)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
|
|
||
| 525.743/code/test scripts/tmp512.py | ||
|---|---|---|
|
import sys
|
||
|
import time
|
||
|
|
||
|
import smbus
|
||
|
|
||
|
class TMP512(object):
|
||
|
|
||
|
def __init__(self, address):
|
||
|
self.bus = smbus.SMBus(1)
|
||
|
self.address = address
|
||
|
|
||
|
# Shunt Measurement Configuration
|
||
|
# - turn off shunt/bus voltage measurement
|
||
|
self.write_reg(0x00, 0x3998)
|
||
|
|
||
|
# Temperature Measurement Configuration
|
||
|
# - continous conversion at 1 conversion/second
|
||
|
# - enable remote 1, remote 2 and local
|
||
|
self.write_reg(0x01, 0xBE00)
|
||
|
|
||
|
def _reverse16(self, value):
|
||
|
""" A quick hack method for reversing byte order for a 16 bit value """
|
||
|
str_val = '%04X' % value
|
||
|
new_val = str_val[2:4] + str_val[0:2]
|
||
|
return int(new_val, 16)
|
||
|
|
||
|
def read_reg(self, reg):
|
||
|
raw = self.bus.read_word_data(self.address, reg)
|
||
|
return self._reverse16(raw)
|
||
|
|
||
|
def write_reg(self, reg, data):
|
||
|
raw = self._reverse16(data)
|
||
|
self.bus.write_word_data(self.address, reg, raw)
|
||
|
|
||
|
def reset(self):
|
||
|
self.write_reg(0x00, 0xB998)
|
||
|
time.sleep(1)
|
||
|
|
||
|
def status(self):
|
||
|
return self.read_reg(0x02)
|
||
|
|
||
|
def device_id(self):
|
||
|
return '0x%04X' % self.read_reg(0x1F)
|
||
|
|
||
|
def _convert_temp(self, raw_val):
|
||
|
# Raw temp is a 13 bit number, the units are 0.0625 C
|
||
|
temp = (raw_val >> 3) * 0.0625
|
||
|
# Convert C to F
|
||
|
return (temp * 1.8) + 32
|
||
|
|
||
|
def local_temp(self):
|
||
|
return self._convert_temp(self.read_reg(0x08))
|
||
|
|
||
|
def temp_sensor1(self):
|
||
|
return self._convert_temp(self.read_reg(0x09))
|
||
|
|
||
|
def temp_sensor2(self):
|
||
|
return self._convert_temp(self.read_reg(0x0A))
|
||
|
|
||
|
|
||
|
def main():
|
||
|
import time
|
||
|
|
||
|
temp1 = TMP512(0x5d)
|
||
|
# temp1 = TMP512(0x5d)
|
||
|
# temp1.write_reg(0x00, 0x8000)
|
||
|
# time.sleep(1)
|
||
|
# temp1.reset()
|
||
|
print 'Status: 0x%04X' % temp1.status()
|
||
|
print temp1.device_id()
|
||
|
print 'The local temp1 is %d' % temp1.local_temp()
|
||
|
|
||
|
print 'Remote 1 temp limit: 0x%04X' % temp1.read_reg(0x12)
|
||
|
print 'Remote 1 nfactor: 0x%04X' % temp1.read_reg(0x16)
|
||
|
|
||
|
print 'The remote1 temp1 is 0x%04X' % temp1.temp_sensor1()
|
||
|
print 'The remote2 temp1 is %d' % temp1.temp_sensor2()
|
||
|
time.sleep(1)
|
||
|
print 'The remote1 temp1 is %d' % temp1.temp_sensor1()
|
||
|
print 'The remote2 temp1 is %d' % temp1.temp_sensor2()
|
||
|
time.sleep(1)
|
||
|
print 'The remote1 temp1 is %d' % temp1.temp_sensor1()
|
||
|
print 'The remote2 temp1 is %d' % temp1.temp_sensor2()
|
||
|
|
||
|
# bus = smbus.SMBus(1)
|
||
|
# address = 0x5c
|
||
|
# # 2 digits at 9ma sink
|
||
|
# # bus.write_byte_data(self.address, 0x00, 0x35)
|
||
|
# print '0x%02X' % bus.read_word_data(address, 0x00)
|
||
|
# print '0x%02X' % bus.read_word_data(address, 0x1F)
|
||
|
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
I'm finally starting to put all the pieces together and build the RPI client. So far so good, although I still need to work a few details. I also reorganized my "test scripts" into a proper library so that I can call the relevant code from my client program.