ESPHome  2022.8.0
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 namespace esphome {
10 namespace sx1509 {
11 
12 // These are used for clock config:
13 const uint8_t INTERNAL_CLOCK_2MHZ = 2;
14 const uint8_t EXTERNAL_CLOCK = 1;
15 const uint8_t SOFTWARE_RESET = 0;
16 const uint8_t HARDWARE_RESET = 1;
17 
21 
22 // for all components that implement the process(uint16_t data )
24  public:
25  virtual void process(uint16_t data){};
26 };
27 
28 class SX1509Component : public Component, public i2c::I2CDevice {
29  public:
30  SX1509Component() = default;
31 
32  void setup() override;
33  void dump_config() override;
34  float get_setup_priority() const override { return setup_priority::HARDWARE; }
35  void loop() override;
36 
37  bool digital_read(uint8_t pin);
38  uint16_t read_key_data();
39  void set_pin_value(uint8_t pin, uint8_t i_on) { this->write_byte(REG_I_ON[pin], i_on); };
40  void pin_mode(uint8_t pin, gpio::Flags flags);
41  void digital_write(uint8_t pin, bool bit_value);
42  uint32_t get_clock() { return this->clk_x_; };
43  void set_rows_cols(uint8_t rows, uint8_t cols) {
44  this->rows_ = rows;
45  this->cols_ = cols;
46  this->has_keypad_ = true;
47  };
48  void set_sleep_time(uint16_t sleep_time) { this->sleep_time_ = sleep_time; };
49  void set_scan_time(uint8_t scan_time) { this->scan_time_ = scan_time; };
50  void set_debounce_time(uint8_t debounce_time = 1) { this->debounce_time_ = debounce_time; };
52  this->keypad_binary_sensors_.push_back(binary_sensor);
53  }
54  void setup_led_driver(uint8_t pin);
55 
56  protected:
57  uint32_t clk_x_ = 2000000;
58  uint8_t frequency_ = 0;
59  uint16_t ddr_mask_ = 0x00;
60  uint16_t input_mask_ = 0x00;
61  uint16_t port_mask_ = 0x00;
62  bool has_keypad_ = false;
63  uint8_t rows_ = 0;
64  uint8_t cols_ = 0;
65  uint16_t sleep_time_ = 128;
66  uint8_t scan_time_ = 1;
67  uint8_t debounce_time_ = 1;
68  std::vector<SX1509Processor *> keypad_binary_sensors_;
69 
70  void setup_keypad_();
71  void set_debounce_config_(uint8_t config_value);
72  void set_debounce_time_(uint8_t time);
73  void set_debounce_pin_(uint8_t pin);
74  void set_debounce_enable_(uint8_t pin);
75  void set_debounce_keypad_(uint8_t time, uint8_t num_rows, uint8_t num_cols);
76  void clock_(uint8_t osc_source = 2, uint8_t osc_pin_function = 1, uint8_t osc_freq_out = 0, uint8_t osc_divider = 0);
77 };
78 
79 } // namespace sx1509
80 } // namespace esphome
void setup()
void register_keypad_binary_sensor(SX1509Processor *binary_sensor)
Definition: sx1509.h:51
void loop()
const uint8_t REG_I_ON[16]
Definition: sx1509.h:18
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:34
void set_debounce_time(uint8_t debounce_time=1)
Definition: sx1509.h:50
const uint8_t EXTERNAL_CLOCK
Definition: sx1509.h:14
const uint8_t REG_I_ON_4
const uint8_t REG_I_ON_2
virtual void process(uint16_t data)
Definition: sx1509.h:25
const uint8_t REG_I_ON_15
const uint8_t INTERNAL_CLOCK_2MHZ
Definition: sx1509.h:13
void set_pin_value(uint8_t pin, uint8_t i_on)
Definition: sx1509.h:39
void set_sleep_time(uint16_t sleep_time)
Definition: sx1509.h:48
const uint8_t REG_I_ON_7
const uint8_t REG_I_ON_5
std::vector< SX1509Processor * > keypad_binary_sensors_
Definition: sx1509.h:68
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:15
void set_scan_time(uint8_t scan_time)
Definition: sx1509.h:49
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:43
const uint8_t HARDWARE_RESET
Definition: sx1509.h:16