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,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 <System_Events.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <string.h>
@ -18,11 +41,11 @@ static TaskHandle_t xTaskHandle;
static muxed_led_strip_handle_t NeoPixel_Out;
// GPIO assignments
#define NEOPIXEL_OUT_GPIO GPIO_NUM_48
#define BARREL_ENABLE_GPIO GPIO_NUM_18
#define RECEIVER_ENABLE_GPIO GPIO_NUM_45
#define DISPLAY_ENABLE_GPIO GPIO_NUM_10
#define EFFECTS_ENABLE_GPIO GPIO_NUM_17
#define NEOPIXEL_OUT_GPIO GPIO_NUM_48
#define BARREL_ENABLE_GPIO GPIO_NUM_18
#define RECEIVER_ENABLE_GPIO GPIO_NUM_45
#define DISPLAY_ENABLE_GPIO GPIO_NUM_10
#define EFFECTS_ENABLE_GPIO GPIO_NUM_17
// 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution)
#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)) = {
.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
.spi_bus = SPI2_HOST, // SPI bus ID
.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
.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);
@ -147,13 +170,14 @@ void Initialize_SystemK_NeoPixels(SemaphoreHandle_t init_complete)
&xTaskBuffer, // Variable to hold the task's data structure.
APP_CPU_NUM); // Specify the task's core affinity.
xEventGroupSetBits(Get_System_Events(), SYS_NEOPIXELS_READY);
KLOG_INFO(TAG, "Initialization complete.");
xSemaphoreGive(init_complete);
}
SystemKResult_T HW_NeoPixels_Init(void)
{
KLOG_INFO(TAG, "Initializing NeoPixels...");
if (CONFIG_KTAG_N_NEOPIXEL_CHANNELS > 0)
{
// Initialize the NeoPixel Out and the Barrel Enable.
@ -220,11 +244,11 @@ color_t HW_NeoPixels_Get_My_Color(void)
uint8_t Player_ID;
uint8_t Weapon_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_WEAPONID, &Weapon_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_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);
return result;
}

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
#define HW_NEOPIXELS_H
void Initialize_SystemK_NeoPixels(SemaphoreHandle_t init_complete);
void Initialize_SystemK_NeoPixels(void);
#endif // HW_NEOPIXELS_H

View file

@ -55,7 +55,6 @@
#include "Reprogramming.h"
static const char *TAG = "KTag 2024A";
static SemaphoreHandle_t init_complete_semaphore;
static const uint16_t INITIALIZATION_TIMEOUT_IN_ms = 10 * 1000;
void app_main(void)
@ -66,8 +65,6 @@ void app_main(void)
Initialize_System_Events();
init_complete_semaphore = xSemaphoreCreateBinary();
// Initialize NVS — it is used by both the BLE and WiFi drivers.
esp_err_t ret = nvs_flash_init();
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));
}
Initialize_SPIFFS(init_complete_semaphore);
if (xSemaphoreTake(init_complete_semaphore, pdMS_TO_TICKS(INITIALIZATION_TIMEOUT_IN_ms)) != pdTRUE)
{
KLOG_ERROR(TAG, "Timeout initializing SPIFFS!");
}
Initialize_SPIFFS();
Wait_For_System_Event(SYS_SPIFFS_READY, "Timeout initializing SPIFFS!", INITIALIZATION_TIMEOUT_IN_ms);
Initialize_USB();
EventBits_t bits = xEventGroupWaitBits(
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!");
}
Wait_For_System_Event(SYS_USB_FS_PRESENT, "Timeout initializing USB!", INITIALIZATION_TIMEOUT_IN_ms);
if (OTA_File_Exists() == true)
{
@ -115,20 +98,17 @@ void app_main(void)
else
{
Initialize_Audio();
Wait_For_System_Event(SYS_AUDIO_READY, "Timeout initializing Audio!", INITIALIZATION_TIMEOUT_IN_ms);
Initialize_SystemK_NeoPixels(init_complete_semaphore);
if (xSemaphoreTake(init_complete_semaphore, pdMS_TO_TICKS(INITIALIZATION_TIMEOUT_IN_ms)) != pdTRUE)
{
KLOG_ERROR(TAG, "Timeout initializing NeoPixels!");
}
Initialize_SystemK_NeoPixels();
Wait_For_System_Event(SYS_NEOPIXELS_READY, "Timeout initializing NeoPixels!", INITIALIZATION_TIMEOUT_IN_ms);
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)
{
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_Switches();
vSemaphoreDelete(init_complete_semaphore);
KLOG_INFO(TAG, "Initialization complete.");
}