GithubHelp home page GithubHelp logo

Comments (1)

NicoFR75 avatar NicoFR75 commented on June 7, 2024

After a long time, i decided to work again with this chip. I used the library above and added missing function for reading input register (https://github.com/NicoFR75/PCA9533).

With using the custom binarysensor, custom floatoutput and custom switch, i manage to make it work.
Below the code is used if someone want to use this chip

I have 3 files (one per custom type) included in my YAML config file, the setup part is the same for the 3 files.

YAML
For binarysensor (detect switch position):

binary_sensor:
  - platform: custom
    lambda: |-
      auto pca9533_binarysensor = new PCA9533_BinarySensor_IO2();
      App.register_component(pca9533_binarysensor);
      return {pca9533_binarysensor};
    binary_sensors:
      name: '233_Interrupteur_IO2'
      on_state:
        then:
          - light.toggle:
              id: light_pwm
              transition_length: 500ms  
  - platform: custom
    lambda: |-
      auto pca9533_binarysensor = new PCA9533_BinarySensor_IO3();
      App.register_component(pca9533_binarysensor);
      return {pca9533_binarysensor};
    binary_sensors:
      name: '233_Interrupteur_IO3'
      on_state:
        then:
          - switch.toggle:
              id: custom_switch

For switch (i use it to command a relay for light bulb):

switch:
  - platform: custom
    lambda: |-
      auto pca9533_switch = new PCA9533_Switch_IO1();
      App.register_component(pca9533_switch);
      return {pca9533_switch};
    switches:
      name: "233_Relai"
      id: custom_switch
      inverted: true

For output (linked to a light for dimming led):

output:
  - platform: custom
    type: float
    lambda: |-
      auto pca9533_floatoutput = new PCA9533_FloatOutput_IO0();
      App.register_component(pca9533_floatoutput);
      return {pca9533_floatoutput};
    outputs:
      id: custom_floatoutput

light:
  - platform: monochromatic
    name: "233_Variateur"
    id: light_pwm
    output: custom_floatoutput

.H FILES
For binarysensor :

#include "esphome.h"
#include "PCA9533.h"

using namespace esphome::binary_sensor;

class PCA9533_BinarySensor_IO2 : public Component, public BinarySensor {
 public:
  
  PCA9533 myself;

  void setup() override {
    // Setup PCA9533
    myself.init();
    myself.setPSC(REG_PSC0, 0); // Prescaler for PWM0 150Hz
    myself.setPSC(REG_PSC1, 0); // Prescaler for PWM1 150Hz
    myself.setMODE(IO0, LED_MODE_PWM0);
    myself.setMODE(IO1, LED_MODE_OFF);
    myself.setMODE(IO2, LED_MODE_OFF);
    myself.setMODE(IO3, LED_MODE_OFF);
  }

  void loop() override {
    bool state = myself.getINPUT(IO2);
    publish_state(state);
  }
};

class PCA9533_BinarySensor_IO3 : public Component, public BinarySensor {
 public:
  
  PCA9533 myself;

  void setup() override {
    // Setup PCA9533
    myself.init();
    myself.setPSC(REG_PSC0, 0); // Prescaler for PWM0 150Hz
    myself.setPSC(REG_PSC1, 0); // Prescaler for PWM1 150Hz
    myself.setMODE(IO0, LED_MODE_PWM0);
    myself.setMODE(IO1, LED_MODE_OFF);
    myself.setMODE(IO2, LED_MODE_OFF);
    myself.setMODE(IO3, LED_MODE_OFF);
  }

  void loop() override {
    bool state = myself.getINPUT(IO3);
    publish_state(state);
  }
};

For switch :

#include "esphome.h"
#include "PCA9533.h"

using namespace esphome::output;

class PCA9533_Switch_IO1 : public Component, public Switch {
 public:

  PCA9533 myself;

  void setup() override {
    // Setup PCA9533
    myself.init();
    myself.setPSC(REG_PSC0, 0); // Prescaler for PWM0 150Hz
    myself.setPSC(REG_PSC1, 0); // Prescaler for PWM1 150Hz
    myself.setMODE(IO0, LED_MODE_PWM0);
    myself.setMODE(IO1, LED_MODE_OFF);
    myself.setMODE(IO2, LED_MODE_OFF);
    myself.setMODE(IO3, LED_MODE_OFF);
  }

  void write_state(bool state) override {
    if (state) {
      myself.setMODE(IO1, LED_MODE_OFF);
    }
    else {
      myself.setMODE(IO1, LED_MODE_ON);
    }   
    publish_state(state);
  }
};

For floatoutput :

#include "esphome.h"
#include "PCA9533.h"

using namespace esphome::output;

class PCA9533_FloatOutput_IO0 : public Component, public FloatOutput {
 public:

  PCA9533 myself;

  void setup() override {
    // Setup PCA9533
    myself.init();
    myself.setPSC(REG_PSC0, 0); // Prescaler for PWM0 150Hz
    myself.setPSC(REG_PSC1, 0); // Prescaler for PWM1 150Hz
    myself.setMODE(IO0, LED_MODE_PWM0);
    myself.setMODE(IO1, LED_MODE_OFF);
    myself.setMODE(IO2, LED_MODE_OFF);
    myself.setMODE(IO3, LED_MODE_OFF);
  }

  void write_state(float state) override {
    int value = state * 255;
    myself.setPWM(REG_PWM0, value);    
  }
};

from feature-requests.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.