128 lines
4.1 KiB
C
128 lines
4.1 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 "message_handler.h"
|
|
#include "message_parser.h"
|
|
#include "nvs_flash.h"
|
|
|
|
#include "communication_handler.h"
|
|
#include "main.h"
|
|
#include "portmacro.h"
|
|
#include "uart_handler.h"
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
|
|
#include "message_builder.h"
|
|
|
|
static const char *TAG = "ALOX - MAIN";
|
|
static const uint16_t version = 0x0001;
|
|
|
|
void echoCallback(uint8_t msgid, const uint8_t *payload, size_t payload_len) {
|
|
// Code für den Button-Press
|
|
ESP_LOGI(TAG, "Echo command 0x01...");
|
|
uint8_t send_buffer[64];
|
|
size_t len = build_message(0x01, payload, payload_len, send_buffer,
|
|
sizeof(send_buffer));
|
|
uart_write_bytes(MASTER_UART, send_buffer, len);
|
|
}
|
|
|
|
void versionCallback(uint8_t msgid, const uint8_t *payload,
|
|
size_t payload_len) {
|
|
// Code für den Button-Press
|
|
ESP_LOGI(TAG, "Version command 0x02...");
|
|
uint8_t send_buffer[64];
|
|
size_t git_build_hash_len = strlen(BUILD_GIT_HASH);
|
|
|
|
uint8_t send_payload[2 + git_build_hash_len];
|
|
send_payload[0] = (uint8_t)(version & 0xFF);
|
|
send_payload[1] = (uint8_t)((version >> 8) & 0xFF);
|
|
memcpy(&send_payload[2], &BUILD_GIT_HASH, git_build_hash_len);
|
|
|
|
size_t len = build_message(0x02, send_payload, sizeof(send_payload),
|
|
send_buffer, sizeof(send_buffer));
|
|
uart_write_bytes(MASTER_UART, send_buffer, len);
|
|
}
|
|
|
|
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);
|
|
QueueHandle_t parsed_message_queue =
|
|
xQueueCreate(10, sizeof(ParsedMessage_t));
|
|
init_uart(parsed_message_queue);
|
|
InitMessageBroker();
|
|
|
|
xTaskCreate(MessageBrokerTask, "message_handler_task", 4096,
|
|
(void *)&parsed_message_queue, 5, NULL);
|
|
|
|
RegisterCallback(0x01, echoCallback);
|
|
RegisterCallback(0x02, versionCallback);
|
|
|
|
// 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);
|
|
}
|
|
}
|