Static functions

This commit is contained in:
Jesse Hills 2024-11-05 16:51:14 +13:00
parent def65ba1f1
commit 8d211eb257
2 changed files with 37 additions and 39 deletions

View file

@ -104,6 +104,39 @@ static uint8_t find_nearest_index(float value, const float *arr, int size) {
return nearest_index; return nearest_index;
} }
/**
* @brief Convert a float value to a byte array.
*
* This function converts a float value to a byte array.
*
* @param value The float value to convert.
* @param bytes The byte array to store the converted value.
*/
static void float_to_bytes(float value, unsigned char *bytes) {
union {
float float_value;
unsigned char byte_array[4];
} u;
u.float_value = value;
memcpy(bytes, u.byte_array, 4);
}
/**
* @brief Convert a 32-bit unsigned integer to a byte array.
*
* This function converts a 32-bit unsigned integer to a byte array.
*
* @param value The 32-bit unsigned integer to convert.
* @param bytes The byte array to store the converted value.
*/
static void int_to_bytes(uint32_t value, unsigned char *bytes) {
bytes[0] = value & 0xFF;
bytes[1] = (value >> 8) & 0xFF;
bytes[2] = (value >> 16) & 0xFF;
bytes[3] = (value >> 24) & 0xFF;
}
void MR60FDA2Component::split_frame_(uint8_t buffer) { void MR60FDA2Component::split_frame_(uint8_t buffer) {
switch (this->current_frame_locate_) { switch (this->current_frame_locate_) {
case LOCATE_FRAME_HEADER: // starting buffer case LOCATE_FRAME_HEADER: // starting buffer
@ -294,43 +327,10 @@ void MR60FDA2Component::process_frame_() {
// Sending data frames // Sending data frames
void MR60FDA2Component::send_query_(uint8_t *query, size_t string_length) { this->write_array(query, string_length); } void MR60FDA2Component::send_query_(uint8_t *query, size_t string_length) { this->write_array(query, string_length); }
/**
* @brief Convert a float value to a byte array.
*
* This function converts a float value to a byte array.
*
* @param value The float value to convert.
* @param bytes The byte array to store the converted value.
*/
void MR60FDA2Component::float_to_bytes_(float value, unsigned char *bytes) {
union {
float float_value;
unsigned char byte_array[4];
} u;
u.float_value = value;
memcpy(bytes, u.byte_array, 4);
}
/**
* @brief Convert a 32-bit unsigned integer to a byte array.
*
* This function converts a 32-bit unsigned integer to a byte array.
*
* @param value The 32-bit unsigned integer to convert.
* @param bytes The byte array to store the converted value.
*/
void MR60FDA2Component::int_to_bytes_(uint32_t value, unsigned char *bytes) {
bytes[0] = value & 0xFF;
bytes[1] = (value >> 8) & 0xFF;
bytes[2] = (value >> 16) & 0xFF;
bytes[3] = (value >> 24) & 0xFF;
}
// Send Heartbeat Packet Command // Send Heartbeat Packet Command
void MR60FDA2Component::set_install_height(uint8_t index) { void MR60FDA2Component::set_install_height(uint8_t index) {
uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00}; uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00};
float_to_bytes_(INSTALL_HEIGHT[index], &send_data[8]); float_to_bytes(INSTALL_HEIGHT[index], &send_data[8]);
send_data[12] = calculate_checksum(send_data + 8, 4); send_data[12] = calculate_checksum(send_data + 8, 4);
this->send_query_(send_data, 13); this->send_query_(send_data, 13);
ESP_LOGV(TAG, "SEND INSTALL HEIGHT FRAME: %s", format_hex_pretty(send_data, 13).c_str()); ESP_LOGV(TAG, "SEND INSTALL HEIGHT FRAME: %s", format_hex_pretty(send_data, 13).c_str());
@ -338,7 +338,7 @@ void MR60FDA2Component::set_install_height(uint8_t index) {
void MR60FDA2Component::set_height_threshold(uint8_t index) { void MR60FDA2Component::set_height_threshold(uint8_t index) {
uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x08, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00}; uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x08, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00};
float_to_bytes_(INSTALL_HEIGHT[index], &send_data[8]); float_to_bytes(INSTALL_HEIGHT[index], &send_data[8]);
send_data[12] = calculate_checksum(send_data + 8, 4); send_data[12] = calculate_checksum(send_data + 8, 4);
this->send_query_(send_data, 13); this->send_query_(send_data, 13);
ESP_LOGV(TAG, "SEND HEIGHT THRESHOLD: %s", format_hex_pretty(send_data, 13).c_str()); ESP_LOGV(TAG, "SEND HEIGHT THRESHOLD: %s", format_hex_pretty(send_data, 13).c_str());
@ -347,7 +347,7 @@ void MR60FDA2Component::set_height_threshold(uint8_t index) {
void MR60FDA2Component::set_sensitivity(uint8_t index) { void MR60FDA2Component::set_sensitivity(uint8_t index) {
uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x0A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00}; uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x0A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00};
int_to_bytes_(SENSITIVITY[index], &send_data[8]); int_to_bytes(SENSITIVITY[index], &send_data[8]);
send_data[12] = calculate_checksum(send_data + 8, 4); send_data[12] = calculate_checksum(send_data + 8, 4);
this->send_query_(send_data, 13); this->send_query_(send_data, 13);

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "esphome/core/defines.h"
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/defines.h"
#ifdef USE_BINARY_SENSOR #ifdef USE_BINARY_SENSOR
#include "esphome/components/binary_sensor/binary_sensor.h" #include "esphome/components/binary_sensor/binary_sensor.h"
#endif #endif
@ -85,8 +85,6 @@ class MR60FDA2Component : public Component,
void split_frame_(uint8_t buffer); void split_frame_(uint8_t buffer);
void process_frame_(); void process_frame_();
void send_query_(uint8_t *query, size_t string_length); void send_query_(uint8_t *query, size_t string_length);
void float_to_bytes_(float value, unsigned char *bytes);
void int_to_bytes_(uint32_t value, unsigned char *bytes);
public: public:
float get_setup_priority() const override { return esphome::setup_priority::LATE; } float get_setup_priority() const override { return esphome::setup_priority::LATE; }