simulation of a defective light bulb
Signed-off-by: Peter Siegmund <mars3142@users.noreply.github.com>
This commit is contained in:
@@ -4,5 +4,7 @@ idf_component_register(SRCS
|
||||
"outdoor.c"
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES
|
||||
esp_driver_gpio
|
||||
esp_driver_gptimer
|
||||
)
|
||||
esp_driver_ledc
|
||||
)
|
||||
|
@@ -2,8 +2,6 @@
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
esp_err_t outdoor_init(void);
|
||||
|
||||
esp_err_t outdoor_start(void);
|
||||
|
||||
esp_err_t outdoor_stop(void);
|
||||
|
@@ -1,16 +1,96 @@
|
||||
#include "outdoor.h"
|
||||
#include "driver/ledc.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_random.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
esp_err_t outdoor_init(void)
|
||||
static const char *TAG = "outdoor";
|
||||
|
||||
// Timer resolution (10 bit = 1024 steps)
|
||||
#define LEDC_RESOLUTION LEDC_TIMER_10_BIT
|
||||
#define MAX_DUTY 1023
|
||||
|
||||
// Constant brightness for the "normal state" (approx. 90%)
|
||||
#define NORMAL_DUTY (MAX_DUTY * 0.9)
|
||||
|
||||
// Parameters for flickering
|
||||
#define FLICKER_CHANCE 5 // 5% chance of flickering per cycle
|
||||
#define FLICKER_COUNT 8 // Number of brightness changes during a flicker
|
||||
|
||||
TaskHandle_t outdoor_task_handle = NULL;
|
||||
|
||||
void outdoor_task(void *pvParameters)
|
||||
{
|
||||
return ESP_OK;
|
||||
ESP_LOGI(TAG, "Simulation of a defective light bulb started.");
|
||||
|
||||
int led_pin = *(int *)pvParameters;
|
||||
|
||||
// 1. LEDC timer configuration
|
||||
ledc_timer_config_t ledc_timer = {.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.timer_num = LEDC_TIMER_0,
|
||||
.duty_resolution = LEDC_RESOLUTION,
|
||||
.freq_hz = 5000,
|
||||
.clk_cfg = LEDC_AUTO_CLK};
|
||||
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
|
||||
|
||||
// 2. LEDC channel configuration
|
||||
ledc_channel_config_t ledc_channel = {.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.channel = LEDC_CHANNEL_0,
|
||||
.timer_sel = LEDC_TIMER_0,
|
||||
.intr_type = LEDC_INTR_DISABLE,
|
||||
.gpio_num = led_pin,
|
||||
.duty = 0,
|
||||
.hpoint = 0};
|
||||
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
|
||||
|
||||
// 3. Main loop with flicker logic
|
||||
while (1)
|
||||
{
|
||||
// First, set the LED to its normal brightness
|
||||
ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, NORMAL_DUTY);
|
||||
ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
|
||||
|
||||
// Random trigger for flickering
|
||||
uint32_t random_val = esp_random() % 100; // Random number between 0 and 99
|
||||
|
||||
if (random_val < FLICKER_CHANCE)
|
||||
{
|
||||
// Start flicker sequence
|
||||
for (int i = 0; i < FLICKER_COUNT; i++)
|
||||
{
|
||||
// Set a random, lower brightness (e.g., 30-70% of normal brightness)
|
||||
uint32_t flicker_duty = (NORMAL_DUTY * 0.3) + (esp_random() % (uint32_t)(NORMAL_DUTY * 0.4));
|
||||
ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, flicker_duty);
|
||||
ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
|
||||
|
||||
// Short, random delay for irregular flickering
|
||||
vTaskDelay(pdMS_TO_TICKS(20 + (esp_random() % 50)));
|
||||
}
|
||||
}
|
||||
|
||||
// A fixed delay in normal operation
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t outdoor_start(void)
|
||||
{
|
||||
static const int led_pin = 13;
|
||||
xTaskCreate(outdoor_task, "outdoor_task", 2048, (void *)&led_pin, 5, &outdoor_task_handle);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t outdoor_stop(void)
|
||||
{
|
||||
if (outdoor_task_handle == NULL)
|
||||
{
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
vTaskDelete(outdoor_task_handle);
|
||||
outdoor_task_handle = NULL;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
@@ -15,26 +15,4 @@ menu "Warnemuende Lighthouse"
|
||||
int
|
||||
default 1 if WLED_USE_DMA
|
||||
default 0 if !WLED_USE_DMA
|
||||
|
||||
choice LIGHT_CHARACTERISTIC_CHOICE
|
||||
prompt "Light characteristic"
|
||||
default LIGHT_CHARACTERISTIC_GREEN
|
||||
help
|
||||
The color of the main WLED light.
|
||||
|
||||
config LIGHT_CHARACTERISTIC_GREEN
|
||||
bool "Green flashing light"
|
||||
help
|
||||
The light will be flashing green.
|
||||
|
||||
config LIGHT_CHARACTERISTIC_RED
|
||||
bool "Red flashing light"
|
||||
help
|
||||
The light will be flashing red.
|
||||
endchoice
|
||||
|
||||
config LIGHT_CHARACTERISTIC_VALUE
|
||||
int
|
||||
default 1 if LIGHT_CHARACTERISTIC_GREEN
|
||||
default 2 if LIGHT_CHARACTERISTIC_RED
|
||||
endmenu
|
||||
|
@@ -27,11 +27,6 @@ void app_main(void)
|
||||
}
|
||||
|
||||
/// start outdoor light service
|
||||
if (outdoor_init() != ESP_OK)
|
||||
{
|
||||
printf("Failed to initialize outdoor");
|
||||
return;
|
||||
}
|
||||
if (outdoor_start() != ESP_OK)
|
||||
{
|
||||
printf("Failed to start outdoor");
|
||||
|
@@ -5,6 +5,8 @@ CONFIG_BT_NIMBLE_ENABLED=y
|
||||
# NimBLE Options
|
||||
CONFIG_BT_NIMBLE_SECURITY_ENABLE=n
|
||||
CONFIG_BT_NIMBLE_SVC_GAP_DEVICE_NAME="lighthouse"
|
||||
CONFIG_BT_NIMBLE_NVS_PERSIST=y
|
||||
CONFIG_BT_NIMBLE_SMP_ID_RESET=y
|
||||
|
||||
# Flash Size
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
|
||||
|
Reference in New Issue
Block a user