From d17acdc5a9885fdcc96bbe2ee0a4c88b68d0c07e Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Thu, 27 Oct 2016 09:09:30 +0200 Subject: Update --- Raspberry Pi/i2cPixel.py | 109 ++++++++++++++++++++++++++++------------------- Raspberry Pi/python.py | 56 ++++++------------------ 2 files changed, 78 insertions(+), 87 deletions(-) (limited to 'Raspberry Pi') diff --git a/Raspberry Pi/i2cPixel.py b/Raspberry Pi/i2cPixel.py index 360b3f3..23c7bdb 100644 --- a/Raspberry Pi/i2cPixel.py +++ b/Raspberry Pi/i2cPixel.py @@ -31,32 +31,43 @@ lockdown = False def version(): print "i2cPixel is Version 1.0.0" +def lockBus(): + lockdown = True + +def unlockBus(): + lockdown = False + def available(): global lockdown - return lockdown + if lockdown == True: + return False + elif lockdown == False: + return True + else: + return False def setAddress(address): global arduinoAddress arduinoAddress = address def setBus(n): - if available == False: + if not available: return 2 else: - lockdown = False + lockBus() global bus try: bus = smbus.SMBus(n) except: errorMsg = sys.exc_info()[0] errorHandler(5, errorMsg) - lockdown = True + unlockBus() def greeting(): - if available == False: + if not available: return 2 else: - lockdown = False + lockBus() try: """ Send heartbeat """ bus.write_byte(arduinoAddress, 0x01) @@ -79,45 +90,37 @@ def greeting(): except: errorMsg = sys.exc_info()[0] errorHandler(5, errorMsg) - lockdown = True + unlockBus() def ping(): - # Send 0x07 vil faa tilbake 42 hvis live + # Send 0x02 vil faa tilbake 42 hvis live return False def setBrightness(intensity): - if available == False: + if not available: return 2 else: - lockdown = False + lockBus() try: - bus.write_block_data(arduinoAddress, 0x06, [intensity]) - except: - errorMsg = sys.exec_info()[0] - errorHandler(5, errorMsg) - lockdown = True - -def disableTimeout(): - if available == False: - return 2 - else: - lockdown = False - try: - bus.write_byte(arduinoAddress, 0x05) + if intensity > 255: + n1 = 255 + n2 = n - 255 + else: + n1 = 0 + n2 = intensity + + bus.write_block_data(arduinoAddress, 0x03, [n1, n2]) except: errorMsg = sys.exec_info()[0] errorHandler(5, errorMsg) - lockdown = True - -def blink(n, Red, Green, Blue): - bus.write_block_data(arduinoAddress, 0x04, [Red, Green, Blue, n]) - + unlockBus() + def setPixel(n, red, green, blue): """ Send values for changing pixel values """ - if available == False: + if not available: return 2 else: - lockdown = False + lockBus() try: if n > 255: n1 = 255 @@ -125,38 +128,50 @@ def setPixel(n, red, green, blue): else: n1 = 0 n2 = n - #print("{", n1, n2, "}") - bus.write_block_data(arduinoAddress, 0x02, [n1, n2, red, green, blue]) + bus.write_block_data(arduinoAddress, 0x04, [n1, n2, red, green, blue]) except: errorMsg = sys.exc_info()[0] errorHandler(5, errorMsg) - lockdown = True + unlockBus() +def blink(time, red, green, blue): + """ Flash all pixels with a colour """ + if not available: + return 2 + else: + lockBus() + try: + bus.write_block_data(arduinoAddress, 0x05, [red, green, blue, time]) + except: + errorMsg = sys.exc_info()[0] + errorHandler(5, errorMsg) + unlockBus() + + def show(): """ Send values for turning pixels on """ - if available == False: + if not available: return 2 else: - lockdown = False + lockBus() try: - bus.write_byte(arduinoAddress, 0x03) + bus.write_byte(arduinoAddress, 0x06) except: errorMsg = sys.exc_info()[0] errorHandler(5, errorMsg) - lockdown = True + unlockBus() -def blink(time, red, green, blue): - """ Flash all pixels with a colour """ - if available == False: +def disableTimeout(): + if not available: return 2 else: - lockdown = False + lockBus() try: - bus.write_block_data(arduinoAddress, 0x04, [red, green, blue, time]) + bus.write_byte(arduinoAddress, 0x07) except: - errorMsg = sys.exc_info()[0] + errorMsg = sys.exec_info()[0] errorHandler(5, errorMsg) - lockdown = True + unlockBus() def waitForSensor(): while True: @@ -172,3 +187,9 @@ def waitForSensor(): break; except Exception: pass + + +def HEX(value): + value = value.lstrip('#') + lv = len(value) + return tuple(int(value[i:i+lv/3], 16) for i in range(0, lv, lv/3)) \ No newline at end of file diff --git a/Raspberry Pi/python.py b/Raspberry Pi/python.py index 3078683..bd43944 100644 --- a/Raspberry Pi/python.py +++ b/Raspberry Pi/python.py @@ -1,32 +1,13 @@ """ Imports """ import logging -import smbus import sys import time import json -import i2cPixel +import i2cPixel as strip """ Decalrations """ pixels = 348 # Change this to the appropriate number for your setup -addresses = [0, 24, 25, 35] # legg til en start og en stopp for hvert trinn - -def hexToRgb(value): - value = value.lstrip('#') - lv = len(value) - return tuple(int(value[i:i+lv/3], 16) for i in range(0, lv, lv/3)) - -def lightStaircase(direction): - repeats = len(addresses) - repeats = repeats / 2 - - if direction == "up": - loopCondition = repeats - - - else: - loopCondition = 0 - - return true +pixelsPerStair = [21, 23, 23, 24, 25, 27, 31, 33, 28, 26, 25, 24, 24] def errorHandler(type, errorMsg): if type == 1: # Debug @@ -56,26 +37,21 @@ def setup(): """ Set time """ start_time = time.time() - + """ Setup i2c communication """ - i2cPixel.version() - i2cPixel.setBus(1) - i2cPixel.setAddress(0x04) + strip.version() + strip.setBus(1) + strip.setAddress(0x04) """ Wait for heartbeat from Arduino """ while True: try: - if i2cPixel.greeting(): + if strip.greeting(): print "Arduino is Online" time.sleep(1) break except Exception: pass - - """i = 0 - while i < pixels: - i2cPixel.setPixel(i, 0, 0, 0) - i2cPixel.show()""" def main(): @@ -84,20 +60,18 @@ def main(): timer = time.time() i = 0 while i < pixels: - i2cPixel.setPixel(i, 255, 255, 255) - i2cPixel.show() + strip.setPixel(i, 255, 255, 255) + strip.show() i = i + 1 print("--- %s seconds ---" % (time.time() - timer)) timer = time.time() i = 0 while i < pixels: - i2cPixel.setPixel(i, 255, 255, 255) + strip.setPixel(i, 255, 255, 255) i = i + 1 - i2cPixel.show() + strip.show() print("--- %s seconds ---" % (time.time() - timer)) - - pixelsPerStair = [21, 23, 23, 24, 25, 27, 31, 33, 28, 26, 25, 24, 24] i = 0 o = 0 @@ -124,17 +98,13 @@ def main(): j = 0 while o <= mellomRekning: - i2cPixel.setPixel(o, *color) + strip.setPixel(o, *color) o = o + 1 - i2cPixel.show() + strip.show() i = i + 1 print("--- %s seconds ---" % (time.time() - timer)) - - - - """ Start script """ setup() #configure main() #Start main -- cgit v1.2.3