-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp23017.cpp
More file actions
122 lines (102 loc) · 3.1 KB
/
mcp23017.cpp
File metadata and controls
122 lines (102 loc) · 3.1 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
#include "mcp23017.h"
// TODO (optional): Define macros for useful register below:
#define IO_DIR_ADDR 0x00
#define GPIO_ADDR 0x09
// TODO: Initialize i2cBus member
Mcp23017::Mcp23017(int addr) : addr(addr) {} //member initialization
uint8_t Mcp23017::get_dir(int pin) {
// first establish a connection with the i2bus
Wire.beginTransmission(Mcp23017::addr);
//Specify the register
Wire.write(IO_DIR_ADDR);
Wire.endTransmission();
// request 1 byte from the register at 0x00 (iodir)
// and store in the variable byteRead
Wire.requestFrom(IO_DIR_ADDR, 1);
uint8_t byteRead;
while (Wire.available()){
byteRead = Wire.read();
}
//moves desired bit to LSB and compares to 1
uint8_t pinDir = (byteRead >> pin) & 1;
return pinDir;
}
// TODO: Read from state register
uint8_t Mcp23017::get_state(int pin) {
// Same functionality as above method
Wire.beginTransmission(Mcp23017::addr);
//Specify the register
Wire.write(GPIO_ADDR);
Wire.endTransmission();
Wire.requestFrom(GPIO_ADDR, 1);
uint8_t byteRead;
while (Wire.available()){
byteRead = Wire.read();
}
uint8_t pinDir = (byteRead >> pin) & 1;
return pinDir;
}
// TODO: Write to directions register
int Mcp23017::set_dir(int pin, uint8_t dir) {
Wire.beginTransmission(Mcp23017::addr);
Wire.write(IO_DIR_ADDR);
Wire.endTransmission();
Wire.requestFrom(IO_DIR_ADDR, 1);
uint8_t byte;
while (Wire.available()){
byte = Wire.read();
}
if(dir == 1){ //If we want the pin to be on, we use OR logic
byte |= (dir << pin); // shift the mask by the pin amount
}
else if(dir == 0){ //Use AND NOT logic to get the pin to be off
byte &= ~(1 << pin);
}
Wire.beginTransmission(Mcp23017::addr);
Wire.write(IO_DIR_ADDR);
Wire.write(byte);
Wire.endTransmission();
return get_dir(pin); //checks if what we did worked
}
// TODO: Write to state register
int Mcp23017::set_state(int pin, uint8_t val) {
Wire.beginTransmission(Mcp23017::addr);
Wire.write(GPIO_ADDR);
Wire.endTransmission();
Wire.requestFrom(GPIO_ADDR, 1);
uint8_t byte;
while (Wire.available()){
byte = Wire.read();
}
if(val == 1){
byte |= (val << pin);
}
else if(val == 0){
byte &= ~(1 << pin);
}
Wire.beginTransmission(Mcp23017::addr);
Wire.write(GPIO_ADDR);
Wire.write(byte);
Wire.endTransmission();
return get_state(pin);
}
// Verifies that the device is accessible over I2C and sets pin directions
int Mcp23017::begin(uint8_t directions[8]) {
int rc;
Wire.beginTransmission(Mcp23017::addr);
Wire.write(IO_DIR_ADDR);
Wire.endTransmission();
Wire.requestFrom(IO_DIR_ADDR, 1);
uint8_t rc;
while (Wire.available()){
rc = Wire.read();
}
if(rc != 0xFF){ //according to manual, all of the bits should be on at first
return 0; //failed
}
//set the direction of each of the pins to the desired value
for(int i = 0; i < 8; i++){
set_dir(i, directions[i]);
}
return 1; //succeeded
}