From acf9db482074173f33a3741f711ba4626f7158dd Mon Sep 17 00:00:00 2001 From: RavenSystem Date: Wed, 17 May 2023 20:06:29 +0200 Subject: [PATCH] Home Accessory Architect v12.3.0 Merlin --- HAA/HAA_Installer/main/header.h | 2 +- HAA/HAA_Installer/main/ota.c | 2 +- HAA/HAA_Installer/main/setup.c | 37 ++--- HAA/HAA_Main/main/header.h | 10 +- HAA/HAA_Main/main/main.c | 217 +++++++++++++++++------------- HAA/HAA_Main/main/setup.c | 51 ++++--- HAA/HAA_Main/main/setup.h | 2 +- HAA/HAA_Main/main/types.h | 1 + external_libs/cJSON/cJSON/cJSON.c | 4 +- libs/adv_button/adv_button.c | 100 ++++++++------ libs/homekit-rsf/src/server.c | 37 ++--- libs/new_dht/dht.c | 12 +- 12 files changed, 262 insertions(+), 213 deletions(-) diff --git a/HAA/HAA_Installer/main/header.h b/HAA/HAA_Installer/main/header.h index a24127f1..b3f14a48 100644 --- a/HAA/HAA_Installer/main/header.h +++ b/HAA/HAA_Installer/main/header.h @@ -10,6 +10,6 @@ #include "../../common/common_headers.h" -#define INSTALLER_VERSION "7.1.0" +#define INSTALLER_VERSION "7.2.0" #endif // __HAA_OTA_HEADER_H__ diff --git a/HAA/HAA_Installer/main/ota.c b/HAA/HAA_Installer/main/ota.c index 40cf975c..51e9493d 100644 --- a/HAA/HAA_Installer/main/ota.c +++ b/HAA/HAA_Installer/main/ota.c @@ -81,7 +81,7 @@ static char last_location[LOCATION_LEN]; #ifdef DEBUG_WOLFSSL void MyLoggingCallback(const int logLevel, const char* const logMessage) { /*custom logging function*/ - INFO("loglevel: %d - %s", logLevel, logMessage); + INFO("loglevel: %i - %s", logLevel, logMessage); } #endif diff --git a/HAA/HAA_Installer/main/setup.c b/HAA/HAA_Installer/main/setup.c index eab295af..d55a06c1 100644 --- a/HAA/HAA_Installer/main/setup.c +++ b/HAA/HAA_Installer/main/setup.c @@ -107,7 +107,7 @@ typedef struct { TaskHandle_t ota_task; wifi_network_info_t* wifi_networks; - SemaphoreHandle_t wifi_networks_mutex; + SemaphoreHandle_t wifi_networks_semaph; uint8_t check_counter; @@ -462,7 +462,7 @@ static void wifi_networks_free() { #ifdef ESP_PLATFORM static void wifi_scan_done_cb() { - xSemaphoreTake(context->wifi_networks_mutex, portMAX_DELAY); + xSemaphoreTake(context->wifi_networks_semaph, portMAX_DELAY); wifi_networks_free(); @@ -497,7 +497,7 @@ static void wifi_scan_done_cb() { } } - xSemaphoreGive(context->wifi_networks_mutex); + xSemaphoreGive(context->wifi_networks_semaph); } #else static void wifi_scan_done_cb(void *arg, sdk_scan_status_t status) { @@ -506,7 +506,7 @@ static void wifi_scan_done_cb(void *arg, sdk_scan_status_t status) { return; } - xSemaphoreTake(context->wifi_networks_mutex, portMAX_DELAY); + xSemaphoreTake(context->wifi_networks_semaph, portMAX_DELAY); wifi_networks_free(); @@ -541,7 +541,7 @@ static void wifi_scan_done_cb(void *arg, sdk_scan_status_t status) { bss = bss->next.stqe_next; } - xSemaphoreGive(context->wifi_networks_mutex); + xSemaphoreGive(context->wifi_networks_semaph); } #endif @@ -681,7 +681,7 @@ static void wifi_config_server_on_settings(client_t *client) { // Wifi Networks char buffer[150]; char bssid[13]; - if (xSemaphoreTake(context->wifi_networks_mutex, MS_TO_TICKS(4000))) { + if (xSemaphoreTake(context->wifi_networks_semaph, MS_TO_TICKS(4000))) { wifi_network_info_t* net = context->wifi_networks; while (net) { snprintf(bssid, sizeof(bssid), "%02x%02x%02x%02x%02x%02x", net->bssid[0], net->bssid[1], net->bssid[2], net->bssid[3], net->bssid[4], net->bssid[5]); @@ -695,7 +695,7 @@ static void wifi_config_server_on_settings(client_t *client) { net = net->next; } - xSemaphoreGive(context->wifi_networks_mutex); + xSemaphoreGive(context->wifi_networks_semaph); } client_send_chunk(client, html_settings_wifi); @@ -1100,11 +1100,13 @@ static void wifi_config_softap_start() { uint8_t macaddr[6]; sdk_wifi_get_macaddr(STATION_IF, macaddr); - + + const uint8_t wifi_channel = ((hwrand() % 3) * 5) + 1; + #ifdef ESP_PLATFORM wifi_config_t softap_config = { .ap = { - .channel = 6, + .channel = wifi_channel, .authmode = WIFI_AUTH_OPEN, .max_connection = 2, .ssid_hidden = 0, @@ -1116,7 +1118,7 @@ static void wifi_config_softap_start() { "%s-%02X%02X%02X", context->ssid_prefix, macaddr[3], macaddr[4], macaddr[5] ); - INFO("Start AP %s", softap_config.ap.ssid); + INFO("Wifi AP %s Ch%i", softap_config.ap.ssid, softap_config.ap.channel); esp_netif_t* ap_netif = esp_netif_create_default_wifi_ap(); assert(ap_netif); @@ -1126,12 +1128,12 @@ static void wifi_config_softap_start() { IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0); esp_netif_set_ip_info(ap_netif, &ap_ip); - INFO("Start DHCP"); esp_wifi_set_config(WIFI_IF_AP, &softap_config); + esp_wifi_set_bandwidth(WIFI_IF_AP, WIFI_BW_HT20); #else struct sdk_softap_config softap_config; softap_config.ssid_hidden = 0; - softap_config.channel = 6; + softap_config.channel = wifi_channel; softap_config.authmode = AUTH_OPEN; softap_config.max_connection = 2; softap_config.beacon_interval = 100; @@ -1140,8 +1142,8 @@ static void wifi_config_softap_start() { (char*) softap_config.ssid, sizeof(softap_config.ssid), "%s-%02X%02X%02X", context->ssid_prefix, macaddr[3], macaddr[4], macaddr[5] ); - - INFO("Start AP %s", softap_config.ssid); + + INFO("Wifi AP %s Ch%i", softap_config.ssid, softap_config.channel); struct ip_info ap_ip; IP4_ADDR(&ap_ip.ip, 192, 168, 4, 1); @@ -1154,12 +1156,11 @@ static void wifi_config_softap_start() { ip4_addr_t first_client_ip; first_client_ip.addr = ap_ip.ip.addr + htonl(1); - INFO("Start DHCP"); dhcpserver_start(&first_client_ip, 4); #endif - context->wifi_networks_mutex = xSemaphoreCreateBinary(); - xSemaphoreGive(context->wifi_networks_mutex); + context->wifi_networks_semaph = xSemaphoreCreateBinary(); + xSemaphoreGive(context->wifi_networks_semaph); xTaskCreate(http_task, "WEB", (TASK_SIZE_FACTOR * 640), NULL, (tskIDLE_PRIORITY + 1), NULL); } @@ -1264,7 +1265,7 @@ static uint8_t wifi_config_connect(const uint8_t phy) { }; sdk_wifi_station_set_config(&sta_config); - + esp_wifi_set_bandwidth(WIFI_IF_STA, WIFI_BW_HT20); esp_wifi_set_ps(WIFI_PS_NONE); #endif diff --git a/HAA/HAA_Main/main/header.h b/HAA/HAA_Main/main/header.h index 49c94d75..287b70af 100644 --- a/HAA/HAA_Main/main/header.h +++ b/HAA/HAA_Main/main/header.h @@ -11,7 +11,7 @@ #include "../../common/common_headers.h" // Version -#define HAA_FIRMWARE_VERSION "12.2.3" +#define HAA_FIRMWARE_VERSION "12.3.0" #define HAA_FIRMWARE_BETA_REVISION "" #define HAA_FIRMWARE_CODENAME "Merlin" @@ -123,7 +123,6 @@ #define ACCESSORIES_ARRAY "a" #define EXTRA_SERVICES_ARRAY "es" #define BUTTONS_ARRAY "b" -#define BUTTONS_ARRAY_1 "b1" #define FIXED_BUTTONS_ARRAY_0 "f0" #define FIXED_BUTTONS_ARRAY_1 "f1" #define FIXED_BUTTONS_ARRAY_2 "f2" @@ -750,7 +749,7 @@ #define SETUP_MODE_ACTIVATE_COUNT "z" #define SETUP_MODE_DEFAULT_ACTIVATE_COUNT (8) #define SETUP_MODE_TOGGLE_TIME_MS (1050) -#define CUSTOM_HAA_COMMAND "io" +#define CUSTOM_HAA_COMMAND "zc" #define HAA_SETUP_ACCESSORY_SET "s" #define ACTION_TASK_TYPE_UART (0) @@ -766,13 +765,14 @@ #define WIFI_STATUS_CONNECTED (3) #define WIFI_PING_ERRORS "w" #define WIFI_SLEEP_MODE_SET "d" +#define WIFI_BANDWIDTH_40_SET "dt" #define WIFI_RECONNECTION_POLL_PERIOD_MS (5000) #define WIFI_DISCONNECTED_LONG_TIME (60) // * WIFI_RECONNECTION_POLL_PERIOD_MS #define WIFI_WATCHDOG_POLL_PERIOD_MS (1500) #define WIFI_WATCHDOG_ARP_PERIOD_SET "e" -#define WIFI_WATCHDOG_ARP_PERIOD_DEFAULT (40) // * WIFI_WATCHDOG_POLL_PERIOD_MS +#define WIFI_WATCHDOG_ARP_PERIOD_DEFAULT (190) // * WIFI_WATCHDOG_POLL_PERIOD_MS #define WIFI_WATCHDOG_ROAMING_PERIOD (1234) // * WIFI_WATCHDOG_POLL_PERIOD_MS #define HAA_RMT_LED_STRIP_BLOCK_SYMBOLS (256) @@ -783,7 +783,7 @@ #define STATUS_LED_DURATION_OFF (120) #define SAVE_STATES_DELAY_MS (3000) -#define RANDOM_DELAY_MS (4000) +#define RANDOM_DELAY_MS (4500) #define HOMEKIT_RE_PAIR_TIME_MS (300000) diff --git a/HAA/HAA_Main/main/main.c b/HAA/HAA_Main/main/main.c index 9de5034a..4a76a88e 100644 --- a/HAA/HAA_Main/main/main.c +++ b/HAA/HAA_Main/main/main.c @@ -32,6 +32,8 @@ #define HAA_ENTER_CRITICAL_TASK() portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; taskENTER_CRITICAL(&mux) #define HAA_EXIT_CRITICAL_TASK() taskEXIT_CRITICAL(&mux) +#define HAA_LONGINT_F "li" + #ifndef CONFIG_IDF_TARGET_ESP32C2 #include "driver/rmt_tx.h" #include "driver/rmt_encoder.h" @@ -53,6 +55,8 @@ #define HAA_ENTER_CRITICAL_TASK() taskENTER_CRITICAL() #define HAA_EXIT_CRITICAL_TASK() taskEXIT_CRITICAL() +#define HAA_LONGINT_F "i" + #endif #include @@ -150,11 +154,7 @@ static unsigned int IRAM private_abs(int number) { } static void show_freeheap() { -#ifdef ESP_PLATFORM - INFO("Free Heap %li", xPortGetFreeHeapSize()); -#else - INFO("Free Heap %d", xPortGetFreeHeapSize()); -#endif + INFO("Free Heap %"HAA_LONGINT_F, xPortGetFreeHeapSize()); } // PWM Part @@ -432,8 +432,16 @@ void free_heap_watchdog() { } #endif // HAA_DEBUG -void random_task_delay() { - vTaskDelay( ( hwrand() % MS_TO_TICKS(RANDOM_DELAY_MS) ) + MS_TO_TICKS(200) ); +static void _random_task_delay(const uint16_t ticks) { + vTaskDelay( ( hwrand() % ticks ) + MS_TO_TICKS(200) ); +} + +void random_task_short_delay() { + _random_task_delay(MS_TO_TICKS(RANDOM_DELAY_MS)); +} + +void random_task_long_delay() { + _random_task_delay(MS_TO_TICKS(RANDOM_DELAY_MS * 2)); } void disable_emergency_setup(TimerHandle_t xTimer) { @@ -760,7 +768,7 @@ void reboot_task() { INFO("\nRebooting\n"); rs_esp_timer_stop_forced(WIFI_WATCHDOG_TIMER); - random_task_delay(); + random_task_short_delay(); sdk_system_restart(); } @@ -926,7 +934,7 @@ void wifi_reconnection_task(void* args) { save_last_working_phy(); - random_task_delay(); + random_task_short_delay(); homekit_mdns_announce(); @@ -1015,11 +1023,13 @@ void wifi_watchdog() { do_actions(ch_group_find_by_serv(SERV_TYPE_ROOT_DEVICE), 7); } - main_config.wifi_arp_count++; - if (main_config.wifi_arp_count >= main_config.wifi_arp_count_max) { - main_config.wifi_arp_count = 0; - - wifi_resend_arp(); + if (main_config.wifi_arp_count_max > 0) { + main_config.wifi_arp_count++; + if (main_config.wifi_arp_count >= main_config.wifi_arp_count_max) { + main_config.wifi_arp_count = 0; + + wifi_resend_arp(); + } } if (main_config.wifi_ping_max_errors != 255 && !homekit_is_pairing()) { @@ -6584,11 +6594,8 @@ void do_actions(ch_group_t* ch_group, uint8_t action) { while (action_binary_output) { if (action_binary_output->action == action) { extended_gpio_write(action_binary_output->gpio, action_binary_output->value); -#ifdef ESP_PLATFORM - INFO("<%i> DigO %i->%i (%li)", ch_group->serv_index, action_binary_output->gpio, action_binary_output->value, action_binary_output->inching); -#else - INFO("<%i> DigO %i->%i (%i)", ch_group->serv_index, action_binary_output->gpio, action_binary_output->value, action_binary_output->inching); -#endif + + INFO("<%i> DigO %i->%i (%"HAA_LONGINT_F")", ch_group->serv_index, action_binary_output->gpio, action_binary_output->value, action_binary_output->inching); if (action_binary_output->inching > 0) { rs_esp_timer_start(rs_esp_timer_create(action_binary_output->inching, false, (void*) action_binary_output, autoswitch_timer)); @@ -7217,7 +7224,7 @@ void run_homekit_server() { show_freeheap(); if (main_config.enable_homekit_server) { - random_task_delay(); + random_task_short_delay(); homekit_server_init(&config); show_freeheap(); } @@ -7318,20 +7325,29 @@ void normal_mode_init() { int active = false; for (int j = 0; j < cJSON_GetArraySize(json_buttons); j++) { - const int gpio = (uint16_t) cJSON_GetObjectItemCaseSensitive(cJSON_GetArrayItem(json_buttons, j), PIN_GPIO)->valuefloat; + int button_data[2] = { 0, 1 }; - int button_type = 1; - if (cJSON_GetObjectItemCaseSensitive(cJSON_GetArrayItem(json_buttons, j), BUTTON_PRESS_TYPE) != NULL) { - button_type = (uint8_t) cJSON_GetObjectItemCaseSensitive(cJSON_GetArrayItem(json_buttons, j), BUTTON_PRESS_TYPE)->valuefloat; + cJSON* json_button = cJSON_GetArrayItem(json_buttons, j); + + if (cJSON_GetObjectItemCaseSensitive(json_button, PIN_GPIO) == NULL) { + for (int k = 0; k < cJSON_GetArraySize(json_button); k++) { + button_data[k] = (uint16_t) cJSON_GetArrayItem(json_button, k)->valuefloat; + } + } else { // OLD WAY + button_data[0] = (uint16_t) cJSON_GetObjectItemCaseSensitive(json_button, PIN_GPIO)->valuefloat; + + if (cJSON_GetObjectItemCaseSensitive(json_button, BUTTON_PRESS_TYPE) != NULL) { + button_data[1] = (uint8_t) cJSON_GetObjectItemCaseSensitive(json_button, BUTTON_PRESS_TYPE)->valuefloat; + } } - adv_button_register_callback_fn(gpio, callback, button_type, (void*) ch_group, param); + adv_button_register_callback_fn(button_data[0], callback, button_data[1], (void*) ch_group, param); - if (adv_button_read_by_gpio(gpio) == button_type) { + if (adv_button_read_by_gpio(button_data[0]) == button_data[1]) { active = true; } - INFO("New DigI GPIO %i t %i, s %i", gpio, button_type, active); + INFO("DigI GPIO %i t %i, s %i", button_data[0], button_data[1], active); } return active; @@ -7382,7 +7398,7 @@ void normal_mode_init() { ping_input->callback_0 = ping_input_callback_fn; } - INFO("New PingI h %s, r %i", ping_input->host, response_type); + INFO("PingI h %s, r %i", ping_input->host, response_type); } } @@ -7499,7 +7515,7 @@ void normal_mode_init() { action_copy->next = last_action; last_action = action_copy; - INFO("New A%i Copy v %i", new_int_action, action_copy->new_action); + INFO("A%i Copy v %i", new_int_action, action_copy->new_action); } } } @@ -7526,30 +7542,42 @@ void normal_mode_init() { if (cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(json_accessory, action), BINARY_OUTPUTS_ARRAY) != NULL) { cJSON* json_relays = cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(json_accessory, action), BINARY_OUTPUTS_ARRAY); for (int i = cJSON_GetArraySize(json_relays) - 1; i >= 0; i--) { - action_binary_output_t* action_binary_output = malloc(sizeof(action_binary_output_t)); - memset(action_binary_output, 0, sizeof(*action_binary_output)); + int binary_output_data[3] = { 0, 0, 0 }; cJSON* json_relay = cJSON_GetArrayItem(json_relays, i); - action_binary_output->action = new_int_action; - action_binary_output->gpio = (uint16_t) cJSON_GetObjectItemCaseSensitive(json_relay, PIN_GPIO)->valuefloat; - - if (cJSON_GetObjectItemCaseSensitive(json_relay, VALUE) != NULL) { - action_binary_output->value = (bool) cJSON_GetObjectItemCaseSensitive(json_relay, VALUE)->valuefloat; + if (cJSON_GetObjectItemCaseSensitive(json_relay, PIN_GPIO) == NULL) { + for (int k = 0; k < cJSON_GetArraySize(json_relay); k++) { + if (k == 2) { + binary_output_data[2] = cJSON_GetArrayItem(json_relay, k)->valuefloat * 1000; + } else { + binary_output_data[k] = (uint16_t) cJSON_GetArrayItem(json_relay, k)->valuefloat; + } + } + } else { // OLD WAY + binary_output_data[0] = (uint16_t) cJSON_GetObjectItemCaseSensitive(json_relay, PIN_GPIO)->valuefloat; + + if (cJSON_GetObjectItemCaseSensitive(json_relay, VALUE) != NULL) { + binary_output_data[1] = (bool) cJSON_GetObjectItemCaseSensitive(json_relay, VALUE)->valuefloat; + } + + if (cJSON_GetObjectItemCaseSensitive(json_relay, AUTOSWITCH_TIME) != NULL) { + binary_output_data[2] = cJSON_GetObjectItemCaseSensitive(json_relay, AUTOSWITCH_TIME)->valuefloat * 1000; + } } - if (cJSON_GetObjectItemCaseSensitive(json_relay, AUTOSWITCH_TIME) != NULL) { - action_binary_output->inching = cJSON_GetObjectItemCaseSensitive(json_relay, AUTOSWITCH_TIME)->valuefloat * 1000; - } + action_binary_output_t* action_binary_output = malloc(sizeof(action_binary_output_t)); + memset(action_binary_output, 0, sizeof(*action_binary_output)); + + action_binary_output->action = new_int_action; + action_binary_output->gpio = binary_output_data[0]; + action_binary_output->value = binary_output_data[1]; + action_binary_output->inching = binary_output_data[2]; action_binary_output->next = last_action; last_action = action_binary_output; -#ifdef ESP_PLATFORM - INFO("New A%i DigO g %i, v %i, i %li", new_int_action, action_binary_output->gpio, action_binary_output->value, action_binary_output->inching); -#else - INFO("New A%i DigO g %i, v %i, i %i", new_int_action, action_binary_output->gpio, action_binary_output->value, action_binary_output->inching); -#endif + INFO("A%i DigO g %i, v %i, i %"HAA_LONGINT_F"ms", new_int_action, action_binary_output->gpio, action_binary_output->value, action_binary_output->inching); } } } @@ -7602,7 +7630,7 @@ void normal_mode_init() { } } - INFO("New A%i ServNot %i->%g", new_int_action, action_serv_manager->serv_index, action_serv_manager->value); + INFO("A%i ServNot %i->%g", new_int_action, action_serv_manager->serv_index, action_serv_manager->value); } } } @@ -7619,7 +7647,7 @@ void normal_mode_init() { ch_group->action_serv_manager = last_action; } - // Service Manager + // Set Characteristic Actions inline void new_action_set_ch(ch_group_t* ch_group, cJSON* json_context, uint8_t fixed_action) { action_set_ch_t* last_action = ch_group->action_set_ch; @@ -7662,7 +7690,7 @@ void normal_mode_init() { } } - INFO("New A%i SetCh %i.%i->%i.%i", new_int_action, action_set_ch->source_serv, action_set_ch->source_ch, action_set_ch->target_serv, action_set_ch->target_ch); + INFO("A%i SetCh %i.%i->%i.%i", new_int_action, action_set_ch->source_serv, action_set_ch->source_ch, action_set_ch->target_serv, action_set_ch->target_ch); } } } @@ -7693,16 +7721,14 @@ void normal_mode_init() { action_system_t* action_system = malloc(sizeof(action_system_t)); memset(action_system, 0, sizeof(*action_system)); - cJSON* json_action_system = cJSON_GetArrayItem(json_action_systems, i); - action_system->action = new_int_action; - action_system->value = (uint8_t) cJSON_GetObjectItemCaseSensitive(json_action_system, SYSTEM_ACTION)->valuefloat; + action_system->value = (uint8_t) cJSON_GetArrayItem(json_action_systems, i)->valuefloat; action_system->next = last_action; last_action = action_system; - INFO("New A%i Sys v %i", new_int_action, action_system->value); + INFO("A%i Sys v %i", new_int_action, action_system->value); } } } @@ -7785,7 +7811,7 @@ void normal_mode_init() { } } - INFO("New A%i Net %s:%i", new_int_action, action_network->host, action_network->port_n); + INFO("A%i Net %s:%i", new_int_action, action_network->host, action_network->port_n); action_network->next = last_action; last_action = action_network; @@ -7857,7 +7883,7 @@ void normal_mode_init() { action_irrf_tx->next = last_action; last_action = action_irrf_tx; - INFO("New A%i IRRF r %i, p %i", new_int_action, action_irrf_tx->repeats, action_irrf_tx->pause); + INFO("A%i IRRF r %i, p %i", new_int_action, action_irrf_tx->repeats, action_irrf_tx->pause); } } } @@ -7885,11 +7911,11 @@ void normal_mode_init() { if (cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(json_accessory, action), UART_ACTIONS_ARRAY) != NULL) { cJSON* json_action_uarts = cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(json_accessory, action), UART_ACTIONS_ARRAY); for (int i = cJSON_GetArraySize(json_action_uarts) - 1; i >= 0; i--) { + cJSON* json_action_uart = cJSON_GetArrayItem(json_action_uarts, i); + action_uart_t* action_uart = malloc(sizeof(action_uart_t)); memset(action_uart, 0, sizeof(*action_uart)); - cJSON* json_action_uart = cJSON_GetArrayItem(json_action_uarts, i); - action_uart->action = new_int_action; if (cJSON_GetObjectItemCaseSensitive(json_action_uart, UART_ACTION_PAUSE) != NULL) { @@ -7916,7 +7942,7 @@ void normal_mode_init() { action_uart->next = last_action; last_action = action_uart; - INFO("New A%i UART%i p %i, l %i", new_int_action, action_uart->uart, action_uart->pause, action_uart->len); + INFO("A%i UART%i p %i, l %i", new_int_action, action_uart->uart, action_uart->pause, action_uart->len); } } } @@ -7974,7 +8000,7 @@ void normal_mode_init() { } } - INFO("New A%i PWM g %i->%i, d %i, f %i", new_int_action, action_pwm->gpio, action_pwm->duty, action_pwm->freq, action_pwm->dithering); + INFO("A%i PWM g %i->%i, d %i, f %i", new_int_action, action_pwm->gpio, action_pwm->duty, action_pwm->freq, action_pwm->dithering); } } } @@ -8405,11 +8431,7 @@ void normal_mode_init() { const unsigned int i2c_res = adv_i2c_init_hz(i, i2c_scl_gpio, i2c_sda_gpio, i2c_freq_hz, i2c_scl_gpio_pullup, i2c_sda_gpio_pullup); -#ifdef ESP_PLATFORM - INFO("I2C bus %i: scl %i, sda %i, f %li, r %i", i, i2c_scl_gpio, i2c_sda_gpio, i2c_freq_hz, i2c_res); -#else - INFO("I2C bus %i: scl %i, sda %i, f %i, r %i", i, i2c_scl_gpio, i2c_sda_gpio, i2c_freq_hz, i2c_res); -#endif + INFO("I2C bus %i: scl %i, sda %i, f %"HAA_LONGINT_F", r %i", i, i2c_scl_gpio, i2c_sda_gpio, i2c_freq_hz, i2c_res); } } @@ -8549,6 +8571,10 @@ void normal_mode_init() { io_value[j - 1] = cJSON_GetArrayItem(json_io_config, j)->valuefloat; } +#ifdef ESP_PLATFORM + int gpio_ret = -99; +#endif + if (gpio < 100) { #ifdef ESP_PLATFORM gpio_reset_pin(gpio); @@ -8579,7 +8605,7 @@ void normal_mode_init() { if (IO_GPIO_MODE <= 5) { #ifdef ESP_PLATFORM - gpio_set_direction(gpio, IO_GPIO_MODE); + gpio_ret = gpio_set_direction(gpio, IO_GPIO_MODE); gpio_sleep_set_direction(gpio, IO_GPIO_MODE); #else if (IO_GPIO_MODE == 0) { @@ -8593,7 +8619,11 @@ void normal_mode_init() { gpio_write(gpio, IO_GPIO_OUTPUT_INIT_VALUE); } - INFO("GPIO %i, m %i, p %i, v %i", gpio, IO_GPIO_MODE, IO_GPIO_PULL_UP_DOWN, IO_GPIO_OUTPUT_INIT_VALUE); +#ifdef ESP_PLATFORM + INFO("GPIO %i: M %i (0x%x), p %i, v %i", gpio, IO_GPIO_MODE, gpio_ret, IO_GPIO_PULL_UP_DOWN, IO_GPIO_OUTPUT_INIT_VALUE); +#else + INFO("GPIO %i: M %i, p %i, v %i", gpio, IO_GPIO_MODE, IO_GPIO_PULL_UP_DOWN, IO_GPIO_OUTPUT_INIT_VALUE); +#endif } else if (IO_GPIO_MODE == 6) { const int inverted = (bool) (IO_GPIO_BUTTON_MODE & 0b01); @@ -8607,15 +8637,19 @@ void normal_mode_init() { const int pulse_mode = (bool) (IO_GPIO_BUTTON_MODE & 0b10); +#ifdef ESP_PLATFORM + gpio_ret = adv_button_create(gpio, inverted, pulse_mode, 0, IO_GPIO_BUTTON_FILTER); + INFO("GPIO %i: M 6 (0x%x), p %i, i %i, f %i, m %i", gpio, gpio_ret, IO_GPIO_PULL_UP_DOWN, inverted, IO_GPIO_BUTTON_FILTER, pulse_mode); +#else adv_button_create(gpio, inverted, pulse_mode, 0, IO_GPIO_BUTTON_FILTER); - - INFO("DigI GPIO %i, p %i, i %i, f %i, m %i", gpio, IO_GPIO_PULL_UP_DOWN, inverted, IO_GPIO_BUTTON_FILTER, pulse_mode); + INFO("GPIO %i: M 6, p %i, i %i, f %i, m %i", gpio, IO_GPIO_PULL_UP_DOWN, inverted, IO_GPIO_BUTTON_FILTER, pulse_mode); +#endif } else { // MCP23017 mcp23017_t* mcp = mcp_find_by_index(gpio / 100); adv_button_create(gpio, inverted, mcp->addr, mcp->bus, IO_GPIO_BUTTON_FILTER); - INFO("DigI MCP Pin %i i %i, f %i, b %i, a %i", gpio, inverted, IO_GPIO_BUTTON_FILTER, mcp->bus, mcp->addr); + INFO("MCP Pin %i: M 6, i %i, f %i, b %i, a %i", gpio, inverted, IO_GPIO_BUTTON_FILTER, mcp->bus, mcp->addr); } } else if (IO_GPIO_MODE <= 8) { @@ -8636,13 +8670,13 @@ void normal_mode_init() { if (IO_GPIO_MODE == 7) { #ifdef ESP_PLATFORM - gpio_set_direction(gpio, GPIO_MODE_OUTPUT); + gpio_ret = gpio_set_direction(gpio, GPIO_MODE_OUTPUT); #else gpio_enable(gpio, GPIO_OUTPUT); #endif } else { #ifdef ESP_PLATFORM - gpio_set_direction(gpio, GPIO_MODE_OUTPUT_OD); + gpio_ret = gpio_set_direction(gpio, GPIO_MODE_OUTPUT_OD); #else gpio_enable(gpio, GPIO_OUT_OPEN_DRAIN); #endif @@ -8653,7 +8687,11 @@ void normal_mode_init() { adv_pwm_new_channel(gpio, inverted, leading, IO_GPIO_PWM_DITHERING, IO_GPIO_OUTPUT_INIT_VALUE); - INFO("PWM-S GPIO %i, m %i, v %i, i %i, l %i, d %i", gpio, IO_GPIO_MODE, IO_GPIO_OUTPUT_INIT_VALUE, inverted, leading, IO_GPIO_PWM_DITHERING); +#ifdef ESP_PLATFORM + INFO("GPIO %i: M %i (0x%x), v %i, i %i, l %i, d %i", gpio, IO_GPIO_MODE, gpio_ret, IO_GPIO_OUTPUT_INIT_VALUE, inverted, leading, IO_GPIO_PWM_DITHERING); +#else + INFO("GPIO %i: M %i, v %i, i %i, l %i, d %i", gpio, IO_GPIO_MODE, IO_GPIO_OUTPUT_INIT_VALUE, inverted, leading, IO_GPIO_PWM_DITHERING); +#endif #ifdef ESP_PLATFORM } else if (IO_GPIO_MODE == 9) { // Only ESP32 @@ -8716,9 +8754,9 @@ void normal_mode_init() { .hpoint = 0, .flags.output_invert = inverted ^ leading, }; - ledc_channel_config(&ledc_channel); + gpio_ret = ledc_channel_config(&ledc_channel); - INFO("PWM-H GPIO %i, m %i, v %i, i %i, l %i", gpio, IO_GPIO_MODE, IO_GPIO_OUTPUT_INIT_VALUE, inverted, leading); + INFO("GPIO %i: M 9 (0x%x), v %i, i %i, l %i", gpio, gpio_ret, IO_GPIO_OUTPUT_INIT_VALUE, inverted, leading); } else if (IO_GPIO_MODE == 10) { // Only ESP32 if (main_config.adc_dac_data == NULL) { @@ -8739,9 +8777,9 @@ void normal_mode_init() { .bitwidth = ADC_BITWIDTH_12, // Max supported width: 12 bits (0 - 4095) .atten = IO_GPIO_ADC_ATTENUATION, }; - adc_oneshot_config_channel(main_config.adc_dac_data->adc_oneshot_handle, adc_channel, &config); + gpio_ret = adc_oneshot_config_channel(main_config.adc_dac_data->adc_oneshot_handle, adc_channel, &config); - INFO("ADC GPIO %i, m %i a %i", gpio, IO_GPIO_MODE, IO_GPIO_ADC_ATTENUATION); + INFO("GPIO %i: M 10 (0x%x), a %i", gpio, gpio_ret, IO_GPIO_ADC_ATTENUATION); #endif } @@ -8883,6 +8921,11 @@ void normal_mode_init() { if (cJSON_GetObjectItemCaseSensitive(json_config, WIFI_SLEEP_MODE_SET) != NULL) { wifi_sleep_mode = (uint8_t) cJSON_GetObjectItemCaseSensitive(json_config, WIFI_SLEEP_MODE_SET)->valuefloat; } + + int wifi_bandwidth_40 = false; + if (cJSON_GetObjectItemCaseSensitive(json_config, WIFI_BANDWIDTH_40_SET) != NULL) { + wifi_bandwidth_40 = (bool) cJSON_GetObjectItemCaseSensitive(json_config, WIFI_BANDWIDTH_40_SET)->valuefloat; + } #endif // ARP Gratuitous period @@ -9079,11 +9122,7 @@ void normal_mode_init() { serial_index--; } if (use_config_number) { -#ifdef ESP_PLATFORM - snprintf(serial_str, serial_str_len, "%li-%02X%02X%02X-%i", -#else - snprintf(serial_str, serial_str_len, "%i-%02X%02X%02X-%i", -#endif + snprintf(serial_str, serial_str_len, "%"HAA_LONGINT_F"-%02X%02X%02X-%i", last_config_number, macaddr[3], macaddr[4], macaddr[5], serial_index); } else { snprintf(serial_str, serial_str_len, "%s%s%02X%02X%02X-%i", @@ -12221,11 +12260,8 @@ void normal_mode_init() { // New HomeKit Service void new_service(const uint16_t acc_count, uint16_t serv_count, const uint16_t total_services, cJSON* json_accessory, const uint8_t serv_type) { service_numerator++; -#ifdef ESP_PLATFORM - INFO("\n* SERV %li (%i)", service_numerator, serv_type); -#else - INFO("\n* SERV %i (%i)", service_numerator, serv_type); -#endif + + INFO("\n* SERV %"HAA_LONGINT_F" (%i)", service_numerator, serv_type); if (serv_type == SERV_TYPE_BUTTON || serv_type == SERV_TYPE_DOORBELL) { @@ -12488,12 +12524,12 @@ void normal_mode_init() { } main_config.wifi_mode = (uint8_t) wifi_mode; - random_task_delay(); + random_task_long_delay(); //main_config.wifi_status = WIFI_STATUS_CONNECTING; // Not needed #ifdef ESP_PLATFORM - wifi_config_init("HAA", run_homekit_server, custom_hostname, 0, wifi_sleep_mode); + wifi_config_init("HAA", run_homekit_server, custom_hostname, 0, wifi_sleep_mode, wifi_bandwidth_40); #else wifi_config_init("HAA", run_homekit_server, custom_hostname, 0); #endif @@ -12577,7 +12613,7 @@ void irrf_capture_task(void* args) { } void wifi_done() { - + // Do noting, but needed to be not NULL } void init_task() { @@ -12623,10 +12659,11 @@ void init_task() { char *wifi_ssid = NULL; sysparam_get_string(WIFI_SSID_SYSPARAM, &wifi_ssid); + // DEBUG //sysparam_set_int8(HAA_SETUP_MODE_SYSPARAM, 2); // Force to enter always in setup mode. Only for tests. Keep commented for releases - //sysparam_set_string("ota_repo", "1"); // Simulates Installation with OTA. Only for tests. Keep commented for releases + void enter_setup(const int param) { reset_uart(); @@ -12639,7 +12676,7 @@ void init_task() { printf_header(); INFO("SETUP"); #ifdef ESP_PLATFORM - wifi_config_init("HAA", NULL, main_config.name_value, param, 0); + wifi_config_init("HAA", NULL, main_config.name_value, param, 0, false); #else wifi_config_init("HAA", NULL, main_config.name_value, param); #endif @@ -12654,7 +12691,7 @@ void init_task() { if (wifi_ssid) { adv_logger_init(ADV_LOGGER_UART0_UDP, NULL, false); #ifdef ESP_PLATFORM - wifi_config_init("HAA", wifi_done, main_config.name_value, 0, 0); + wifi_config_init("HAA", wifi_done, main_config.name_value, 0, 0, false); #else wifi_config_init("HAA", wifi_done, main_config.name_value, 0); #endif diff --git a/HAA/HAA_Main/main/setup.c b/HAA/HAA_Main/main/setup.c index 9a836b95..26b299d9 100644 --- a/HAA/HAA_Main/main/setup.c +++ b/HAA/HAA_Main/main/setup.c @@ -118,12 +118,13 @@ typedef struct { TaskHandle_t setup_announcer; wifi_network_info_t* wifi_networks; - SemaphoreHandle_t wifi_networks_mutex; + SemaphoreHandle_t wifi_networks_semaph; uint8_t check_counter; #ifdef ESP_PLATFORM uint8_t wifi_sleep_mode; + bool bandwidth_40; // 1 bit #endif bool end_setup; // 1 bit @@ -499,7 +500,7 @@ static void wifi_networks_free() { #ifdef ESP_PLATFORM static void wifi_scan_done_cb() { - xSemaphoreTake(context->wifi_networks_mutex, portMAX_DELAY); + xSemaphoreTake(context->wifi_networks_semaph, portMAX_DELAY); wifi_networks_free(); @@ -534,7 +535,7 @@ static void wifi_scan_done_cb() { } } - xSemaphoreGive(context->wifi_networks_mutex); + xSemaphoreGive(context->wifi_networks_semaph); } #else static void wifi_scan_done_cb(void *arg, sdk_scan_status_t status) { @@ -543,7 +544,7 @@ static void wifi_scan_done_cb(void *arg, sdk_scan_status_t status) { return; } - xSemaphoreTake(context->wifi_networks_mutex, portMAX_DELAY); + xSemaphoreTake(context->wifi_networks_semaph, portMAX_DELAY); wifi_networks_free(); @@ -578,7 +579,7 @@ static void wifi_scan_done_cb(void *arg, sdk_scan_status_t status) { bss = bss->next.stqe_next; } - xSemaphoreGive(context->wifi_networks_mutex); + xSemaphoreGive(context->wifi_networks_semaph); } #endif @@ -728,7 +729,7 @@ static void wifi_config_server_on_settings(client_t *client) { // Wifi Networks char buffer[150]; char bssid[13]; - if (xSemaphoreTake(context->wifi_networks_mutex, MS_TO_TICKS(4000))) { + if (xSemaphoreTake(context->wifi_networks_semaph, MS_TO_TICKS(4000))) { wifi_network_info_t* net = context->wifi_networks; while (net) { snprintf(bssid, sizeof(bssid), "%02x%02x%02x%02x%02x%02x", net->bssid[0], net->bssid[1], net->bssid[2], net->bssid[3], net->bssid[4], net->bssid[5]); @@ -742,7 +743,7 @@ static void wifi_config_server_on_settings(client_t *client) { net = net->next; } - xSemaphoreGive(context->wifi_networks_mutex); + xSemaphoreGive(context->wifi_networks_semaph); } client_send_chunk(client, html_settings_footer); @@ -1138,11 +1139,13 @@ static void wifi_config_softap_start() { uint8_t macaddr[6]; sdk_wifi_get_macaddr(STATION_IF, macaddr); - + + const uint8_t wifi_channel = ((hwrand() % 3) * 5) + 1; + #ifdef ESP_PLATFORM wifi_config_t softap_config = { .ap = { - .channel = 6, + .channel = wifi_channel, .authmode = WIFI_AUTH_OPEN, .max_connection = 2, .ssid_hidden = 0, @@ -1154,7 +1157,7 @@ static void wifi_config_softap_start() { "%s-%02X%02X%02X", context->ssid_prefix, macaddr[3], macaddr[4], macaddr[5] ); - INFO("Start AP %s", softap_config.ap.ssid); + INFO("Wifi AP %s Ch%i", softap_config.ap.ssid, softap_config.ap.channel); esp_netif_t* ap_netif = esp_netif_create_default_wifi_ap(); assert(ap_netif); @@ -1164,12 +1167,12 @@ static void wifi_config_softap_start() { IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0); esp_netif_set_ip_info(ap_netif, &ap_ip); - INFO("Start DHCP"); esp_wifi_set_config(WIFI_IF_AP, &softap_config); + esp_wifi_set_bandwidth(WIFI_IF_AP, WIFI_BW_HT20); #else struct sdk_softap_config softap_config; softap_config.ssid_hidden = 0; - softap_config.channel = 6; + softap_config.channel = wifi_channel; softap_config.authmode = AUTH_OPEN; softap_config.max_connection = 2; softap_config.beacon_interval = 100; @@ -1179,25 +1182,24 @@ static void wifi_config_softap_start() { "%s-%02X%02X%02X", context->ssid_prefix, macaddr[3], macaddr[4], macaddr[5] ); - INFO("Start AP %s", softap_config.ssid); - + INFO("Wifi AP %s Ch%i", softap_config.ssid, softap_config.channel); + struct ip_info ap_ip; IP4_ADDR(&ap_ip.ip, 192, 168, 4, 1); IP4_ADDR(&ap_ip.netmask, 255, 255, 255, 0); IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0); sdk_wifi_set_ip_info(SOFTAP_IF, &ap_ip); - + sdk_wifi_softap_set_config(&softap_config); - + ip4_addr_t first_client_ip; first_client_ip.addr = ap_ip.ip.addr + htonl(1); - - INFO("Start DHCP"); + dhcpserver_start(&first_client_ip, 4); #endif - context->wifi_networks_mutex = xSemaphoreCreateBinary(); - xSemaphoreGive(context->wifi_networks_mutex); + context->wifi_networks_semaph = xSemaphoreCreateBinary(); + xSemaphoreGive(context->wifi_networks_semaph); xTaskCreate(http_task, "WEB", (TASK_SIZE_FACTOR * 640), NULL, (tskIDLE_PRIORITY + 1), NULL); } @@ -1285,7 +1287,7 @@ static void wifi_config_sta_connect_timeout_task() { } else if (is_station_mode) { context->check_counter++; - if (context->check_counter > 32) { + if (context->check_counter > 35) { context->check_counter = 0; #ifdef ESP_PLATFORM wifi_config_connect(0, true); @@ -1345,6 +1347,10 @@ uint8_t wifi_config_connect(const uint8_t mode, const uint8_t phy, const bool wi sdk_wifi_station_set_config(&sta_config); + if (!context->bandwidth_40) { + esp_wifi_set_bandwidth(WIFI_IF_STA, WIFI_BW_HT20); + } + if (context->wifi_sleep_mode == 0) { esp_wifi_set_ps(WIFI_PS_NONE); } else { @@ -1509,7 +1515,7 @@ static void wifi_config_station_connect() { } #ifdef ESP_PLATFORM -void wifi_config_init(const char* ssid_prefix, void (*on_wifi_ready)(), const char* custom_hostname, const int param, const uint8_t wifi_sleep_mode) { +void wifi_config_init(const char* ssid_prefix, void (*on_wifi_ready)(), const char* custom_hostname, const int param, const uint8_t wifi_sleep_mode, const bool bandwidth_40) { #else void wifi_config_init(const char* ssid_prefix, void (*on_wifi_ready)(), const char* custom_hostname, const int param) { #endif @@ -1522,6 +1528,7 @@ void wifi_config_init(const char* ssid_prefix, void (*on_wifi_ready)(), const ch #ifdef ESP_PLATFORM context->wifi_sleep_mode = wifi_sleep_mode; + context->bandwidth_40 = bandwidth_40; if (on_wifi_ready && custom_hostname) { esp_netif_set_hostname(setup_esp_netif, strdup(custom_hostname)); diff --git a/HAA/HAA_Main/main/setup.h b/HAA/HAA_Main/main/setup.h index d736e44a..6189d5f9 100644 --- a/HAA/HAA_Main/main/setup.h +++ b/HAA/HAA_Main/main/setup.h @@ -7,7 +7,7 @@ uint32_t wifi_config_get_full_gw(); #ifdef ESP_PLATFORM -void wifi_config_init(const char* ssid_prefix, void (*on_wifi_ready)(), const char* custom_hostname, const int param, const uint8_t wifi_sleep_mode); +void wifi_config_init(const char* ssid_prefix, void (*on_wifi_ready)(), const char* custom_hostname, const int param, const uint8_t wifi_sleep_mode, const bool bandwidth_40); void setup_set_esp_netif(esp_netif_t* esp_net_if); esp_netif_t* setup_get_esp_netif(); void setup_set_boot_installer(); diff --git a/HAA/HAA_Main/main/types.h b/HAA/HAA_Main/main/types.h index bd6dfabc..6d10d1da 100644 --- a/HAA/HAA_Main/main/types.h +++ b/HAA/HAA_Main/main/types.h @@ -101,6 +101,7 @@ typedef struct _action_uart { uint16_t len; uint16_t pause; + uint8_t* command; struct _action_uart* next; diff --git a/external_libs/cJSON/cJSON/cJSON.c b/external_libs/cJSON/cJSON/cJSON.c index 620173e8..9e1e2517 100644 --- a/external_libs/cJSON/cJSON/cJSON.c +++ b/external_libs/cJSON/cJSON/cJSON.c @@ -1755,14 +1755,14 @@ static cJSON *get_object_item(const cJSON * const object, const char * const nam current_element = object->child; if (case_sensitive) { - while ((current_element != NULL) && (strcmp(name, current_element->string) != 0)) + while ((current_element != NULL) && ( !current_element->string || (strcmp(name, current_element->string) != 0) )) { current_element = current_element->next; } } else { - while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0)) + while ((current_element != NULL) && ( !current_element->string || (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0)) ) { current_element = current_element->next; } diff --git a/libs/adv_button/adv_button.c b/libs/adv_button/adv_button.c index 68407b41..eded7730 100644 --- a/libs/adv_button/adv_button.c +++ b/libs/adv_button/adv_button.c @@ -122,7 +122,7 @@ typedef struct _adv_button_main_config { static adv_button_main_config_t* adv_button_main_config = NULL; -static adv_button_t* IRAM button_find_by_gpio(const unsigned int gpio) { +static adv_button_t* IRAM button_find_by_gpio(const uint16_t gpio) { if (adv_button_main_config) { adv_button_t* button = adv_button_main_config->buttons; @@ -136,7 +136,7 @@ static adv_button_t* IRAM button_find_by_gpio(const unsigned int gpio) { return NULL; } -static adv_button_mcp_t* mcp_find_by_index(const unsigned int index) { +static adv_button_mcp_t* mcp_find_by_index(const uint8_t index) { if (adv_button_main_config) { adv_button_mcp_t* mcp = adv_button_main_config->mcps; @@ -161,10 +161,10 @@ int adv_button_read_by_gpio(const uint16_t gpio) { return result; } -static int adv_button_read_mcp_gpio(const unsigned int gpio) { +static bool adv_button_read_mcp_gpio(const uint16_t gpio) { adv_button_mcp_t* adv_button_mcp = mcp_find_by_index(gpio / 100); if (adv_button_mcp) { - const int mcp_gpio = gpio % 100; + const unsigned int mcp_gpio = gpio % 100; if (mcp_gpio >= 8) { // Channel B return (bool) ((1 << (mcp_gpio - 8)) & adv_button_mcp->value_b); @@ -177,7 +177,7 @@ static int adv_button_read_mcp_gpio(const unsigned int gpio) { return false; } -static void adv_button_run_callback_fn(adv_button_callback_fn_t* callbacks, const unsigned int gpio) { +static void adv_button_run_callback_fn(adv_button_callback_fn_t* callbacks, const uint16_t gpio) { adv_button_callback_fn_t* adv_button_callback_fn = callbacks; while (adv_button_callback_fn) { @@ -186,10 +186,10 @@ static void adv_button_run_callback_fn(adv_button_callback_fn_t* callbacks, cons } } -static void push_down(const unsigned int used_gpio) { +static inline void push_down(const uint16_t used_gpio) { const uint32_t now = xTaskGetTickCount(); - if (now - adv_button_main_config->disable_time > DISABLE_TIME / portTICK_PERIOD_MS) { + if ((now - adv_button_main_config->disable_time) > (DISABLE_TIME / portTICK_PERIOD_MS)) { adv_button_t *button = button_find_by_gpio(used_gpio); if (button->singlepress0_callback_fn) { adv_button_run_callback_fn(button->singlepress0_callback_fn, button->gpio); @@ -201,10 +201,10 @@ static void push_down(const unsigned int used_gpio) { } } -static inline void push_up(const unsigned int used_gpio) { +static inline void push_up(const uint16_t used_gpio) { const uint32_t now = xTaskGetTickCount(); - if (now - adv_button_main_config->disable_time > DISABLE_TIME / portTICK_PERIOD_MS) { + if ((now - adv_button_main_config->disable_time) > (DISABLE_TIME / portTICK_PERIOD_MS)) { adv_button_t *button = button_find_by_gpio(used_gpio); if (button->press_count == DISABLE_PRESS_COUNT) { @@ -214,7 +214,7 @@ static inline void push_up(const unsigned int used_gpio) { rs_esp_timer_stop(button->hold_timer); - if (now - button->last_event_time > VERYLONGPRESS_TIME / portTICK_PERIOD_MS) { + if ((now - button->last_event_time) > (VERYLONGPRESS_TIME / portTICK_PERIOD_MS)) { // Very Long button pressed button->press_count = 0; if (button->verylongpress_callback_fn) { @@ -224,7 +224,7 @@ static inline void push_up(const unsigned int used_gpio) { } else { adv_button_run_callback_fn(button->singlepress_callback_fn, button->gpio); } - } else if (now - button->last_event_time > LONGPRESS_TIME / portTICK_PERIOD_MS) { + } else if ((now - button->last_event_time) > (LONGPRESS_TIME / portTICK_PERIOD_MS)) { // Long button pressed button->press_count = 0; if (button->longpress_callback_fn) { @@ -292,6 +292,7 @@ static void IRAM adv_button_interrupt_normal(const uint8_t gpio) { gpio_set_interrupt(button->gpio, GPIO_INTTYPE_NONE, NULL); #endif } + button = button->next; } } @@ -307,6 +308,7 @@ static void button_evaluate_fn() { if (!adv_button_main_config->continuos_mode) { if (adv_button_main_config->button_evaluate_sleep_countdown < adv_button_main_config->button_evaluate_sleep_time) { adv_button_main_config->button_evaluate_sleep_countdown++; + } else if (adv_button_main_config->button_evaluate_sleep_countdown == adv_button_main_config->button_evaluate_sleep_time) { adv_button_main_config->button_evaluate_sleep_countdown++; @@ -321,6 +323,7 @@ static void button_evaluate_fn() { button = button->next; } + } else { rs_esp_timer_stop(adv_button_main_config->button_evaluate_timer); } @@ -328,46 +331,59 @@ static void button_evaluate_fn() { adv_button_mcp_t* mcp = adv_button_main_config->mcps; while (mcp) { - if (mcp->channels == MCP_CHANNEL_A || mcp->channels == MCP_CHANNEL_BOTH) { + if (mcp->channels != MCP_CHANNEL_B) { const uint8_t reg = 0x12; adv_i2c_slave_read_no_wait(mcp->bus, mcp->addr, ®, 1, &mcp->value_a, 1); } - if (mcp->channels > MCP_CHANNEL_A) { + if (mcp->channels != MCP_CHANNEL_A) { const uint8_t reg = 0x13; adv_i2c_slave_read_no_wait(mcp->bus, mcp->addr, ®, 1, &mcp->value_b, 1); } mcp = mcp->next; } - + adv_button_t* button = adv_button_main_config->buttons; while (button) { - if (button->mode == ADV_BUTTON_NORMAL_MODE) { + if (button->mode != ADV_BUTTON_PULSE_MODE) { + int read_value; + if (button->mode == ADV_BUTTON_NORMAL_MODE) { #ifdef ESP_PLATFORM - int read_value = gpio_read(button->gpio); -#else - int read_value = false; - if (button->gpio <= 16) { read_value = gpio_read(button->gpio); - } else { - read_value = (sdk_system_adc_read() > ADC_MID_VALUE); - } +#else + if (button->gpio <= 16) { + read_value = gpio_read(button->gpio); + } else { // gpio == 17 (ESP8266 ADC pin) + read_value = (sdk_system_adc_read() > ADC_MID_VALUE); + } #endif + } else { // MCP23017 + read_value = adv_button_read_mcp_gpio(button->gpio); + } if (read_value) { - button->value = ADV_BUTTON_MIN(button->value++, button->max_eval); - if (button->value == button->max_eval) { - button->state = true; + if (button->state) { + button->value = button->max_eval; + } else { + button->value = ADV_BUTTON_MIN(button->value++, button->max_eval); + if (button->value == button->max_eval) { + button->state = true; + } } } else { - button->value = ADV_BUTTON_MAX(button->value--, 0); - if (button->value == 0) { - button->state = false; + if (!button->state) { + button->value = 0; + } else { + button->value = ADV_BUTTON_MAX(button->value--, 0); + if (button->value == 0) { + button->state = false; + } } } - } else if (button->mode == ADV_BUTTON_PULSE_MODE) { + + } else { // button->mode == ADV_BUTTON_PULSE_MODE if (button->value == button->max_eval) { button->value = button->value >> 1; button->state = true; @@ -377,18 +393,6 @@ static void button_evaluate_fn() { button->state = false; } } - } else { // MCP23017 - if (adv_button_read_mcp_gpio(button->gpio) != 0) { - button->value = ADV_BUTTON_MIN(button->value++, button->max_eval); - if (button->value == button->max_eval) { - button->state = true; - } - } else { - button->value = ADV_BUTTON_MAX(button->value--, 0); - if (button->value == 0) { - button->state = false; - } - } } if (button->state != button->old_state) { @@ -477,6 +481,10 @@ int adv_button_create(const uint16_t gpio, const bool inverted, const uint8_t mo } */ +#ifdef ESP_PLATFORM + int ret = 0; +#endif + if (mode == ADV_BUTTON_NORMAL_MODE) { //vTaskDelay(1); @@ -485,7 +493,7 @@ int adv_button_create(const uint16_t gpio, const bool inverted, const uint8_t mo #else if (gpio <= 16) { button->state = gpio_read(gpio); - } else { // gpio == 17 + } else { // gpio == 17 (ESP8266 ADC pin) button->state = (sdk_system_adc_read() > ADC_MID_VALUE); } #endif @@ -500,7 +508,7 @@ int adv_button_create(const uint16_t gpio, const bool inverted, const uint8_t mo #ifdef ESP_PLATFORM gpio_set_intr_type(gpio, GPIO_INTR_ANYEDGE); - gpio_isr_handler_add(gpio, adv_button_interrupt_normal, (void*) ((uint32_t) gpio)); + ret = gpio_isr_handler_add(gpio, adv_button_interrupt_normal, (void*) ((uint32_t) gpio)); #else gpio_set_interrupt(gpio, GPIO_INTTYPE_EDGE_ANY, adv_button_interrupt_normal); #endif @@ -511,7 +519,7 @@ int adv_button_create(const uint16_t gpio, const bool inverted, const uint8_t mo #ifdef ESP_PLATFORM gpio_set_intr_type(gpio, GPIO_INTR_NEGEDGE); - gpio_isr_handler_add(gpio, adv_button_interrupt_pulse, (void*) ((uint32_t) gpio)); + ret = gpio_isr_handler_add(gpio, adv_button_interrupt_pulse, (void*) ((uint32_t) gpio)); #else gpio_set_interrupt(gpio, GPIO_INTTYPE_EDGE_NEG, adv_button_interrupt_pulse); #endif @@ -559,7 +567,11 @@ int adv_button_create(const uint16_t gpio, const bool inverted, const uint8_t mo } } +#ifdef ESP_PLATFORM + return ret; +#else return 0; +#endif } return -1; diff --git a/libs/homekit-rsf/src/server.c b/libs/homekit-rsf/src/server.c index af44e93a..2390f407 100644 --- a/libs/homekit-rsf/src/server.c +++ b/libs/homekit-rsf/src/server.c @@ -18,6 +18,8 @@ #include "esp_attr.h" #define IRAM IRAM_ATTR +#define HK_LONGINT_F "li" + #else #include @@ -27,6 +29,8 @@ #include #include +#define HK_LONGINT_F "i" + #endif #include @@ -93,15 +97,9 @@ #define HOMEKIT_INFO(message, ...) INFO(message, ##__VA_ARGS__) #define HOMEKIT_ERROR(message, ...) ERROR(message, ##__VA_ARGS__) -#ifdef ESP_PLATFORM -#define CLIENT_DEBUG(client, message, ...) DEBUG("[%li] " message, client->socket, ##__VA_ARGS__) -#define CLIENT_INFO(client, message, ...) INFO("[%li] " message, client->socket, ##__VA_ARGS__) -#define CLIENT_ERROR(client, message, ...) ERROR("[%li] " message, client->socket, ##__VA_ARGS__) -#else -#define CLIENT_DEBUG(client, message, ...) DEBUG("[%d] " message, client->socket, ##__VA_ARGS__) -#define CLIENT_INFO(client, message, ...) INFO("[%d] " message, client->socket, ##__VA_ARGS__) -#define CLIENT_ERROR(client, message, ...) ERROR("[%d] " message, client->socket, ##__VA_ARGS__) -#endif +#define CLIENT_DEBUG(client, message, ...) DEBUG("[%"HK_LONGINT_F"] " message, client->socket, ##__VA_ARGS__) +#define CLIENT_INFO(client, message, ...) INFO("[%"HK_LONGINT_F"] " message, client->socket, ##__VA_ARGS__) +#define CLIENT_ERROR(client, message, ...) ERROR("[%"HK_LONGINT_F"] " message, client->socket, ##__VA_ARGS__) struct _client_context_t; typedef struct _client_context_t client_context_t; @@ -404,11 +402,7 @@ void pairing_context_free(pairing_context_t *context) { static int IRAM homekit_low_dram() { const uint32_t free_heap = xPortGetFreeHeapSize(); if (free_heap < HOMEKIT_MIN_FREEHEAP) { -#ifdef ESP_PLATFORM - HOMEKIT_ERROR("DRAM Free HEAP %li", free_heap); -#else - HOMEKIT_ERROR("DRAM Free HEAP %i", free_heap); -#endif + HOMEKIT_ERROR("DRAM Free HEAP %"HK_LONGINT_F, free_heap); return true; } @@ -3362,20 +3356,15 @@ static inline void IRAM homekit_server_accept_client() { if (s > homekit_server->max_fd) { homekit_server->max_fd = s; } -#ifdef ESP_PLATFORM - HOMEKIT_INFO("[%i] New %s:%d %i/%i Free HEAP %li", s, address_buffer, addr.sin_port, homekit_server->client_count, homekit_server->config->max_clients, free_heap); -#else - HOMEKIT_INFO("[%d] New %s:%d %i/%i Free HEAP %d", s, address_buffer, addr.sin_port, homekit_server->client_count, homekit_server->config->max_clients, free_heap); -#endif + + HOMEKIT_INFO("[%i] New %s:%d %i/%i Free HEAP %"HK_LONGINT_F, s, address_buffer, addr.sin_port, homekit_server->client_count, homekit_server->config->max_clients, free_heap); + HOMEKIT_NOTIFY_EVENT(homekit_server, HOMEKIT_EVENT_CLIENT_CONNECTED); } else { close(s); -#ifdef ESP_PLATFORM - HOMEKIT_ERROR("[%i] DRAM %s:%d %i/%i Free HEAP %li", s, address_buffer, addr.sin_port, homekit_server->client_count, homekit_server->config->max_clients, free_heap); -#else - HOMEKIT_ERROR("[%d] DRAM %s:%d %i/%i Free HEAP %d", s, address_buffer, addr.sin_port, homekit_server->client_count, homekit_server->config->max_clients, free_heap); -#endif + + HOMEKIT_ERROR("[%i] DRAM %s:%d %i/%i Free HEAP %"HK_LONGINT_F, s, address_buffer, addr.sin_port, homekit_server->client_count, homekit_server->config->max_clients, free_heap); } } diff --git a/libs/new_dht/dht.c b/libs/new_dht/dht.c index ca09c5bf..8b6a1fdf 100644 --- a/libs/new_dht/dht.c +++ b/libs/new_dht/dht.c @@ -129,13 +129,13 @@ static inline bool dht_fetch_data(dht_sensor_type_t sensor_type, uint8_t pin, bo gpio_write(pin, 0); sdk_os_delay_us(sensor_type == DHT_TYPE_SI7021 ? 500 : 20000); gpio_write(pin, 1); - + // Step through Phase 'B', 200us if (!dht_await_pin_state(pin, sensor_type == DHT_TYPE_SI7021 ? 200 : 40, false, NULL)) { debug("Initialization error, problem in phase 'B'\n"); return false; } - + // Step through Phase 'C', 88us if (!dht_await_pin_state(pin, 88, true, NULL)) { debug("Initialization error, problem in phase 'C'\n"); @@ -189,7 +189,7 @@ bool dht_read_data(dht_sensor_type_t sensor_type, uint8_t pin, int16_t *humidity { bool bits[DHT_DATA_BITS]; uint8_t data[DHT_DATA_BITS / 8] = { 0 }; - bool result; + bool result = false; xSemaphoreTake(dht_lock, pdMS_TO_TICKS(DHT_TIMEOUT_MS)); @@ -213,6 +213,8 @@ bool dht_read_data(dht_sensor_type_t sensor_type, uint8_t pin, int16_t *humidity gpio_disable(pin); #endif + vTaskDelay(pdMS_TO_TICKS(50)); + xSemaphoreGive(dht_lock); if (!result) { @@ -221,8 +223,8 @@ bool dht_read_data(dht_sensor_type_t sensor_type, uint8_t pin, int16_t *humidity for (int i = 0; i < DHT_DATA_BITS; i++) { // Read each bit into 'result' byte array... - data[i/8] <<= 1; - data[i/8] |= bits[i]; + data[i / 8] <<= 1; + data[i / 8] |= bits[i]; } if (data[4] != ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {