From 3b146b3bdf7c43677875d76e8659e57e9212f2d2 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Thu, 30 Nov 2023 20:01:39 +0700 Subject: [PATCH] refactor and comment --- src/espmega_iot_core.cpp | 76 ++++++++++++++++++++++++++++++++++++++++ src/espmega_iot_core.hpp | 6 +++- src/user_code.hpp | 35 +++++++++++++----- 3 files changed, 108 insertions(+), 9 deletions(-) diff --git a/src/espmega_iot_core.cpp b/src/espmega_iot_core.cpp index 64a7450..e895f15 100644 --- a/src/espmega_iot_core.cpp +++ b/src/espmega_iot_core.cpp @@ -1866,6 +1866,11 @@ void disable_adc(int id) publish_adc_state(id); } +/** + * Publishes the state of an ADC (Analog-to-Digital Converter) to the MQTT broker. + * + * @param id The ID of the ADC. + */ void publish_adc_state(int id) { ADC_STATE_TOPIC[base_topic_length + 4] = ((id - id % 10) / 10) + '0'; @@ -1873,6 +1878,11 @@ void publish_adc_state(int id) mqtt.publish(ADC_STATE_TOPIC, adc_report_enable[id] ? "on" : "off"); } +/** + * @brief Publishes the ADC states. + * + * This function iterates over the ADC channels and publishes the state of each channel. + */ void publish_adc_states() { for (int i = 0; i < ADC_COUNT; i++) @@ -1958,6 +1968,11 @@ void publish_adc_values() } } +/** + * Publishes the state of a DAC (Digital-to-Analog Converter) to the MQTT broker. + * + * @param id The ID of the DAC. + */ void publish_dac_state(int id) { DAC_STATE_TOPIC[base_topic_length + 4] = ((id - id % 10) / 10) + '0'; @@ -1965,6 +1980,11 @@ void publish_dac_state(int id) mqtt.publish(DAC_STATE_TOPIC, dac_states[id] ? "on" : "off"); } +/** + * Publishes the DAC value for a given ID. + * + * @param id The ID of the DAC value to publish. + */ void publish_dac_value(int id) { DAC_VALUE_TOPIC[base_topic_length + 4] = ((id - id % 10) / 10) + '0'; @@ -2052,6 +2072,13 @@ void dac_set_state(int id, bool state) publish_dac_state(id); } +/** + * @brief Publishes the states of all DACs. + * + * This function iterates through all DACs and publishes their states. + * + * @return void + */ void publish_dac_states() { for (int i = 0; i < DAC_COUNT; i++) @@ -2060,6 +2087,13 @@ void publish_dac_states() } } +/** + * @brief Publishes the DAC values. + * + * This function iterates through all the DAC channels and publishes their values. + * + * @return void + */ void publish_dac_values() { for (int i = 0; i < DAC_COUNT; i++) @@ -2109,4 +2143,46 @@ void dac_set_state_callback(char *topic, uint8_t topic_length, char *payload, un dac_set_state(id, false); } } + +/** + * @brief Get the ADC value for the specified ID. + * + * @param id The ID of the ADC channel. + * @return The ADC value. + */ +uint16_t adc_get_value(int id) +{ + return adc_values[id]; +} +/** + * @brief Get the state of the ADC with the specified ID. + * + * @param id The ID of the ADC. + * @return true if the ADC is enabled, false otherwise. + */ +bool adc_get_state(int id) +{ + return adc_report_enable[id]; +} +/** + * @brief Get the value of a DAC channel. + * + * @param id The ID of the DAC channel. + * @return The value of the DAC channel. + */ +uint16_t dac_get_value(int id) +{ + return dac_values[id]; +} +/** + * @brief Get the state of the DAC with the specified ID. + * + * @param id The ID of the DAC. + * @return The state of the DAC. + */ +bool dac_get_state(int id) +{ + return dac_states[id]; +} + #endif diff --git a/src/espmega_iot_core.hpp b/src/espmega_iot_core.hpp index 531735a..f0b63af 100644 --- a/src/espmega_iot_core.hpp +++ b/src/espmega_iot_core.hpp @@ -152,4 +152,8 @@ void publish_dac_state(int id); void publish_dac_values(); void publish_dac_states(); void publish_adc_state(int id); -void publish_adc_states(); \ No newline at end of file +void publish_adc_states(); +uint16_t adc_get_value(int id); +bool adc_get_state(int id); +uint16_t dac_get_value(int id); +bool dac_get_state(int id); \ No newline at end of file diff --git a/src/user_code.hpp b/src/user_code.hpp index 395a6e4..9de5311 100644 --- a/src/user_code.hpp +++ b/src/user_code.hpp @@ -41,18 +41,20 @@ #define ANALOG_REPORTING_INTERVAL 500 // User Defined Functions +void timer1_callback(); +void bt0PopCallback(void *ptr); + +// User Defined IoT Core Callback Functions (Required) +void user_mqtt_callback(char* topic, uint8_t topic_length, char* payload, unsigned int payload_length); +void user_state_request_callback(); +void mqtt_connected_user_callback(); void user_pre_init(); void user_init(); void user_loop(); -void virtual_interrupt_user_callback(int pin, int state); void pwm_changed_user_callback(int pin); void ac_changed_user_callback(int mode, int temperature, int fan_speed); void timer_tick_callback(); -void timer1_callback(); -void mqtt_connected_user_callback(); -void bt0PopCallback(void *ptr); -void user_state_request_callback(); -void user_mqtt_callback(char* topic, uint8_t topic_length, char* payload, unsigned int payload_length); +void virtual_interrupt_user_callback(int pin, int state); // ESPMega IoT Core Build-in Functions extern void pwm_set_state(int id, int state); @@ -64,9 +66,26 @@ extern bool pwm_get_state(int id); extern uint16_t pwm_get_value(int id); extern boolean pwm_group_state(int id1, int id2); extern bool input_get_state(int id); +extern bool standalone; +extern PubSubClient mqtt; + +// IR Kit Build-in Functions +#ifdef ENABLE_IR_MODULE extern void ac_set_state(int mode, int temperature, int fan_speed); extern uint8_t ac_get_temperature(); extern uint8_t ac_get_mode(); extern uint8_t ac_get_fan_speed(); -extern bool standalone; -extern PubSubClient mqtt; \ No newline at end of file +#endif + +//Analog Expansion Card Build-in Functions +#ifdef ENABLE_ANALOG_MODULE +extern void dac_set_value(int id, int value); +extern void dac_set_state(int id, bool state); +extern void enable_adc(int id); +extern void disable_adc(int id); +extern void enable_dac(int id); +extern uint16_t adc_get_value(int id); +extern bool adc_get_state(int id); +extern uint16_t dac_get_value(int id); +extern bool dac_get_state(int id); +#endif \ No newline at end of file