Init Code

This commit is contained in:
TheJoKlLa 2023-12-29 17:24:30 +01:00
parent ada27d1214
commit 06a800c6f6
11 changed files with 607 additions and 91 deletions

6
CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(FabLight)

View File

@ -1,93 +1,3 @@
# FabLight2 # FabLight2
New FabLight project for version 2.
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin https://gitlab.com/fabinfra/fabhardware/fablight2.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools
- [ ] [Set up project integrations](https://gitlab.com/fabinfra/fabhardware/fablight2/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.

5
main/CMakeLists.txt Normal file
View File

@ -0,0 +1,5 @@
idf_component_register( SRCS
"LEDController_Task.c"
"MQTTClient_Task.c"
"main.c"
INCLUDE_DIRS ".")

32
main/Kconfig.projbuild Normal file
View File

@ -0,0 +1,32 @@
menu "FabLight Configuration"
config FABLIGHT_WIFI_SSID
string "WiFi SSID"
default "SSID"
help
SSID (network name) to connect to.
config FABLIGHT_WIFI_PASSWORD
string "WiFi Password"
default "password"
help
WiFi password (WPA or WPA2) to use.
config FABLIGHT_MQTT_HOST
string "MQTT Host"
default "mqtt://example.com"
help
MQTT Host (mqtt://example.com) to use.
config FABLIGHT_MQTT_USER
string "MQTT Username"
default "username"
help
MQTT Username to use.
config FABLIGHT_MQTT_PASSWORD
string "MQTT Password"
default "password"
help
MQTT Password to use.
endmenu

121
main/LEDController_Task.c Normal file
View File

@ -0,0 +1,121 @@
#include "LEDController_Task.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_log.h"
#include "led_strip.h"
static const char* TAG_LEDCONTROLLER = "LEDCONTROLLER";
#define LED_STRIP_RMT_RES_HZ (10 * 1000 * 1000)
void LEDController_Init(LEDController_Config* config, LEDController_Data* data)
{
ESP_LOGI(TAG_LEDCONTROLLER, "Init");
data->LED_Handle_Count = config->Segment_Count;
data->LED_Handles = malloc(sizeof(led_strip_handle_t) * data->LED_Handle_Count);
led_strip_rmt_config_t rmt_config =
{
.clk_src = RMT_CLK_SRC_DEFAULT,
.resolution_hz = LED_STRIP_RMT_RES_HZ,
.flags.with_dma = false,
};
for(int i=0; i < config->Segment_Count; i++)
{
led_strip_config_t led_strip_config =
{
.strip_gpio_num = config->Segment_Pins[i],
.max_leds = config->Ring_Count * config->LED_Count,
.led_pixel_format = LED_PIXEL_FORMAT_GRB,
.led_model = LED_MODEL_WS2812,
.flags.invert_out = false,
};
ESP_ERROR_CHECK(led_strip_new_rmt_device(&led_strip_config, &rmt_config, &data->LED_Handles[i]));
}
}
void LEDController_Task(void *pvParameter)
{
LEDController_Config* config = pvParameter;
LEDController_Data data;
LEDController_Init(config, &data);
ESP_LOGI(TAG_LEDCONTROLLER, "Run");
while(1)
{
LEDController_Command command;
if(xQueueReceive(*(config->Effect_Queue), &command, 1000/portTICK_PERIOD_MS))
{
ESP_LOGD(TAG_LEDCONTROLLER, "Update");
if(command.Segment >= config->Segment_Count || command.Segment < -1)
{
ESP_LOGW(TAG_LEDCONTROLLER, "SegmentID %d is invalid", command.Segment);
continue;
}
if(command.Ring >= config->Ring_Count || command.Ring < -1)
{
ESP_LOGW(TAG_LEDCONTROLLER, "RingID %d is invalid", command.Ring);
continue;
}
// TODO
if(command.Effect > 0 || command.Effect < 0)
{
ESP_LOGW(TAG_LEDCONTROLLER, "EffectID %d is invalid", command.Effect);
continue;
}
if(command.Effect == (enum LEDController_Effect)STATIC)
{
if(command.Segment == -1)
{
for(int i=0; i < data.LED_Handle_Count; i++)
{
if(command.Ring == -1)
{
for(int e=0; e < config->Ring_Count * config->LED_Count; e++)
{
ESP_ERROR_CHECK(led_strip_set_pixel(data.LED_Handles[i], e, command.Red, command.Green, command.Blue));
}
}
else
{
for(int e=command.Ring * config->LED_Count; e < (command.Ring * config->LED_Count) + config->LED_Count; e++)
{
ESP_ERROR_CHECK(led_strip_set_pixel(data.LED_Handles[i], e, command.Red, command.Green, command.Blue));
}
}
ESP_ERROR_CHECK(led_strip_refresh(data.LED_Handles[i]));
}
}
else
{
if(command.Ring == -1)
{
for(int e=0; e < config->Ring_Count * config->LED_Count; e++)
{
ESP_ERROR_CHECK(led_strip_set_pixel(data.LED_Handles[command.Segment], e, command.Red, command.Green, command.Blue));
}
}
else
{
for(int e=command.Ring * config->LED_Count; e < (command.Ring * config->LED_Count) + config->LED_Count; e++)
{
ESP_ERROR_CHECK(led_strip_set_pixel(data.LED_Handles[command.Segment], e, command.Red, command.Green, command.Blue));
}
}
ESP_ERROR_CHECK(led_strip_refresh(data.LED_Handles[command.Segment]));
}
}
}
}
}

58
main/LEDController_Task.h Normal file
View File

@ -0,0 +1,58 @@
#ifndef LED_CONTROLLER_TASK1
#define LED_CONTROLLER_TASK1
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "led_strip.h"
enum LEDController_Effect
{
STATIC = 0,
};
typedef struct LEDController_Config
{
// Number of Rings
int Ring_Count;
// Number of LEDs per Ring
int LED_Count;
// Number of Stripes
int Segment_Count;
// Pins of Stripes
int* Segment_Pins;
// Effect Queue
QueueHandle_t* Effect_Queue;
} LEDController_Config;
typedef struct LEDController_Data
{
// Number of Stripes
int LED_Handle_Count;
// LED Stripe Handle
led_strip_handle_t* LED_Handles;
} LEDController_Data;
typedef struct LEDController_Command
{
// Effect
enum LEDController_Effect Effect;
// Segment, -1 are ALL
int Segment;
// Ring, -1 are ALL
int Ring;
// Colors
uint8_t Red;
uint8_t Green;
uint8_t Blue;
} LEDController_Command;
void LEDController_Init(LEDController_Config* config, LEDController_Data* data);
void LEDController_Task(void *pvParameter);
#endif

222
main/MQTTClient_Task.c Normal file
View File

@ -0,0 +1,222 @@
#include "MQTTClient_Task.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/event_groups.h"
#include <string.h>
#include "esp_log.h"
#include "esp_wifi.h"
#include "mqtt_client.h"
#include "esp_crt_bundle.h"
#include "LEDController_Task.h"
static const char* TAG_MQTTClient = "MQTTClient";
static void MQTTClient_EventHandlerWifi(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)
{
ESP_LOGI(TAG_MQTTClient, "WIFI connect ...");
esp_wifi_connect();
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
ESP_LOGI(TAG_MQTTClient, "WIFI disconnected");
esp_wifi_connect();
}
else if(event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED)
{
ESP_LOGI(TAG_MQTTClient, "WIFI connected");
}
}
static void MQTTClient_EventHandlerMQTT(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
MQTTClient_Data* data = handler_args;
esp_mqtt_event_handle_t event = event_data;
esp_mqtt_client_handle_t client = event->client;
switch ((esp_mqtt_event_id_t)event_id)
{
case MQTT_EVENT_CONNECTED:
esp_mqtt_client_publish(client, "fablight", data->ID, 0, 0, 0);
ESP_LOGI(TAG_MQTTClient, "MQTT_EVENT_CONNECTED");
esp_mqtt_client_subscribe(client, data->Topic, 1);
break;
case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(TAG_MQTTClient, "MQTT_EVENT_DISCONNECTED");
break;
case MQTT_EVENT_DATA:
ESP_LOGI(TAG_MQTTClient, "MQTT_EVENT_DATA");
ESP_LOGD(TAG_MQTTClient, "TOPIC=%.*s\r\n", event->topic_len, event->topic);
ESP_LOGD(TAG_MQTTClient, "DATA=%.*s\r\n", event->data_len, event->data);
char* topic = malloc(sizeof(char) * event->topic_len + 1);
char* payload = malloc(sizeof(char) * event->data_len + 1);
topic[event->topic_len] = 0;
topic[event->data_len] = 0;
strncpy(topic, event->topic, event->topic_len);
strncpy(payload, event->data, event->data_len);
char* fablight = strtok(topic, "/");
if(fablight == NULL)
{
break;
}
char* id = strtok(NULL, "/");
if(id == NULL)
{
break;
}
char* effect = strtok(NULL, "/");
if(effect == NULL)
{
break;
}
char* segment = strtok(NULL, "/");
if(segment == NULL)
{
break;
}
char* ring = strtok(NULL, "/");
if(ring == NULL)
{
break;
}
LEDController_Command command =
{
.Effect = -1,
.Segment = -1,
.Ring = -1,
.Red = 0,
.Green = 0,
.Blue = 0
};
if(!strcmp(effect, "static"))
{
command.Effect = (enum LEDController_Effect)STATIC;
}
else
{
break;
}
command.Segment = atoi(segment);
if(command.Segment < -1 && command.Segment > 99999)
{
break;
}
command.Ring = atoi(ring);
if(command.Ring < -1 && command.Ring > 99999)
{
break;
}
if(event->data_len == strlen("FFFFFF"))
{
char r[3] = {0};
char g[3] = {0};
char b[3] = {0};
strncpy(r, &(payload[0]), 2);
strncpy(g, &(payload[2]), 2);
strncpy(b, &(payload[4]), 2);
command.Red = strtol(r, NULL, 16);
command.Green = strtol(g, NULL, 16);
command.Blue = strtol(b, NULL, 16);
xQueueSend(*(data->Effect_Queue), (void*) &command, (TickType_t)0);
}
break;
default:
ESP_LOGD(TAG_MQTTClient, "Unhandled Event");
}
}
void MQTTClient_Init(MQTTClient_Config* config, MQTTClient_Data* data)
{
ESP_LOGI(TAG_MQTTClient, "Delay for Programming");
vTaskDelay(10000 / portTICK_PERIOD_MS);
data->Effect_Queue = config->Effect_Queue;
data->ID = config->ID;
sprintf(data->Topic, "fablight/%s/#", config->ID);
ESP_LOGI(TAG_MQTTClient, "Init");
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 event_wifi;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&MQTTClient_EventHandlerWifi,
NULL,
&event_wifi));
wifi_config_t wifi_config =
{
.sta =
{
//.threshold.authmode = WIFI_AUTH_WPA2_PSK
.threshold.authmode = WIFI_AUTH_OPEN
},
};
strcpy((char*)wifi_config.sta.ssid, config->SSID);
strcpy((char*)wifi_config.sta.password, config->PSK);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGW(TAG_MQTTClient, "%s", config->Host);
esp_mqtt_client_config_t mqtt_cfg =
{
.broker.address.uri = config->Host,
.credentials.username = config->Username,
.credentials.authentication.password = config->Password,
.broker.verification.crt_bundle_attach = esp_crt_bundle_attach
};
data->client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event(data->client, ESP_EVENT_ANY_ID, MQTTClient_EventHandlerMQTT, data);
esp_mqtt_client_start(data->client);
}
void MQTTClient_Task(void *pvParameter)
{
MQTTClient_Config* config = pvParameter;
char topic[] = "fablight/00:00:00:00:00:00/#";
MQTTClient_Data data;
data.Topic = topic;
MQTTClient_Init(config, &data);
ESP_LOGI(TAG_MQTTClient, "Run");
char topic_msg[] = "fablight/00:00:00:00:00:00";
sprintf(topic_msg, "fablight/%s", data.ID);
while(1)
{
vTaskDelay(1000 / portTICK_PERIOD_MS);
ESP_LOGD(TAG_MQTTClient, "Update");
}
}

37
main/MQTTClient_Task.h Normal file
View File

@ -0,0 +1,37 @@
#ifndef MQTT_CLIENT_TASK
#define MQTT_CLIENT_TASK
#include "led_strip.h"
#include "mqtt_client.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
typedef struct MQTTClient_Config
{
char* SSID;
char* PSK;
char* Host;
char* Username;
char* Password;
char* ID;
// Effect Queue
QueueHandle_t* Effect_Queue;
} MQTTClient_Config;
typedef struct MQTTClient_Data
{
esp_mqtt_client_handle_t client;
char* ID;
char* Topic;
// Effect Queue
QueueHandle_t* Effect_Queue;
} MQTTClient_Data;
void MQTTClient_Init(MQTTClient_Config* config, MQTTClient_Data* data);
void MQTTClient_Task(void *pvParameter);
#endif

4
main/idf_component.yml Normal file
View File

@ -0,0 +1,4 @@
## IDF Component Manager Manifest File
dependencies:
espressif/led_strip:
version: '^2'

118
main/main.c Normal file
View File

@ -0,0 +1,118 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include <string.h>
#include "esp_log.h"
#include "esp_netif.h"
#include "nvs_flash.h"
#include "esp_mac.h"
#include "LEDController_Task.h"
#include "MQTTClient_Task.h"
static const char *TAG_MAIN = "MAIN";
///////////////////////////////////////////////////////////////
// WARNING
// NEED FIXS:
// https://github.com/espressif/esp-idf/pull/12675
///////////////////////////////////////////////////////////////
void app_main(void)
{
ESP_LOGI(TAG_MAIN, "Init");
// INIT for WLAN
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
int Segment_GPIOs[3] = {10, 12, 14};
QueueHandle_t ledcontroller_queue = xQueueCreate(9+1, sizeof(LEDController_Command));
LEDController_Config ledcontroller_config =
{
.Ring_Count = 10,
.LED_Count = 2,
.Segment_Count = sizeof(Segment_GPIOs) / sizeof(int),
.Segment_Pins = Segment_GPIOs,
.Effect_Queue = &ledcontroller_queue
};
xTaskCreate(&LEDController_Task, "LEDController_Task", 4096, (void*) &ledcontroller_config, 5, NULL);
unsigned char mac_base[6] = {0};
esp_efuse_mac_get_default(mac_base);
esp_read_mac(mac_base, ESP_MAC_WIFI_STA);
char mac[] = "00:00:00:00:00:00";
sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", mac_base[0],mac_base[1],mac_base[2],mac_base[3],mac_base[4],mac_base[5]);
MQTTClient_Config mqttclient_config =
{
.SSID = CONFIG_FABLIGHT_WIFI_SSID,
.PSK = CONFIG_FABLIGHT_WIFI_PASSWORD,
.Host = CONFIG_FABLIGHT_MQTT_HOST,
.Username = CONFIG_FABLIGHT_MQTT_USER,
.Password = CONFIG_FABLIGHT_MQTT_PASSWORD,
.ID = mac,
.Effect_Queue = &ledcontroller_queue
};
xTaskCreate(&MQTTClient_Task, "MQTTClient_Task", 4096, (void*) &mqttclient_config, 5, NULL);
ESP_LOGI(TAG_MAIN, "Run");
LEDController_Command command1 =
{
.Effect = (enum LEDController_Effect)STATIC,
.Segment = -1,
.Ring = -1,
.Red = 255,
.Green = 200,
.Blue = 0
};
LEDController_Command command2 =
{
.Effect = (enum LEDController_Effect)STATIC,
.Segment = -1,
.Ring = -1,
.Red = 0,
.Green = 0,
.Blue = 0
};
int active = 0;
while(42)
{
// for(int i=0; i < ledcontroller_config.Segment_Count; i++)
// {
// if(i == active)
// {
// command1.Segment = i;
// xQueueSend(ledcontroller_queue, (void*) &command1, (TickType_t)0);
// }
// else
// {
// command2.Segment = i;
// xQueueSend(ledcontroller_queue, (void*) &command2, (TickType_t)0);
// }
// }
// vTaskDelay(256 / portTICK_PERIOD_MS);
// active++;
// if(active >= ledcontroller_config.Segment_Count)
// {
// active = 0;
// }
vTaskDelay(10000 / portTICK_PERIOD_MS);
ESP_LOGD(TAG_MAIN, "Update");
// //xQueueSend(ledcontroller_queue, (void*) &command1, (TickType_t)0);
// vTaskDelay(1000 / portTICK_PERIOD_MS);
// ESP_LOGD(TAG_MAIN, "Update");
// //xQueueSend(ledcontroller_queue, (void*) &command2, (TickType_t)0);
}
}

3
sdkconfig.defaults Normal file
View File

@ -0,0 +1,3 @@
CONFIG_ESP_PHY_ENABLE_USB=y
CONFIG_ESP_CONSOLE_USB_CDC=y
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y