From bf1de69f242eace57e4f74c6928d71acafb36b56 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 30 Sep 2023 00:06:49 +0700 Subject: [PATCH] user code --- src/espmega_iot_core.cpp | 45 ++++++++++++++++++++++++++--- src/espmega_iot_core.hpp | 18 +++++++----- src/external_lcd.hpp | 2 +- src/{daikin_ir.hpp => ir_codes.hpp} | 9 ++---- src/user_code.cpp | 24 +++++++++++++-- src/user_code.hpp | 39 +++++++++++++++++++++---- 6 files changed, 110 insertions(+), 27 deletions(-) rename src/{daikin_ir.hpp => ir_codes.hpp} (90%) diff --git a/src/espmega_iot_core.cpp b/src/espmega_iot_core.cpp index 5f1e1e2..3dcaaf3 100644 --- a/src/espmega_iot_core.cpp +++ b/src/espmega_iot_core.cpp @@ -1,5 +1,4 @@ #include -#include // OS Configuration #define FASTBOOT @@ -47,7 +46,7 @@ char PWM_SET_STATE_TOPIC[70]; char PWM_SET_VALUE_TOPIC[70]; // Infrared Transciever -#define IR_RECIEVE_PIN 32 +#define IR_RECIEVE_PIN 35 #define IR_SEND_PIN 15 #define MARK_EXCESS_MICROS 20 #define RAW_BUFFER_LENGTH 750 @@ -56,13 +55,16 @@ char PWM_SET_VALUE_TOPIC[70]; int lcd_current_page = 1; int lcd_pwmAdj_id = 0; EasyNex panel(Serial); +#ifdef ENABLE_EXTERNAL_LCD +EasyNex user_panel(Serial); +#endif // Air Conditioner Control /* Mode 0: Off, 1: Cool, 2: Fan Fan Speed 0: Auto, 1: High, 2: Mid, 3: Low */ -#define DHT22_PIN 17 +#define DHT22_PIN 32 uint8_t ac_mode = 0; uint8_t ac_fan_speed = 0; uint8_t ac_temperature = 25; @@ -105,7 +107,8 @@ DHTNEW env_sensor(DHT22_PIN); Thread mqtt_reconnector = Thread(); Thread environment_reporter = Thread(); Thread eeprom_pwm_updater = Thread(); -StaticThreadController<3> thread_controller(&mqtt_reconnector, &environment_reporter, &eeprom_pwm_updater); +Thread user_timer_tick = Thread(); +StaticThreadController<4> thread_controller(&mqtt_reconnector, &environment_reporter, &eeprom_pwm_updater, &user_timer_tick); Thread top_bar_updater = Thread(); Thread page_updater = Thread(); @@ -114,10 +117,14 @@ StaticThreadController<2> lcd_thread_controller(&top_bar_updater, &page_updater) void setup() { Serial.begin(115200); + #ifdef ENABLE_EXTERNAL_LCD Serial2.begin(115200); + user_panel.begin(115200); + #endif panel.begin(115200); Serial.println("ESPMega R3 Initializing"); ESPMega_begin(); + user_pre_init(); io_begin(); eeprom_retrieve_init(); lcd_send_stop_bit(); @@ -138,6 +145,12 @@ void setup() Serial.println("Jumping to User Code."); user_init(); lcd_send_command("page dashboard"); + #ifdef ENABLE_EXTERNAL_LCD + Serial2.print("rest"); + Serial2.write(0xFF); + Serial2.write(0xFF); + Serial2.write(0xFF); + #endif } void loop() @@ -322,6 +335,8 @@ void thread_initialization() environment_reporter.setInterval(5000); eeprom_pwm_updater.onRun(eeprom_pwm_update); eeprom_pwm_updater.setInterval(1000); + user_timer_tick.onRun(timer_tick_callback); + user_timer_tick.setInterval(5000); } void pwm_state_callback(String topic, String message) @@ -943,4 +958,26 @@ void eeprom_mqtt_port_retrieve() uint8_t port_arr[2]; ESPMega_FRAM.read(EEPROM_ADDRESS_MQTT_PORT, port_arr, 2); memcpy(&MQTT_PORT, port_arr, 2); +} + +boolean pwm_get_state(int id) { + return pwm_states[id]; +} + +uint16_t pwm_get_value(int id) { + return pwm_values[id]; +} + +boolean input_get_state(int id) { + return virtual_interupt_state[id]; +} + +uint8_t ac_get_temperature() { + return ac_temperature; +} +uint8_t ac_get_mode() { + return ac_mode; +} +uint8_t ac_get_fan_speed() { + return ac_fan_speed; } \ No newline at end of file diff --git a/src/espmega_iot_core.hpp b/src/espmega_iot_core.hpp index 9f64f4e..bcf4a4e 100644 --- a/src/espmega_iot_core.hpp +++ b/src/espmega_iot_core.hpp @@ -1,5 +1,4 @@ -#ifndef CORE_LOADED -#define CORE_LOADED +#pragma once #include #include @@ -8,11 +7,12 @@ #include #include #include -#include #include #include #include -#include +#include "lcd.hpp" +#include "user_code.hpp" +#include "ir_codes.hpp" void virtual_interrupt_loop(); void virtual_interrupt_callback(int pin, int state); @@ -33,10 +33,16 @@ 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); +bool pwm_get_state(int id); +uint16_t pwm_get_value(int id); boolean pwm_group_state(int id1, int id2); +bool input_get_state(int id); void publish_ac_state(); void publish_env_state(); +uint8_t ac_get_temperature(); +uint8_t ac_get_mode(); +uint8_t ac_get_fan_speed(); void ac_state_callback(String topic, String message); void ac_set_state(int mode, int temperature, int fan_speed); @@ -82,6 +88,4 @@ 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 +void eeprom_mqtt_port_retrieve(); \ No newline at end of file diff --git a/src/external_lcd.hpp b/src/external_lcd.hpp index 3398b29..3e16ecd 100644 --- a/src/external_lcd.hpp +++ b/src/external_lcd.hpp @@ -4,7 +4,7 @@ #endif #ifndef EXTERNAL_LCD_LOADED #define EXTERNAL_LCD_LOADED -#include +#include "user_code.hpp" class ExternalLCD: public EasyNex { protected: void callTriggerFunction(); diff --git a/src/daikin_ir.hpp b/src/ir_codes.hpp similarity index 90% rename from src/daikin_ir.hpp rename to src/ir_codes.hpp index d3ae69a..8318594 100644 --- a/src/daikin_ir.hpp +++ b/src/ir_codes.hpp @@ -1,9 +1,5 @@ -#ifndef DAIKIN_IR -#define DAIKIN_IR -#ifndef ESPMEGA +#pragma once #include -#endif - extern const uint16_t ir_code_cool[3][13][750] = { // Fan Speed High { @@ -60,5 +56,4 @@ extern const uint16_t ir_code_fan[3][750] = { {0}, // MED {0} // HIGH }; -extern const uint16_t ir_code_off[750] = {0}; -#endif \ No newline at end of file +extern const uint16_t ir_code_off[750] = {0}; \ No newline at end of file diff --git a/src/user_code.cpp b/src/user_code.cpp index fb0ed74..0e841ee 100644 --- a/src/user_code.cpp +++ b/src/user_code.cpp @@ -1,17 +1,37 @@ #include +/* +This Code will run right after ESPMega PRO's +Peripheral Initialization Routine +*/ void user_pre_init() { } + +/* +This code will run after every component is initialized +*/ void user_init() { } + +/* +This code will run once every event loop +*/ void user_loop() { - + } + +/* +This code will run when an input pin changed state +*/ void virtual_interrupt_user_callback(int pin, int state) { - + } + +/* +This code will run every 5 seconds +*/ void timer_tick_callback() { } diff --git a/src/user_code.hpp b/src/user_code.hpp index f2e39a4..ff77f21 100644 --- a/src/user_code.hpp +++ b/src/user_code.hpp @@ -1,13 +1,40 @@ -#ifndef USER_CODE -#define USER_CODE -#ifndef CORE_LOADED -#define CORE_LOADED -#include +#pragma once +#include +#include + + + + +// External LCD Configuration +#define ENABLE_EXTERNAL_LCD +#define TXD2 4 +#define RXD2 17 + +#ifdef ENABLE_EXTERNAL_LCD +extern EasyNex user_panel; #endif + +// User Defined Functions 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 +void timer_tick_callback(); + + +// ESPMega IoT Core Build-in Functions +extern void pwm_set_state(int id, int state); +extern void pwm_set_value(int id, int value); +extern void pwm_toggle(int id); +extern void pwm_toggle(int id1, int id2); +extern void pwm_cycle_value(int id); +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 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(); \ No newline at end of file