uint8_t USI_TWI::requestFrom(uint8_t slaveAddr, uint8_t numBytes){ // setup for receiving from slave bool xferOK = false; uint8_t errorCode = 0; USI_LastRead = 0; USI_BytesAvail = numBytes; // save this off in a global numBytes++; // add extra byte to transmit header USI_Buf[0] = (slaveAddr<<TWI_ADR_BITS) | USI_RCVE; // setup address & Rcve bit xferOK = USI_TWI_Start_Read_Write(USI_Buf,numBytes); // core func that does the work // USI_Buf now holds the data read if (xferOK) return 0; else { // there was an error errorCode = USI_TWI_Get_State_Info(); // this function returns the error number return errorCode; } } uint8_t USI_TWI::read(){ // returns the bytes received one at a time USI_LastRead++; // inc first since first uint8_t read is in USI_Buf[1] return USI_Buf[USI_LastRead]; } uint8_t USI_TWI::available(){ // the bytes available that haven't been read yet return USI_BytesAvail - (USI_LastRead); } // Preinstantiate Objects ////////////////////////////////////////////////////// USI_TWI TinyWireM = USI_TWI();
USI_Buf[0] = (slaveAddr<<TWI_ADR_BITS) | USI_RCVE; // setup address & Rcve bit xferOK = USI_TWI_Start_Read_Write(USI_Buf,numBytes); // core func that does the work // USI_Buf now holds the data read if (xferOK) { errorCode = USI_TWI_Master_Stop(); if (errorCode == 0) { errorCode = USI_TWI_Get_State_Info(); return errorCode; } return 0; } else { // there was an error errorCode = USI_TWI_Get_State_Info(); // this function returns the error number return errorCode; } } uint8_t USI_TWI::read(){ // returns the bytes received one at a time USI_LastRead++; // inc first since first uint8_t read is in USI_Buf[1] return USI_Buf[USI_LastRead]; } int USI_TWI::available(){ // the bytes available that haven't been read yet return USI_BytesAvail - (USI_LastRead); } // Preinstantiate Objects ////////////////////////////////////////////////////// USI_TWI Wire = USI_TWI();