diff --git a/developpment/interfacer.cpp b/developpment/interfacer.cpp index e396442..c58d028 100644 --- a/developpment/interfacer.cpp +++ b/developpment/interfacer.cpp @@ -20,7 +20,7 @@ unsigned int miliSecond = 1000; ErrorHandler errorHandle; CommandManager commander; -i2c_ch1_pImpL i2c(1); +i2c_ch1_pImpL i2c(1, &errorHandle); Bh1750 lightSens(&i2c); @@ -49,10 +49,12 @@ int main(int argc, char *argv[]) // Init initPlatform(); std::cout << "Main" << std::endl; - //errorHandle.addNewError(-34,"Test eroor 1"); - //errorHandle.handleError(-34); - //commander.addNewCommand("test", "The test command for testing the test", dummy); - //commander(argv[1]); + // errorHandle.addNewError(-34,__FILE__,"Test eroor 1",SPARE); + // errorHandle.addNewError(-34,__FILE__,"Test eroor 2",KILL); + // errorHandle.handleError(-34,__FILE__); + // errorHandle.handleError(-34,"main"); + // commander.addNewCommand("test", "The test command for testing the test", dummy); + // commander(argv[1]); commander.addNewCommand("i2c", "The test command for testing the test", dummy); commander(argv[1]); diff --git a/developpment/interfacer.o b/developpment/interfacer.o index 763439b..55dd162 100644 Binary files a/developpment/interfacer.o and b/developpment/interfacer.o differ diff --git a/developpment/management/errorHandling.cpp b/developpment/management/errorHandling.cpp index 9e30a38..5adac38 100644 --- a/developpment/management/errorHandling.cpp +++ b/developpment/management/errorHandling.cpp @@ -5,11 +5,11 @@ ErrorHandler::ErrorHandler(): emptyIndex(0) } -void ErrorHandler::handleError(int errNo) +void ErrorHandler::handleError(int errNo, std::string source) { int index = 0; - index = getLookUpIndex(errNo); + index = getLookUpIndex(errNo, source); if(index < 0) { @@ -17,11 +17,15 @@ void ErrorHandler::handleError(int errNo) exit(1); } - std::cout << "Error | " << errNo << " | \"" << errorLookup[index].errMessage << "\""<> Error << | " << errorLookup[index].errSource << " | " << errNo << " | \"" << errorLookup[index].errMessage << "\""<= MAX_NUMBER_OF_ERRORS) // Check if list is full { @@ -29,14 +33,16 @@ void ErrorHandler::addNewError(const int& no, const std::string& message) exit(1); } - if(getLookUpIndex(no) >= 0) // Check if Error No already exists + if(getLookUpIndex(no, source) >= 0) // Check if Error No already exists { - std::cout << "Error | Intern | Error No: "<< no << " Exists ! PLease choose a different number !" << std::endl; + std::cout << "Error | Intern | Error No: "<< no << " From Source: "<< source <<" Exists ! PLease choose a different number !" << std::endl; exit(1); } errorLookup[emptyIndex].errNo = no; + errorLookup[emptyIndex].errSource = source; errorLookup[emptyIndex].errMessage = message; + errorLookup[emptyIndex].errExit = kill; emptyIndex++; } @@ -44,14 +50,17 @@ void ErrorHandler::addNewError(const int& no, const std::string& message) // Privat memeber functions // -int ErrorHandler::getLookUpIndex(int errNo) +int ErrorHandler::getLookUpIndex(int errNo, std::string source) { int i = 0; for(i = 0; i < emptyIndex; i++) { if(errNo == errorLookup[i].errNo) { - return i; + if(source == errorLookup[i].errSource) + { + return i; + } } } return -1; diff --git a/developpment/management/errorHandling.h b/developpment/management/errorHandling.h index b9dbe92..b12d553 100644 --- a/developpment/management/errorHandling.h +++ b/developpment/management/errorHandling.h @@ -18,24 +18,27 @@ #include #define MAX_NUMBER_OF_ERRORS 255 - +#define KILL 1 +#define SPARE 0 class ErrorHandler { public: ErrorHandler(); - void addNewError(const int& no, const std::string& message); - void handleError(int no); + void addNewError(const int& no, const std::string& source, const std::string& message, const unsigned char kill); + void handleError(int no, std::string source); private: struct error_t { //Struture of error entry for the errorLookup table int errNo; std::string errMessage; + std::string errSource; + unsigned char errExit; }; unsigned int emptyIndex; // Indicates the next empty slot in the errorLookup table. std::array errorLookup; // Where the errors go. - int getLookUpIndex(int errNo); // If error number exists returns the index otherwise -1 + int getLookUpIndex(int errNo, std::string source); // If error number exists returns the index otherwise -1 }; #endif // _ERRORHANDLIG_H_ diff --git a/developpment/management/errorHandling.o b/developpment/management/errorHandling.o index 5826368..38985d5 100644 Binary files a/developpment/management/errorHandling.o and b/developpment/management/errorHandling.o differ diff --git a/developpment/periferals/i2c/i2c_ch1_pImpL.cpp b/developpment/periferals/i2c/i2c_ch1_pImpL.cpp index 7818b65..ef4354a 100644 --- a/developpment/periferals/i2c/i2c_ch1_pImpL.cpp +++ b/developpment/periferals/i2c/i2c_ch1_pImpL.cpp @@ -16,15 +16,22 @@ // some curious things https://www.john.geek.nz/2012/12/update-reading-data-from-a-bosch-bmp085-with-a-raspberry-pi/ -i2c_ch1_pImpL::i2c_ch1_pImpL(const uint8_t& mode) +i2c_ch1_pImpL::i2c_ch1_pImpL(const uint8_t& mode, ErrorHandler* err) { + errorHandling = err; char filename[20]; - snprintf(filename, 19, "/dev/i2c-%d", 1); + + + errorHandling->addNewError(-1,__FILE__,"Unable to open device",KILL); + errorHandling->addNewError(-2,__FILE__,"Unable to reach the device",KILL); + errorHandling->addNewError(-3,__FILE__,"Unable to write to device",KILL); + errorHandling->addNewError(-4,__FILE__,"Unable to read from device",KILL); + snprintf(filename, 19, "/dev/i2c-%d", 1); + deviceDescriptor = open(filename, O_RDWR); if (deviceDescriptor < 0) { - std::cout << "unable to open : "<< deviceDescriptor << " ! quiting" << std::endl; - exit(1); + errorHandling->handleError(-1,__FILE__); } } @@ -32,16 +39,14 @@ uint8_t i2c_ch1_pImpL::writeByte(const uint8_t& address, const uint8_t& data) { if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0) { - std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl; - exit(1); + errorHandling->handleError(-2,__FILE__); } send_buffer[0] = data; if ((write(deviceDescriptor, send_buffer, 1)) != 1) { - std::cout << "Unable to write quiting" << std::endl; - exit(0); + errorHandling->handleError(-3,__FILE__); } return 0; } @@ -50,16 +55,14 @@ uint8_t i2c_ch1_pImpL::readByte(const uint8_t& address, const uint8_t& reg) { if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0) { - std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl; - exit(1); + errorHandling->handleError(-2,__FILE__); } writeByte(address,reg); //Initiate a write to indicate the desired register to read if (read(deviceDescriptor, recieve_buffer, 1) != 1) // An then initare a read request of 1 byte { - std::cout << "Unable to read quiting" << std::endl; - exit(1); + errorHandling->handleError(-4,__FILE__); } return recieve_buffer[0] ; } @@ -70,8 +73,7 @@ uint8_t i2c_ch1_pImpL::writeWord(const uint8_t& address, const uint8_t& reg, con if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0) { - std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl; - exit(1); + errorHandling->handleError(-2,__FILE__); } send_buffer[0] = reg; @@ -79,8 +81,7 @@ uint8_t i2c_ch1_pImpL::writeWord(const uint8_t& address, const uint8_t& reg, con if ((write(deviceDescriptor, send_buffer, 2)) != 2) { - std::cout << "Unable to write quiting" << std::endl; - exit(0); + errorHandling->handleError(-3,__FILE__); } return 0; } @@ -90,16 +91,14 @@ uint16_t i2c_ch1_pImpL::readWord(const uint8_t& address, const uint8_t& reg) uint16_t result = 0 ; if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0) { - std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl; - exit(1); + errorHandling->handleError(-2,__FILE__); } writeByte(address,reg); //Initiate a write to indicate the desired register to read if (read(deviceDescriptor, recieve_buffer, 2) != 2) // An then initare a read request of 2 bytes { - std::cout << "Unable to read quiting" << std::endl; - exit(1); + errorHandling->handleError(-4,__FILE__); } result = (recieve_buffer[0] << 8) + recieve_buffer[1] ; return result ; diff --git a/developpment/periferals/i2c/i2c_ch1_pImpL.hpp b/developpment/periferals/i2c/i2c_ch1_pImpL.hpp index d53d85f..9c0312c 100644 --- a/developpment/periferals/i2c/i2c_ch1_pImpL.hpp +++ b/developpment/periferals/i2c/i2c_ch1_pImpL.hpp @@ -10,7 +10,7 @@ class i2c_ch1_pImpL { public: - i2c_ch1_pImpL(const uint8_t& mode); // Mode : Master or Slave + i2c_ch1_pImpL(const uint8_t& mode, ErrorHandler* err); // Mode : Master or Slave uint8_t readByte(const uint8_t& address, const uint8_t& reg); uint16_t readWord(const uint8_t& address, const uint8_t& reg); uint8_t writeByte(const uint8_t& address, const uint8_t& data); @@ -18,6 +18,7 @@ class i2c_ch1_pImpL private: + ErrorHandler *errorHandling; int16_t deviceDescriptor; uint8_t device_address; uint8_t device_reg; diff --git a/developpment/periferals/i2c/i2c_ch1_pImpL.o b/developpment/periferals/i2c/i2c_ch1_pImpL.o index 8402b60..e2f3341 100644 Binary files a/developpment/periferals/i2c/i2c_ch1_pImpL.o and b/developpment/periferals/i2c/i2c_ch1_pImpL.o differ diff --git a/developpment/runtest b/developpment/runtest index f4621ab..6be4608 100755 Binary files a/developpment/runtest and b/developpment/runtest differ