From 3c6a622c71c8f092e3de3f9a2fbbbfcca67cf720 Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Fri, 21 Oct 2016 07:57:13 +0200 Subject: Make Arduino Project. update scripts --- Arduino/i2cPixelSlave.ino | 275 -------------------------- Arduino/i2cPixelSlave/i2cPixelSlave.ino | 336 ++++++++++++++++++++++++++++++++ Raspberry Pi/i2cPixel.py | 198 +++++++++++++------ Raspberry Pi/python.py | 166 ++++++++++------ 4 files changed, 576 insertions(+), 399 deletions(-) delete mode 100644 Arduino/i2cPixelSlave.ino create mode 100644 Arduino/i2cPixelSlave/i2cPixelSlave.ino diff --git a/Arduino/i2cPixelSlave.ino b/Arduino/i2cPixelSlave.ino deleted file mode 100644 index fa5bf45..0000000 --- a/Arduino/i2cPixelSlave.ino +++ /dev/null @@ -1,275 +0,0 @@ -#include -#include - -#define PIN 6 -#define PIXELS 10 // Set this to the number of pixels in yout strip -#define SLAVE_ADDRESS 0x04 - -// Parameter 1 = number of pixels in strip -// Parameter 2 = pin number (most are valid) -// Parameter 3 = pixel type flags, add together as needed: -// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) -// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) -// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) -// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) -Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELS, PIN, NEO_GRB + NEO_KHZ800); - -// i2c Config -int number = 0; -int state = 0; - -// PIR Config -long calibrationTime = 60000; // How long the sensors need to calibrate -long unsigned int lowIn; -long unsigned int pause = 5000; // How long time before we assume no motion - - // Sensor 1 -int pir1 = 7; // choose the input pin (for First sensor) -int pir1State = LOW; // we start, assuming no motion detected -int val1 = 0; // variable for reading the pin status -boolean lockLow1 = true; -boolean takeLowTime1; - - // Sensor 2 -int pir2 = 8; // choose the input pin (for Second sensor) -int pir2State = LOW; // we start, assuming no motion detected -int val2 = 0; // variable for reading the pin status -boolean lockLow2 = true; -boolean takeLowTime2; - -void setup() { - - // Set pins - pinMode(13, OUTPUT); - pinMode(pir1, INPUT); - pinMode(pir2, INPUT); - - // Open Serial port (not really neccesary) - Serial.begin(9600); - - // Initiate Neopixels - strip.begin(); - strip.show(); // Initialize all pixels to 'off' - - // Set all pixels to white - for (int i = 0; i <= PIXELS; i++) { - strip.setPixelColor(i, 255, 255, 255); - } - strip.show(); - - //give the sensor some time to calibrate. Animate with neopixels - Serial.print("calibrating sensor "); - - long delayTime = calibrationTime/PIXELS; - - for(int i = 0; i < PIXELS; i++){ - strip.setPixelColor(PIXELS-i, 0, 0, 0); - strip.show(); - - Serial.print("."); - delay(delayTime); - } - - strip.setPixelColor(0, 0, 0, 0); - strip.show(); - - Serial.println(" done"); - Serial.println("SENSOR ACTIVE"); - delay(50); - - // Open i2c connection - Wire.begin(SLAVE_ADDRESS); - - Wire.onReceive(receiveData); - Wire.onRequest(sendData); - - // Done booting! - Serial.println("Ready!"); - - // Flash once Red - flashColor(204, 0, 0, 1100); - -} - -void loop() { - - // Sensor 1 // - if(digitalRead(pir1) == HIGH){ - digitalWrite(13, HIGH); //the led visualizes the sensors output pin state - number = 2; - sendData(); - if(lockLow1){ - //makes sure we wait for a transition to LOW before any further output is made: - lockLow1 = false; - Serial.println("---"); - Serial.print("motion detected at "); - Serial.print(millis()/1000); - Serial.println(" sec"); - delay(50); - } - takeLowTime1 = true; - } - - if(digitalRead(pir1) == LOW){ - digitalWrite(13, LOW); //the led visualizes the sensors output pin state - number = 0; - if(takeLowTime1){ - lowIn = millis(); //save the time of the transition from high to LOW - takeLowTime1 = false; //make sure this is only done at the start of a LOW phase - } - //if the sensor is low for more than the given pause, - //we assume that no more motion is going to happen - if(!lockLow1 && millis() - lowIn > pause){ - //makes sure this block of code is only executed again after - //a new motion sequence has been detected - lockLow1 = true; - Serial.print("motion ended at "); //output - Serial.print((millis() - pause)/1000); - Serial.println(" sec"); - delay(50); - } - } - // ./Sensor 1 // - - - - // Sensor 2 // - if(digitalRead(pir2) == HIGH){ - digitalWrite(13, HIGH); //the led visualizes the sensors output pin state - number = 3; - sendData(); - if(lockLow2){ - //makes sure we wait for a transition to LOW before any further output is made: - lockLow2 = false; - Serial.println("---"); - Serial.print("motion detected at "); - Serial.print(millis()/1000); - Serial.println(" sec"); - delay(50); - } - takeLowTime2 = true; - } - - if(digitalRead(pir2) == LOW){ - digitalWrite(13, LOW); //the led visualizes the sensors output pin state - number = 0; - if(takeLowTime2){ - lowIn = millis(); //save the time of the transition from high to LOW - takeLowTime2 = false; //make sure this is only done at the start of a LOW phase - } - //if the sensor is low for more than the given pause, - //we assume that no more motion is going to happen - if(!lockLow2 && millis() - lowIn > pause){ - //makes sure this block of code is only executed again after - //a new motion sequence has been detected - lockLow2 = true; - Serial.print("motion ended at "); //output - Serial.print((millis() - pause)/1000); - Serial.println(" sec"); - delay(50); - } - } - // ./Sensor 2 // -} - -void receiveData(int byteCount) { - digitalWrite(13, HIGH); - int bytes[byteCount]; - int i = 0; - - while (Wire.available()) { - number = Wire.read(); - bytes[i] = number; - - Serial.println(number); - i++; - } - - switch (bytes[0]) { - - case 0x01: - Serial.println("Life is discovered"); - number = 1; - flashColor(0, 204, 0, 1100); - break; - - case 0x02: - strip.setPixelColor(bytes[2], bytes[3], bytes[4], bytes[5]); - break; - - case 0x03: - strip.show(); - break; - - case 0x04: - flashColor(bytes[2], bytes[3], bytes[4], bytes[5]); - break; - - default: - Serial.println("Nothing New"); - break; - - } - - digitalWrite(13, LOW); -} - -void sendData() { - Wire.write(number); -} - -void sendString(int Data) { - Wire.write(Data); -} - -void testCoid() { - flashColor(0, 204, 0, 1100); -} - -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; - - for(long i = 0; i < n; i++) // larger values of 'n' will give a smoother/slower transition. - { - byte Rnew = Rstart + (Rend - Rstart) * i / n; - byte Gnew = Gstart + (Gend - Gstart) * i / n; - byte Bnew = Bstart + (Bend - Bstart) * i / n; - // set pixel color here - for(int j=0; j < PIXELS; j++) { - strip.setPixelColor(j, strip.Color(Rnew, Gnew, Bnew)); - } - strip.show(); - } - - 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. - { - byte Rnew = Rstart + (Rend - Rstart) * i / n; - byte Gnew = Gstart + (Gend - Gstart) * i / n; - byte Bnew = Bstart + (Bend - Bstart) * i / n; - // set pixel color here - for(int j=0; j < PIXELS; j++) { - strip.setPixelColor(j, strip.Color(Rnew, Gnew, Bnew)); - } - strip.show(); - } - - for(int i = 0; i < PIXELS; i++) { - strip.setPixelColor(i, 0, 0, 0); - } - strip.show(); - -} - diff --git a/Arduino/i2cPixelSlave/i2cPixelSlave.ino b/Arduino/i2cPixelSlave/i2cPixelSlave.ino new file mode 100644 index 0000000..bc7dc56 --- /dev/null +++ b/Arduino/i2cPixelSlave/i2cPixelSlave.ino @@ -0,0 +1,336 @@ +#include +#include + +#define PIN 6 +#define PIXELS 348 // Set this to the number of pixels in yout strip +#define SLAVE_ADDRESS 0x04 + +// Parameter 1 = number of pixels in strip +// Parameter 2 = pin number (most are valid) +// Parameter 3 = pixel type flags, add together as needed: +// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) +// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) +// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) +// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) +Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELS, PIN, NEO_GRB + NEO_KHZ800); + +// Timer +unsigned long previousMillis = 0; +long millisLockTime = 60000; + +// i2c Config +int number = 0; +int state = 0; + +// PIR Config +long calibrationTime = 60000; // How long the sensors need to calibrate +long unsigned int lowIn; +long unsigned int pause = 5000; // How long time before we assume no motion + + // Sensor 1 +int pir1 = 7; // choose the input pin (for First sensor) +int pir1State = LOW; // we start, assuming no motion detected +int val1 = 0; // variable for reading the pin status +boolean lockLow1 = true; +boolean takeLowTime1; + + // Sensor 2 +int pir2 = 8; // choose the input pin (for Second sensor) +int pir2State = LOW; // we start, assuming no motion detected +int val2 = 0; // variable for reading the pin status +boolean lockLow2 = true; +boolean takeLowTime2; + +boolean timeoutHappened = false; +boolean timeoutEnabled = true; + +// pixelInt +long pixelInt = 0; + +void setup() { + + // Set pins + pinMode(13, OUTPUT); + pinMode(pir1, INPUT); + pinMode(pir2, INPUT); + + // Open Serial port (not really neccesary) + Serial.begin(9600); + + // Initiate Neopixels + strip.begin(); + strip.show(); // Initialize all pixels to 'off' + + // Set all pixels to white + for (int i = 0; i <= PIXELS; i++) { + strip.setPixelColor(i, 255, 255, 255); + } + strip.show(); + + //give the sensor some time to calibrate. Animate with neopixels + Serial.print("calibrating sensor "); + + long delayTime = calibrationTime/PIXELS; + + for(int i = 0; i < PIXELS; i++){ + strip.setPixelColor(PIXELS-i, 0, 0, 0); + strip.show(); + + Serial.print("."); + delay(delayTime); + } + + strip.setPixelColor(0, 0, 0, 0); + strip.show(); + + Serial.println(" done"); + Serial.println("SENSOR ACTIVE"); + delay(50); + + // Open i2c connection + Wire.setClock(400000L); + Wire.begin(SLAVE_ADDRESS); + + Wire.onReceive(receiveData); + Wire.onRequest(sendData); + + // Done booting! + Serial.println("Ready!"); + + // Flash once Red + // flashColor(0, 5, 165, 70); + +} + +void loop() { + + if(!timeoutEnabled) { + unsigned long currentMillis = millis(); + + if (currentMillis-previousMillis >= millisLockTime) { + for(int j=0; j < PIXELS; j++) { + if (!timeoutHappened) { + fadeFromBlack(255, 0, 0, 30); + timeoutHappened = true; + } + } + strip.show(); + } else { + timeoutHappened = false; + } + } + // Sensor 1 // + if(digitalRead(pir1) == HIGH){ + digitalWrite(13, HIGH); //the led visualizes the sensors output pin state + number = 2; + sendData(); + if(lockLow1){ + //makes sure we wait for a transition to LOW before any further output is made: + lockLow1 = false; + Serial.println("---"); + Serial.print("motion detected at "); + Serial.print(millis()/1000); + Serial.println(" sec"); + delay(50); + } + takeLowTime1 = true; + } + + if(digitalRead(pir1) == LOW){ + digitalWrite(13, LOW); //the led visualizes the sensors output pin state + number = 0; + if(takeLowTime1){ + lowIn = millis(); //save the time of the transition from high to LOW + takeLowTime1 = false; //make sure this is only done at the start of a LOW phase + } + //if the sensor is low for more than the given pause, + //we assume that no more motion is going to happen + if(!lockLow1 && millis() - lowIn > pause){ + //makes sure this block of code is only executed again after + //a new motion sequence has been detected + lockLow1 = true; + Serial.print("motion ended at "); //output + Serial.print((millis() - pause)/1000); + Serial.println(" sec"); + delay(50); + } + } + // ./Sensor 1 // + + + + // Sensor 2 // + if(digitalRead(pir2) == HIGH){ + digitalWrite(13, HIGH); //the led visualizes the sensors output pin state + number = 3; + sendData(); + if(lockLow2){ + //makes sure we wait for a transition to LOW before any further output is made: + lockLow2 = false; + Serial.println("---"); + Serial.print("motion detected at "); + Serial.print(millis()/1000); + Serial.println(" sec"); + delay(50); + } + takeLowTime2 = true; + } + + if(digitalRead(pir2) == LOW){ + digitalWrite(13, LOW); //the led visualizes the sensors output pin state + number = 0; + if(takeLowTime2){ + lowIn = millis(); //save the time of the transition from high to LOW + takeLowTime2 = false; //make sure this is only done at the start of a LOW phase + } + //if the sensor is low for more than the given pause, + //we assume that no more motion is going to happen + if(!lockLow2 && millis() - lowIn > pause){ + //makes sure this block of code is only executed again after + //a new motion sequence has been detected + lockLow2 = true; + Serial.print("motion ended at "); //output + Serial.print((millis() - pause)/1000); + Serial.println(" sec"); + delay(50); + } + } + // ./Sensor 2 // +} + +void receiveData(int byteCount) { + previousMillis = millis(); + + digitalWrite(13, HIGH); + int bytes[byteCount]; + int i = 0; + + while (Wire.available()) { + number = Wire.read(); + bytes[i] = number; + + Serial.println(number); + i++; + } + + + switch (bytes[0]) { + + case 0x01: + Serial.println("Life is discovered"); + number = 1; + flashColor(0, 204, 0, 30); + break; + + case 0x02: + pixelInt = bytes[2] + bytes[3]; + strip.setPixelColor(pixelInt, bytes[4], bytes[5], bytes[6]); + break; + + case 0x03: + strip.show(); + break; + + case 0x04: + flashColor(bytes[2], bytes[3], bytes[4], bytes[5]); + break; + + case 0x05: + timeoutEnabled = false; + break; + + case 0x06: + strip.setBrightness(bytes[2]); + break; + + case 0x07: + number = 42; + break; + + + default: + Serial.println("Nothing New"); + break; + + } + + digitalWrite(13, LOW); +} + +void sendData() { + previousMillis = millis(); + Wire.write(number); +} + +void sendString(int Data) { + Wire.write(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. + { + byte Rnew = Rstart + (Rend - Rstart) * i / n; + byte Gnew = Gstart + (Gend - Gstart) * i / n; + byte Bnew = Bstart + (Bend - Bstart) * 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; + + for(long i = 0; i < n; i++) // larger values of 'n' will give a smoother/slower transition. + { + byte Rnew = Rstart + (Rend - Rstart) * i / n; + byte Gnew = Gstart + (Gend - Gstart) * i / n; + byte Bnew = Bstart + (Bend - Bstart) * i / n; + // set pixel color here + for(int j=0; j < PIXELS; j++) { + strip.setPixelColor(j, strip.Color(Rnew, Gnew, Bnew)); + } + strip.show(); + } + + 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. + { + byte Rnew = Rstart + (Rend - Rstart) * i / n; + byte Gnew = Gstart + (Gend - Gstart) * i / n; + byte Bnew = Bstart + (Bend - Bstart) * i / n; + // set pixel color here + for(int j=0; j < PIXELS; j++) { + strip.setPixelColor(j, strip.Color(Rnew, Gnew, Bnew)); + } + strip.show(); + } + + for(int i = 0; i < PIXELS; i++) { + strip.setPixelColor(i, 0, 0, 0); + } + strip.show(); + +} + diff --git a/Raspberry Pi/i2cPixel.py b/Raspberry Pi/i2cPixel.py index 4e6785c..360b3f3 100644 --- a/Raspberry Pi/i2cPixel.py +++ b/Raspberry Pi/i2cPixel.py @@ -2,6 +2,28 @@ import smbus import sys import time +import logging + +logging.basicConfig(filename='error.log',level=logging.DEBUG) + +def errorHandler(type, errorMsg): + if type == 1: # Debug + logging.debug(errorMsg) + print("Debug Message: See logfile") + elif type == 2: # info + logging.info(errorMsg) + elif type == 3: # Warning + logging.warning(errorMsg) + elif type == 4: # Error + logging.error(errorMsg) + print("Error: See logfile") + elif type == 5: # Error + logging.critical(errorMsg) + print("Critical Error: See logfile") + else: + logging.critical(errorMsg) + print("Something went terribly wrong! Not even the errorhandler was able to find out what. Which basically means 'You are doomed'") + """ Variables """ lockdown = False @@ -9,84 +31,132 @@ lockdown = False def version(): print "i2cPixel is Version 1.0.0" +def available(): + global lockdown + return lockdown + def setAddress(address): global arduinoAddress arduinoAddress = address def setBus(n): - if available = False: - return 2 - else: - global bus - try: - bus = smbus.SMBus(n) - except: - errorMsg = sys.exc_info()[0] - errorHandler(5, errorMsg) - -def available(): - global lockdown - return lockdown - + if available == False: + return 2 + else: + lockdown = False + global bus + try: + bus = smbus.SMBus(n) + except: + errorMsg = sys.exc_info()[0] + errorHandler(5, errorMsg) + lockdown = True + def greeting(): - if available = False: - return 2 - else: - try: - """ Send heartbeat """ - bus.write_byte(arduinoAddress, 0x01) + if available == False: + return 2 + else: + lockdown = False + try: + """ Send heartbeat """ + bus.write_byte(arduinoAddress, 0x01) + + """ Wait for response """ + try: + response = bus.read_byte(arduinoAddress) + if response == 0x01: + returnMsg = True + else: + returnMsg = False + except: + errorMsg = sys.exc_info()[0] + errorHandler(5, errorMsg) + returnMsg = False + + """ Return if heartbeat was received """ + return returnMsg + + except: + errorMsg = sys.exc_info()[0] + errorHandler(5, errorMsg) + lockdown = True - """ Wait for response """ - try: - response = bus.read_byte(arduinoAddress) - if response == 0x01: - returnMsg = True - else: - returnMsg = False - except: - errorMsg = sys.exc_info()[0] - errorHandler(5, errorMsg) - returnMsg = False - - """ Return if heartbeat was received """ - return returnMsg - - except: - errorMsg = sys.exc_info()[0] - errorHandler(5, errorMsg) +def ping(): + # Send 0x07 vil faa tilbake 42 hvis live + return False +def setBrightness(intensity): + if available == False: + return 2 + else: + lockdown = False + 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) + 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]) + def setPixel(n, red, green, blue): """ Send values for changing pixel values """ - if available = False: - return 2 - else: - try: - bus.write_block_data(arduinoAddress, 0x02, [n, red, green, blue]) - except: - errorMsg = sys.exc_info()[0] - errorHandler(5, errorMsg) + if available == False: + return 2 + else: + lockdown = False + try: + if n > 255: + n1 = 255 + n2 = n - 255 + else: + n1 = 0 + n2 = n + #print("{", n1, n2, "}") + bus.write_block_data(arduinoAddress, 0x02, [n1, n2, red, green, blue]) + except: + errorMsg = sys.exc_info()[0] + errorHandler(5, errorMsg) + lockdown = True def show(): - """ Send values for turning pixels on """ - if available = False: - return 2 - else: - try: - bus.write_byte(arduinoAddress, 0x03) - except: - errorMsg = sys.exc_info()[0] - errorHandler(5, errorMsg) + """ Send values for turning pixels on """ + if available == False: + return 2 + else: + lockdown = False + try: + bus.write_byte(arduinoAddress, 0x03) + except: + errorMsg = sys.exc_info()[0] + errorHandler(5, errorMsg) + lockdown = True def blink(time, red, green, blue): - """ Flash all pixels with a colour """ - if available = False: - return 2 - else: - try: - bus.write_block_data(arduinoAddress, 0x04, [red, green, blue, time]) - except: - errorMsg = sys.exc_info()[0] - errorHandler(5, errorMsg) + """ Flash all pixels with a colour """ + if available == False: + return 2 + else: + lockdown = False + try: + bus.write_block_data(arduinoAddress, 0x04, [red, green, blue, time]) + except: + errorMsg = sys.exc_info()[0] + errorHandler(5, errorMsg) + lockdown = True def waitForSensor(): while True: diff --git a/Raspberry Pi/python.py b/Raspberry Pi/python.py index ee61bec..3078683 100644 --- a/Raspberry Pi/python.py +++ b/Raspberry Pi/python.py @@ -7,8 +7,7 @@ import json import i2cPixel """ Decalrations """ -pixels = 72 # Change this to the appropriate number for your setup -pixels = 10 +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): @@ -17,76 +16,123 @@ def hexToRgb(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 - while loopCondition >> pixels - - else: - loopCondition = 0 - - return true - + repeats = len(addresses) + repeats = repeats / 2 + + if direction == "up": + loopCondition = repeats + + + else: + loopCondition = 0 + + return true + def errorHandler(type, errorMsg): - if type = 1: # Debug - logging.debug(errorMsg) - print("Debug Message: See logfile") - else if type = 2: # info - logging.info(errorMsg) - else if type = 3: # Warning - logging.warning(errorMsg) - else if type = 4: # Error - logging.error(errorMsg) - print("Error: See logfile") - else if type = 5: # Error - logging.critical(errorMsg) - print("Critical Error: See logfile") - else: - logging.critical(errorMsg) - print("Something went terribly wrong! Not even the errorhandler was able to find out what. Which basically means 'You are doomed'") - + if type == 1: # Debug + logging.debug(errorMsg) + print("Debug Message: See logfile") + elif type == 2: # info + logging.info(errorMsg) + elif type == 3: # Warning + logging.warning(errorMsg) + elif type == 4: # Error + logging.error(errorMsg) + print("Error: See logfile") + elif type == 5: # Error + logging.critical(errorMsg) + print("Critical Error: See logfile") + else: + logging.critical(errorMsg) + print("Something went terribly wrong! Not even the errorhandler was able to find out what. Which basically means 'You are doomed'") + def setup(): - - """ Setup Log File """ - logging.basicConfig(filename='error.log',level=logging.DEBUG) - - """ Print first line of log file """ - logging.info('Starting App') - + + """ Setup Log File """ + logging.basicConfig(filename='error.log',level=logging.DEBUG,mode='w') + + """ Print first line of log file """ + logging.info('Starting App') + + """ Set time """ + start_time = time.time() + """ Setup i2c communication """ i2cPixel.version() i2cPixel.setBus(1) i2cPixel.setAddress(0x04) - -def main(): - """ Wait for heartbeat from Arduino """ while True: - try: - if i2cPixel.greeting(): - print "Arduino is Online" - break - except Exception: - pass + try: + if i2cPixel.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()""" - """while True: - i2cPixel.waitForSensor()""" - """ Test, set all pixels to entered color """ - while True: - test = raw_input() - colour = hexToRgb(test) - - i = 0 - while i < pixels: - i2cPixel.setPixel(i, colour[0], colour[1], colour[2]) - i = i + 1 - i2cPixel.show() +def main(): + + print "GO!" + timer = time.time() + i = 0 + while i < pixels: + i2cPixel.setPixel(i, 255, 255, 255) + i2cPixel.show() + i = i + 1 + print("--- %s seconds ---" % (time.time() - timer)) - i2cPixel.waitForSensor() + timer = time.time() + i = 0 + while i < pixels: + i2cPixel.setPixel(i, 255, 255, 255) + i = i + 1 + i2cPixel.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 + j = 0 + timer = time.time() + while i < len(pixelsPerStair): + time1 = time.time() + mellomRekning = o + pixelsPerStair[i] + + if j == 0: + color = (255, 0, 225) + j = j + 1 + elif j == 1: + color = (0, 63, 255) + j = j + 1 + elif j == 2: + color = (25, 255, 0) + j = j + 1 + elif j == 3: + color = (255, 0, 4) + j = j + 1 + else: + color = (255, 250, 0) + j = 0 + + while o <= mellomRekning: + i2cPixel.setPixel(o, *color) + o = o + 1 + i2cPixel.show() + i = i + 1 + print("--- %s seconds ---" % (time.time() - timer)) + + + + """ Start script """ -- cgit v1.2.3