peary/hardware/components/PCA9539.hpp

PCA9539 Component Class. More…

Namespaces

Name
peary
peary::component

Classes

Name
class peary::component::PCA9539 PCA9539 I/O Expander Component.

Detailed Description

PCA9539 Component Class.

Copyright: Copyright (c) 2016-2025 CERN and the Peary Caribou authors. This software is distributed under the terms of the LGPL-3.0-only License, copied verbatim in the file “LICENSE.md”. SPDX-License-Identifier: LGPL-3.0-only

Source code

  
#pragma once

#include <cstdint>
#include <string>

#include "peary/hal/Components.hpp"
#include "peary/hardware/components/exceptions.hpp"
#include "peary/interfaces/I2C/i2c.hpp"
#include "peary/utils/datatypes.hpp"

namespace peary::component {
    class PCA9539 : public hal::Component {
    public:
        // Register addresses
        enum class Register {
            INPUT_PORT_0 = 0x00,              // Input port 0 register
            INPUT_PORT_1 = 0x01,              // Input port 1 register
            OUTPUT_PORT_0 = 0x02,             // Output port 0 register
            OUTPUT_PORT_1 = 0x03,             // Output port 1 register
            POLARITY_INVERSION_PORT_0 = 0x04, // Polarity inversion port 0 register
            POLARITY_INVERSION_PORT_1 = 0x05, // Polarity inversion port 1 register
            CONFIGURATION_PORT_0 = 0x06,      // Configuration port 0 register
            CONFIGURATION_PORT_1 = 0x07       // Configuration port 1 register
        };

        // Ports IDs
        enum class Port { //
            P0 = 0,
            P1 = 1
        };
        using enum Port;

        // Channel IDs
        enum class Channel { //
            CH0 = 0,
            CH1 = 1,
            CH2 = 2,
            CH3 = 3,
            CH4 = 4,
            CH5 = 5,
            CH6 = 6,
            CH7 = 7
        };
        using enum Channel;

        PCA9539(const std::string& i2c_bus, uint8_t device_address);

        // FIXME Need to use utils::Polarity to describe level
        bool getInputLevel(Port port, Channel channel);

        utils::Polarity getOutputLevel(Port port, Channel channel);

        void setOutputLevel(Port port, Channel channel, utils::Polarity polarity);

        void invertInputPolarity(Port port, Channel channel, bool invert);

        utils::Direction getDirection(Port port, Channel channel);

        void setDirection(Port port, Channel channel, utils::Direction direction);

    private:
        std::string _bus;
        uint8_t _address;
    };

} // namespace peary::component
  

Updated on 2025-11-14 at 11:31:23 +0100