From ac4213de4d42f415cc16eb6475e8e7f8289e0022 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Thu, 26 Oct 2023 22:50:27 +0700 Subject: [PATCH] smart meeting proto --- platformio.ini | 1 + src/user_code.cpp | 169 ++++++++++++++++++++++++++++++++++++++-------- src/user_code.hpp | 5 ++ 3 files changed, 145 insertions(+), 30 deletions(-) diff --git a/platformio.ini b/platformio.ini index 0074e59..fe2dc6d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -22,4 +22,5 @@ lib_deps = siwats/ESPMegaPROR3@^1.3.0 seithan/Easy Nextion Library@^1.0.6 robtillaart/FRAM_I2C@^0.6.1 esphome/ESPAsyncWebServer-esphome@^3.1.0 + bblanchon/ArduinoJson@^6.21.3 monitor_speed = 115200 \ No newline at end of file diff --git a/src/user_code.cpp b/src/user_code.cpp index 7507243..3721aa5 100644 --- a/src/user_code.cpp +++ b/src/user_code.cpp @@ -1,20 +1,80 @@ #include -//Timer Components -ESPMega_Timer timer1(0, 50, timer1_callback, 15001); +char MEETING_STATE_REPORT_TOPIC[75]; +char MEETING_STATE_REQUEST_TOPIC[75]; +char MEETING_ROOM_NAME[50] = "Meeting Room 04"; +StaticJsonDocument<2048> meeting_info; +#define TOTAL_SLOT 22 // max init cycle 17 +uint8_t selected_slot = 1; +uint8_t cycle_slot_start = 1; +uint8_t slot_hours[TOTAL_SLOT + 1] = {8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19}; +uint8_t slot_minutes[TOTAL_SLOT + 1] = {0, 30, 0, 30, 0, 30, 0, 30, 0, 30, 0, 30, 0, 30, 0, 30, 0, 30, 0, 30, 0, 30, 0}; // Display Componets +#define elcd ESPMega_EXTLCD // Link with Dual state button object with id 2 on page 1 named bt0 -NexDSButton bt0 = NexDSButton(1, 2, "bt0"); +NexPicture lt_bt = NexPicture(1, 1, "lt_bt"); +NexPicture av_bt = NexPicture(1, 3, "lt_bt"); +NexButton left_bt = NexButton(1, 5, "left_bt"); +NexButton right_bt = NexButton(1, 6, "right_bt"); +NexPicture slot1 = NexPicture(1, 7, "slot1"); +NexPicture slot2 = NexPicture(1, 8, "slot2"); +NexPicture slot3 = NexPicture(1, 9, "slot3"); +NexPicture slot4 = NexPicture(1, 10, "slot4"); +NexPicture slot5 = NexPicture(1, 11, "slot5"); + +// Lights on 0,1,2,3,4,5 +// A/V on 6 + // List of Component ID Message to listen to NexTouch *nex_listen_list[] = { - &bt0, + <_bt, + &av_bt, + &slot1, + &slot2, + &slot3, + &slot4, + &slot5, + NULL}; -void bt0PopCallback(void *ptr) +bool light_group_state() { - pwm_toggle(2); + for (int i = 0; i <= 5; i++) + { + if (pwm_get_state(i)) + return true; + } + return false; +} + +void lt_bt_cb(void *ptr) +{ + bool state = !light_group_state(); + for (int i = 0; i++; i <= 5) + pwm_set_state(i, state); +} + +void av_bt_cb(void *ptr) +{ + pwm_toggle(3); +} + +void left_bt_cb(void *ptr) +{ + if(cycle_slot_start>0){ + cycle_slot_start--; + write_time_slot(); + } +} + +void right_bt_cb(void *ptr) +{ + if(cycle_slot_start= 0 && pin <= 5) { - // inform the lcd when pwm 2 changed - bt0.setValue(pwm_get_state(2)); + if (light_group_state()) + { + elcd.write("lt_bt.pic=6"); + send_stop_bit(); + } + else + { + elcd.write("lt_bt.pic=5"); + send_stop_bit(); + } + } + else if (pin == 6) + { + if (pwm_get_state(6)) + { + elcd.write("av_bt.pic=8"); + send_stop_bit(); + } + else + { + elcd.write("av_bt.pic=7"); + send_stop_bit(); + } } } @@ -68,28 +154,51 @@ This code will run every 15 seconds */ void timer_tick_callback() { - if (standalone) - { - timer1.loop(); - } } -void timer1_callback() +void ac_changed_user_callback(int mode, int temperature, int fan_speed) { - for (int i = 0; i < 16; i++) +} + +void meeting_state_callback(String topic, String payload) +{ + DeserializationError error = deserializeJson(meeting_info, payload); +} + +void mqtt_connected_user_callback() +{ + mqtt.subscribe(MEETING_STATE_REPORT_TOPIC, meeting_state_callback); + mqtt.publish(MEETING_STATE_REQUEST_TOPIC, "request"); +} + +void user_state_request_callback() +{ +} + +void write_time_slot() +{ + for (int i = 1; i <= 5; i++) { - pwm_set_state(i, 1); + elcd.printf("slot%d_time.txt=%02d:%02d\n%02d:%02d", i, slot_hours[cycle_slot_start + i - 1], + slot_minutes[cycle_slot_start + i - 1], slot_hours[cycle_slot_start + i], + slot_minutes[cycle_slot_start + i]); } } -void ac_changed_user_callback(int mode, int temperature, int fan_speed) { - +void send_stop_bit() +{ + ESPMega_EXTLCD.write(0xFF); + ESPMega_EXTLCD.write(0xFF); + ESPMega_EXTLCD.write(0xFF); } -void mqtt_connected_user_callback() { - -} - -void user_state_request_callback() { - +uint8_t get_current_time_slot() +{ + rtctime_t curtime = ESPMega_getTime(); + for(uint8_t i=0;i<=TOTAL_SLOT;i++) { + if(slot_hours[i]>curtime.hours || (slot_hours[i]==curtime.hours && slot_minutes[i]>curtime.hours)) { + return i; + } + } + return 255; } \ No newline at end of file diff --git a/src/user_code.hpp b/src/user_code.hpp index 72cc5a0..f811a30 100644 --- a/src/user_code.hpp +++ b/src/user_code.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "espmega_iot_timer.hpp" #include "espmega_iot_external_lcd.hpp" @@ -27,6 +28,10 @@ void timer1_callback(); void mqtt_connected_user_callback(); void bt0PopCallback(void *ptr); void user_state_request_callback(); +void meeting_state_callback(String topic, String payload); +void send_stop_bit(); +void write_time_slot(); +extern char MQTT_BASE_TOPIC[]; // ESPMega IoT Core Build-in Functions