Wire.h
Go to the documentation of this file.
1 /*
2  TwoWire.h - TWI/I2C library for Arduino & Wiring
3  Copyright (c) 2006 Nicholas Zambetti. All right reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19  Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
20  Modified December 2014 by Ivan Grokhotkov (ivan@esp8266.com) - esp8266 support
21  Modified April 2015 by Hrsto Gochkov (ficeto@ficeto.com) - alternative esp8266 support
22 */
23 
24 #pragma once
25 
26 #include <inttypes.h>
27 #include "Stream.h"
28 
29 #define BUFFER_LENGTH 32
30 
31 class TwoWire : public Stream
32 {
33 public:
34  TwoWire();
35 
36  void begin(int sda, int scl);
37  void pins(int sda, int scl);
38  void begin();
39  void begin(uint8_t);
40  void begin(int);
41  void setClock(uint32_t);
42  void setClockStretchLimit(uint32_t);
43  void beginTransmission(uint8_t);
44  void beginTransmission(int);
45  uint8_t endTransmission();
46  uint8_t endTransmission(uint8_t);
47  size_t requestFrom(uint8_t address, size_t size, bool sendStop);
48  uint8_t status();
49 
50  uint8_t requestFrom(uint8_t, uint8_t);
51  uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
52  uint8_t requestFrom(int, int);
53  uint8_t requestFrom(int, int, int);
54 
55  size_t write(uint8_t) override;
56  size_t write(const uint8_t*, size_t) override;
57  int available() override;
58  int read() override;
59  int peek() override;
60  void flush() override;
61 
62  void onReceive(void (*)(int));
63  void onRequest(void (*)());
64 
65  size_t write(unsigned long n)
66  {
67  return write(uint8_t(n));
68  }
69 
70  size_t write(long n)
71  {
72  return write(uint8_t(n));
73  }
74 
75  size_t write(unsigned int n)
76  {
77  return write(uint8_t(n));
78  }
79 
80  size_t write(int n)
81  {
82  return write(uint8_t(n));
83  }
84 
85  using Print::write;
86 
87 private:
88  static uint8_t rxBuffer[];
89  static uint8_t rxBufferIndex;
90  static uint8_t rxBufferLength;
91 
92  static uint8_t txAddress;
93  static uint8_t txBuffer[];
94  static uint8_t txBufferIndex;
95  static uint8_t txBufferLength;
96 
97  static uint8_t transmitting;
98  static void (*user_onRequest)();
99  static void (*user_onReceive)(int);
100  static void onRequestService();
101  static void onReceiveService(uint8_t*, int);
102 };
103 
104 #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_TWOWIRE)
105 extern TwoWire Wire;
106 #endif
int read() override
size_t write(unsigned long n)
Definition: Wire.h:65
virtual size_t write(uint8_t)=0
Writes a single character to output stream.
void pins(int sda, int scl)
void begin()
TwoWire Wire
void setClockStretchLimit(uint32_t)
size_t write(int n)
Definition: Wire.h:80
Definition: Wire.h:31
uint8_t status()
void onRequest(void(*)())
uint8_t endTransmission()
size_t write(unsigned int n)
Definition: Wire.h:75
void setClock(uint32_t)
size_t requestFrom(uint8_t address, size_t size, bool sendStop)
void beginTransmission(uint8_t)
void onReceive(void(*)(int))
size_t write(uint8_t) override
Writes a single character to output stream.
void flush() override
int peek() override
int available() override
size_t write(long n)
Definition: Wire.h:70
Base Stream class.
Definition: Stream.h:32