Wire.h和Wire.c代码
时间: 2024-04-29 19:22:03 浏览: 104
Wire.h和Wire.c是Arduino中用于I2C通信的库文件,其中Wire.h是头文件,包含了I2C通信所需要的常量和函数的声明,而Wire.c是源文件,包含了I2C通信所需要的具体实现代码。
以下是Wire.h的代码示例:
```
#ifndef TwoWire_h
#define TwoWire_h
#include <inttypes.h>
#define BUFFER_LENGTH 32
class TwoWire {
public:
TwoWire();
void begin();
void begin(uint8_t);
void begin(int);
void end();
void setClock(uint32_t);
void setClockStretchLimit(uint32_t);
void beginTransmission(uint8_t);
void beginTransmission(int);
uint8_t endTransmission(void);
uint8_t endTransmission(uint8_t);
uint8_t requestFrom(uint8_t, uint8_t);
uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
uint8_t requestFrom(int, int);
uint8_t requestFrom(int, int, int);
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *, size_t);
virtual int available(void);
virtual int read(void);
virtual int peek(void);
virtual void flush(void);
void onReceive(void(*)(int));
void onRequest(void(*)(void));
void onService(void);
private:
static uint8_t rxBuffer[];
static uint8_t rxBufferIndex;
static uint8_t rxBufferLength;
static uint8_t txAddress;
static uint8_t txBuffer[];
static uint8_t txBufferIndex;
static uint8_t txBufferLength;
static uint8_t transmitting;
static void (*user_onRequest)(void);
static void (*user_onReceive)(int);
static void onRequestService(void);
static void onReceiveService(uint8_t*, int);
};
extern TwoWire Wire;
#endif
```
以下是Wire.c的代码示例:
```
#include "Wire.h"
#include <util/twi.h>
TwoWire Wire = TwoWire();
static void (*user_onRequest)(void);
static void (*user_onReceive)(int);
void onRequestService(void);
void onReceiveService(uint8_t*, int);
TwoWire::TwoWire() {
rxBufferIndex = 0;
rxBufferLength = 0;
txBufferIndex = 0;
txBufferLength = 0;
transmitting = 0;
user_onRequest = NULL;
user_onReceive = NULL;
}
void TwoWire::begin() {
twi_init();
}
void TwoWire::begin(uint8_t address) {
twi_setAddress(address);
twi_attachSlaveTxEvent(onRequestService);
twi_attachSlaveRxEvent(onReceiveService);
twi_init();
}
void TwoWire::begin(int address) {
begin((uint8_t)address);
}
void TwoWire::end() {
twi_disable();
}
void TwoWire::setClock(uint32_t frequency) {
twi_setFrequency(frequency);
}
void TwoWire::setClockStretchLimit(uint32_t limit) {
twi_setClockStretchLimit(limit);
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) {
if(quantity > BUFFER_LENGTH){
quantity = BUFFER_LENGTH;
}
rxBufferLength = quantity;
rxBufferIndex = 0;
twi_readFrom(address, rxBuffer, quantity);
return quantity;
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) {
twi_setStop(sendStop);
return requestFrom(address, quantity);
}
uint8_t TwoWire::requestFrom(int address, int quantity) {
return requestFrom((uint8_t)address, (uint8_t)quantity);
}
uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop) {
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
}
void TwoWire::beginTransmission(uint8_t address) {
txAddress = address;
txBufferLength = 0;
transmitting = 1;
}
void TwoWire::beginTransmission(int address) {
beginTransmission((uint8_t)address);
}
uint8_t TwoWire::endTransmission() {
return endTransmission(true);
}
uint8_t TwoWire::endTransmission(uint8_t sendStop) {
uint8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1, sendStop);
txBufferLength = 0;
transmitting = 0;
return ret;
}
size_t TwoWire::write(uint8_t data) {
if(transmitting){
if(txBufferLength >= BUFFER_LENGTH){
return 0;
}
txBuffer[txBufferLength++] = data;
return 1;
}else{
return 0;
}
}
size_t TwoWire::write(const uint8_t *data, size_t quantity) {
if(transmitting){
for(size_t i = 0; i < quantity; ++i){
if(txBufferLength >= BUFFER_LENGTH){
return i;
}
txBuffer[txBufferLength++] = data[i];
}
return quantity;
}else{
return 0;
}
}
int TwoWire::available(void) {
return rxBufferLength - rxBufferIndex;
}
int TwoWire::read(void) {
int value = -1;
if(rxBufferIndex < rxBufferLength){
value = rxBuffer[rxBufferIndex];
++rxBufferIndex;
}
return value;
}
int TwoWire::peek(void) {
int value = -1;
if(rxBufferIndex < rxBufferLength){
value = rxBuffer[rxBufferIndex];
}
return value;
}
void TwoWire::flush(void) {
// XXX: to be implemented.
}
void TwoWire::onReceive(void(*function)(int)) {
user_onReceive = function;
}
void TwoWire::onRequest(void(*function)(void)) {
user_onRequest = function;
}
void TwoWire::onService(void) {
if(twi_isAddressed()){
// onRequest callback
if(twi_isMasterWrite()){
if(user_onRequest){
user_onRequest();
}
}
// onReceive callback
else {
if(user_onReceive){
user_onReceive(twi_receiveByte());
}
}
}
}
void onRequestService(void) {
if(Wire.user_onRequest){
Wire.user_onRequest();
}
}
void onReceiveService(uint8_t* inBytes, int numBytes) {
Wire.rxBuffer = inBytes;
Wire.rxBufferIndex = 0;
Wire.rxBufferLength = numBytes;
if(Wire.user_onReceive){
Wire.user_onReceive(numBytes);
}
}
```
阅读全文