From af2d66f9e7a604cfec51d6e7b66d5f90827fe7bb Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Tue, 18 Oct 2016 12:46:55 +0200 Subject: Added functions, error-handling Added errorhandeler Added try-catch --- Raspberry Pi/i2cPixel.py | 43 ++++++++++++++++++++++++++++++++++++------- Raspberry Pi/python.py | 28 +++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/Raspberry Pi/i2cPixel.py b/Raspberry Pi/i2cPixel.py index 936c828..6d74a68 100644 --- a/Raspberry Pi/i2cPixel.py +++ b/Raspberry Pi/i2cPixel.py @@ -1,6 +1,11 @@ +""" Includes """ import smbus +import sys import time +""" Variables """ +lockdown = False + def version(): print "i2cPixel is Version 1.0.0" @@ -10,36 +15,60 @@ def setAddress(address): def setBus(n): global bus - bus = smbus.SMBus(n) + try: + bus = smbus.SMBus(n) + except: + errorMsg = sys.exc_info()[0] + errorHandler(5, errorMsg) +def available(): + global lockdown + return lockdown + def greeting(): """ Send heartbeat """ bus.write_byte(arduinoAddress, 0x01) """ Wait for response """ - try: + 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 def setPixel(n, red, green, blue): - """ Send values for switching a pixel on """ - bus.write_block_data(arduinoAddress, 0x02, [n, red, green, blue]) + """ Send values for changing pixel values """ + try: + bus.write_block_data(arduinoAddress, 0x02, [n, red, green, blue]) + except: + errorMsg = sys.exc_info()[0] + errorHandler(5, errorMsg) def show(): - bus.write_byte(arduinoAddress, 0x03) + """ Send values for turning pixels on """ + try: + bus.write_byte(arduinoAddress, 0x03) + except: + errorMsg = sys.exc_info()[0] + errorHandler(5, errorMsg) def blink(time, red, green, blue): - bus.write_block_data(arduinoAddress, 0x04, [red, green, blue, time]) + """ Flash all pixels with a colour """ + try: + bus.write_block_data(arduinoAddress, 0x04, [red, green, blue, time]) + except: + errorMsg = sys.exc_info()[0] + errorHandler(5, errorMsg) def waitForSensor(): - while True: try: sensorData = bus.read_byte(arduinoAddress) diff --git a/Raspberry Pi/python.py b/Raspberry Pi/python.py index 57cf380..6ebb805 100644 --- a/Raspberry Pi/python.py +++ b/Raspberry Pi/python.py @@ -1,6 +1,7 @@ """ Imports """ import logging import smbus +import sys import time import json import i2cPixel @@ -28,13 +29,38 @@ def lightStaircase(direction): 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'") + def setup(): - + + """ Setup Log File """ + logging.basicConfig(filename='error.log',level=logging.DEBUG) + + """ Print first line of log file """ + logging.info('Starting App') + """ Setup i2c communication """ i2cPixel.version() i2cPixel.setBus(1) i2cPixel.setAddress(0x04) + def main(): """ Wait for heartbeat from Arduino """ -- cgit v1.2.3