ESPHome  2022.12.8
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  bool has_keypad_ = false;
65  uint8_t rows_ = 0;
66  uint8_t cols_ = 0;
67  uint16_t sleep_time_ = 128;
68  uint8_t scan_time_ = 1;
69  uint8_t debounce_time_ = 1;
70  std::vector<SX1509Processor *> keypad_binary_sensors_;
71 
72  void setup_keypad_();
73  void set_debounce_config_(uint8_t config_value);
74  void set_debounce_time_(uint8_t time);
75  void set_debounce_pin_(uint8_t pin);
76  void set_debounce_enable_(uint8_t pin);
77  void set_debounce_keypad_(uint8_t time, uint8_t num_rows, uint8_t num_cols);
78  void clock_(uint8_t osc_source = 2, uint8_t osc_pin_function = 1, uint8_t osc_freq_out = 0, uint8_t osc_divider = 0);
79 };
80 
81 } // namespace sx1509
82 } // 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:70
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:17
Definition: a4988.cpp:4
const uint8_t REG_I_ON_0
const uint8_t REG_I_ON_8
void set_rows_cols(uint8_t rows, uint8_t cols)
Definition: sx1509.h:45
const uint8_t HARDWARE_RESET
Definition: sx1509.h:18