Error manager is also implement as funtion pointer

master
Kerem Yollu 4 years ago
parent 897f669682
commit e5179d632d

@ -20,7 +20,7 @@ unsigned int miliSecond = 1000;
ErrorHandler errorHandle; ErrorHandler errorHandle;
CommandManager commander; CommandManager commander;
i2c_ch1_pImpL i2c(1); i2c_ch1_pImpL i2c(1, &errorHandle);
Bh1750 lightSens(&i2c); Bh1750 lightSens(&i2c);
@ -49,10 +49,12 @@ int main(int argc, char *argv[])
// Init // Init
initPlatform(); initPlatform();
std::cout << "Main" << std::endl; std::cout << "Main" << std::endl;
//errorHandle.addNewError(-34,"Test eroor 1"); // errorHandle.addNewError(-34,__FILE__,"Test eroor 1",SPARE);
//errorHandle.handleError(-34); // errorHandle.addNewError(-34,__FILE__,"Test eroor 2",KILL);
//commander.addNewCommand("test", "The test command for testing the test", dummy); // errorHandle.handleError(-34,__FILE__);
//commander(argv[1]); // 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.addNewCommand("i2c", "The test command for testing the test", dummy);
commander(argv[1]); commander(argv[1]);

Binary file not shown.

@ -5,11 +5,11 @@ ErrorHandler::ErrorHandler(): emptyIndex(0)
} }
void ErrorHandler::handleError(int errNo) void ErrorHandler::handleError(int errNo, std::string source)
{ {
int index = 0; int index = 0;
index = getLookUpIndex(errNo); index = getLookUpIndex(errNo, source);
if(index < 0) if(index < 0)
{ {
@ -17,11 +17,15 @@ void ErrorHandler::handleError(int errNo)
exit(1); exit(1);
} }
std::cout << "Error | " << errNo << " | \"" << errorLookup[index].errMessage << "\""<<std::endl; std::cout << ">> Error << | " << errorLookup[index].errSource << " | " << errNo << " | \"" << errorLookup[index].errMessage << "\""<<std::endl;
exit(1);
if(errorLookup[index].errExit)
{
exit(1);
}
} }
void ErrorHandler::addNewError(const int& no, const std::string& message) void ErrorHandler::addNewError(const int& no, const std::string& source, const std::string& message, const unsigned char kill)
{ {
if(emptyIndex >= MAX_NUMBER_OF_ERRORS) // Check if list is full if(emptyIndex >= 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); 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); exit(1);
} }
errorLookup[emptyIndex].errNo = no; errorLookup[emptyIndex].errNo = no;
errorLookup[emptyIndex].errSource = source;
errorLookup[emptyIndex].errMessage = message; errorLookup[emptyIndex].errMessage = message;
errorLookup[emptyIndex].errExit = kill;
emptyIndex++; emptyIndex++;
} }
@ -44,14 +50,17 @@ void ErrorHandler::addNewError(const int& no, const std::string& message)
// Privat memeber functions // Privat memeber functions
// //
int ErrorHandler::getLookUpIndex(int errNo) int ErrorHandler::getLookUpIndex(int errNo, std::string source)
{ {
int i = 0; int i = 0;
for(i = 0; i < emptyIndex; i++) for(i = 0; i < emptyIndex; i++)
{ {
if(errNo == errorLookup[i].errNo) if(errNo == errorLookup[i].errNo)
{ {
return i; if(source == errorLookup[i].errSource)
{
return i;
}
} }
} }
return -1; return -1;

@ -18,24 +18,27 @@
#include <string> #include <string>
#define MAX_NUMBER_OF_ERRORS 255 #define MAX_NUMBER_OF_ERRORS 255
#define KILL 1
#define SPARE 0
class ErrorHandler class ErrorHandler
{ {
public: public:
ErrorHandler(); ErrorHandler();
void addNewError(const int& no, const std::string& message); void addNewError(const int& no, const std::string& source, const std::string& message, const unsigned char kill);
void handleError(int no); void handleError(int no, std::string source);
private: private:
struct error_t { //Struture of error entry for the errorLookup table struct error_t { //Struture of error entry for the errorLookup table
int errNo; int errNo;
std::string errMessage; std::string errMessage;
std::string errSource;
unsigned char errExit;
}; };
unsigned int emptyIndex; // Indicates the next empty slot in the errorLookup table. unsigned int emptyIndex; // Indicates the next empty slot in the errorLookup table.
std::array <error_t,MAX_NUMBER_OF_ERRORS> errorLookup; // Where the errors go. std::array <error_t,MAX_NUMBER_OF_ERRORS> 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_ #endif // _ERRORHANDLIG_H_

@ -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/ // 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]; 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); deviceDescriptor = open(filename, O_RDWR);
if (deviceDescriptor < 0) { if (deviceDescriptor < 0) {
std::cout << "unable to open : "<< deviceDescriptor << " ! quiting" << std::endl; errorHandling->handleError(-1,__FILE__);
exit(1);
} }
} }
@ -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) if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
{ {
std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl; errorHandling->handleError(-2,__FILE__);
exit(1);
} }
send_buffer[0] = data; send_buffer[0] = data;
if ((write(deviceDescriptor, send_buffer, 1)) != 1) if ((write(deviceDescriptor, send_buffer, 1)) != 1)
{ {
std::cout << "Unable to write quiting" << std::endl; errorHandling->handleError(-3,__FILE__);
exit(0);
} }
return 0; 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) if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
{ {
std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl; errorHandling->handleError(-2,__FILE__);
exit(1);
} }
writeByte(address,reg); //Initiate a write to indicate the desired register to read 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 if (read(deviceDescriptor, recieve_buffer, 1) != 1) // An then initare a read request of 1 byte
{ {
std::cout << "Unable to read quiting" << std::endl; errorHandling->handleError(-4,__FILE__);
exit(1);
} }
return recieve_buffer[0] ; 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) if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
{ {
std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl; errorHandling->handleError(-2,__FILE__);
exit(1);
} }
send_buffer[0] = reg; 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) if ((write(deviceDescriptor, send_buffer, 2)) != 2)
{ {
std::cout << "Unable to write quiting" << std::endl; errorHandling->handleError(-3,__FILE__);
exit(0);
} }
return 0; return 0;
} }
@ -90,16 +91,14 @@ uint16_t i2c_ch1_pImpL::readWord(const uint8_t& address, const uint8_t& reg)
uint16_t result = 0 ; uint16_t result = 0 ;
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0) if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
{ {
std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl; errorHandling->handleError(-2,__FILE__);
exit(1);
} }
writeByte(address,reg); //Initiate a write to indicate the desired register to read 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 if (read(deviceDescriptor, recieve_buffer, 2) != 2) // An then initare a read request of 2 bytes
{ {
std::cout << "Unable to read quiting" << std::endl; errorHandling->handleError(-4,__FILE__);
exit(1);
} }
result = (recieve_buffer[0] << 8) + recieve_buffer[1] ; result = (recieve_buffer[0] << 8) + recieve_buffer[1] ;
return result ; return result ;

@ -10,7 +10,7 @@
class i2c_ch1_pImpL class i2c_ch1_pImpL
{ {
public: 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); uint8_t readByte(const uint8_t& address, const uint8_t& reg);
uint16_t readWord(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); uint8_t writeByte(const uint8_t& address, const uint8_t& data);
@ -18,6 +18,7 @@ class i2c_ch1_pImpL
private: private:
ErrorHandler *errorHandling;
int16_t deviceDescriptor; int16_t deviceDescriptor;
uint8_t device_address; uint8_t device_address;
uint8_t device_reg; uint8_t device_reg;

Binary file not shown.
Loading…
Cancel
Save