/* Multitasking Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "ScopeGuard.hpp"
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "SDCardSDIO.h"
#include "ffsutils.h"
#include <sys/stat.h>
#include "esp_log.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "SDCard.h"
extern "C"
{
void app_main(void);
}
class I2S_DataBuffer {
signed short *buffers[2];
unsigned char buf_select;
unsigned char buf_ready;
unsigned int buf_count;
unsigned int n_samples;
unsigned int subsampling_rate;
/**
* @brief Ideally this bool would be encapsulated in the EdgeImpulse class
* but issues with conflicting paths
*/
bool status_running;
void notifyConsumerTask() {
if (status_running && mTaskHandle != NULL) {
buf_ready = 1;
xTaskNotify(mTaskHandle, (0), eNoAction);
}
}
I2S_DataBuffer(TaskHandle_t& taskHandle) : mTaskHandle(taskHandle)
{}
private:
TaskHandle_t& mTaskHandle;
};
static const char* TAG = "main";
#define USE_SPI_VERSION
bool gMountedSDCard = false;
#ifndef USE_SPI_VERSION
SDCardSDIO *theSDCardObject;
#endif
#ifdef USE_SPI_VERSION
SDCard *theSDCardObject;
#define PIN_NUM_MISO GPIO_NUM_19
#define PIN_NUM_CLK GPIO_NUM_18
#define PIN_NUM_MOSI GPIO_NUM_23
#define PIN_NUM_CS GPIO_NUM_5
#endif
#define LED_RED GPIO_NUM_15
#define LED_GREEN GPIO_NUM_2
#define LED_BLUE GPIO_NUM_4
struct led_task_parameters_t
{
gpio_num_t led_gpio;
TickType_t blink_time;
};
static led_task_parameters_t red_led_gpio = {LED_RED, 2000};
static led_task_parameters_t blue_led_gpio = {LED_BLUE, 1000};
static led_task_parameters_t green_led_gpio = {LED_GREEN, 500};
TaskHandle_t task_green = nullptr;
TaskHandle_t task_red = nullptr;
TaskHandle_t task_blue = nullptr;
void led_task(void *pvParameter)
{
gpio_num_t led_gpio = ((led_task_parameters_t *)pvParameter)->led_gpio;
TickType_t blink_time = ((led_task_parameters_t *)pvParameter)->blink_time;
uint8_t led_value = 0;
gpio_reset_pin(led_gpio);
gpio_set_direction(led_gpio, GPIO_MODE_OUTPUT);
while (1) {
gpio_set_level(led_gpio, led_value);
led_value = !led_value;
vTaskDelay(blink_time / portTICK_PERIOD_MS);
}
vTaskDelete( NULL );
}
void triggered_task(void *pvParameter) {
gpio_num_t led_gpio = ((led_task_parameters_t *)pvParameter)->led_gpio;
TickType_t blink_time = ((led_task_parameters_t *)pvParameter)->blink_time;
uint8_t led_value = 0;
gpio_reset_pin(led_gpio);
gpio_set_direction(led_gpio, GPIO_MODE_OUTPUT);
uint32_t ulNotifiedValue;
while (1) {
printf("xTaskNotifyWait\n");
/* Block indefinitely (without a timeout, so no need to check the function's
return value) to wait for a notification.
Bits in this RTOS task's notification value are set by the notifying
tasks and interrupts to indicate which events have occurred. */
xTaskNotifyWait(
0x00, /* Don't clear any notification bits on entry. */
ULONG_MAX, /* Reset the notification value to 0 on exit. */
&ulNotifiedValue, /* Notified value pass out in
ulNotifiedValue. */
portMAX_DELAY ); /* Block indefinitely. */
printf("ACK\n");
gpio_set_level(led_gpio, led_value);
led_value = !led_value;
vTaskDelay(3000 / portTICK_PERIOD_MS);
}
vTaskDelete( NULL );
}
void hello_task(void *pvParameter)
{
while (1) {
printf("Hello world!\n");
for (int i = 10; i >= 0; i--) {
printf("Restarting in %d seconds...\n", i);
if ((i%2) == 0) {
printf("SIG\n");
xTaskNotify(task_red, 0, eNoAction );
}
vTaskDelay(1000 / portTICK_RATE_MS);
}
printf("Restarting now.\n");
fflush(stdout);
//esp_restart();
}
}
void app_main()
{
esp_log_level_set("*", ESP_LOG_INFO);
ESP_LOGI(TAG, "TRYING to mount SDCArd, SDIO ");
ESP_LOGI(TAG, "TRYING to mount SDCArd, SDIO ");
#ifndef USE_SPI_VERSION
theSDCardObject = new SDCardSDIO("/sdcard");
#endif
#ifdef USE_SPI_VERSION
ESP_LOGI(TAG, "TRYING to mount SDCArd, SPI ");
theSDCardObject = new SDCard("/sdcard",PIN_NUM_MISO, PIN_NUM_MOSI, PIN_NUM_CLK, PIN_NUM_CS);
gMountedSDCard = true;
#endif
if (gMountedSDCard) {
ESP_LOGI(TAG, "SD card mounted ");
const char* ELOC_FOLDER = "/sdcard/eloc";
if (!ffsutil::folderExists(ELOC_FOLDER)) {
ESP_LOGI(TAG, "%s does not exist, creating empty folder", ELOC_FOLDER);
mkdir(ELOC_FOLDER, 0777);
}
}
xTaskCreate(
&triggered_task, // task function
"red_led_task", // task name
2048, // stack size in words
&red_led_gpio, // pointer to parameters
5, // priority
&task_red); // out pointer to task handle
#if 1
xTaskCreate(
&led_task, // task function
"blue_led_task", // task name
2048, // stack size in words
&blue_led_gpio, // pointer to parameters
5, // priority
&task_blue); // out pointer to task handle
#endif
#if 1
xTaskCreate(
&led_task, // task function
"green_led_task", // task name
2048, // stack size in words
&green_led_gpio, // pointer to parameters
5, // priority
&task_green); // out pointer to task handle
#endif
xTaskCreate(&hello_task, "hello_task", 2048, NULL, 5, NULL);
}