esphome/esphome/components/hmc5883l/hmc5883l.h
Tim P fa1adfd934 Add QMC5883L Sensor + Improvements to HMC5883L (#671)
* Add QMC5883L and Updated HMC5883L

* add tests

* changed to oversampling

* fix pylint

* fix private method

* typo fix

* fix protected method

* Clean up code and PR recomendations

* fix tests

* remote file

* fix qmc oversampling unit

* Remove hmc5883l config logging

Either the units are converted to the user values like 1x, 8x oversampling or not printed at all. Printing the machine-value of these is only confusing users.

* Changes for validate_enum

Move stuff that can be done beforehand out of the bound function, use text_type for py2/3 compatability.

* Remove unused constant

* Remove duplicate tests

* Repeat remove config print

* remove changes to test2 since bin is to large

* Add comment to HMC5583L


Co-authored-by: Timothy Purchas <timothy@TPF.local>
Co-authored-by: Otto Winter <otto@otto-winter.com>
2019-11-26 18:43:11 +01:00

69 lines
2.1 KiB
C++

#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace hmc5883l {
enum HMC5883LOversampling {
HMC5883L_OVERSAMPLING_1 = 0b000,
HMC5883L_OVERSAMPLING_2 = 0b001,
HMC5883L_OVERSAMPLING_4 = 0b010,
HMC5883L_OVERSAMPLING_8 = 0b011,
};
enum HMC5883LDatarate {
HMC5883L_DATARATE_0_75_HZ = 0b000,
HMC5883L_DATARATE_1_5_HZ = 0b001,
HMC5883L_DATARATE_3_0_HZ = 0b010,
HMC5883L_DATARATE_7_5_HZ = 0b011,
HMC5883L_DATARATE_15_0_HZ = 0b100,
HMC5883L_DATARATE_30_0_HZ = 0b101,
HMC5883L_DATARATE_75_0_HZ = 0b110,
};
enum HMC5883LRange {
HMC5883L_RANGE_88_UT = 0b000,
HMC5883L_RANGE_130_UT = 0b001,
HMC5883L_RANGE_190_UT = 0b010,
HMC5883L_RANGE_250_UT = 0b011,
HMC5883L_RANGE_400_UT = 0b100,
HMC5883L_RANGE_470_UT = 0b101,
HMC5883L_RANGE_560_UT = 0b110,
HMC5883L_RANGE_810_UT = 0b111,
};
class HMC5883LComponent : public PollingComponent, public i2c::I2CDevice {
public:
void setup() override;
void dump_config() override;
float get_setup_priority() const override;
void update() override;
void set_oversampling(HMC5883LOversampling oversampling) { oversampling_ = oversampling; }
void set_datarate(HMC5883LDatarate datarate) { datarate_ = datarate; }
void set_range(HMC5883LRange range) { range_ = range; }
void set_x_sensor(sensor::Sensor *x_sensor) { x_sensor_ = x_sensor; }
void set_y_sensor(sensor::Sensor *y_sensor) { y_sensor_ = y_sensor; }
void set_z_sensor(sensor::Sensor *z_sensor) { z_sensor_ = z_sensor; }
void set_heading_sensor(sensor::Sensor *heading_sensor) { heading_sensor_ = heading_sensor; }
protected:
HMC5883LOversampling oversampling_{HMC5883L_OVERSAMPLING_1};
HMC5883LDatarate datarate_{HMC5883L_DATARATE_15_0_HZ};
HMC5883LRange range_{HMC5883L_RANGE_130_UT};
sensor::Sensor *x_sensor_;
sensor::Sensor *y_sensor_;
sensor::Sensor *z_sensor_;
sensor::Sensor *heading_sensor_;
enum ErrorCode {
NONE = 0,
COMMUNICATION_FAILED,
ID_REGISTERS,
} error_code_;
};
} // namespace hmc5883l
} // namespace esphome