1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-19 00:58:13 +00:00

C Code cleanup

This commit is contained in:
Marcel Koonstra 2024-12-23 06:37:38 +01:00
parent 51b7777e91
commit dcf05efc3e

View File

@ -3,6 +3,8 @@
#include "esphome/components/sensor/sensor.h" #include "esphome/components/sensor/sensor.h"
#include "esphome/components/binary_sensor/binary_sensor.h" #include "esphome/components/binary_sensor/binary_sensor.h"
#pragma once
namespace esphome { namespace esphome {
namespace obd { namespace obd {
@ -34,7 +36,7 @@ class OBDComponent : public PollingComponent {
std::vector<PIDRequest *> pidrequests_{}; std::vector<PIDRequest *> pidrequests_{};
PIDRequest* current_request_{nullptr}; PIDRequest *current_request_{nullptr};
}; };
using data_to_value_t = std::function<float(std::vector<uint8_t>)>; using data_to_value_t = std::function<float(std::vector<uint8_t>)>;
@ -51,7 +53,11 @@ class PIDRequest : public Component {
public: public:
explicit PIDRequest(OBDComponent *parent, const std::uint32_t can_id, const std::uint32_t pid, explicit PIDRequest(OBDComponent *parent, const std::uint32_t can_id, const std::uint32_t pid,
const std::uint32_t can_response_id, const bool use_extended_id) const std::uint32_t can_response_id, const bool use_extended_id)
: parent_(parent), can_id_(can_id), pid_(pid), can_response_id_(can_response_id), use_extended_id_(use_extended_id){ : parent_(parent),
can_id_(can_id),
pid_(pid),
can_response_id_(can_response_id),
use_extended_id_(use_extended_id) {
this->state_ = WAITING; this->state_ = WAITING;
}; };
@ -90,7 +96,7 @@ class PIDRequest : public Component {
class OBDPidTrigger : public Trigger<std::vector<uint8_t>>, public Component { class OBDPidTrigger : public Trigger<std::vector<uint8_t>>, public Component {
public: public:
explicit OBDPidTrigger(PIDRequest *parent) : parent_(parent){} explicit OBDPidTrigger(PIDRequest *parent) : parent_(parent) {}
void setup() override { this->parent_->add_trigger(this); } void setup() override { this->parent_->add_trigger(this); }
@ -105,11 +111,11 @@ class OBDSensorBase {
class OBDSensor : public OBDSensorBase, public sensor::Sensor, public Component { class OBDSensor : public OBDSensorBase, public sensor::Sensor, public Component {
public: public:
explicit OBDSensor(PIDRequest *parent) : parent_(parent){} explicit OBDSensor(PIDRequest *parent) : parent_(parent) {}
void set_template(data_to_value_t &&lambda) { this->data_to_value_func_ = lambda; } void set_template(data_to_value_t &&lambda) { this->data_to_value_func_ = lambda; }
void update(const std::vector<uint8_t> &data) override ; void update(const std::vector<uint8_t> &data) override;
void setup() override { this->parent_->add_sensor(this); } void setup() override { this->parent_->add_sensor(this); }
void dump_config() override; void dump_config() override;
@ -120,7 +126,7 @@ class OBDSensor : public OBDSensorBase, public sensor::Sensor, public Component
class OBDBinarySensor : public OBDSensorBase, public binary_sensor::BinarySensor, public Component { class OBDBinarySensor : public OBDSensorBase, public binary_sensor::BinarySensor, public Component {
public: public:
explicit OBDBinarySensor(PIDRequest *parent) : parent_(parent){} explicit OBDBinarySensor(PIDRequest *parent) : parent_(parent) {}
void set_template(data_to_bool_t &&lambda) { this->data_to_value_func_ = lambda; } void set_template(data_to_bool_t &&lambda) { this->data_to_value_func_ = lambda; }
void update(const std::vector<uint8_t> &data) override; void update(const std::vector<uint8_t> &data) override;
@ -136,19 +142,18 @@ class OBDBinarySensor : public OBDSensorBase, public binary_sensor::BinarySensor
class OBDCanbusTrigger : public canbus::CanbusTrigger, public Action<std::vector<uint8_t>, uint32_t, bool> { class OBDCanbusTrigger : public canbus::CanbusTrigger, public Action<std::vector<uint8_t>, uint32_t, bool> {
public: public:
explicit OBDCanbusTrigger(PIDRequest *parent) : CanbusTrigger(parent->parent_->canbus_, parent->can_response_id_, 0x1FFFFFFF, parent->use_extended_id_), parent_(parent){ explicit OBDCanbusTrigger(PIDRequest *parent)
: CanbusTrigger(parent->parent_->canbus_, parent->can_response_id_, 0x1FFFFFFF, parent->use_extended_id_),
parent_(parent) {
auto automation = new Automation<std::vector<uint8_t>, uint32_t, bool>(this); auto automation = new Automation<std::vector<uint8_t>, uint32_t, bool>(this);
automation->add_action(this); automation->add_action(this);
}; };
void play(std::vector<uint8_t> data, uint32_t can_id, bool rx) override { void play(std::vector<uint8_t> data, uint32_t can_id, bool rx) override { this->parent_->handle_incoming(data); }
this->parent_->handle_incoming(data);
}
protected: protected:
PIDRequest *parent_; PIDRequest *parent_;
}; };
} } // namespace obd
} } // namespace esphome