ESPHome  2024.3.1
sx1509.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/hal.h"
6 #include "sx1509_gpio_pin.h"
7 #include "sx1509_registers.h"
8 
9 #include <vector>
10 
11 namespace esphome {
12 namespace sx1509 {
13 
14 // These are used for clock config:
15 const uint8_t INTERNAL_CLOCK_2MHZ = 2;
16 const uint8_t EXTERNAL_CLOCK = 1;
17 const uint8_t SOFTWARE_RESET = 0;
18 const uint8_t HARDWARE_RESET = 1;
19 
23 
24 // for all components that implement the process(uint16_t data )
26  public:
27  virtual void process(uint16_t data){};
28 };
29 
30 class SX1509Component : public Component, public i2c::I2CDevice {
31  public:
32  SX1509Component() = default;
33 
34  void setup() override;
35  void dump_config() override;
36  float get_setup_priority() const override { return setup_priority::HARDWARE; }
37  void loop() override;
38 
39  bool digital_read(uint8_t pin);
40  uint16_t read_key_data();
41  void set_pin_value(uint8_t pin, uint8_t i_on) { this->write_byte(REG_I_ON[pin], i_on); };
42  void pin_mode(uint8_t pin, gpio::Flags flags);
43  void digital_write(uint8_t pin, bool bit_value);
44  uint32_t get_clock() { return this->clk_x_; };
45  void set_rows_cols(uint8_t rows, uint8_t cols) {
46  this->rows_ = rows;
47  this->cols_ = cols;
48  this->has_keypad_ = true;
49  };
50  void set_sleep_time(uint16_t sleep_time) { this->sleep_time_ = sleep_time; };
51  void set_scan_time(uint8_t scan_time) { this->scan_time_ = scan_time; };
52  void set_debounce_time(uint8_t debounce_time = 1) { this->debounce_time_ = debounce_time; };
54  this->keypad_binary_sensors_.push_back(binary_sensor);
55  }
56  void setup_led_driver(uint8_t pin);
57 
58  protected:
59  uint32_t clk_x_ = 2000000;
60  uint8_t frequency_ = 0;
61  uint16_t ddr_mask_ = 0x00;
62  uint16_t input_mask_ = 0x00;
63  uint16_t port_mask_ = 0x00;
64  uint16_t output_state_ = 0x00;
65  bool has_keypad_ = false;
66  uint8_t rows_ = 0;
67  uint8_t cols_ = 0;
68  uint16_t sleep_time_ = 128;
69  uint8_t scan_time_ = 1;
70  uint8_t debounce_time_ = 1;
71  std::vector<SX1509Processor *> keypad_binary_sensors_;
72 
73  uint32_t last_loop_timestamp_ = 0;
74  const uint32_t min_loop_period_ = 15; // ms
75 
76  void setup_keypad_();
77  void set_debounce_config_(uint8_t config_value);
78  void set_debounce_time_(uint8_t time);
79  void set_debounce_pin_(uint8_t pin);
80  void set_debounce_enable_(uint8_t pin);
81  void set_debounce_keypad_(uint8_t time, uint8_t num_rows, uint8_t num_cols);
82  void clock_(uint8_t osc_source = 2, uint8_t osc_pin_function = 1, uint8_t osc_freq_out = 0, uint8_t osc_divider = 0);
83 };
84 
85 } // namespace sx1509
86 } // namespace esphome
void setup()
void register_keypad_binary_sensor(SX1509Processor *binary_sensor)
Definition: sx1509.h:53
void loop()
const uint8_t REG_I_ON[16]
Definition: sx1509.h:20
const uint8_t REG_I_ON_9
const uint8_t REG_I_ON_6
const uint8_t REG_I_ON_10
const uint8_t REG_I_ON_3
float get_setup_priority() const override
Definition: sx1509.h:36
void set_debounce_time(uint8_t debounce_time=1)
Definition: sx1509.h:52
const uint8_t EXTERNAL_CLOCK
Definition: sx1509.h:16
const uint8_t REG_I_ON_4
const uint8_t REG_I_ON_2
virtual void process(uint16_t data)
Definition: sx1509.h:27
const uint8_t REG_I_ON_15
const uint8_t INTERNAL_CLOCK_2MHZ
Definition: sx1509.h:15
void set_pin_value(uint8_t pin, uint8_t i_on)
Definition: sx1509.h:41
void set_sleep_time(uint16_t sleep_time)
Definition: sx1509.h:50
const uint8_t REG_I_ON_7
const uint8_t REG_I_ON_5
std::vector< SX1509Processor * > keypad_binary_sensors_
Definition: sx1509.h:71
const uint8_t REG_I_ON_14
const uint8_t REG_I_ON_13
const uint8_t REG_I_ON_12
const uint32_t flags
Definition: stm32flash.h:85
const uint8_t REG_I_ON_11
const uint8_t REG_I_ON_1
const uint8_t SOFTWARE_RESET
Definition: sx1509.h:17
void set_scan_time(uint8_t scan_time)
Definition: sx1509.h:51
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
const uint8_t REG_I_ON_0
const uint8_t REG_I_ON_8
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133
void set_rows_cols(uint8_t rows, uint8_t cols)
Definition: sx1509.h:45
const uint8_t HARDWARE_RESET
Definition: sx1509.h:18