Skip to content

Commit 024e089

Browse files
committedAug 13, 2015
[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
1 parent 6e7409f commit 024e089

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed
 

‎libraries/Wire/Wire.cpp

+8-19
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,15 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
6666
rxBuffer.store_char(sercom->readDataWIRE());
6767

6868
// Connected to slave
69-
//while(toRead--)
70-
for(byteRead = 0; byteRead < quantity; ++byteRead)
69+
for (byteRead = 1; byteRead < quantity; ++byteRead)
7170
{
72-
if( byteRead == quantity - 1) // Stop transmission
73-
{
74-
sercom->prepareNackBitWIRE(); // Prepare NACK to stop slave transmission
75-
//sercom->readDataWIRE(); // Clear data register to send NACK
76-
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); // Send Stop
77-
}
78-
else // Continue transmission
79-
{
80-
sercom->prepareAckBitWIRE(); // Prepare Acknowledge
81-
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_READ); // Prepare the ACK command for the slave
82-
rxBuffer.store_char( sercom->readDataWIRE() ); // Read data and send the ACK
83-
}
71+
sercom->prepareAckBitWIRE(); // Prepare Acknowledge
72+
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_READ); // Prepare the ACK command for the slave
73+
rxBuffer.store_char(sercom->readDataWIRE()); // Read data and send the ACK
8474
}
75+
sercom->prepareNackBitWIRE(); // Prepare NACK to stop slave transmission
76+
//sercom->readDataWIRE(); // Clear data register to send NACK
77+
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); // Send Stop
8578
}
8679

8780
return byteRead;
@@ -132,12 +125,8 @@ uint8_t TwoWire::endTransmission(bool stopBit)
132125
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
133126
return 3 ; // Nack or error
134127
}
135-
136-
if(txBuffer.available() == 0)
137-
{
138-
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
139-
}
140128
}
129+
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
141130

142131
return 0;
143132
}

0 commit comments

Comments
 (0)
Please sign in to comment.