D:/DRISSI/arduino-0022/arduino-0022/libraries/Wire/Wire.cpp
00001 /*
00002   TwoWire.cpp - TWI/I2C library for Wiring & Arduino
00003   Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009 
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014 
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the Free Software
00017   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00020 extern "C" {
00021   #include <stdlib.h>
00022   #include <string.h>
00023   #include <inttypes.h>
00024   #include "twi.h"
00025 }
00026 
00027 #include "Wire.h"
00028 
00029 // Initialize Class Variables //////////////////////////////////////////////////
00030 
00031 uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
00032 uint8_t TwoWire::rxBufferIndex = 0;
00033 uint8_t TwoWire::rxBufferLength = 0;
00034 
00035 uint8_t TwoWire::txAddress = 0;
00036 uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
00037 uint8_t TwoWire::txBufferIndex = 0;
00038 uint8_t TwoWire::txBufferLength = 0;
00039 
00040 uint8_t TwoWire::transmitting = 0;
00041 void (*TwoWire::user_onRequest)(void);
00042 void (*TwoWire::user_onReceive)(int);
00043 
00044 // Constructors ////////////////////////////////////////////////////////////////
00045 
00046 TwoWire::TwoWire()
00047 {
00048 }
00049 
00050 // Public Methods //////////////////////////////////////////////////////////////
00051 
00052 void TwoWire::begin(void)
00053 {
00054   rxBufferIndex = 0;
00055   rxBufferLength = 0;
00056 
00057   txBufferIndex = 0;
00058   txBufferLength = 0;
00059 
00060   twi_init();
00061 }
00062 
00063 void TwoWire::begin(uint8_t address)
00064 {
00065   twi_setAddress(address);
00066   twi_attachSlaveTxEvent(onRequestService);
00067   twi_attachSlaveRxEvent(onReceiveService);
00068   begin();
00069 }
00070 
00071 void TwoWire::begin(int address)
00072 {
00073   begin((uint8_t)address);
00074 }
00075 
00076 uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
00077 {
00078   // clamp to buffer length
00079   if(quantity > BUFFER_LENGTH){
00080     quantity = BUFFER_LENGTH;
00081   }
00082   // perform blocking read into buffer
00083   uint8_t read = twi_readFrom(address, rxBuffer, quantity);
00084   // set rx buffer iterator vars
00085   rxBufferIndex = 0;
00086   rxBufferLength = read;
00087 
00088   return read;
00089 }
00090 
00091 uint8_t TwoWire::requestFrom(int address, int quantity)
00092 {
00093   return requestFrom((uint8_t)address, (uint8_t)quantity);
00094 }
00095 
00096 void TwoWire::beginTransmission(uint8_t address)
00097 {
00098   // indicate that we are transmitting
00099   transmitting = 1;
00100   // set address of targeted slave
00101   txAddress = address;
00102   // reset tx buffer iterator vars
00103   txBufferIndex = 0;
00104   txBufferLength = 0;
00105 }
00106 
00107 void TwoWire::beginTransmission(int address)
00108 {
00109   beginTransmission((uint8_t)address);
00110 }
00111 
00112 uint8_t TwoWire::endTransmission(void)
00113 {
00114   // transmit buffer (blocking)
00115   int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1);
00116   // reset tx buffer iterator vars
00117   txBufferIndex = 0;
00118   txBufferLength = 0;
00119   // indicate that we are done transmitting
00120   transmitting = 0;
00121   return ret;
00122 }
00123 
00124 // must be called in:
00125 // slave tx event callback
00126 // or after beginTransmission(address)
00127 void TwoWire::send(uint8_t data)
00128 {
00129   if(transmitting){
00130   // in master transmitter mode
00131     // don't bother if buffer is full
00132     if(txBufferLength >= BUFFER_LENGTH){
00133       return;
00134     }
00135     // put byte in tx buffer
00136     txBuffer[txBufferIndex] = data;
00137     ++txBufferIndex;
00138     // update amount in buffer   
00139     txBufferLength = txBufferIndex;
00140   }else{
00141   // in slave send mode
00142     // reply to master
00143     twi_transmit(&data, 1);
00144   }
00145 }
00146 
00147 // must be called in:
00148 // slave tx event callback
00149 // or after beginTransmission(address)
00150 void TwoWire::send(uint8_t* data, uint8_t quantity)
00151 {
00152   if(transmitting){
00153   // in master transmitter mode
00154     for(uint8_t i = 0; i < quantity; ++i){
00155       send(data[i]);
00156     }
00157   }else{
00158   // in slave send mode
00159     // reply to master
00160     twi_transmit(data, quantity);
00161   }
00162 }
00163 
00164 // must be called in:
00165 // slave tx event callback
00166 // or after beginTransmission(address)
00167 void TwoWire::send(char* data)
00168 {
00169   send((uint8_t*)data, strlen(data));
00170 }
00171 
00172 // must be called in:
00173 // slave tx event callback
00174 // or after beginTransmission(address)
00175 void TwoWire::send(int data)
00176 {
00177   send((uint8_t)data);
00178 }
00179 
00180 // must be called in:
00181 // slave rx event callback
00182 // or after requestFrom(address, numBytes)
00183 uint8_t TwoWire::available(void)
00184 {
00185   return rxBufferLength - rxBufferIndex;
00186 }
00187 
00188 // must be called in:
00189 // slave rx event callback
00190 // or after requestFrom(address, numBytes)
00191 uint8_t TwoWire::receive(void)
00192 {
00193   // default to returning null char
00194   // for people using with char strings
00195   uint8_t value = '\0';
00196   
00197   // get each successive byte on each call
00198   if(rxBufferIndex < rxBufferLength){
00199     value = rxBuffer[rxBufferIndex];
00200     ++rxBufferIndex;
00201   }
00202 
00203   return value;
00204 }
00205 
00206 // behind the scenes function that is called when data is received
00207 void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
00208 {
00209   // don't bother if user hasn't registered a callback
00210   if(!user_onReceive){
00211     return;
00212   }
00213   // don't bother if rx buffer is in use by a master requestFrom() op
00214   // i know this drops data, but it allows for slight stupidity
00215   // meaning, they may not have read all the master requestFrom() data yet
00216   if(rxBufferIndex < rxBufferLength){
00217     return;
00218   }
00219   // copy twi rx buffer into local read buffer
00220   // this enables new reads to happen in parallel
00221   for(uint8_t i = 0; i < numBytes; ++i){
00222     rxBuffer[i] = inBytes[i];    
00223   }
00224   // set rx iterator vars
00225   rxBufferIndex = 0;
00226   rxBufferLength = numBytes;
00227   // alert user program
00228   user_onReceive(numBytes);
00229 }
00230 
00231 // behind the scenes function that is called when data is requested
00232 void TwoWire::onRequestService(void)
00233 {
00234   // don't bother if user hasn't registered a callback
00235   if(!user_onRequest){
00236     return;
00237   }
00238   // reset tx buffer iterator vars
00239   // !!! this will kill any pending pre-master sendTo() activity
00240   txBufferIndex = 0;
00241   txBufferLength = 0;
00242   // alert user program
00243   user_onRequest();
00244 }
00245 
00246 // sets function called on slave write
00247 void TwoWire::onReceive( void (*function)(int) )
00248 {
00249   user_onReceive = function;
00250 }
00251 
00252 // sets function called on slave read
00253 void TwoWire::onRequest( void (*function)(void) )
00254 {
00255   user_onRequest = function;
00256 }
00257 
00258 // Preinstantiate Objects //////////////////////////////////////////////////////
00259 
00260 TwoWire Wire = TwoWire();
00261