|
5 | 5 |
|
6 | 6 | #if defined(GPIO_DEVICE) && GPIO_DEVICE == X8574 |
7 | 7 |
|
8 | | -#ifndef GPIO_PCF8574_I2C_ADDRESS1 |
9 | | - #define GPIO_PCF8574_I2C_ADDRESS1 0x39 |
10 | | -#endif |
11 | | -#ifndef GPIO_PCF8574_I2C_ADDRESS2 |
12 | | - #define GPIO_PCF8574_I2C_ADDRESS2 0x3A |
13 | | -#endif |
14 | | -#ifndef GPIO_PCF8574_I2C_ADDRESS3 |
15 | | - #define GPIO_PCF8574_I2C_ADDRESS3 0x3B |
16 | | -#endif |
17 | | -#ifndef GPIO_PCF8574_I2C_ADDRESS4 |
18 | | - #define GPIO_PCF8574_I2C_ADDRESS4 0x3C |
19 | | -#endif |
20 | | -#ifndef GPIO_PCF8574_I2C_NUM_DEVICES |
21 | | - #define GPIO_PCF8574_I2C_NUM_DEVICES 1 |
22 | | -#endif |
23 | | - |
24 | | -#if defined(ESP32) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_RENESAS) |
25 | | - PCF8574 pcf0(&HAL_WIRE, GPIO_PCF8574_I2C_ADDRESS1); |
26 | | - #if GPIO_PCF8574_I2C_NUM_DEVICES > 1 |
27 | | - PCF8574 pcf1(&HAL_WIRE, GPIO_PCF8574_I2C_ADDRESS2); |
28 | | - #if GPIO_PCF8574_I2C_NUM_DEVICES > 2 |
29 | | - PCF8574 pcf2(&HAL_WIRE, GPIO_PCF8574_I2C_ADDRESS3); |
30 | | - #if GPIO_PCF8574_I2C_NUM_DEVICES > 3 |
31 | | - PCF8574 pcf3(&HAL_WIRE, GPIO_PCF8574_I2C_ADDRESS4); |
32 | | - #endif |
33 | | - #endif |
34 | | - #endif |
35 | | -#else |
36 | | - PCF8574 pcf0(GPIO_PCF8574_I2C_ADDRESS1); |
37 | | - #if GPIO_PCF8574_I2C_NUM_DEVICES > 1 |
38 | | - PCF8574 pcf1(GPIO_PCF8574_I2C_ADDRESS2); |
39 | | - #if GPIO_PCF8574_I2C_NUM_DEVICES > 2 |
40 | | - PCF8574 pcf2(GPIO_PCF8574_I2C_ADDRESS3); |
41 | | - #if GPIO_PCF8574_I2C_NUM_DEVICES > 3 |
42 | | - PCF8574 pcf3(GPIO_PCF8574_I2C_ADDRESS4); |
43 | | - #endif |
44 | | - #endif |
45 | | - #endif |
46 | | -#endif |
47 | | - |
48 | 8 | // check for up to four PCF8574 devices on the I2C bus |
49 | 9 | bool GpioPcf8574::init() { |
50 | 10 | static bool initialized = false; |
51 | 11 | if (initialized) return found; |
52 | 12 |
|
53 | | - for (int i = 0; i < 4; i++) { pcf[i] = NULL; } |
54 | | - pcf[0] = &pcf0; |
55 | | - #if GPIO_PCF8574_I2C_NUM_DEVICES > 1 |
56 | | - pcf[1] = &pcf1; |
57 | | - #if GPIO_PCF8574_I2C_NUM_DEVICES > 2 |
58 | | - pcf[2] = &pcf2; |
59 | | - #if GPIO_PCF8574_I2C_NUM_DEVICES > 3 |
60 | | - pcf[3] = &pcf3; |
61 | | - #endif |
62 | | - #endif |
63 | | - #endif |
| 13 | + for (int i = 0; i < 32; i++) mode[i] = -1; |
| 14 | + |
| 15 | + HAL_WIRE.begin(); |
| 16 | + HAL_WIRE_SET_CLOCK(); |
64 | 17 |
|
65 | | - for (int i = 0; i < 4; i++) { |
66 | | - if (pcf[i] != NULL) { |
67 | | - if (pcf[i]->begin()) { |
68 | | - found = true; |
69 | | - } else { |
| 18 | + // check to be sure we find all devices |
| 19 | + for (int i = 0; i < GPIO_PCF8574_I2C_NUM_DEVICES; i++) { |
| 20 | + HAL_WIRE.beginTransmission(iicAddress[i]); |
| 21 | + uint8_t error = HAL_WIRE.endTransmission(); |
| 22 | + if (error) { |
| 23 | + // try again |
| 24 | + HAL_WIRE.beginTransmission(iicAddress[i]); |
| 25 | + uint8_t error = HAL_WIRE.endTransmission(); |
| 26 | + if (error) { |
| 27 | + if (DEBUG != OFF) { DF("WRN: Gpio.init(), PCF8574 (I2C 0x"); SERIAL_DEBUG.print(iicAddress[i], HEX); DLF(") not found"); } |
70 | 28 | found = false; |
71 | | - DF("WRN: Gpio.init(), PCF8574 (I2C 0x"); |
72 | | - if (DEBUG != OFF) { |
73 | | - switch (i) { |
74 | | - case 0: SERIAL_DEBUG.print(GPIO_PCF8574_I2C_ADDRESS1, HEX); break; |
75 | | - #if GPIO_PCF8574_I2C_NUM_DEVICES > 1 |
76 | | - case 1: SERIAL_DEBUG.print(GPIO_PCF8574_I2C_ADDRESS2, HEX); break; |
77 | | - #endif |
78 | | - #if GPIO_PCF8574_I2C_NUM_DEVICES > 2 |
79 | | - case 2: SERIAL_DEBUG.print(GPIO_PCF8574_I2C_ADDRESS3, HEX); break; |
80 | | - #endif |
81 | | - #if GPIO_PCF8574_I2C_NUM_DEVICES > 3 |
82 | | - case 3: SERIAL_DEBUG.print(GPIO_PCF8574_I2C_ADDRESS4, HEX); break; |
83 | | - #endif |
84 | | - } |
85 | | - } |
86 | | - DLF(") not found"); |
| 29 | + return false; |
87 | 30 | } |
88 | 31 | } |
89 | 32 | } |
90 | | - HAL_WIRE_SET_CLOCK(); |
91 | | - |
92 | | - for (int i = 0; i < 32; i++) { mode[i] = INPUT; state[i] = false; }; |
93 | 33 |
|
| 34 | + found = true; |
94 | 35 | return found; |
95 | 36 | } |
96 | 37 |
|
97 | 38 | // set GPIO pin (0 to 31) mode for INPUT or OUTPUT |
98 | 39 | void GpioPcf8574::pinMode(int pin, int mode) { |
99 | 40 | if (found && pin >= 0 && pin <= GPIO_PCF8574_I2C_NUM_DEVICES*8 - 1) { |
| 41 | + uint8_t device = pin >> 3; |
| 42 | + uint8_t devicePin = pin & 0b0111; |
| 43 | + |
100 | 44 | #ifdef INPUT_PULLDOWN |
101 | 45 | if (mode == INPUT_PULLDOWN) mode = INPUT; |
102 | 46 | #endif |
103 | 47 | if (mode == INPUT_PULLUP) mode = INPUT; |
104 | | - pcf[pin >> 3]->pinMode(pin & 0b111, mode); |
105 | | - this->mode[pin] = mode; |
| 48 | + if (mode != INPUT && mode != OUTPUT) return; |
| 49 | + |
| 50 | + if (this->mode[pin] != mode) { |
| 51 | + bitWrite(state[device], devicePin, (mode == INPUT) ? 1 : 0); |
| 52 | + |
| 53 | + HAL_WIRE.beginTransmission(iicAddress[device]); |
| 54 | + HAL_WIRE.write(state[device]); |
| 55 | + HAL_WIRE.endTransmission(); |
| 56 | + |
| 57 | + this->mode[pin] = mode; |
| 58 | + } |
106 | 59 | } |
107 | 60 | } |
108 | 61 |
|
109 | 62 | // get GPIO pin (0 to 31) state |
110 | 63 | int GpioPcf8574::digitalRead(int pin) { |
111 | | - if (found && pin >= 0 && pin <= GPIO_PCF8574_I2C_NUM_DEVICES*8 - 1) { |
| 64 | + if (found && pin >= 0 && pin <= GPIO_PCF8574_I2C_NUM_DEVICES*8 - 1 && mode[pin] > 0) { |
| 65 | + uint8_t device = pin >> 3; |
| 66 | + uint8_t devicePin = pin & 0b0111; |
| 67 | + |
112 | 68 | if (mode[pin] == INPUT) { |
113 | | - return pcf[pin >> 3]->digitalRead(pin & 0b111); |
114 | | - } else return state[pin]; |
115 | | - } else return 0; |
| 69 | + |
| 70 | + HAL_WIRE.requestFrom(iicAddress[device], (uint8_t)1); |
| 71 | + |
| 72 | + unsigned long timeout = millis() + 1000UL; |
| 73 | + do { if ((long)(timeout - millis()) < 0) return 0; } while (Wire.available() < 1); |
| 74 | + |
| 75 | + uint8_t data = HAL_WIRE.read(); |
| 76 | + return bitRead(data, devicePin); |
| 77 | + } else |
| 78 | + return bitRead(state[device], devicePin); |
| 79 | + } |
| 80 | + |
| 81 | + return 0; |
116 | 82 | } |
117 | 83 |
|
118 | 84 | // set GPIO pin (0 to 31) state |
119 | 85 | void GpioPcf8574::digitalWrite(int pin, int value) { |
120 | | - if (found && pin >= 0 && pin <= GPIO_PCF8574_I2C_NUM_DEVICES*8 - 1) { |
121 | | - state[pin] = value; |
| 86 | + if (found && pin >= 0 && pin <= GPIO_PCF8574_I2C_NUM_DEVICES*8 - 1 && mode[pin] > 0) { |
122 | 87 | if (mode[pin] == OUTPUT) { |
123 | | - pcf[pin >> 3]->digitalWrite(pin & 0b111, value); |
| 88 | + uint8_t device = pin >> 3; |
| 89 | + uint8_t devicePin = pin & 0b0111; |
| 90 | + |
| 91 | + bitWrite(state[device], devicePin, (value == 0) ? 0 : 1); |
| 92 | + |
| 93 | + HAL_WIRE.beginTransmission(iicAddress[device]); |
| 94 | + HAL_WIRE.write(state[device]); |
| 95 | + HAL_WIRE.endTransmission(); |
124 | 96 | } |
125 | | - } else return; |
| 97 | + } |
126 | 98 | } |
127 | 99 |
|
128 | 100 | GpioPcf8574 gpio; |
|
0 commit comments