MCPcopy Create free account
hub / github.com/RobTillaart/Arduino / pinMode

Method pinMode

libraries/MCP23017_RT/MCP23017.cpp:93–134  ·  view source on GitHub ↗

single pin interface pin = 0..15 mode = INPUT, OUTPUT, INPUT_PULLUP (= same as INPUT)

Source from the content-addressed store, hash-verified

91// pin = 0..15
92// mode = INPUT, OUTPUT, INPUT_PULLUP (= same as INPUT)
93bool MCP23017::pinMode(uint8_t pin, uint8_t mode)
94{
95 if (pin > 15)
96 {
97 _error = MCP23017_PIN_ERROR;
98 return false;
99 }
100 if ((mode != INPUT) && (mode != INPUT_PULLUP) && (mode != OUTPUT))
101 {
102 _error = MCP23017_VALUE_ERROR;
103 return false;
104 }
105
106 uint8_t dataDirectionRegister = MCP23017_DDR_A;
107 if (pin > 7)
108 {
109 dataDirectionRegister = MCP23017_DDR_B;
110 pin -= 8;
111 }
112 uint8_t val = readReg(dataDirectionRegister);
113 if (_error != MCP23017_OK)
114 {
115 return false;
116 }
117 uint8_t mask = 1 << pin;
118 // only work with valid
119 if ((mode == INPUT) || (mode == INPUT_PULLUP))
120 {
121 val |= mask;
122 }
123 else if (mode == OUTPUT)
124 {
125 val &= ~mask;
126 }
127 // other values won't change val ....
128 writeReg(dataDirectionRegister, val);
129 if (_error != MCP23017_OK)
130 {
131 return false;
132 }
133 return true;
134}
135
136
137// pin = 0..15

Callers 1

unittestFunction · 0.45

Calls

no outgoing calls

Tested by 1

unittestFunction · 0.36