-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWire.cpp
More file actions
executable file
·145 lines (118 loc) · 2.8 KB
/
Wire.cpp
File metadata and controls
executable file
·145 lines (118 loc) · 2.8 KB
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
140
141
142
143
144
145
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <string.h>
/*
*Wire.cpp:
*
* Welcome to MikuQ.com! MikuDuino for BananaPi
*
* by MikuQ(i@mikuq.com)
*
* https://github.com/bpiq/MikuPi
*
*/
#include "MikuDuino.h"
#include "Wire.h"
uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
uint8_t TwoWire::rxBufferIndex = 0;
uint8_t TwoWire::rxBufferLength = 0;
uint8_t TwoWire::txAddress = 0;
uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
uint16_t TwoWire::txBufferIndex = 0;
uint16_t TwoWire::txBufferLength = 0;
TwoWire::TwoWire()
{
}
void TwoWire::begin()
{
rxBufferIndex = 0;
rxBufferLength = 0;
txBufferIndex = 0;
txBufferLength = 0;
I2cError = 0;
if ((I2cDevHandle = open(i2cDevice, O_RDWR)) < 0) I2cError |= ERROR_I2C_OPEN;
}
void TwoWire::requestFrom(int address, int quantity)
{
I2cError = 0;
if (ioctl(I2cDevHandle, I2C_SLAVE,address) < 0) I2cError |= ERROR_I2C_SETUP;
if(!I2cError)
{
if (::read(I2cDevHandle, rxBuffer,quantity) != quantity) I2cError |= ERROR_I2C_READ;
else
{
rxBufferIndex = 0;
rxBufferLength = quantity;
}
}
}
void TwoWire::beginTransmission(int address)
{
txAddress = address;
txBufferIndex = 0;
txBufferLength = 0;
}
void TwoWire::endTransmission()
{
I2cError = 0;
if (ioctl(I2cDevHandle, I2C_SLAVE,txAddress) < 0) I2cError |= ERROR_I2C_SETUP;
if(!I2cError)
{
if((::write(I2cDevHandle, txBuffer, txBufferLength)) != txBufferLength) I2cError |= ERROR_I2C_WRITE;
}
//printf("\n leng = %d \n",txBufferLength);
txBufferIndex = 0;
txBufferLength = 0;
//close(I2cDevHandle);
}
void TwoWire::write(uint8_t cmd)
{
if(txBufferLength >= BUFFER_LENGTH){
return;
}
// put byte in tx buffer
txBuffer[txBufferIndex] = cmd;
++txBufferIndex;
// update amount in buffer
txBufferLength = txBufferIndex;
}
void TwoWire::write(uint8_t reg, uint8_t dat)
{
uint8_t buf[2];
buf[0]=reg;
buf[1]=dat;
if(!I2cError)
{
if((::write(I2cDevHandle, buf, 2)) != 2) I2cError |= ERROR_I2C_WRITE;
}
}
void TwoWire::write(uint8_t* dat, int length)
{
if(txBufferLength+length >= BUFFER_LENGTH){
return;
}
memcpy(txBuffer, dat, length);
txBufferIndex+=length;
txBufferLength = txBufferIndex;
return;
if(!I2cError)
{
if((::write(I2cDevHandle, dat, length)) != length) I2cError |= ERROR_I2C_WRITE;
}
}
int TwoWire::available(void)
{
return rxBufferLength - rxBufferIndex;
}
int TwoWire::read(void)
{
int value = -1;
if(rxBufferIndex < rxBufferLength){
value = rxBuffer[rxBufferIndex];
++rxBufferIndex;
}
return value;
}
TwoWire Wire = TwoWire();