@@ -37,6 +37,9 @@ USI_TWI::USI_TWI(){
3737
3838// Public Methods //////////////////////////////////////////////////////////////
3939
40+ // int USI_TWI::peek(){}
41+ // void USI_TWI::flush(){}
42+
4043void USI_TWI::begin (){ // initialize I2C lib
4144 USI_TWI_Master_Initialise ();
4245}
@@ -46,18 +49,32 @@ void USI_TWI::beginTransmission(uint8_t slaveAddr){ // setup address & write bit
4649 USI_Buf[USI_BufIdx] = (slaveAddr<<TWI_ADR_BITS) | USI_SEND;
4750}
4851
49- void USI_TWI::send (uint8_t data){ // buffers up data to send
50- if (USI_BufIdx >= USI_BUF_SIZE) return ; // dont blow out the buffer
52+ size_t USI_TWI::write (uint8_t data){ // buffers up data to send
53+ if (USI_BufIdx >= USI_BUF_SIZE) return 0 ; // dont blow out the buffer
5154 USI_BufIdx++; // inc for next byte in buffer
5255 USI_Buf[USI_BufIdx] = data;
56+ return 1 ;
57+ }
58+
59+ uint8_t USI_TWI::endTransmission () {
60+ endTransmission (1 );
5361}
5462
55- uint8_t USI_TWI::endTransmission (){ // actually sends the buffer
63+ uint8_t USI_TWI::endTransmission (uint8_t stop ){ // actually sends the buffer
5664 bool xferOK = false ;
5765 uint8_t errorCode = 0 ;
5866 xferOK = USI_TWI_Start_Read_Write (USI_Buf,USI_BufIdx+1 ); // core func that does the work
5967 USI_BufIdx = 0 ;
60- if (xferOK) return 0 ;
68+ if (xferOK) {
69+ if (stop) {
70+ errorCode = USI_TWI_Master_Stop ();
71+ if (errorCode == 0 ) {
72+ errorCode = USI_TWI_Get_State_Info ();
73+ return errorCode;
74+ }
75+ }
76+ return 0 ;
77+ }
6178 else { // there was an error
6279 errorCode = USI_TWI_Get_State_Info (); // this function returns the error number
6380 return errorCode;
@@ -73,19 +90,26 @@ uint8_t USI_TWI::requestFrom(uint8_t slaveAddr, uint8_t numBytes){ // setup for
7390 USI_Buf[0 ] = (slaveAddr<<TWI_ADR_BITS) | USI_RCVE; // setup address & Rcve bit
7491 xferOK = USI_TWI_Start_Read_Write (USI_Buf,numBytes); // core func that does the work
7592 // USI_Buf now holds the data read
76- if (xferOK) return 0 ;
93+ if (xferOK) {
94+ errorCode = USI_TWI_Master_Stop ();
95+ if (errorCode == 0 ) {
96+ errorCode = USI_TWI_Get_State_Info ();
97+ return errorCode;
98+ }
99+ return 0 ;
100+ }
77101 else { // there was an error
78102 errorCode = USI_TWI_Get_State_Info (); // this function returns the error number
79103 return errorCode;
80104 }
81105}
82106
83- uint8_t USI_TWI::receive (){ // returns the bytes received one at a time
107+ int USI_TWI::read (){ // returns the bytes received one at a time
84108 USI_LastRead++; // inc first since first uint8_t read is in USI_Buf[1]
85109 return USI_Buf[USI_LastRead];
86110}
87111
88- uint8_t USI_TWI::available (){ // the bytes available that haven't been read yet
112+ int USI_TWI::available (){ // the bytes available that haven't been read yet
89113 return USI_BytesAvail - (USI_LastRead);
90114}
91115
0 commit comments