If I still wanted to maintain some semblance of parameter passing, does the following code snippet represent the correct approach.
.hpp
Code:
class I2C {
protected:
unsigned char mDataW;
unsigned char mDataR;
unsigned char mCondition;
public:
I2C();
unsigned char GetData(){return mDataR};
void SetData (unsigned char Data)
{mDataW = Data};
void SetCondition (unsigned char Condition)
{mCondition = Condition;}
void I2C_Start ();
void Bus_Not_Busy ();
void I2C_Stop ();
void I2C_Write(unsigned char Data);
unsigned char I2C_Read(unsigned char Condition);
};
Corresponding .CPP file (portion) to Pass Parameters
Code:
//Write a byte followed by an ACKNOWLEDGE. If no Acknowledge is forthcoming, an error
//is generated
//INPUT: data byte
//OUTPUT: error = 1
void I2C::I2C_Write (unsigned char Data){
unsigned char temp = 0;
unsigned char error = 0;
SetData(Data); //mDataW = Data
for ( unsigned char x = 0; x < 8; x++ ){
temp = mDataW & MSB8; //Mask the highest-most bit and put result in temp
if (temp == MSB8){
gpio_set_gpio_pin(SDA);}
else {
gpio_clr_gpio_pin(SDA);}
cpu_delay_cy(DELAY_ELEMENT);
gpio_set_gpio_pin(SCL); //SCL line goes high
mDataW = mDataW << 1;
cpu_delay_cy(DELAY_ELEMENT);
gpio_clr_gpio_pin(SCL);
}//end for(x)- exits after the 8th Clock Cycle
//The Slave indicates proper reception of a command by pulling the DATA
//pin LOW (ACK bit) after the FALLING Edge of the 8th SCK
//data line released
gpio_enable_gpio_pin(SDA); //Set SDA as an INPUT
cpu_delay_cy(DELAY_ELEMENT);
gpio_set_gpio_pin(SCL); //SCL line goes high
cpu_delay_cy(DELAY_ELEMENT);
try{
error = (unsigned char)gpio_get_pin_value(SDA); //Release when SDA line goes LOW - ACKNOWLEDGE
//ACKNOWLEDGE is a LOW SIGNAL.
if (error){
throw "Communication Error: No Return Acknowledge!";
}
};
gpio_clr_gpio_pin(SCL); //Bring SCL LOW
cpu_delay_cy(DELAY_ELEMENT);
gpio_set_gpio_pin(SDA); //Set SDA as an OUTPUT
}//end I2C_Write ()
Corresponding .CPP file (portion) to Return By Value
Code:
//BASIC Read a byte, followed by an ACK to read more data or a NACK to
//end transmission.
//INPUT: Condition = ACK or NACK. Use NACK if acquiring last byte. This will
//be followed by a Stop Condition.
//OUTPUT: byte of data.
unsigned char I2C::I2C_Read (unsigned char Condition){
mDataR = 0x00;
SetCondition(Condition); //mCondition = Condition;
gpio_enable_gpio_pin(SDA); //Set SDA as an INPUT
for ( unsigned char x = 0; x < 8; x++ ){
gpio_set_gpio_pin(SCL); //SCL line goes high
cpu_delay_cy(DELAY_ELEMENT);
mDataR = mDataR << 1;
if((unsigned char)gpio_get_pin_value(SDA)) mDataR++;
cpu_delay_cy(DELAY_ELEMENT);
gpio_clr_gpio_pin(SCL); //Bring SCL LOW
cpu_delay_cy(DELAY_ELEMENT);
}//end for()
//Generate Extra Clock for Acknowledge or Not Acknowledge
if (mCondition == ACK) {
gpio_clr_gpio_pin(SDA);}
else {
gpio_set_gpio_pin(SDA);} //Set SDA as an OUTPUT}
cpu_delay_cy(DELAY_ELEMENT);
gpio_set_gpio_pin(SCL);
cpu_delay_cy(DELAY_ELEMENT);
gpio_clr_gpio_pin(SCL);
cpu_delay_cy(DELAY_ELEMENT);
return(mDataR);
}//end I2C_Read ()