From 024e089d66e3ea439daec6e4ac431233e524b41c Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 13 Aug 2015 18:07:53 +0200 Subject: [PATCH] [Wire] simplified coding unnecessarily complex (hfvogt) In the wire library there are several functions where an unnecessarily complex coding has been used: - endTransmission: the availability of data is already checked in while(...), therefore need not be checked again in the loop. - requestFrom: the for-loop has a predefined and fixed number of loops. Therefore a check whether the last element has been reached is unnecessary and does not add any benefit. Fixes #20 --- libraries/Wire/Wire.cpp | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index aabc1eff6..10a03d012 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -66,22 +66,15 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit) rxBuffer.store_char(sercom->readDataWIRE()); // Connected to slave - //while(toRead--) - for(byteRead = 0; byteRead < quantity; ++byteRead) + for (byteRead = 1; byteRead < quantity; ++byteRead) { - if( byteRead == quantity - 1) // Stop transmission - { - sercom->prepareNackBitWIRE(); // Prepare NACK to stop slave transmission - //sercom->readDataWIRE(); // Clear data register to send NACK - sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); // Send Stop - } - else // Continue transmission - { - sercom->prepareAckBitWIRE(); // Prepare Acknowledge - sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_READ); // Prepare the ACK command for the slave - rxBuffer.store_char( sercom->readDataWIRE() ); // Read data and send the ACK - } + sercom->prepareAckBitWIRE(); // Prepare Acknowledge + sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_READ); // Prepare the ACK command for the slave + rxBuffer.store_char(sercom->readDataWIRE()); // Read data and send the ACK } + sercom->prepareNackBitWIRE(); // Prepare NACK to stop slave transmission + //sercom->readDataWIRE(); // Clear data register to send NACK + sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); // Send Stop } return byteRead; @@ -132,12 +125,8 @@ uint8_t TwoWire::endTransmission(bool stopBit) sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); return 3 ; // Nack or error } - - if(txBuffer.available() == 0) - { - sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); - } } + sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); return 0; }