mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-27 21:54:30 +01:00
Changing Wire API to inherit from Stream.
Renaming send() to write(), receive() to read(), etc.
This commit is contained in:
parent
b6ff5d58df
commit
9f412a2628
@ -124,7 +124,7 @@ uint8_t TwoWire::endTransmission(void)
|
||||
// must be called in:
|
||||
// slave tx event callback
|
||||
// or after beginTransmission(address)
|
||||
void TwoWire::send(uint8_t data)
|
||||
void TwoWire::write(uint8_t data)
|
||||
{
|
||||
if(transmitting){
|
||||
// in master transmitter mode
|
||||
@ -147,12 +147,12 @@ void TwoWire::send(uint8_t data)
|
||||
// must be called in:
|
||||
// slave tx event callback
|
||||
// or after beginTransmission(address)
|
||||
void TwoWire::send(uint8_t* data, uint8_t quantity)
|
||||
void TwoWire::write(const uint8_t *data, size_t quantity)
|
||||
{
|
||||
if(transmitting){
|
||||
// in master transmitter mode
|
||||
for(uint8_t i = 0; i < quantity; ++i){
|
||||
send(data[i]);
|
||||
for(size_t i = 0; i < quantity; ++i){
|
||||
write(data[i]);
|
||||
}
|
||||
}else{
|
||||
// in slave send mode
|
||||
@ -164,23 +164,15 @@ void TwoWire::send(uint8_t* data, uint8_t quantity)
|
||||
// must be called in:
|
||||
// slave tx event callback
|
||||
// or after beginTransmission(address)
|
||||
void TwoWire::send(char* data)
|
||||
void TwoWire::write(const char *data)
|
||||
{
|
||||
send((uint8_t*)data, strlen(data));
|
||||
}
|
||||
|
||||
// must be called in:
|
||||
// slave tx event callback
|
||||
// or after beginTransmission(address)
|
||||
void TwoWire::send(int data)
|
||||
{
|
||||
send((uint8_t)data);
|
||||
write((uint8_t*)data, strlen(data));
|
||||
}
|
||||
|
||||
// must be called in:
|
||||
// slave rx event callback
|
||||
// or after requestFrom(address, numBytes)
|
||||
uint8_t TwoWire::available(void)
|
||||
int TwoWire::available(void)
|
||||
{
|
||||
return rxBufferLength - rxBufferIndex;
|
||||
}
|
||||
@ -188,11 +180,9 @@ uint8_t TwoWire::available(void)
|
||||
// must be called in:
|
||||
// slave rx event callback
|
||||
// or after requestFrom(address, numBytes)
|
||||
uint8_t TwoWire::receive(void)
|
||||
int TwoWire::read(void)
|
||||
{
|
||||
// default to returning null char
|
||||
// for people using with char strings
|
||||
uint8_t value = '\0';
|
||||
int value = -1;
|
||||
|
||||
// get each successive byte on each call
|
||||
if(rxBufferIndex < rxBufferLength){
|
||||
@ -203,6 +193,25 @@ uint8_t TwoWire::receive(void)
|
||||
return value;
|
||||
}
|
||||
|
||||
// must be called in:
|
||||
// slave rx event callback
|
||||
// or after requestFrom(address, numBytes)
|
||||
int TwoWire::peek(void)
|
||||
{
|
||||
int value = -1;
|
||||
|
||||
if(rxBufferIndex < rxBufferLength){
|
||||
value = rxBuffer[rxBufferIndex];
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
void TwoWire::flush(void)
|
||||
{
|
||||
// XXX: to be implemented.
|
||||
}
|
||||
|
||||
// behind the scenes function that is called when data is received
|
||||
void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
|
||||
{
|
||||
|
@ -21,10 +21,11 @@
|
||||
#define TwoWire_h
|
||||
|
||||
#include <inttypes.h>
|
||||
#include "Stream.h"
|
||||
|
||||
#define BUFFER_LENGTH 32
|
||||
|
||||
class TwoWire
|
||||
class TwoWire : public Stream
|
||||
{
|
||||
private:
|
||||
static uint8_t rxBuffer[];
|
||||
@ -51,12 +52,13 @@ class TwoWire
|
||||
uint8_t endTransmission(void);
|
||||
uint8_t requestFrom(uint8_t, uint8_t);
|
||||
uint8_t requestFrom(int, int);
|
||||
void send(uint8_t);
|
||||
void send(uint8_t*, uint8_t);
|
||||
void send(int);
|
||||
void send(char*);
|
||||
uint8_t available(void);
|
||||
uint8_t receive(void);
|
||||
virtual void write(uint8_t);
|
||||
virtual void write(const char *);
|
||||
virtual void 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) );
|
||||
};
|
||||
|
@ -26,8 +26,8 @@ void loop()
|
||||
Wire.beginTransmission(112); // transmit to device #112 (0x70)
|
||||
// the address specified in the datasheet is 224 (0xE0)
|
||||
// but i2c adressing uses the high 7 bits so it's 112
|
||||
Wire.send(0x00); // sets register pointer to the command register (0x00)
|
||||
Wire.send(0x50); // command sensor to measure in "inches" (0x50)
|
||||
Wire.write(byte(0x00)); // sets register pointer to the command register (0x00)
|
||||
Wire.write(byte(0x50)); // command sensor to measure in "inches" (0x50)
|
||||
// use 0x51 for centimeters
|
||||
// use 0x52 for ping microseconds
|
||||
Wire.endTransmission(); // stop transmitting
|
||||
@ -37,7 +37,7 @@ void loop()
|
||||
|
||||
// step 3: instruct sensor to return a particular echo reading
|
||||
Wire.beginTransmission(112); // transmit to device #112
|
||||
Wire.send(0x02); // sets register pointer to echo #1 register (0x02)
|
||||
Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02)
|
||||
Wire.endTransmission(); // stop transmitting
|
||||
|
||||
// step 4: request reading from sensor
|
||||
@ -46,9 +46,9 @@ void loop()
|
||||
// step 5: receive reading from sensor
|
||||
if(2 <= Wire.available()) // if two bytes were received
|
||||
{
|
||||
reading = Wire.receive(); // receive high byte (overwrites previous reading)
|
||||
reading = Wire.read(); // receive high byte (overwrites previous reading)
|
||||
reading = reading << 8; // shift high byte to be high 8 bits
|
||||
reading |= Wire.receive(); // receive low byte as lower 8 bits
|
||||
reading |= Wire.read(); // receive low byte as lower 8 bits
|
||||
Serial.println(reading); // print the reading
|
||||
}
|
||||
|
||||
@ -64,23 +64,23 @@ void loop()
|
||||
void changeAddress(byte oldAddress, byte newAddress)
|
||||
{
|
||||
Wire.beginTransmission(oldAddress);
|
||||
Wire.send(0x00);
|
||||
Wire.send(0xA0);
|
||||
Wire.write(byte(0x00));
|
||||
Wire.write(byte(0xA0));
|
||||
Wire.endTransmission();
|
||||
|
||||
Wire.beginTransmission(oldAddress);
|
||||
Wire.send(0x00);
|
||||
Wire.send(0xAA);
|
||||
Wire.write(byte(0x00));
|
||||
Wire.write(byte(0xAA));
|
||||
Wire.endTransmission();
|
||||
|
||||
Wire.beginTransmission(oldAddress);
|
||||
Wire.send(0x00);
|
||||
Wire.send(0xA5);
|
||||
Wire.write(byte(0x00));
|
||||
Wire.write(byte(0xA5));
|
||||
Wire.endTransmission();
|
||||
|
||||
Wire.beginTransmission(oldAddress);
|
||||
Wire.send(0x00);
|
||||
Wire.send(newAddress);
|
||||
Wire.write(byte(0x00));
|
||||
Wire.write(newAddress);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
|
@ -25,8 +25,8 @@ void loop()
|
||||
{
|
||||
Wire.beginTransmission(44); // transmit to device #44 (0x2c)
|
||||
// device address is specified in datasheet
|
||||
Wire.send(0x00); // sends instruction byte
|
||||
Wire.send(val); // sends potentiometer value byte
|
||||
Wire.write(byte(0x00)); // sends instruction byte
|
||||
Wire.write(val); // sends potentiometer value byte
|
||||
Wire.endTransmission(); // stop transmitting
|
||||
|
||||
val++; // increment value
|
||||
|
@ -24,7 +24,7 @@ void loop()
|
||||
|
||||
while(Wire.available()) // slave may send less than requested
|
||||
{
|
||||
char c = Wire.receive(); // receive a byte as character
|
||||
char c = Wire.read(); // receive a byte as character
|
||||
Serial.print(c); // print the character
|
||||
}
|
||||
|
||||
|
@ -22,8 +22,8 @@ byte x = 0;
|
||||
void loop()
|
||||
{
|
||||
Wire.beginTransmission(4); // transmit to device #4
|
||||
Wire.send("x is "); // sends five bytes
|
||||
Wire.send(x); // sends one byte
|
||||
Wire.write("x is "); // sends five bytes
|
||||
Wire.write(x); // sends one byte
|
||||
Wire.endTransmission(); // stop transmitting
|
||||
|
||||
x++;
|
||||
|
@ -30,9 +30,9 @@ void receiveEvent(int howMany)
|
||||
{
|
||||
while(1 < Wire.available()) // loop through all but the last
|
||||
{
|
||||
char c = Wire.receive(); // receive byte as a character
|
||||
char c = Wire.read(); // receive byte as a character
|
||||
Serial.print(c); // print the character
|
||||
}
|
||||
int x = Wire.receive(); // receive byte as an integer
|
||||
int x = Wire.read(); // receive byte as an integer
|
||||
Serial.println(x); // print the integer
|
||||
}
|
||||
|
@ -27,6 +27,6 @@ void loop()
|
||||
// this function is registered as an event, see setup()
|
||||
void requestEvent()
|
||||
{
|
||||
Wire.send("hello "); // respond with message of 6 bytes
|
||||
Wire.write("hello "); // respond with message of 6 bytes
|
||||
// as expected by master
|
||||
}
|
||||
|
@ -232,7 +232,7 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
|
||||
* 2 not slave transmitter
|
||||
* 0 ok
|
||||
*/
|
||||
uint8_t twi_transmit(uint8_t* data, uint8_t length)
|
||||
uint8_t twi_transmit(const uint8_t* data, uint8_t length)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
|
@ -46,7 +46,7 @@
|
||||
void twi_setAddress(uint8_t);
|
||||
uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t);
|
||||
uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t);
|
||||
uint8_t twi_transmit(uint8_t*, uint8_t);
|
||||
uint8_t twi_transmit(const uint8_t*, uint8_t);
|
||||
void twi_attachSlaveRxEvent( void (*)(uint8_t*, int) );
|
||||
void twi_attachSlaveTxEvent( void (*)(void) );
|
||||
void twi_reply(uint8_t);
|
||||
|
Loading…
x
Reference in New Issue
Block a user