user code

This commit is contained in:
Siwat Sirichai 2023-09-30 00:06:49 +07:00
parent 7bf49d0997
commit bf1de69f24
6 changed files with 110 additions and 27 deletions

View File

@ -1,5 +1,4 @@
#include <espmega_iot_core.hpp> #include <espmega_iot_core.hpp>
#include <user_code.hpp>
// OS Configuration // OS Configuration
#define FASTBOOT #define FASTBOOT
@ -47,7 +46,7 @@ char PWM_SET_STATE_TOPIC[70];
char PWM_SET_VALUE_TOPIC[70]; char PWM_SET_VALUE_TOPIC[70];
// Infrared Transciever // Infrared Transciever
#define IR_RECIEVE_PIN 32 #define IR_RECIEVE_PIN 35
#define IR_SEND_PIN 15 #define IR_SEND_PIN 15
#define MARK_EXCESS_MICROS 20 #define MARK_EXCESS_MICROS 20
#define RAW_BUFFER_LENGTH 750 #define RAW_BUFFER_LENGTH 750
@ -56,13 +55,16 @@ char PWM_SET_VALUE_TOPIC[70];
int lcd_current_page = 1; int lcd_current_page = 1;
int lcd_pwmAdj_id = 0; int lcd_pwmAdj_id = 0;
EasyNex panel(Serial); EasyNex panel(Serial);
#ifdef ENABLE_EXTERNAL_LCD
EasyNex user_panel(Serial);
#endif
// Air Conditioner Control // Air Conditioner Control
/* /*
Mode 0: Off, 1: Cool, 2: Fan Mode 0: Off, 1: Cool, 2: Fan
Fan Speed 0: Auto, 1: High, 2: Mid, 3: Low 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_mode = 0;
uint8_t ac_fan_speed = 0; uint8_t ac_fan_speed = 0;
uint8_t ac_temperature = 25; uint8_t ac_temperature = 25;
@ -105,7 +107,8 @@ DHTNEW env_sensor(DHT22_PIN);
Thread mqtt_reconnector = Thread(); Thread mqtt_reconnector = Thread();
Thread environment_reporter = Thread(); Thread environment_reporter = Thread();
Thread eeprom_pwm_updater = 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 top_bar_updater = Thread();
Thread page_updater = Thread(); Thread page_updater = Thread();
@ -114,10 +117,14 @@ StaticThreadController<2> lcd_thread_controller(&top_bar_updater, &page_updater)
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
#ifdef ENABLE_EXTERNAL_LCD
Serial2.begin(115200); Serial2.begin(115200);
user_panel.begin(115200);
#endif
panel.begin(115200); panel.begin(115200);
Serial.println("ESPMega R3 Initializing"); Serial.println("ESPMega R3 Initializing");
ESPMega_begin(); ESPMega_begin();
user_pre_init();
io_begin(); io_begin();
eeprom_retrieve_init(); eeprom_retrieve_init();
lcd_send_stop_bit(); lcd_send_stop_bit();
@ -138,6 +145,12 @@ void setup()
Serial.println("Jumping to User Code."); Serial.println("Jumping to User Code.");
user_init(); user_init();
lcd_send_command("page dashboard"); lcd_send_command("page dashboard");
#ifdef ENABLE_EXTERNAL_LCD
Serial2.print("rest");
Serial2.write(0xFF);
Serial2.write(0xFF);
Serial2.write(0xFF);
#endif
} }
void loop() void loop()
@ -322,6 +335,8 @@ void thread_initialization()
environment_reporter.setInterval(5000); environment_reporter.setInterval(5000);
eeprom_pwm_updater.onRun(eeprom_pwm_update); eeprom_pwm_updater.onRun(eeprom_pwm_update);
eeprom_pwm_updater.setInterval(1000); 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) void pwm_state_callback(String topic, String message)
@ -944,3 +959,25 @@ void eeprom_mqtt_port_retrieve()
ESPMega_FRAM.read(EEPROM_ADDRESS_MQTT_PORT, port_arr, 2); ESPMega_FRAM.read(EEPROM_ADDRESS_MQTT_PORT, port_arr, 2);
memcpy(&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;
}

View File

@ -1,5 +1,4 @@
#ifndef CORE_LOADED #pragma once
#define CORE_LOADED
#include <ESPMegaPRO.h> #include <ESPMegaPRO.h>
#include <ETH.h> #include <ETH.h>
@ -8,11 +7,12 @@
#include <Thread.h> #include <Thread.h>
#include <StaticThreadController.h> #include <StaticThreadController.h>
#include <IRremote.hpp> #include <IRremote.hpp>
#include <daikin_ir.hpp>
#include <dhtnew.h> #include <dhtnew.h>
#include <time.h> #include <time.h>
#include <EasyNextionLibrary.h> #include <EasyNextionLibrary.h>
#include <lcd.hpp> #include "lcd.hpp"
#include "user_code.hpp"
#include "ir_codes.hpp"
void virtual_interrupt_loop(); void virtual_interrupt_loop();
void virtual_interrupt_callback(int pin, int state); 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 id);
void pwm_toggle(int id1, int id2); void pwm_toggle(int id1, int id2);
void pwm_cycle_value(int id); 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); boolean pwm_group_state(int id1, int id2);
bool input_get_state(int id);
void publish_ac_state(); void publish_ac_state();
void publish_env_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_state_callback(String topic, String message);
void ac_set_state(int mode, int temperature, int fan_speed); void ac_set_state(int mode, int temperature, int fan_speed);
@ -83,5 +89,3 @@ void set_basetopic(String topic);
void eeprom_basetopic_retrieve(); void eeprom_basetopic_retrieve();
void mqtt_port_set(uint16_t port); void mqtt_port_set(uint16_t port);
void eeprom_mqtt_port_retrieve(); void eeprom_mqtt_port_retrieve();
#endif

View File

@ -4,7 +4,7 @@
#endif #endif
#ifndef EXTERNAL_LCD_LOADED #ifndef EXTERNAL_LCD_LOADED
#define EXTERNAL_LCD_LOADED #define EXTERNAL_LCD_LOADED
#include <user_code.hpp> #include "user_code.hpp"
class ExternalLCD: public EasyNex { class ExternalLCD: public EasyNex {
protected: protected:
void callTriggerFunction(); void callTriggerFunction();

View File

@ -1,9 +1,5 @@
#ifndef DAIKIN_IR #pragma once
#define DAIKIN_IR
#ifndef ESPMEGA
#include <ESPMegaPRO.h> #include <ESPMegaPRO.h>
#endif
extern const uint16_t ir_code_cool[3][13][750] = { extern const uint16_t ir_code_cool[3][13][750] = {
// Fan Speed High // Fan Speed High
{ {
@ -61,4 +57,3 @@ extern const uint16_t ir_code_fan[3][750] = {
{0} // HIGH {0} // HIGH
}; };
extern const uint16_t ir_code_off[750] = {0}; extern const uint16_t ir_code_off[750] = {0};
#endif

View File

@ -1,17 +1,37 @@
#include <user_code.hpp> #include <user_code.hpp>
/*
This Code will run right after ESPMega PRO's
Peripheral Initialization Routine
*/
void user_pre_init() { void user_pre_init() {
} }
/*
This code will run after every component is initialized
*/
void user_init() { void user_init() {
} }
/*
This code will run once every event loop
*/
void user_loop() { void user_loop() {
} }
/*
This code will run when an input pin changed state
*/
void virtual_interrupt_user_callback(int pin, int state) { void virtual_interrupt_user_callback(int pin, int state) {
} }
/*
This code will run every 5 seconds
*/
void timer_tick_callback() { void timer_tick_callback() {
} }

View File

@ -1,13 +1,40 @@
#ifndef USER_CODE #pragma once
#define USER_CODE #include <ESPMegaPRO.h>
#ifndef CORE_LOADED #include <EasyNextionLibrary.h>
#define CORE_LOADED
#include <espmega_iot_core.hpp>
// External LCD Configuration
#define ENABLE_EXTERNAL_LCD
#define TXD2 4
#define RXD2 17
#ifdef ENABLE_EXTERNAL_LCD
extern EasyNex user_panel;
#endif #endif
// User Defined Functions
void user_pre_init(); void user_pre_init();
void user_init(); void user_init();
void user_loop(); void user_loop();
void virtual_interrupt_user_callback(int pin, int state); void virtual_interrupt_user_callback(int pin, int state);
void timer_tick_callback(); void timer_tick_callback();
void external_lcd_callback(int touch_hex); void external_lcd_callback(int touch_hex);
#endif 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();