updated to latest adafruit version

This commit is contained in:
Erik Tylek Kettenburg
2015-06-23 14:08:14 -07:00
parent 68ad726f9b
commit 4e2f6ffa1c
7 changed files with 88 additions and 37 deletions

View File

@@ -37,6 +37,9 @@ USI_TWI::USI_TWI(){
// Public Methods //////////////////////////////////////////////////////////////
//int USI_TWI::peek(){}
//void USI_TWI::flush(){}
void USI_TWI::begin(){ // initialize I2C lib
USI_TWI_Master_Initialise();
}
@@ -46,18 +49,32 @@ void USI_TWI::beginTransmission(uint8_t slaveAddr){ // setup address & write bit
USI_Buf[USI_BufIdx] = (slaveAddr<<TWI_ADR_BITS) | USI_SEND;
}
void USI_TWI::send(uint8_t data){ // buffers up data to send
if (USI_BufIdx >= USI_BUF_SIZE) return; // dont blow out the buffer
size_t USI_TWI::write(uint8_t data){ // buffers up data to send
if (USI_BufIdx >= USI_BUF_SIZE) return 0; // dont blow out the buffer
USI_BufIdx++; // inc for next byte in buffer
USI_Buf[USI_BufIdx] = data;
return 1;
}
uint8_t USI_TWI::endTransmission(){ // actually sends the buffer
uint8_t USI_TWI::endTransmission() {
endTransmission(1);
}
uint8_t USI_TWI::endTransmission(uint8_t stop){ // actually sends the buffer
bool xferOK = false;
uint8_t errorCode = 0;
xferOK = USI_TWI_Start_Read_Write(USI_Buf,USI_BufIdx+1); // core func that does the work
USI_BufIdx = 0;
if (xferOK) return 0;
if (xferOK) {
if (stop) {
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;
@@ -73,19 +90,26 @@ uint8_t USI_TWI::requestFrom(uint8_t slaveAddr, uint8_t numBytes){ // setup for
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;
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::receive(){ // returns the bytes received one at a time
int 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
int USI_TWI::available(){ // the bytes available that haven't been read yet
return USI_BytesAvail - (USI_LastRead);
}