1d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen/* 2d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen TwoWire.cpp - TWI/I2C library for Wiring & Arduino 3d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen Copyright (c) 2006 Nicholas Zambetti. All right reserved. 4d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 5d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen This library is free software; you can redistribute it and/or 6d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen modify it under the terms of the GNU Lesser General Public 7d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen License as published by the Free Software Foundation; either 8d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen version 2.1 of the License, or (at your option) any later version. 9d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 10d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen This library is distributed in the hope that it will be useful, 11d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen but WITHOUT ANY WARRANTY; without even the implied warranty of 12d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen Lesser General Public License for more details. 14d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 15d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen You should have received a copy of the GNU Lesser General Public 16d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen License along with this library; if not, write to the Free Software 17d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen*/ 19d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 20d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenextern "C" { 21d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen #include <stdlib.h> 22d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen #include <string.h> 23d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen #include <inttypes.h> 24d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen #include "twi.h" 25d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 26d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 27d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen#include "Wire.h" 28d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 29d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// Initialize Class Variables ////////////////////////////////////////////////// 30d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 31d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenuint8_t TwoWire::rxBuffer[BUFFER_LENGTH]; 32d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenuint8_t TwoWire::rxBufferIndex = 0; 33d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenuint8_t TwoWire::rxBufferLength = 0; 34d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 35d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenuint8_t TwoWire::txAddress = 0; 36d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenuint8_t TwoWire::txBuffer[BUFFER_LENGTH]; 37d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenuint8_t TwoWire::txBufferIndex = 0; 38d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenuint8_t TwoWire::txBufferLength = 0; 39d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 40d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenuint8_t TwoWire::transmitting = 0; 41d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid (*TwoWire::user_onRequest)(void); 42d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid (*TwoWire::user_onReceive)(int); 43d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 44d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// Constructors //////////////////////////////////////////////////////////////// 45d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 46d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. ChenTwoWire::TwoWire() 47d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 48d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 49d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 50d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// Public Methods ////////////////////////////////////////////////////////////// 51d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 52d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid TwoWire::begin(void) 53d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 54d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen rxBufferIndex = 0; 55d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen rxBufferLength = 0; 56d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 57d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen txBufferIndex = 0; 58d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen txBufferLength = 0; 59d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 60d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen twi_init(); 61d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 62d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 63d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid TwoWire::begin(uint8_t address) 64d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 65d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen twi_setAddress(address); 66d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen twi_attachSlaveTxEvent(onRequestService); 67d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen twi_attachSlaveRxEvent(onReceiveService); 68d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen begin(); 69d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 70d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 71d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid TwoWire::begin(int address) 72d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 73d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen begin((uint8_t)address); 74d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 75d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 76d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenuint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) 77d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 78d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // clamp to buffer length 79d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen if(quantity > BUFFER_LENGTH){ 80d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen quantity = BUFFER_LENGTH; 81d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen } 82d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // perform blocking read into buffer 83d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen uint8_t read = twi_readFrom(address, rxBuffer, quantity); 84d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // set rx buffer iterator vars 85d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen rxBufferIndex = 0; 86d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen rxBufferLength = read; 87d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 88d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen return read; 89d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 90d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 91d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenuint8_t TwoWire::requestFrom(int address, int quantity) 92d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 93d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen return requestFrom((uint8_t)address, (uint8_t)quantity); 94d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 95d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 96d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid TwoWire::beginTransmission(uint8_t address) 97d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 98d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // indicate that we are transmitting 99d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen transmitting = 1; 100d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // set address of targeted slave 101d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen txAddress = address; 102d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // reset tx buffer iterator vars 103d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen txBufferIndex = 0; 104d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen txBufferLength = 0; 105d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 106d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 107d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid TwoWire::beginTransmission(int address) 108d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 109d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen beginTransmission((uint8_t)address); 110d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 111d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 112d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenuint8_t TwoWire::endTransmission(void) 113d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 114d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // transmit buffer (blocking) 115d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1); 116d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // reset tx buffer iterator vars 117d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen txBufferIndex = 0; 118d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen txBufferLength = 0; 119d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // indicate that we are done transmitting 120d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen transmitting = 0; 121d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen return ret; 122d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 123d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 124d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// must be called in: 125d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// slave tx event callback 126d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// or after beginTransmission(address) 127d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid TwoWire::send(uint8_t data) 128d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 129d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen if(transmitting){ 130d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // in master transmitter mode 131d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // don't bother if buffer is full 132d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen if(txBufferLength >= BUFFER_LENGTH){ 133d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen return; 134d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen } 135d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // put byte in tx buffer 136d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen txBuffer[txBufferIndex] = data; 137d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen ++txBufferIndex; 138d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // update amount in buffer 139d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen txBufferLength = txBufferIndex; 140d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen }else{ 141d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // in slave send mode 142d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // reply to master 143d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen twi_transmit(&data, 1); 144d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen } 145d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 146d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 147d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// must be called in: 148d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// slave tx event callback 149d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// or after beginTransmission(address) 150d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid TwoWire::send(uint8_t* data, uint8_t quantity) 151d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 152d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen if(transmitting){ 153d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // in master transmitter mode 154d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen for(uint8_t i = 0; i < quantity; ++i){ 155d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen send(data[i]); 156d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen } 157d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen }else{ 158d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // in slave send mode 159d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // reply to master 160d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen twi_transmit(data, quantity); 161d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen } 162d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 163d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 164d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// must be called in: 165d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// slave tx event callback 166d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// or after beginTransmission(address) 167d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid TwoWire::send(char* data) 168d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 169d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen send((uint8_t*)data, strlen(data)); 170d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 171d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 172d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// must be called in: 173d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// slave tx event callback 174d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// or after beginTransmission(address) 175d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid TwoWire::send(int data) 176d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 177d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen send((uint8_t)data); 178d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 179d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 180d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// must be called in: 181d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// slave rx event callback 182d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// or after requestFrom(address, numBytes) 183d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenuint8_t TwoWire::available(void) 184d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 185d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen return rxBufferLength - rxBufferIndex; 186d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 187d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 188d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// must be called in: 189d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// slave rx event callback 190d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// or after requestFrom(address, numBytes) 191d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenuint8_t TwoWire::receive(void) 192d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 193d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // default to returning null char 194d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // for people using with char strings 195d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen uint8_t value = '\0'; 196d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 197d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // get each successive byte on each call 198d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen if(rxBufferIndex < rxBufferLength){ 199d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen value = rxBuffer[rxBufferIndex]; 200d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen ++rxBufferIndex; 201d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen } 202d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 203d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen return value; 204d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 205d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 206d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// behind the scenes function that is called when data is received 207d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid TwoWire::onReceiveService(uint8_t* inBytes, int numBytes) 208d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 209d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // don't bother if user hasn't registered a callback 210d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen if(!user_onReceive){ 211d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen return; 212d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen } 213d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // don't bother if rx buffer is in use by a master requestFrom() op 214d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // i know this drops data, but it allows for slight stupidity 215d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // meaning, they may not have read all the master requestFrom() data yet 216d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen if(rxBufferIndex < rxBufferLength){ 217d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen return; 218d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen } 219d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // copy twi rx buffer into local read buffer 220d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // this enables new reads to happen in parallel 221d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen for(uint8_t i = 0; i < numBytes; ++i){ 222d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen rxBuffer[i] = inBytes[i]; 223d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen } 224d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // set rx iterator vars 225d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen rxBufferIndex = 0; 226d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen rxBufferLength = numBytes; 227d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // alert user program 228d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen user_onReceive(numBytes); 229d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 230d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 231d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// behind the scenes function that is called when data is requested 232d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid TwoWire::onRequestService(void) 233d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 234d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // don't bother if user hasn't registered a callback 235d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen if(!user_onRequest){ 236d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen return; 237d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen } 238d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // reset tx buffer iterator vars 239d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // !!! this will kill any pending pre-master sendTo() activity 240d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen txBufferIndex = 0; 241d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen txBufferLength = 0; 242d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen // alert user program 243d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen user_onRequest(); 244d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 245d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 246d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// sets function called on slave write 247d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid TwoWire::onReceive( void (*function)(int) ) 248d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 249d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen user_onReceive = function; 250d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 251d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 252d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// sets function called on slave read 253d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chenvoid TwoWire::onRequest( void (*function)(void) ) 254d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen{ 255d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen user_onRequest = function; 256d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen} 257d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 258d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen// Preinstantiate Objects ////////////////////////////////////////////////////// 259d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 260d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. ChenTwoWire Wire = TwoWire(); 261d5790d78880d4bd60be277ee20e53a851aa8c11Mike J. Chen 262