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
#pragma once
#include "Arduino.h"
#include "Packet.h"
#include "Wire.h"
class I2CTransfer
{
public: // <<---------------------------------------//public
Packet packet;
static I2CTransfer* classToUse;
uint8_t bytesRead = 0;
int8_t status = 0;
I2CTransfer()
{
classToUse = this;
};
void begin(TwoWire& _port, const configST& configs);
void begin(TwoWire& _port, const bool& _debug = true, Stream& _debugPort = Serial);
uint8_t sendData(const uint16_t& messageLen, const uint8_t& packetID = 0, const uint8_t& targetAddress = 0);
uint8_t currentPacketID();
void reset();
/*
uint16_t I2CTransfer::txObj(const T &val, const uint16_t &index=0, const uint16_t &len=sizeof(T))
Description:
------------
* Stuffs "len" number of bytes of an arbitrary object (byte, int,
float, double, struct, etc...) into the transmit buffer (txBuff)
starting at the index as specified by the argument "index"
Inputs:
-------
* const T &val - Pointer to the object to be copied to the
transmit buffer (txBuff)
* const uint16_t &index - Starting index of the object within the
transmit buffer (txBuff)
* const uint16_t &len - Number of bytes of the object "val" to transmit
Return:
-------
* uint16_t maxIndex - uint16_t maxIndex - Index of the transmit buffer (txBuff) that directly follows the bytes processed
by the calling of this member function
*/
template <typename T>
uint16_t txObj(const T& val, const uint16_t& index = 0, const uint16_t& len = sizeof(T))
{
return packet.txObj(val, index, len);
}
/*
uint16_t I2CTransfer::rxObj(const T &val, const uint16_t &index=0, const uint16_t &len=sizeof(T))
Description:
------------
* Reads "len" number of bytes from the receive buffer (rxBuff)
starting at the index as specified by the argument "index"
into an arbitrary object (byte, int, float, double, struct, etc...)
Inputs:
-------
* const T &val - Pointer to the object to be copied into from the
receive buffer (rxBuff)
* const uint16_t &index - Starting index of the object within the
receive buffer (rxBuff)
* const uint16_t &len - Number of bytes in the object "val" received
Return:
-------
* uint16_t maxIndex - Index of the receive buffer (rxBuff) that directly follows the bytes processed
by the calling of this member function
*/
template <typename T>
uint16_t rxObj(const T& val, const uint16_t& index = 0, const uint16_t& len = sizeof(T))
{
return packet.rxObj(val, index, len);
}
/*
uint8_t I2CTransfer::sendDatum(const T &val, const uint8_t &packetID=0, const uint8_t &targetAddress=0, const uint16_t &len=sizeof(T))
Description:
------------
* Stuffs "len" number of bytes of an arbitrary object (byte, int,
float, double, struct, etc...) into the transmit buffer (txBuff)
starting at the index as specified by the argument "index" and
automatically transmits the bytes in an individual packet
Inputs:
-------
* const T &val - Pointer to the object to be copied to the
transmit buffer (txBuff)
* const uint8_t &packetID - The packet 8-bit identifier
* const uint8_t &targetAddress - I2C address to the device the packet
will be transmitted to
* const uint16_t &len - Number of bytes of the object "val" to transmit
Return:
-------
* uint8_t - Number of payload bytes included in packet
*/
template <typename T>
uint8_t sendDatum(const T& val, const uint8_t& packetID = 0, const uint8_t& targetAddress = 0, const uint16_t& len = sizeof(T))
{
return sendData(packet.txObj(val, packetID, len), packetID, targetAddress);
}
private: // <<---------------------------------------//private
TwoWire* port;
static void processData();
};