diff options
author | Jakob Stendahl <jakob@Jakobs-MacBook-Pro.local> | 2016-10-27 09:09:30 +0200 |
---|---|---|
committer | Jakob Stendahl <jakob@Jakobs-MacBook-Pro.local> | 2016-10-27 09:09:30 +0200 |
commit | d17acdc5a9885fdcc96bbe2ee0a4c88b68d0c07e (patch) | |
tree | 1d50929069c70d965e2aea5ee5e9e65752cc877a | |
parent | 3c6a622c71c8f092e3de3f9a2fbbbfcca67cf720 (diff) | |
download | i2c-Neopixel-d17acdc5a9885fdcc96bbe2ee0a4c88b68d0c07e.tar.gz i2c-Neopixel-d17acdc5a9885fdcc96bbe2ee0a4c88b68d0c07e.zip |
Update
-rw-r--r-- | Arduino/i2cPixelSlave/i2cPixelSlave.ino | 60 | ||||
-rw-r--r-- | Raspberry Pi/i2cPixel.py | 109 | ||||
-rw-r--r-- | Raspberry Pi/python.py | 56 |
3 files changed, 101 insertions, 124 deletions
diff --git a/Arduino/i2cPixelSlave/i2cPixelSlave.ino b/Arduino/i2cPixelSlave/i2cPixelSlave.ino index bc7dc56..fc5d390 100644 --- a/Arduino/i2cPixelSlave/i2cPixelSlave.ino +++ b/Arduino/i2cPixelSlave/i2cPixelSlave.ino @@ -46,6 +46,7 @@ boolean timeoutEnabled = true; // pixelInt long pixelInt = 0; +long brightness = 0; void setup() { @@ -221,33 +222,33 @@ void receiveData(int byteCount) { number = 1; flashColor(0, 204, 0, 30); break; - + case 0x02: - pixelInt = bytes[2] + bytes[3]; - strip.setPixelColor(pixelInt, bytes[4], bytes[5], bytes[6]); + number = 42; break; - case 0x03: - strip.show(); + case 0x03: + brightness = bytes[2] + bytes[3]; + strip.setBrightness(brightness); break; - - case 0x04: - flashColor(bytes[2], bytes[3], bytes[4], bytes[5]); + + case 0x04: + pixelInt = bytes[2] + bytes[3]; + strip.setPixelColor(pixelInt, bytes[4], bytes[5], bytes[6]); break; - case 0x05: - timeoutEnabled = false; + case 0x05: + flashColor(bytes[2], bytes[3], bytes[4], bytes[5]); break; case 0x06: - strip.setBrightness(bytes[2]); + strip.show(); break; case 0x07: - number = 42; + timeoutEnabled = false; break; - default: Serial.println("Nothing New"); break; @@ -267,36 +268,26 @@ void sendString(int Data) { } void fadeFromBlack(byte Red, byte Green, byte Blue, long n) { - byte Rstart=0; - byte Gstart=0; - byte Bstart=0; - byte Rend=Red; - byte Gend=Green; - byte Bend=Blue; - for(long i = 0; i < n; i++) // larger values of 'n' will give a smoother/slower transition. + for(long i = 0; i < n; i++) { - byte Rnew = Rstart + (Rend - Rstart) * i / n; - byte Gnew = Gstart + (Gend - Gstart) * i / n; - byte Bnew = Bstart + (Bend - Bstart) * i / n; + byte Rnew = 0 + (Red - 0) * i / n; + byte Gnew = 0 + (Green - 0) * i / n; + byte Bnew = 0 + (Blue - 0) * i / n; // set pixel color here for(int j=0; j < PIXELS; j++) { strip.setPixelColor(j, strip.Color(Rnew, Gnew, Bnew)); } strip.show(); } + } void flashColor(byte Red, byte Green, byte Blue, long n) { - byte Rstart=0; - byte Gstart=0; - byte Bstart=0; - byte Rend=Red; - byte Gend=Green; - byte Bend=Blue; + byte Rstart=0;byte Gstart=0;byte Bstart=0;byte Rend=Red;byte Gend=Green;byte Bend=Blue; - for(long i = 0; i < n; i++) // larger values of 'n' will give a smoother/slower transition. + for(long i = 0; i < n; i++) { byte Rnew = Rstart + (Rend - Rstart) * i / n; byte Gnew = Gstart + (Gend - Gstart) * i / n; @@ -308,14 +299,9 @@ void flashColor(byte Red, byte Green, byte Blue, long n) { strip.show(); } - Rstart=Red; - Gstart=Green; - Bstart=Blue; - Rend=0; - Gend=0; - Bend=0; + Rstart=Red;Gstart=Green;Bstart=Blue;Rend=0;Gend=0;Bend=0; - for(long i = 0; i < n; i++) // larger values of 'n' will give a smoother/slower transition. + for(long i = 0; i < n; i++) { byte Rnew = Rstart + (Rend - Rstart) * i / n; byte Gnew = Gstart + (Gend - Gstart) * i / n; 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 |