-
Notifications
You must be signed in to change notification settings - Fork 1
/
Simple_Wire.h
139 lines (124 loc) · 6.47 KB
/
Simple_Wire.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* ============================================
Simple_Wire device library code is placed under the MIT license
Copyright (c) 2022 Homer Creutz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#ifndef Simple_Wire_h
#define Simple_Wire_h
#include "Arduino.h"
#include <Wire.h>
#ifndef WIRE_BUFFER_LENGTH
#if defined(I2C_BUFFER_LENGTH)
// Arduino ESP32 core Wire uses this
#define WIRE_BUFFER_LENGTH I2C_BUFFER_LENGTH
#elif defined(BUFFER_LENGTH)
// Arduino AVR core Wire and many others use this
#define WIRE_BUFFER_LENGTH BUFFER_LENGTH
#elif defined(SERIAL_BUFFER_SIZE)
// Arduino SAMD core Wire uses this
#define WIRE_BUFFER_LENGTH SERIAL_BUFFER_SIZE
#else
// should be a safe fallback, though possibly inefficient
#define WIRE_BUFFER_LENGTH 32
#endif
#endif
class Simple_Wire{
public:
uint8_t devAddr;
int32_t Val;
uint8_t ErrorMessage = 0;
/*
0 Success
1 Data to long to fit into transmit buffer
2 Received NACK on transmission of address
3 Received NACK on transmission of data
4 Other Error
5 Timeout
*/
Simple_Wire();
// Simple_Wire(int sdaPin , int sclPin );
void begin(int sdaPin = 0, int sclPin = 1);
Simple_Wire & SetAddress(uint8_t address);
Simple_Wire & SetIntMSBPos(bool FirstRead); // is the most Significant Bit Red First?
// read functions
Simple_Wire & ReadBit(uint8_t regAddr, uint8_t length, uint8_t bitNum, uint8_t *data);
Simple_Wire & ReadBit(uint8_t AltAddress,uint8_t regAddr, uint8_t length, uint8_t bitNum, uint8_t *data);
Simple_Wire & ReadByte(uint8_t regAddr, uint8_t *Data);
Simple_Wire & ReadByte(uint8_t AltAddress,uint8_t regAddr, uint8_t *Data);
Simple_Wire & ReadBytes(uint8_t regAddr, uint8_t length, uint8_t *Data);
Simple_Wire & ReadBytes(uint8_t AltAddress,uint8_t regAddr, uint8_t length, uint8_t *Data);
//Signed Ints
Simple_Wire & ReadInt(uint8_t regAddr, int16_t *data);
Simple_Wire & ReadInt(uint8_t AltAddress,uint8_t regAddr, int16_t *data);
Simple_Wire & ReadInts(uint8_t regAddr, uint8_t size, int16_t *Data);
Simple_Wire & ReadInts(uint8_t AltAddress,uint8_t regAddr, uint8_t size, int16_t *Data);
//Un-Signed Ints
Simple_Wire & ReadUInt(uint8_t regAddr, uint16_t *data);
Simple_Wire & ReadUInt(uint8_t AltAddress,uint8_t regAddr, uint16_t *data);
Simple_Wire & ReadUInts(uint8_t regAddr, uint8_t size, uint16_t *Data);
Simple_Wire & ReadUInts(uint8_t AltAddress,uint8_t regAddr, uint8_t size, uint16_t *Data);
// write functions
Simple_Wire & WriteBit(uint8_t regAddr, uint8_t length, uint8_t bitNum, uint8_t Val);
Simple_Wire & WriteBitX(uint8_t regAddr, uint8_t length, uint8_t bitNum, uint8_t Val);
Simple_Wire & WriteBit(uint8_t AltAddress,uint8_t regAddr, uint8_t length, uint8_t bitNum, uint8_t Val);
Simple_Wire & WriteBitX(uint8_t AltAddress,uint8_t regAddr, uint8_t length, uint8_t bitNum, uint8_t Val);
Simple_Wire & WriteBitSkip(uint8_t AltAddress,uint8_t regAddr, uint8_t length, uint8_t bitNum, bool SkipRead, uint8_t Val);
Simple_Wire & WriteByte(uint8_t regAddr, uint8_t Val);
Simple_Wire & WriteByte(uint8_t AltAddress,uint8_t regAddr, uint8_t Val);
Simple_Wire & WriteBytes(uint8_t regAddr, uint8_t length, uint8_t *Data);
Simple_Wire & WriteBytes(uint8_t AltAddress,uint8_t regAddr, uint8_t length, uint8_t *Data);
//Signed Ints
Simple_Wire & WriteInt(uint8_t regAddr, int16_t Val);
Simple_Wire & WriteInt(uint8_t AltAddress,uint8_t regAddr, int16_t Val);
Simple_Wire & WriteInts(uint8_t regAddr, uint8_t size, int16_t *data);
Simple_Wire & WriteInts(uint8_t AltAddress,uint8_t regAddr, uint8_t size, int16_t *data);
//Un-Signed Ints
Simple_Wire & WriteUInt(uint8_t regAddr, uint16_t Val);
Simple_Wire & WriteUInt(uint8_t AltAddress,uint8_t regAddr, uint16_t Val);
Simple_Wire & WriteUInts(uint8_t regAddr, uint8_t size, uint16_t *data);
Simple_Wire & WriteUInts(uint8_t AltAddress,uint8_t regAddr, uint8_t size, uint16_t *data);
// check fundtions
uint8_t CheckAddress(){return(devAddr);}; // deprecated
uint8_t GetAddress(){return(devAddr);};
uint8_t ReadCount() {return(I2CReadCount);};
uint8_t WriteCount() {return(I2CWriteCount);};
bool ReadSuccess() {return(I2CReadCount>0);};
bool WriteSucess() {return(I2CWriteCount>0);};
// Helper functions
Simple_Wire & I2C_Scanner();
uint8_t Find_Address(uint8_t Address,uint8_t Limit );
uint8_t Check_Address(uint8_t Address);
uint8_t Value(uint8_t * V){return(V[0]);};
int8_t Value(int8_t * V){return(V[0]);};
uint16_t Value(uint16_t * V){return(V[0]);};
int16_t Value(int16_t * V){return(V[0]);};
int32_t Value(){return(Val);};
uint8_t GetErrorMessage() {return ErrorMessage;};
bool Success() {return(ErrorMessage == 0);}
Simple_Wire & Delay( uint32_t ms ){delay(ms); return *this;};
private:
uint8_t FirstByteShift = 8;
uint8_t SecondByteShift = 0;
uint8_t I2CReadCount;
uint8_t I2CWriteCount;
uint8_t _sdaPin;
uint8_t _sclPin;
};
extern TwoWire Wire;
extern TwoWire Wire1;
#endif