make code bit more readable

This commit is contained in:
Laila van Reenen 2024-06-18 20:59:22 +02:00
parent c1dc21a3ba
commit 60a5dd7415
Signed by: LailaTheElf
GPG Key ID: 1F4E6EE3E6DDF769
5 changed files with 58 additions and 46 deletions

View File

@ -1,10 +1,11 @@
#include "FIFOBuffChar.h"
#include "boats.h"
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function parameters to match the declaration
bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i)
{
bool ok = false;
if ((fifo->LastEl_p == NULL) && (fifo->FirstEl_p == NULL))
@ -13,7 +14,7 @@ bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function param
fifo->FirstEl_p = malloc(sizeof(FIFOBuffChar_element_t));
if (fifo->FirstEl_p != NULL)
{
fifo->FirstEl_p->character = i; // "value" to "character"
fifo->FirstEl_p->character = i;
fifo->FirstEl_p->nextElement = NULL;
fifo->LastEl_p = fifo->FirstEl_p;
@ -32,7 +33,7 @@ bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function param
FIFOBuffChar_element_t* el = malloc(sizeof(FIFOBuffChar_element_t));
if (el != NULL)
{
el->character = i; // "value" to "character"
el->character = i;
el->nextElement = NULL;
fifo->LastEl_p->nextElement = el;
fifo->LastEl_p = el;
@ -55,7 +56,7 @@ bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function param
//TODO: this while loop is unsave
while (fifo->FirstEl_p != NULL)
{
FIFOBuffChar_get(fifo, NULL); // Replace buffer_get with FIFOBuffChar_get
FIFOBuffChar_get(fifo, NULL);
}
if (fifo->LastEl_p != NULL)
{
@ -63,18 +64,18 @@ bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function param
fifo->LastEl_p = NULL;
printf("buffer_dyn - buffer_put(): ERROR: last pointer is NULL\n");
}
FIFOBuffChar_put(fifo, i); // Replace buffer_put with FIFOBuffChar_put
FIFOBuffChar_put(fifo, i);
ok = false;
}
return ok;
}
bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p) // Change the function parameters to match the declaration
bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p)
{
bool ok = false;
if (fifo->FirstEl_p != NULL)
{
*p = fifo->FirstEl_p->character; // "value" to "character"
*p = fifo->FirstEl_p->character;
FIFOBuffChar_element_t* next = fifo->FirstEl_p->nextElement;
free(fifo->FirstEl_p);
// fifo->FirstEl_p = NULL;
@ -110,6 +111,7 @@ bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p) // Change the function para
else
{
// set first element pointer to next element
fifo->FirstEl_p = next;
ok = true;
}

View File

@ -1,7 +1,7 @@
#ifndef BOATS_h
#define BOATS_h
#include <cstdint>
#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>

View File

@ -5,7 +5,7 @@
int contrl(char* line)
{
return 0;
}
const CMD_t Commands[] = {

View File

@ -9,6 +9,7 @@
#include "../lib/cli/CLI/CLI.h"
#include "../lib/cli/CMDList/CMDList.h"
#include "utils.h"
#include "commands.h"
#define WIFI_SSID "UPC46273"
@ -18,7 +19,7 @@
static const char *TAG = "FR_rx-esp32";
#define MAX_RETRY_ATTEMPTS 2
static int s_ap_creds_num = 0;
// static int s_ap_creds_num = 0;
static int s_retry_num = 0;
uint8_t BoatId = 1;
@ -29,6 +30,9 @@ CMDList_t* cmdList;
bool rxBuffer_overflow = false;
static EventGroupHandle_t s_wifi_event_group;
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
/* The event group allows multiple bits for each event, but we only care about two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
@ -47,23 +51,27 @@ int charOut(const char* line)
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
if (event_base == WIFI_EVENT)
{
esp_wifi_connect();
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
if (s_retry_num < MAX_RETRY_ATTEMPTS)
if (event_id == WIFI_EVENT_STA_START)
{
ESP_LOGI(TAG,"wifi start (first try for connection)");
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
}
else
else if (event_id == WIFI_EVENT_STA_DISCONNECTED)
{
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
ESP_LOGI(TAG,"diconnected");
if (s_retry_num < MAX_RETRY_ATTEMPTS)
{
s_retry_num++;
ESP_LOGI(TAG, "retry num %d", s_retry_num);
esp_wifi_connect();
}
else
{
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
}
ESP_LOGI(TAG,"connect to the AP fail");
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{
@ -74,40 +82,29 @@ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_
}
}
void app_main() {
/* Initialize NVS — it is used to store PHY calibration data */
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);
printChipInfo();
void wifiInit()
{
// create event group
s_wifi_event_group = xEventGroupCreate();
// init network interface
ESP_ERROR_CHECK(esp_netif_init());
// create event loop
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
// init wifi as default station
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));
// register events
ESP_ERROR_CHECK(esp_event_handler_instance_register(
WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(
IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
// set wifi connection config
wifi_config_t wifi_config = {
.sta = {
.ssid = WIFI_SSID,
@ -127,6 +124,18 @@ void app_main() {
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi_init_sta finished.");
}
void app_main() {
/* Initialize NVS — it is used to store PHY calibration data */
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);
printChipInfo();
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */

View File

@ -2,6 +2,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include "esp_system.h"
#include "esp_chip_info.h"