-
Notifications
You must be signed in to change notification settings - Fork 2
/
WireData.h
66 lines (57 loc) · 1.38 KB
/
WireData.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
/*
||
|| @author Brett Hagman <bhagman@wiring.org.co>
|| @url http://wiring.org.co/
|| @url http://loftypremises.com/
|| @contribution Nick Gammon
|| @url http://www.gammon.com.au/i2c
||
|| @description
|| |
|| | WireData - Send data via I2C/TWI, using the Wire library.
|| |
|| | Based on I2C_Anything library by Nick Gammon
|| #
||
|| @notes
|| | Initially based on Atmel AVR 8 bit GCC compiler.
|| |
|| | IMPORTANT: This library bases its deconstruction of data types on the
|| | compiler's data type representations. Be careful! If you use two
|| | drastically different compilers for slaves/masters, then you will have to
|| | reconstruct data accordingly.
||
|| @license Please see license.txt.
||
*/
#include <Arduino.h>
#include <Wire.h>
/*
|| @usage
|| | e.g.
|| | float myNumber = 3.14159;
|| | uint16_t numberOfBytesSent = wireWriteData(myNumber);
|| #
*/
template <typename T> uint16_t wireWriteData(const T &value)
{
const uint8_t *p = (const uint8_t *) &value;
Wire.write(p, sizeof value);
return sizeof value;
}
/*
|| @usage
|| | e.g.
|| | float myNumber;
|| | uint16_t numberOfBytesReceived = wireReadData(myNumber);
|| | Serial.println(myNumber);
|| #
*/
template <typename T> uint16_t wireReadData(T &value)
{
uint8_t *p = (uint8_t *) &value;
uint16_t i;
for (i = 0; i < sizeof value; i++)
*p++ = Wire.read();
return i;
}