91 lines
2.8 KiB
C
91 lines
2.8 KiB
C
#include "driver/gpio.h"
|
|
#include "driver/uart.h"
|
|
#include "esp_log.h"
|
|
#include "esp_phy_init.h"
|
|
#include "esp_rom_gpio.h"
|
|
#include "esp_timer.h"
|
|
#include "esp_wifi.h"
|
|
#include "freertos/idf_additions.h"
|
|
#include "hal/uart_types.h"
|
|
#include "nvs_flash.h"
|
|
|
|
#include "communication_handler.h"
|
|
#include "main.h"
|
|
#include "portmacro.h"
|
|
#include "uart_handler.h"
|
|
#include <stdbool.h>
|
|
|
|
static const char *TAG = "ALOX - MAIN";
|
|
|
|
void SendClientInfoTask() {
|
|
int clientId = 0;
|
|
while (1) {
|
|
ClientInfo info = *get_client_info(clientId);
|
|
send_client_info(clientId, info.isAvailable, info.lastPing);
|
|
clientId += 1;
|
|
clientId = clientId%20;
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
}
|
|
}
|
|
|
|
void app_main(void) {
|
|
ESP_LOGI(TAG, "Starting Alox Powerpod Version %d Build: %s", version,
|
|
BUILD_GIT_HASH);
|
|
|
|
esp_err_t ret = nvs_flash_init();
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
|
|
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
ret = nvs_flash_init();
|
|
}
|
|
ESP_ERROR_CHECK(ret);
|
|
|
|
// GPIO-Pin für Moduserkennung
|
|
gpio_reset_pin(MASTER_MODE_PIN);
|
|
gpio_set_direction(MASTER_MODE_PIN, GPIO_MODE_INPUT);
|
|
bool isMaster = (gpio_get_level(MASTER_MODE_PIN) == 0);
|
|
|
|
// ESP-NOW Initialisieren
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
wifi_config_t wifi_config = {
|
|
.sta =
|
|
{
|
|
.channel = 1, // Kanal 1, stelle sicher, dass alle Geräte
|
|
// denselben Kanal verwenden
|
|
},
|
|
};
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
|
ESP_ERROR_CHECK(esp_now_init());
|
|
|
|
if (isMaster) {
|
|
ESP_ERROR_CHECK(esp_now_register_recv_cb(master_receive_callback));
|
|
} else {
|
|
ESP_ERROR_CHECK(esp_now_register_recv_cb(client_receive_callback));
|
|
}
|
|
|
|
init_com();
|
|
|
|
// Tasks starten basierend auf Master/Client
|
|
if (isMaster) {
|
|
ESP_LOGI(TAG, "Started in Mastermode");
|
|
add_peer(broadcast_address);
|
|
xTaskCreate(master_broadcast_task, "MasterBroadcast", 4096, NULL, 1, NULL);
|
|
// xTaskCreate(master_ping_task, "MasterPing", 4096, NULL, 1, NULL);
|
|
xTaskCreate(master_broadcast_ping, "MasterBroadcastPing", 4096, NULL, 1,
|
|
NULL);
|
|
xTaskCreate(client_monitor_task, "MonitorClientTask", 4096, NULL, 1, NULL);
|
|
init_uart();
|
|
//xTaskCreate(uart_status_task, "MasterUartStatusTask", 4096, NULL, 1, NULL);
|
|
xTaskCreate(SendClientInfoTask, "SendCientInfo", 4096, NULL, 1, NULL);
|
|
} else {
|
|
ESP_LOGI(TAG, "Started in Slavemode");
|
|
xTaskCreate(client_data_sending_task, "ClientDataSending", 4096, NULL, 1,
|
|
NULL);
|
|
}
|
|
}
|