-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp23017.cpp
More file actions
125 lines (92 loc) · 3.7 KB
/
mcp23017.cpp
File metadata and controls
125 lines (92 loc) · 3.7 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
#include "mcp23017.h"
// TODO (optional): Define macros for useful register below:
// TODO: Initialize i2cBus member
Mcp23017::Mcp23017(int addr) {
this->addr = addr;
}
uint8_t Mcp23017::get_dir(int pin) {
//gets information from Mcp23017 register 0x00
Wire.beginTransmission(addr);
Wire.write(0x00);
Wire.endTransmission();
//requests 1 byte from register 0x00 in Mcp23017
Wire.requestFrom(addr, 1);
//reads from the register and shifts the bit position to the LSB, then ands with 1 to get a 1 or 0
return (Wire.read() >> pin) & 1;
}
// TODO: Read from state register
uint8_t Mcp23017::get_state(int pin) {
Wire.beginTransmission(addr);
Wire.write(0x12);
Wire.endTransmission();
Wire.requestFrom(addr, 1);
//reads from the register and shifts the bit position to the LSB, then ands with 1 to get a 1 or 0
return (Wire.read() >> pin) & 1;
}
// TODO: Write to directions register
// 1: input 0: output
int Mcp23017::set_dir(int pin, uint8_t dir) {
// calls IODIRA for the read
Wire.beginTransmission(addr); //addr << 1 makes addr bit with LSB = 0
Wire.write(0x00); // writes the register offset
Wire.endTransmission();
//reads the byte value from the register
uint8_t number;
Wire.requestFrom(addr, 1);
number = Wire.read();
// changes the byte from the register that will convert the correct pin to a 1 or 0
if (dir == 1) {
number = number | (1 << pin); // number is all 0s except a 1 in the input pin position
} else {
number = number & ~(1 << pin); // number is all 1s except a 0 in the input pin position
}
//writes the new register data including the changed pin back to the register
Wire.beginTransmission(addr); //addr << 1 makes addr bit with LSB = 0
Wire.write(0x00); // writes the register offset
Wire.write(number); // writes the new data
Wire.endTransmission();
return 0;
}
// TODO: Write to state register
int Mcp23017::set_state(int pin, uint8_t val) {
// calls GPIO for the read
Wire.beginTransmission(addr); //addr << 1 makes addr bit with LSB = 0
Wire.write(0x12); // writes the register offset
Wire.endTransmission();
//reads the byte value from the register
uint8_t number;
Wire.requestFrom(addr, 1);
number = Wire.read();
// changes the byte from the register that will convert the correct pin to a 1 or 0
if (val == 1) {
number = number | (1 << pin); // number is all 0s except a 1 in the input pin position
} else {
number = number & ~(1 << pin); // number is all 1s except a 0 in the input pin position
}
//writes the new register data including the changed pin back to the register
Wire.beginTransmission(addr); //addr << 1 makes addr bit with LSB = 0
Wire.write(0x12); // writes the register offset
Wire.write(number); // writes the new data
Wire.endTransmission();
return 0;
}
// Verifies that the device is accessible over I2C and sets pin directions
int Mcp23017::begin(uint8_t directions[8]) {
int rc;
// TODO: Add device ID check
Wire.beginTransmission(addr); //addr << 1 makes addr bit with LSB = 0
Wire.write(0x00); // writes the register offset
Wire.endTransmission();
// sets each bit in IODIRA according to the bits in directions
Wire.requestFrom(addr, 1);
uint8_t s = Wire.read();
Serial.printf("%d, %d\n",s, addr);
if (s == 0xFF) { // if the read is all 1s then the default value is correct
for (int i = 0; i < 8; i++) {
this->set_dir(i, directions[i]);
}
return 0;
} else {
return 1; // returns 1 if the default value is wrong
}
}