From 7bf49d099720fc238b05e05fe240be20482da970 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 29 Sep 2023 23:18:21 +0700 Subject: [PATCH] initial structure --- .vscode/settings.json | 3 +- src/{main.cpp => espmega_iot_core.cpp} | 100 ++----------------------- src/espmega_iot_core.hpp | 87 +++++++++++++++++++++ src/external_lcd.cpp | 5 ++ src/external_lcd.hpp | 12 +++ src/user_code.cpp | 17 +++++ src/user_code.hpp | 30 +++----- 7 files changed, 139 insertions(+), 115 deletions(-) rename src/{main.cpp => espmega_iot_core.cpp} (91%) create mode 100644 src/espmega_iot_core.hpp create mode 100644 src/external_lcd.cpp create mode 100644 src/external_lcd.hpp create mode 100644 src/user_code.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json index 531dfd6..d54d5a5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,6 +10,7 @@ "functional": "cpp", "tuple": "cpp", "type_traits": "cpp", - "utility": "cpp" + "utility": "cpp", + "*.d": "cpp" } } \ No newline at end of file diff --git a/src/main.cpp b/src/espmega_iot_core.cpp similarity index 91% rename from src/main.cpp rename to src/espmega_iot_core.cpp index 963efff..5f1e1e2 100644 --- a/src/main.cpp +++ b/src/espmega_iot_core.cpp @@ -1,17 +1,5 @@ -// #define USE_INTERRUPT -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - +#include +#include // OS Configuration #define FASTBOOT @@ -104,77 +92,6 @@ char AC_HUMIDITY_TOPIC[75]; #define EEPROM_ADDRESS_MQTT_SERVER 116 // 04bytes, thru 119 #define EEPROM_ADDRESS_MQTT_PORT 120 // 02bytes, thru 121 -// Forward declaration -void virtual_interrupt_loop(); -void virtual_interrupt_callback(int pin, int state); -void network_begin(); -void mqtt_connect(); -void mqtt_subscribe(); -void thread_initialization(); -void pwm_state_callback(String topic, String message); -void pwm_value_callback(String topic, String message); -void state_request_callback(String topic, String message); -void io_begin(); -void ir_loop(); - -void publish_pwm_states(); -void publish_pwm_state(int id); -void pwm_set_state(int id, int state); -void pwm_set_value(int id, int value); -void pwm_toggle(int id); -void pwm_toggle(int id1, int id2); -void pwm_cycle_value(int id); -boolean pwm_group_state(int id1, int id2); - -void publish_ac_state(); -void publish_env_state(); -void ac_state_callback(String topic, String message); -void ac_set_state(int mode, int temperature, int fan_speed); - -void publish_input_states(); -void publish_input_state(int id); -void publish_input_state(int id, int state); - -void lcd_begin(); -void lcd_loop(); -void lcd_refresh(); -void lcd_refresh_pd(); -void lcd_top_bar_update(); -void trigger0(); -void trigger1(); -void trigger2(); -void trigger3(); -void trigger4(); -void trigger5(); -void trigger6(); -void trigger7(); -void trigger8(); -void trigger9(); -void trigger10(); -void trigger11(); -void trigger12(); -void trigger13(); - -void eeprom_retrieve_init(); -void eeprom_pwm_update(); - -void lcd_ac_refresh_fan(); -void lcd_ac_refresh_mode(); - -void set_ip(String address); -void set_netmask(String address); -void set_dns(String address); -void set_gw(String address); -void set_mqtt_server(String address); -void eeprom_ip_update(uint16_t rom_address, uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4); -IPAddress eeprom_ip_retrieve(uint16_t rom_address); -void set_hostname(String hostname); -void eeprom_hostname_retrieve(); -void set_basetopic(String topic); -void eeprom_basetopic_retrieve(); -void mqtt_port_set(uint16_t port); -void eeprom_mqtt_port_retrieve(); - char PWM_STATE_TOPIC[75]; char PWM_VALUE_TOPIC[75]; char INPUTS_TOPIC[75]; @@ -197,6 +114,7 @@ StaticThreadController<2> lcd_thread_controller(&top_bar_updater, &page_updater) void setup() { Serial.begin(115200); + Serial2.begin(115200); panel.begin(115200); Serial.println("ESPMega R3 Initializing"); ESPMega_begin(); @@ -218,11 +136,7 @@ void setup() thread_initialization(); Serial.println("Initialization Completed."); Serial.println("Jumping to User Code."); - // Begin User Code - - /* - Pre-NTP Implementation - */ + user_init(); lcd_send_command("page dashboard"); } @@ -234,6 +148,7 @@ void loop() ir_loop(); thread_controller.run(); lcd_loop(); + user_loop(); } void eeprom_retrieve_init() @@ -440,10 +355,7 @@ void virtual_interrupt_callback(int pin, int state) // Serial.printf("Pin %d changed to %d\n", pin, state); if (lcd_current_page == 2) panel.writeNum("I" + String(pin) + ".val", state); - if (pin == 15) - { - pwm_toggle(4); - } + virtual_interrupt_user_callback(pin, state); } void virtual_interrupt_loop() diff --git a/src/espmega_iot_core.hpp b/src/espmega_iot_core.hpp new file mode 100644 index 0000000..9f64f4e --- /dev/null +++ b/src/espmega_iot_core.hpp @@ -0,0 +1,87 @@ +#ifndef CORE_LOADED +#define CORE_LOADED + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void virtual_interrupt_loop(); +void virtual_interrupt_callback(int pin, int state); +void network_begin(); +void mqtt_connect(); +void mqtt_subscribe(); +void thread_initialization(); +void pwm_state_callback(String topic, String message); +void pwm_value_callback(String topic, String message); +void state_request_callback(String topic, String message); +void io_begin(); +void ir_loop(); + +void publish_pwm_states(); +void publish_pwm_state(int id); +void pwm_set_state(int id, int state); +void pwm_set_value(int id, int value); +void pwm_toggle(int id); +void pwm_toggle(int id1, int id2); +void pwm_cycle_value(int id); +boolean pwm_group_state(int id1, int id2); + +void publish_ac_state(); +void publish_env_state(); +void ac_state_callback(String topic, String message); +void ac_set_state(int mode, int temperature, int fan_speed); + +void publish_input_states(); +void publish_input_state(int id); +void publish_input_state(int id, int state); + +void lcd_begin(); +void lcd_loop(); +void lcd_refresh(); +void lcd_refresh_pd(); +void lcd_top_bar_update(); +void trigger0(); +void trigger1(); +void trigger2(); +void trigger3(); +void trigger4(); +void trigger5(); +void trigger6(); +void trigger7(); +void trigger8(); +void trigger9(); +void trigger10(); +void trigger11(); +void trigger12(); +void trigger13(); + +void eeprom_retrieve_init(); +void eeprom_pwm_update(); + +void lcd_ac_refresh_fan(); +void lcd_ac_refresh_mode(); + +void set_ip(String address); +void set_netmask(String address); +void set_dns(String address); +void set_gw(String address); +void set_mqtt_server(String address); +void eeprom_ip_update(uint16_t rom_address, uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4); +IPAddress eeprom_ip_retrieve(uint16_t rom_address); +void set_hostname(String hostname); +void eeprom_hostname_retrieve(); +void set_basetopic(String topic); +void eeprom_basetopic_retrieve(); +void mqtt_port_set(uint16_t port); +void eeprom_mqtt_port_retrieve(); + +#endif \ No newline at end of file diff --git a/src/external_lcd.cpp b/src/external_lcd.cpp new file mode 100644 index 0000000..cac60f5 --- /dev/null +++ b/src/external_lcd.cpp @@ -0,0 +1,5 @@ +#include +void ExternalLCD::callTriggerFunction() { + uint8_t touch_hex = Serial2.read(); + external_lcd_callback(touch_hex); +} \ No newline at end of file diff --git a/src/external_lcd.hpp b/src/external_lcd.hpp new file mode 100644 index 0000000..3398b29 --- /dev/null +++ b/src/external_lcd.hpp @@ -0,0 +1,12 @@ +#include +#ifndef trigger_h +#include "trigger.h" +#endif +#ifndef EXTERNAL_LCD_LOADED +#define EXTERNAL_LCD_LOADED +#include +class ExternalLCD: public EasyNex { + protected: + void callTriggerFunction(); +}; +#endif \ No newline at end of file diff --git a/src/user_code.cpp b/src/user_code.cpp new file mode 100644 index 0000000..fb0ed74 --- /dev/null +++ b/src/user_code.cpp @@ -0,0 +1,17 @@ +#include + +void user_pre_init() { + +} +void user_init() { + +} +void user_loop() { + +} +void virtual_interrupt_user_callback(int pin, int state) { + +} +void timer_tick_callback() { + +} diff --git a/src/user_code.hpp b/src/user_code.hpp index a7056a7..f2e39a4 100644 --- a/src/user_code.hpp +++ b/src/user_code.hpp @@ -1,23 +1,13 @@ #ifndef USER_CODE #define USER_CODE -#include -void user_pre_init() { - -} - -void user_init() { - -} - -void user_loop() { - -} -void virtual_interrupt_user_callback(int pin, int state) { - - -} -void timer_tick_callback() { - -} - +#ifndef CORE_LOADED +#define CORE_LOADED +#include +#endif +void user_pre_init(); +void user_init(); +void user_loop(); +void virtual_interrupt_user_callback(int pin, int state); +void timer_tick_callback(); +void external_lcd_callback(int touch_hex); #endif \ No newline at end of file