Converted initialization to use System Events.

This commit is contained in:
Joe Kearney 2025-11-23 16:01:02 -06:00
parent 0dcbae7ec6
commit c3362d42af
17 changed files with 243 additions and 58 deletions

View file

@ -1,3 +1,25 @@
/*
* This program source code file is part of the KTag project, a DIY laser tag
* game with customizable features and wide interoperability.
*
* 🛡 <https://ktag.clubk.club> 🃞
*
* Copyright © 2024-2025 Joseph P. Kearney and the KTag developers.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* There should be a copy of the GNU Affero General Public License in the LICENSE
* file in the root of this repository. If not, see <http://www.gnu.org/licenses/>.
*/
#include <SystemK.h> #include <SystemK.h>
#include <System_Events.h> #include <System_Events.h>
#include <stdio.h> #include <stdio.h>
@ -150,6 +172,9 @@ void Initialize_Audio(void)
{ {
KLOG_ERROR(TAG, "Couldn't create the I2S Audio task."); KLOG_ERROR(TAG, "Couldn't create the I2S Audio task.");
} }
xEventGroupSetBits(Get_System_Events(), SYS_AUDIO_READY);
KLOG_INFO(TAG, "Initialization complete.");
} }
esp_err_t Play_Audio_File(const char *filename) esp_err_t Play_Audio_File(const char *filename)

View file

@ -1,3 +1,25 @@
/*
* This program source code file is part of the KTag project, a DIY laser tag
* game with customizable features and wide interoperability.
*
* 🛡 <https://ktag.clubk.club> 🃞
*
* Copyright © 2024-2025 Joseph P. Kearney and the KTag developers.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* There should be a copy of the GNU Affero General Public License in the LICENSE
* file in the root of this repository. If not, see <http://www.gnu.org/licenses/>.
*/
#include "esp_err.h" #include "esp_err.h"
void Initialize_Audio(void); void Initialize_Audio(void);

View file

@ -1,4 +1,27 @@
/*
* This program source code file is part of the KTag project, a DIY laser tag
* game with customizable features and wide interoperability.
*
* 🛡 <https://ktag.clubk.club> 🃞
*
* Copyright © 2024-2025 Joseph P. Kearney and the KTag developers.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* There should be a copy of the GNU Affero General Public License in the LICENSE
* file in the root of this repository. If not, see <http://www.gnu.org/licenses/>.
*/
#include <SystemK.h> #include <SystemK.h>
#include <System_Events.h>
#include "esp_log.h" #include "esp_log.h"
/* BLE */ /* BLE */
#include "esp_nimble_hci.h" #include "esp_nimble_hci.h"
@ -204,6 +227,9 @@ void Initialize_BLE(void)
ble_store_config_init(); ble_store_config_init();
nimble_port_freertos_init(ble_host_task); nimble_port_freertos_init(ble_host_task);
xEventGroupSetBits(Get_System_Events(), SYS_BLE_READY);
KLOG_INFO(TAG, "Initialization complete.");
} }
void Disable_BLE(void) void Disable_BLE(void)

View file

@ -1,3 +1,25 @@
/*
* This program source code file is part of the KTag project, a DIY laser tag
* game with customizable features and wide interoperability.
*
* 🛡 <https://ktag.clubk.club> 🃞
*
* Copyright © 2024-2025 Joseph P. Kearney and the KTag developers.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* There should be a copy of the GNU Affero General Public License in the LICENSE
* file in the root of this repository. If not, see <http://www.gnu.org/licenses/>.
*/
#include "esp_err.h" #include "esp_err.h"
void Initialize_BLE(void); void Initialize_BLE(void);

View file

@ -5,6 +5,7 @@ idf_component_register(
"." "."
REQUIRES REQUIRES
"SystemK" "SystemK"
"System_Events"
"driver" "driver"
"bt" "bt"
) )

View file

@ -5,5 +5,6 @@ idf_component_register(
"." "."
REQUIRES REQUIRES
"SystemK" "SystemK"
"System_Events"
"driver" "driver"
) )

View file

@ -21,6 +21,7 @@
*/ */
#include <SystemK.h> #include <SystemK.h>
#include <System_Events.h>
#include <IR.h> #include <IR.h>
#include <driver/gpio.h> #include <driver/gpio.h>
#include <driver/rmt_tx.h> #include <driver/rmt_tx.h>
@ -272,7 +273,7 @@ static void Initialize_Receive_Task(void)
} }
} }
void Initialize_IR(SemaphoreHandle_t init_complete) void Initialize_IR()
{ {
KLOG_INFO(TAG, "Initializing IR..."); KLOG_INFO(TAG, "Initializing IR...");
@ -375,7 +376,8 @@ void Initialize_IR(SemaphoreHandle_t init_complete)
ESP_ERROR_CHECK(rmt_enable(Right_Rx_Channel)); ESP_ERROR_CHECK(rmt_enable(Right_Rx_Channel));
ESP_ERROR_CHECK(rmt_receive(Right_Rx_Channel, &Right_Rxd_RMT_Data, sizeof(Right_Rxd_RMT_Data.pulsetrain), &Rx_Config)); ESP_ERROR_CHECK(rmt_receive(Right_Rx_Channel, &Right_Rxd_RMT_Data, sizeof(Right_Rxd_RMT_Data.pulsetrain), &Rx_Config));
xSemaphoreGive(init_complete); xEventGroupSetBits(Get_System_Events(), SYS_IR_READY);
KLOG_INFO(TAG, "Initialization complete.");
} }
static inline void PrintPulseTrainToConsole(TimedPulseTrain_T *train) static inline void PrintPulseTrainToConsole(TimedPulseTrain_T *train)

View file

@ -22,6 +22,6 @@
#include "esp_err.h" #include "esp_err.h"
void Initialize_IR(SemaphoreHandle_t init_complete); void Initialize_IR();
void Enable_IR_Transmit(); void Enable_IR_Transmit();
void Disable_IR_Transmit(); void Disable_IR_Transmit();

View file

@ -27,7 +27,7 @@
static const char *TAG = "SPIFFS"; static const char *TAG = "SPIFFS";
const char *DEFAULT_CONFIG_FILE = "/spiffs/default_config.txt"; const char *DEFAULT_CONFIG_FILE = "/spiffs/default_config.txt";
void Initialize_SPIFFS(SemaphoreHandle_t init_complete) void Initialize_SPIFFS(void)
{ {
KLOG_INFO(TAG, "Initializing SPI flash file system (SPIFFS)..."); KLOG_INFO(TAG, "Initializing SPI flash file system (SPIFFS)...");
@ -84,6 +84,4 @@ void Initialize_SPIFFS(SemaphoreHandle_t init_complete)
xEventGroupSetBits(Get_System_Events(), SYS_SPIFFS_READY); xEventGroupSetBits(Get_System_Events(), SYS_SPIFFS_READY);
KLOG_INFO(TAG, "SPIFFS initialized."); KLOG_INFO(TAG, "SPIFFS initialized.");
xSemaphoreGive(init_complete);
} }

View file

@ -21,6 +21,6 @@
#pragma once #pragma once
void Initialize_SPIFFS(SemaphoreHandle_t init_complete); void Initialize_SPIFFS(void);
extern const char *DEFAULT_CONFIG_FILE; extern const char *DEFAULT_CONFIG_FILE;

View file

@ -1,3 +1,25 @@
/*
* This program source code file is part of the KTag project, a DIY laser tag
* game with customizable features and wide interoperability.
*
* 🛡 <https://ktag.clubk.club> 🃞
*
* Copyright © 2024-2025 Joseph P. Kearney and the KTag developers.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* There should be a copy of the GNU Affero General Public License in the LICENSE
* file in the root of this repository. If not, see <http://www.gnu.org/licenses/>.
*/
#include <SystemK.h> #include <SystemK.h>
#include <driver/gpio.h> #include <driver/gpio.h>
#include <iot_button.h> #include <iot_button.h>

View file

@ -1,2 +1,23 @@
/*
* This program source code file is part of the KTag project, a DIY laser tag
* game with customizable features and wide interoperability.
*
* 🛡 <https://ktag.clubk.club> 🃞
*
* Copyright © 2024-2025 Joseph P. Kearney and the KTag developers.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* There should be a copy of the GNU Affero General Public License in the LICENSE
* file in the root of this repository. If not, see <http://www.gnu.org/licenses/>.
*/
void Initialize_Switches(void); void Initialize_Switches(void);

View file

@ -39,3 +39,21 @@ EventGroupHandle_t Get_System_Events(void)
{ {
return The_System_Events; return The_System_Events;
} }
bool Wait_For_System_Event(EventBits_t event_bit, const char* timeout_message, uint32_t timeout_ms)
{
EventBits_t bits = xEventGroupWaitBits(
The_System_Events,
event_bit,
pdFALSE,
pdTRUE,
pdMS_TO_TICKS(timeout_ms));
if ((bits & event_bit) == 0)
{
KLOG_ERROR(TAG, "%s", timeout_message);
return false;
}
return true;
}

View file

@ -31,11 +31,13 @@
#define SYS_NVS_READY (1 << 0) #define SYS_NVS_READY (1 << 0)
#define SYS_SPIFFS_READY (1 << 1) #define SYS_SPIFFS_READY (1 << 1)
#define SYS_USB_FS_PRESENT (1 << 2) #define SYS_USB_FS_PRESENT (1 << 2)
#define SYS_BLE_READY (1 << 3) #define SYS_AUDIO_READY (1 << 3)
#define SYS_AUDIO_READY (1 << 4) #define SYS_NEOPIXELS_READY (1 << 4)
#define SYS_CONFIG_LOADED (1 << 5) #define SYS_BLE_READY (1 << 5)
#define SYS_IR_READY (1 << 6)
EventGroupHandle_t Get_System_Events(void); EventGroupHandle_t Get_System_Events(void);
void Initialize_System_Events(void); void Initialize_System_Events(void);
bool Wait_For_System_Event(EventBits_t event_bit, const char* timeout_message, uint32_t timeout_ms);
#endif // SYSTEM_EVENTS_H #endif // SYSTEM_EVENTS_H

View file

@ -1,5 +1,28 @@
/*
* This program source code file is part of the KTag project, a DIY laser tag
* game with customizable features and wide interoperability.
*
* 🛡 <https://ktag.clubk.club> 🃞
*
* Copyright © 2024-2025 Joseph P. Kearney and the KTag developers.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* There should be a copy of the GNU Affero General Public License in the LICENSE
* file in the root of this repository. If not, see <http://www.gnu.org/licenses/>.
*/
#include <SystemK.h> #include <SystemK.h>
#include <System_Events.h>
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/task.h> #include <freertos/task.h>
#include <string.h> #include <string.h>
@ -18,11 +41,11 @@ static TaskHandle_t xTaskHandle;
static muxed_led_strip_handle_t NeoPixel_Out; static muxed_led_strip_handle_t NeoPixel_Out;
// GPIO assignments // GPIO assignments
#define NEOPIXEL_OUT_GPIO GPIO_NUM_48 #define NEOPIXEL_OUT_GPIO GPIO_NUM_48
#define BARREL_ENABLE_GPIO GPIO_NUM_18 #define BARREL_ENABLE_GPIO GPIO_NUM_18
#define RECEIVER_ENABLE_GPIO GPIO_NUM_45 #define RECEIVER_ENABLE_GPIO GPIO_NUM_45
#define DISPLAY_ENABLE_GPIO GPIO_NUM_10 #define DISPLAY_ENABLE_GPIO GPIO_NUM_10
#define EFFECTS_ENABLE_GPIO GPIO_NUM_17 #define EFFECTS_ENABLE_GPIO GPIO_NUM_17
// 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution) // 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution)
#define LED_STRIP_RMT_RES_HZ (10 * 1000 * 1000) #define LED_STRIP_RMT_RES_HZ (10 * 1000 * 1000)
@ -127,12 +150,12 @@ static led_strip_rmt_config_t rmt_config_noDMA = {
}; };
static led_strip_spi_config_t spi_config_withDMA __attribute__((unused)) = { static led_strip_spi_config_t spi_config_withDMA __attribute__((unused)) = {
.clk_src = RMT_CLK_SRC_XTAL, // different clock source can lead to different power consumption .clk_src = RMT_CLK_SRC_XTAL, // different clock source can lead to different power consumption
.flags.with_dma = true, // Using DMA can improve performance and help drive more LEDs .flags.with_dma = true, // Using DMA can improve performance and help drive more LEDs
.spi_bus = SPI2_HOST, // SPI bus ID .spi_bus = SPI2_HOST, // SPI bus ID
}; };
void Initialize_SystemK_NeoPixels(SemaphoreHandle_t init_complete) void Initialize_SystemK_NeoPixels(void)
{ {
xStack = (uint8_t *)heap_caps_calloc(1, NEOPIXELS_STACK_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT | MALLOC_CAP_32BIT); xStack = (uint8_t *)heap_caps_calloc(1, NEOPIXELS_STACK_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT | MALLOC_CAP_32BIT);
@ -147,13 +170,14 @@ void Initialize_SystemK_NeoPixels(SemaphoreHandle_t init_complete)
&xTaskBuffer, // Variable to hold the task's data structure. &xTaskBuffer, // Variable to hold the task's data structure.
APP_CPU_NUM); // Specify the task's core affinity. APP_CPU_NUM); // Specify the task's core affinity.
xEventGroupSetBits(Get_System_Events(), SYS_NEOPIXELS_READY);
KLOG_INFO(TAG, "Initialization complete."); KLOG_INFO(TAG, "Initialization complete.");
xSemaphoreGive(init_complete);
} }
SystemKResult_T HW_NeoPixels_Init(void) SystemKResult_T HW_NeoPixels_Init(void)
{ {
KLOG_INFO(TAG, "Initializing NeoPixels...");
if (CONFIG_KTAG_N_NEOPIXEL_CHANNELS > 0) if (CONFIG_KTAG_N_NEOPIXEL_CHANNELS > 0)
{ {
// Initialize the NeoPixel Out and the Barrel Enable. // Initialize the NeoPixel Out and the Barrel Enable.
@ -220,9 +244,9 @@ color_t HW_NeoPixels_Get_My_Color(void)
uint8_t Player_ID; uint8_t Player_ID;
uint8_t Weapon_ID; uint8_t Weapon_ID;
(void) SETTINGS_get_uint8_t(SYSTEMK_SETTING_TEAMID, &Team_ID); (void)SETTINGS_get_uint8_t(SYSTEMK_SETTING_TEAMID, &Team_ID);
(void) SETTINGS_get_uint8_t(SYSTEMK_SETTING_PLAYERID, &Player_ID); (void)SETTINGS_get_uint8_t(SYSTEMK_SETTING_PLAYERID, &Player_ID);
(void) SETTINGS_get_uint8_t(SYSTEMK_SETTING_WEAPONID, &Weapon_ID); (void)SETTINGS_get_uint8_t(SYSTEMK_SETTING_WEAPONID, &Weapon_ID);
result = PROTOCOLS_GetColor(GetWeaponFromID(Weapon_ID).Protocol, Team_ID, Player_ID); result = PROTOCOLS_GetColor(GetWeaponFromID(Weapon_ID).Protocol, Team_ID, Player_ID);

View file

@ -1,6 +1,28 @@
/*
* This program source code file is part of the KTag project, a DIY laser tag
* game with customizable features and wide interoperability.
*
* 🛡 <https://ktag.clubk.club> 🃞
*
* Copyright © 2024-2025 Joseph P. Kearney and the KTag developers.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* There should be a copy of the GNU Affero General Public License in the LICENSE
* file in the root of this repository. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HW_NEOPIXELS_H #ifndef HW_NEOPIXELS_H
#define HW_NEOPIXELS_H #define HW_NEOPIXELS_H
void Initialize_SystemK_NeoPixels(SemaphoreHandle_t init_complete); void Initialize_SystemK_NeoPixels(void);
#endif // HW_NEOPIXELS_H #endif // HW_NEOPIXELS_H

View file

@ -55,7 +55,6 @@
#include "Reprogramming.h" #include "Reprogramming.h"
static const char *TAG = "KTag 2024A"; static const char *TAG = "KTag 2024A";
static SemaphoreHandle_t init_complete_semaphore;
static const uint16_t INITIALIZATION_TIMEOUT_IN_ms = 10 * 1000; static const uint16_t INITIALIZATION_TIMEOUT_IN_ms = 10 * 1000;
void app_main(void) void app_main(void)
@ -66,8 +65,6 @@ void app_main(void)
Initialize_System_Events(); Initialize_System_Events();
init_complete_semaphore = xSemaphoreCreateBinary();
// Initialize NVS — it is used by both the BLE and WiFi drivers. // Initialize NVS — it is used by both the BLE and WiFi drivers.
esp_err_t ret = nvs_flash_init(); esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
@ -86,25 +83,11 @@ void app_main(void)
KLOG_ERROR(TAG, "Error initializing NVS: %s", esp_err_to_name(ret)); KLOG_ERROR(TAG, "Error initializing NVS: %s", esp_err_to_name(ret));
} }
Initialize_SPIFFS();
Initialize_SPIFFS(init_complete_semaphore); Wait_For_System_Event(SYS_SPIFFS_READY, "Timeout initializing SPIFFS!", INITIALIZATION_TIMEOUT_IN_ms);
if (xSemaphoreTake(init_complete_semaphore, pdMS_TO_TICKS(INITIALIZATION_TIMEOUT_IN_ms)) != pdTRUE)
{
KLOG_ERROR(TAG, "Timeout initializing SPIFFS!");
}
Initialize_USB(); Initialize_USB();
EventBits_t bits = xEventGroupWaitBits( Wait_For_System_Event(SYS_USB_FS_PRESENT, "Timeout initializing USB!", INITIALIZATION_TIMEOUT_IN_ms);
Get_System_Events(),
SYS_USB_FS_PRESENT,
pdFALSE,
pdTRUE,
pdMS_TO_TICKS(INITIALIZATION_TIMEOUT_IN_ms));
if ((bits & SYS_USB_FS_PRESENT) == 0)
{
KLOG_ERROR(TAG, "Timeout initializing USB!");
}
if (OTA_File_Exists() == true) if (OTA_File_Exists() == true)
{ {
@ -115,20 +98,17 @@ void app_main(void)
else else
{ {
Initialize_Audio(); Initialize_Audio();
Wait_For_System_Event(SYS_AUDIO_READY, "Timeout initializing Audio!", INITIALIZATION_TIMEOUT_IN_ms);
Initialize_SystemK_NeoPixels(init_complete_semaphore); Initialize_SystemK_NeoPixels();
if (xSemaphoreTake(init_complete_semaphore, pdMS_TO_TICKS(INITIALIZATION_TIMEOUT_IN_ms)) != pdTRUE) Wait_For_System_Event(SYS_NEOPIXELS_READY, "Timeout initializing NeoPixels!", INITIALIZATION_TIMEOUT_IN_ms);
{
KLOG_ERROR(TAG, "Timeout initializing NeoPixels!");
}
Initialize_BLE(); Initialize_BLE();
Wait_For_System_Event(SYS_BLE_READY, "Timeout initializing BLE!", INITIALIZATION_TIMEOUT_IN_ms);
Initialize_IR();
Wait_For_System_Event(SYS_IR_READY, "Timeout initializing IR!", INITIALIZATION_TIMEOUT_IN_ms);
Initialize_IR(init_complete_semaphore);
if (xSemaphoreTake(init_complete_semaphore, pdMS_TO_TICKS(INITIALIZATION_TIMEOUT_IN_ms)) != pdTRUE)
{
KLOG_ERROR(TAG, "Timeout initializing IR!");
}
if (Initialize_SystemK() != SYSTEMK_RESULT_SUCCESS) if (Initialize_SystemK() != SYSTEMK_RESULT_SUCCESS)
{ {
KLOG_ERROR(TAG, "Error initializing SystemK!"); KLOG_ERROR(TAG, "Error initializing SystemK!");
@ -137,7 +117,6 @@ void app_main(void)
// Initialize the switches after SystemK, so xQueueEvents will have already been created. // Initialize the switches after SystemK, so xQueueEvents will have already been created.
Initialize_Switches(); Initialize_Switches();
vSemaphoreDelete(init_complete_semaphore);
KLOG_INFO(TAG, "Initialization complete."); KLOG_INFO(TAG, "Initialization complete.");
} }