SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
xl9555_interrupt.ino
Go to the documentation of this file.
1
30#include <IoExpanderDrv.hpp>
31
32#ifndef SENSOR_SDA
33#define SENSOR_SDA 17
34#endif
35
36#ifndef SENSOR_SCL
37#define SENSOR_SCL 18
38#endif
39
40#ifndef SENSOR_IRQ
41#define SENSOR_IRQ 10
42#endif
43
45
46void printBinary(uint8_t port, uint32_t num, int bits, bool new_line = false)
47{
48
49 Serial.print("Port");
50 Serial.print(port);
51 Serial.print(":");
52
53 char binary[17];
54 int i = bits - 1;
55 binary[bits] = '\0';
56 for (int j = 0; j < bits; j++) {
57 binary[j] = '0';
58 }
59 for (; i >= 0; i--) {
60 binary[i] = (num & 1) + '0';
61 num >>= 1;
62 }
63
64 Serial.print(binary);
65 if (new_line) {
66 Serial.println();
67 } else {
68 Serial.print("\t");
69 }
70}
71
72
73void setup()
74{
75 Serial.begin(115200);
76
77 // Set the interrupt input to input pull-up
78 pinMode(SENSOR_IRQ, INPUT_PULLUP);
79
80 /*
81 *
82 * If the device address is not known, the 0xFF parameter can be passed in.
83 *
84 * XL9555_UNKNOWN_ADDRESS = 0xFF
85 *
86 * If the device address is known, the device address is given
87 *
88 * XL9555_SLAVE_ADDRESS0 = 0x20
89 * XL9555_SLAVE_ADDRESS1 = 0x21
90 * XL9555_SLAVE_ADDRESS2 = 0x22
91 * XL9555_SLAVE_ADDRESS3 = 0x23
92 * XL9555_SLAVE_ADDRESS4 = 0x24
93 * XL9555_SLAVE_ADDRESS5 = 0x25
94 * XL9555_SLAVE_ADDRESS6 = 0x26
95 * XL9555_SLAVE_ADDRESS7 = 0x27
96 */
97 const uint8_t chip_address = XL9555_UNKNOWN_ADDRESS;
98
99 if (!expander.begin(Wire, chip_address, SENSOR_SDA, SENSOR_SCL)) {
100 while (1) {
101 Serial.println("Failed to find XL9555 - check your wiring!");
102 delay(1000);
103 }
104 }
105
106 /*
107 * Note:
108 * XL9555 has an internal pull-up resistor and maintains a high level when idle.
109 * XL9535 has a floating input internally and an uncertain state when idle, requiring an external pull-up/pull-down resistor
110 * * */
111
112 // Set all pins as input
114}
115
116void loop()
117{
118 // When the interrupt occurs, we read the mask value of PORT
119 if (digitalRead(SENSOR_IRQ) == LOW) {
120
121 // Read 16-bit port 0 and port 1 input status
122 uint16_t value = expander.digitalReadPort();
123 // Read 16-bit gpio input status
124 printBinary(3, value, 16, true);
125
126 int i = 15;
127 for (; i >= 0; i--) {
128 if (!(value & 1)) {
129 Serial.print("GPIO: ");
130 Serial.print(15 - i);
131 Serial.println(" is low");
132 }
133 value >>= 1;
134 }
135 }
136}
137
@license MIT License
bool begin(SensorCommCustom::CustomCallback callback, uint8_t addr)
Initialize the sensor using custom callback interface.
void configPins(uint16_t pinMask, uint8_t mode)
Configure multiple pins as input or output simultaneously.
Concrete driver for the XL9555 16‑bit I2C GPIO expander.
uint16_t digitalReadPort() override
Read the values of all 16 input pins as a single 16-bit word.
uint8_t i
void printBinary(uint8_t port, uint32_t num, int bits, bool new_line=false)
void setup()
#define SENSOR_SCL
#define SENSOR_SDA
#define SENSOR_IRQ
void loop()
IoExpanderXL9555 expander