Skip to content

Commit

Permalink
[ExtensionIOXL9555] Update method & examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Sep 14, 2024
1 parent 867f7eb commit 246f746
Show file tree
Hide file tree
Showing 7 changed files with 408 additions and 79 deletions.
106 changes: 87 additions & 19 deletions examples/XL9555_ExtensionIOInterrupt/XL9555_ExtensionIOInterrupt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
* Copyright (c) 2024 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,13 +24,10 @@
*
* @file XL9555_ExtensionIOInterrupt.ino
* @author Lewis He ([email protected])
* @date 2023-01-03
* @date 2024-09-14
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include <time.h>
#include "ExtensionIOXL9555.hpp"

#ifndef SENSOR_SDA
Expand All @@ -45,39 +42,110 @@
#define SENSOR_IRQ -1
#endif

ExtensionIOXL9555 extIO;
ExtensionIOXL9555 io;

void printBinary(uint8_t port, uint32_t num, int bits, bool new_line = false)
{

Serial.print("Port");
Serial.print(port);
Serial.print(":");

char binary[17];
int i = bits - 1;
binary[bits] = '\0';
for (int j = 0; j < bits; j++) {
binary[j] = '0';
}
for (; i >= 0; i--) {
binary[i] = (num & 1) + '0';
num >>= 1;
}

Serial.print(binary);
if (new_line) {
Serial.println();
} else {
Serial.print("\t");
}
}


void setup()
{
Serial.begin(115200);
while (!Serial);

// Set the interrupt input to input pull-up
if (SENSOR_IRQ > 0) {
pinMode(SENSOR_IRQ, INPUT_PULLUP);
}

/*
*
* If the device address is not known, the 0xFF parameter can be passed in.
*
* XL9555_UNKOWN_ADDRESS = 0xFF
*
* If the device address is known, the device address is given
*
* XL9555_SLAVE_ADDRESS0 = 0x20
* XL9555_SLAVE_ADDRESS1 = 0x21
* XL9555_SLAVE_ADDRESS2 = 0x22
* XL9555_SLAVE_ADDRESS3 = 0x23
* XL9555_SLAVE_ADDRESS4 = 0x24
* XL9555_SLAVE_ADDRESS5 = 0x25
* XL9555_SLAVE_ADDRESS6 = 0x26
* XL9555_SLAVE_ADDRESS7 = 0x27
*/
const uint8_t chip_address = XL9555_UNKOWN_ADDRESS;

// Device address 0x20~0x27
if (!extIO.begin(Wire, XL9555_SLAVE_ADDRESS4, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find XL9555 - check your wiring!");
if (!io.init(Wire, SENSOR_SDA, SENSOR_SCL, chip_address)) {
while (1) {
Serial.println("Failed to find XL9555 - check your wiring!");
delay(1000);
}
}

pinMode(SENSOR_IRQ, INPUT_PULLUP);
/*
* Note:
* XL9555 has an internal pull-up resistor and maintains a high level when idle.
* XL9535 has a floating input internally and an uncertain state when idle, requiring an external pull-up/pull-down resistor
* * */

// Set PORT0 as input,mask = 0xFF = all pin input
extIO.configPort(ExtensionIOXL9555::PORT0, 0xFF);
io.configPort(ExtensionIOXL9555::PORT0, 0xFF);

// Set PORT1 as input,mask = 0xFF = all pin input
extIO.configPort(ExtensionIOXL9555::PORT1, 0xFF);
io.configPort(ExtensionIOXL9555::PORT1, 0xFF);


}

void loop()
{
// When the interrupt occurs, we read the mask value of PORT
if (digitalRead(SENSOR_IRQ) == LOW) {
Serial.print("PORT0:0b");
Serial.print(extIO.readPort(ExtensionIOXL9555::PORT0), BIN);
Serial.print("\tPORT1:0b");
Serial.println(extIO.readPort(ExtensionIOXL9555::PORT1), BIN);
}
}

// Read 8-bit port 0 input status
int val = io.readPort(0);

// Read 8-bit port 1 input status
int val1 = io.readPort(1);

printBinary(1, val1, 8);
printBinary(0, val, 8);

// Read 16-bit gpio input status
uint16_t all_val = io.read();
printBinary(3, all_val, 16, true);

int i = 15;
for (; i >= 0; i--) {
if (!(all_val & 1)) {
Serial.printf("GPIO %d is low\n", 15 - i);
}
all_val >>= 1;
}
}
}

79 changes: 48 additions & 31 deletions examples/XL9555_ExtensionIORead/XL9555_ExtensionIORead.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
* Copyright (c) 2024 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,13 +24,10 @@
*
* @file XL9555_ExtensionIORead.ino
* @author Lewis He ([email protected])
* @date 2023-01-03
* @date 2024-09-14
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include <time.h>
#include "ExtensionIOXL9555.hpp"

#ifndef SENSOR_SDA
Expand All @@ -45,70 +42,90 @@
#define SENSOR_IRQ -1
#endif

ExtensionIOXL9555 extIO;
ExtensionIOXL9555 io;

void setup()
{
Serial.begin(115200);
while (!Serial);

// Set the interrupt input to input pull-up
if (SENSOR_IRQ > 0) {
pinMode(SENSOR_IRQ, INPUT_PULLUP);
}

/*
*
* If the device address is not known, the 0xFF parameter can be passed in.
*
* XL9555_UNKOWN_ADDRESS = 0xFF
*
* If the device address is known, the device address is given
*
* XL9555_SLAVE_ADDRESS0 = 0x20
* XL9555_SLAVE_ADDRESS1 = 0x21
* XL9555_SLAVE_ADDRESS2 = 0x22
* XL9555_SLAVE_ADDRESS3 = 0x23
* XL9555_SLAVE_ADDRESS4 = 0x24
* XL9555_SLAVE_ADDRESS5 = 0x25
* XL9555_SLAVE_ADDRESS6 = 0x26
* XL9555_SLAVE_ADDRESS7 = 0x27
*/
const uint8_t chip_address = XL9555_UNKOWN_ADDRESS;

// Device address 0x20~0x27
if (!extIO.begin(Wire, XL9555_SLAVE_ADDRESS4, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find XL9555 - check your wiring!");
if (!io.init(Wire, SENSOR_SDA, SENSOR_SCL, chip_address)) {
while (1) {
Serial.println("Failed to find XL9555 - check your wiring!");
delay(1000);
}
}

pinMode(SENSOR_IRQ, INPUT_PULLUP);
// Set PORT0 as input,mask = 0xFF = all pin input
extIO.configPort(ExtensionIOXL9555::PORT0, 0xFF);
io.configPort(ExtensionIOXL9555::PORT0, 0xFF);
// Set PORT1 as input,mask = 0xFF = all pin input
extIO.configPort(ExtensionIOXL9555::PORT1, 0xFF);
io.configPort(ExtensionIOXL9555::PORT1, 0xFF);
}

void loop()
{
Serial.print("PORT0:0b");
Serial.print(extIO.readPort(ExtensionIOXL9555::PORT0), BIN);
Serial.print(io.readPort(ExtensionIOXL9555::PORT0), BIN);
Serial.print("\tPORT1:0b");
Serial.println(extIO.readPort(ExtensionIOXL9555::PORT1), BIN);
Serial.println(io.readPort(ExtensionIOXL9555::PORT1), BIN);

//Allowable range of parameter input: IO0 ~ IO15
Serial.println("IO0:\tIO1:\tIO2:\tIO3:\tIO4:\tIO5:\tIO6:\tIO7:\tIO8:\tIO9:\tIO10:\tIO11:\tIO12:\tIO13:\tIO14:\tIO15:\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO0));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO0));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO1));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO1));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO2));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO2));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO3));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO3));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO4));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO4));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO5));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO5));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO6));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO6));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO7));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO7));
Serial.print("\t");

Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO8));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO8));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO9));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO9));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO10));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO10));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO11));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO11));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO12));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO12));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO13));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO13));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO14));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO14));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO15));
Serial.print(io.digitalRead(ExtensionIOXL9555::IO15));
Serial.println();
delay(1000);
}
Expand Down
52 changes: 35 additions & 17 deletions examples/XL9555_ExtensionIOWirte/XL9555_ExtensionIOWirte.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
* Copyright (c) 2024 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,13 +24,10 @@
*
* @file XL9555_ExtensionIOWirte.ino
* @author Lewis He ([email protected])
* @date 2023-01-03
* @date 2024-09-14
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include <time.h>
#include "ExtensionIOXL9555.hpp"

#ifndef SENSOR_SDA
Expand All @@ -45,46 +42,67 @@
#define SENSOR_IRQ -1
#endif

ExtensionIOXL9555 extIO;
ExtensionIOXL9555 io;

void setup()
{
Serial.begin(115200);
while (!Serial);


// Set the interrupt input to input pull-up
if (SENSOR_IRQ > 0) {
pinMode(SENSOR_IRQ, INPUT_PULLUP);
}

// Device address 0x20~0x27
if (!extIO.begin(Wire, XL9555_SLAVE_ADDRESS4, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find XL9555 - check your wiring!");
/*
*
* If the device address is not known, the 0xFF parameter can be passed in.
*
* XL9555_UNKOWN_ADDRESS = 0xFF
*
* If the device address is known, the device address is given
*
* XL9555_SLAVE_ADDRESS0 = 0x20
* XL9555_SLAVE_ADDRESS1 = 0x21
* XL9555_SLAVE_ADDRESS2 = 0x22
* XL9555_SLAVE_ADDRESS3 = 0x23
* XL9555_SLAVE_ADDRESS4 = 0x24
* XL9555_SLAVE_ADDRESS5 = 0x25
* XL9555_SLAVE_ADDRESS6 = 0x26
* XL9555_SLAVE_ADDRESS7 = 0x27
*/
const uint8_t chip_address = XL9555_UNKOWN_ADDRESS;

if (!io.init(Wire, SENSOR_SDA, SENSOR_SCL, chip_address)) {
while (1) {
Serial.println("Failed to find XL9555 - check your wiring!");
delay(1000);
}
}

// Set PORT0 as output ,mask = 0x00 = all pin output
extIO.configPort(ExtensionIOXL9555::PORT0, 0x00);
io.configPort(ExtensionIOXL9555::PORT0, 0x00);
// Set PORT1 as output ,mask = 0x00 = all pin output
extIO.configPort(ExtensionIOXL9555::PORT1, 0x00);
io.configPort(ExtensionIOXL9555::PORT1, 0x00);
}

void loop()
{
// Set all PORTs to 1, and the parameters here are mask values, corresponding to the 0~7 bits
Serial.println("Set port HIGH");
extIO.writePort(ExtensionIOXL9555::PORT0, 0xFF);
io.writePort(ExtensionIOXL9555::PORT0, 0xFF);
delay(1000);

Serial.println("Set port LOW");
// Set all PORTs to 0, and the parameters here are mask values, corresponding to the 0~7 bits
extIO.writePort(ExtensionIOXL9555::PORT1, 0x00);
io.writePort(ExtensionIOXL9555::PORT1, 0x00);
delay(1000);

Serial.println("digitalWrite");
extIO.digitalWrite(ExtensionIOXL9555::IO0, HIGH);
io.digitalWrite(ExtensionIOXL9555::IO0, HIGH);
delay(1000);

Serial.println("digitalToggle");
extIO.digitalToggle(ExtensionIOXL9555::IO0);
io.digitalToggle(ExtensionIOXL9555::IO0);
delay(1000);


Expand Down
Loading

0 comments on commit 246f746

Please sign in to comment.