ESPHome  2024.11.1
ens160_spi.cpp
Go to the documentation of this file.
1 #include <cstdint>
2 #include <cstddef>
3 
4 #include "ens160_spi.h"
6 
7 namespace esphome {
8 namespace ens160_spi {
9 
10 static const char *const TAG = "ens160_spi.sensor";
11 
12 inline uint8_t reg_read(uint8_t reg) { return (reg << 1) | 0x01; }
13 
14 inline uint8_t reg_write(uint8_t reg) { return (reg << 1) & 0xFE; }
15 
16 void ENS160SPIComponent::setup() {
17  this->spi_setup();
19 };
20 
21 void ENS160SPIComponent::dump_config() {
22  ENS160Component::dump_config();
23  LOG_PIN(" CS Pin: ", this->cs_);
24 }
25 
26 bool ENS160SPIComponent::read_byte(uint8_t a_register, uint8_t *data) {
27  this->enable();
28  this->transfer_byte(reg_read(a_register));
29  *data = this->transfer_byte(0);
30  this->disable();
31  return true;
32 }
33 
34 bool ENS160SPIComponent::write_byte(uint8_t a_register, uint8_t data) {
35  this->enable();
36  this->transfer_byte(reg_write(a_register));
37  this->transfer_byte(data);
38  this->disable();
39  return true;
40 }
41 
42 bool ENS160SPIComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) {
43  this->enable();
44  this->transfer_byte(reg_read(a_register));
45  this->read_array(data, len);
46  this->disable();
47  return true;
48 }
49 
50 bool ENS160SPIComponent::write_bytes(uint8_t a_register, uint8_t *data, size_t len) {
51  this->enable();
52  this->transfer_byte(reg_write(a_register));
53  this->transfer_array(data, len);
54  this->disable();
55  return true;
56 }
57 
58 } // namespace ens160_spi
59 } // namespace esphome
void setup()
void transfer_array(uint8_t *data, size_t length)
Write the array data, replace with received data.
Definition: spi.h:447
GPIOPin * cs_
Definition: spi.h:378
uint8_t reg_read(uint8_t reg)
Definition: ens160_spi.cpp:12
std::string size_t len
Definition: helpers.h:293
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t reg_write(uint8_t reg)
Definition: ens160_spi.cpp:14