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

Method pinMode

libraries/MCP23008/MCP23008.cpp:80–116  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

78// pin = 0..7
79// mode = INPUT, OUTPUT, INPUT_PULLUP (= same as INPUT)
80bool MCP23008::pinMode(uint8_t pin, uint8_t mode)
81{
82 if (pin > 7)
83 {
84 _error = MCP23008_PIN_ERROR;
85 return false;
86 }
87 if ((mode != INPUT) && (mode != INPUT_PULLUP) && (mode != OUTPUT))
88 {
89 _error = MCP23008_VALUE_ERROR;
90 return false;
91 }
92
93 uint8_t dataDirectionRegister = MCP23008_DDR_A;
94 uint8_t val = readReg(dataDirectionRegister);
95 if (_error != MCP23008_OK)
96 {
97 return false;
98 }
99 uint8_t mask = 1 << pin;
100 // only work with valid
101 if ((mode == INPUT) || (mode == INPUT_PULLUP))
102 {
103 val |= mask;
104 }
105 else if (mode == OUTPUT)
106 {
107 val &= ~mask;
108 }
109 // other values won't change val ....
110 writeReg(dataDirectionRegister, val);
111 if (_error != MCP23008_OK)
112 {
113 return false;
114 }
115 return true;
116}
117
118
119// pin = 0..7

Callers 1

unittestFunction · 0.45

Calls

no outgoing calls

Tested by 1

unittestFunction · 0.36