diff --git a/CMakeLists.txt b/CMakeLists.txt index 6911567..d2d24a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +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.16) + # 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.16) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(Glove_Game) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) + project(Glove_Game) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index d1acf9e..aab7195 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,3 +1,3 @@ -idf_component_register(SRCS "hello_world_main.c" +idf_component_register(SRCS "metronome.c" "global.c" "hello_world_main.c" PRIV_REQUIRES spi_flash INCLUDE_DIRS "") diff --git a/main/global.c b/main/global.c new file mode 100644 index 0000000..8af429b --- /dev/null +++ b/main/global.c @@ -0,0 +1,12 @@ +#include "global.h" +int work_ready = 0; +int workers_done = 0; + +int current_input = 0; +int correct_input = 0; + +bool start = 0; +bool should_terminate = 0; +bool running = 0; + +Scoreboard scoreboard = {0, 0}; diff --git a/main/global.h b/main/global.h new file mode 100644 index 0000000..65b9ee7 --- /dev/null +++ b/main/global.h @@ -0,0 +1,22 @@ +// global.h +#ifndef GLOBAL_H +#define GLOBAL_H + +#include + +// Declare variables +extern bool start; +extern bool running; +extern bool should_terminate; + +extern int current_input; +extern int correct_input; + +typedef struct { + int hit; + int miss; +} Scoreboard; + +extern Scoreboard scoreboard; + +#endif // GLOBAL_H \ No newline at end of file diff --git a/main/hello_world_main.c b/main/hello_world_main.c index d96fe6a..b395786 100644 --- a/main/hello_world_main.c +++ b/main/hello_world_main.c @@ -12,34 +12,88 @@ #include "esp_chip_info.h" #include "esp_flash.h" #include "esp_system.h" -#include "input_handler.h" -#include "timer_task.h" +#include #include "esp_log.h" + +#include "metronome.h" //definitions #define MAIN_DELAY_MS 5000 //variables static const char *TAG_MAIN = "Main"; +pthread_t worker_tid; +pthread_t manager_tid; -void Sys_init(void){ - // Initialize hardware - configure_hardware(); //pressure sensore -} +void start_game(int bpm); void app_main(void) { - // Initialize hardware - configure_hardware(); + + start_game(1000000); + - // Create the pressure sensor task - xTaskCreate( - pressure_sensor_task, // Function that implements the task - "pressure_sensor_task", // Text name for the task - 2048, // Stack size in words - NULL, // Parameter passed into the task - 5, // Priority at which the task is created - NULL // Task handle - ); - - // The scheduler will start automatically + // printf("Hello world!\n"); + // ESP_LOGI(TAG_MAIN,"About to Setup the Task"); + // setup_the_timertask(); + // ESP_LOGI(TAG_MAIN,"task setup starting the main loop"); + // for(;;){ + // ESP_LOGI(TAG_MAIN,"Going to snooze for a bit"); + // vTaskDelay(MAIN_DELAY_MS / portTICK_PERIOD_MS); + + // } + // gpio_reset_pin(13); + // /* Set the GPIO as a push/pull output */ + // gpio_set_direction(13, GPIO_MODE_OUTPUT); + // blink_led(); + // xTaskCreate(echo_task, "uart_echo_task", ECHO_TASK_STACK_SIZE, NULL, 10, NULL); + // xTaskCreate(blink_task, "blink_LED", 1024, NULL, 5, &myTaskHandle); + // vTaskSuspend(myTaskHandle); + /* + Print chip information + esp_chip_info_t chip_info; + uint32_t flash_size; + esp_chip_info(&chip_info); + printf("This is %s chip with %d CPU core(s), %s%s%s%s, ", + CONFIG_IDF_TARGET, + chip_info.cores, + (chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "", + (chip_info.features & CHIP_FEATURE_BT) ? "BT" : "", + (chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "", + (chip_info.features & CHIP_FEATURE_IEEE802154) ? ", 802.15.4 (Zigbee/Thread)" : ""); + + unsigned major_rev = chip_info.revision / 100; + unsigned minor_rev = chip_info.revision % 100; + printf("silicon revision v%d.%d, ", major_rev, minor_rev); + if(esp_flash_get_size(NULL, &flash_size) != ESP_OK) { + printf("Get flash size failed"); + return; + } + + printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024), + (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external"); + + printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size()); + + for (int i = 10; i >= 0; i--) { + printf("Restarting in %d seconds...\n", i); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } + printf("Restarting now.\n"); + fflush(stdout); + esp_restart();*/ +} + + +void start_game(int bpm) { + MetronomeArgs m_args; + m_args.bpm = bpm; + m_args.metronome = create_metronome(); + + pthread_create(&worker_tid, NULL, metronome_worker, (void*)&m_args); + pthread_create(&manager_tid, NULL, manager_thread, NULL); + + pthread_join(manager_tid, NULL); + pthread_join(worker_tid, NULL); + + ESP_LOGI("App", "All threads finished."); } diff --git a/main/metronome.c b/main/metronome.c new file mode 100644 index 0000000..92fdfa8 --- /dev/null +++ b/main/metronome.c @@ -0,0 +1,103 @@ +#include "metronome.h" +#include "global.h" +#include "esp_log.h" +#include "esp_sleep.h" +#include "sdkconfig.h" + + +static const char* TAG = "Metronome"; + +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +pthread_cond_t cond = PTHREAD_COND_INITIALIZER; + +// Private function +static void metronome_callback(void* arg); + +esp_timer_handle_t create_metronome(void) { + const esp_timer_create_args_t periodic_timer_args = { + .callback = &metronome_callback, + .name = "metronome" + }; + + esp_timer_handle_t metronome; + ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &metronome)); + return metronome; +} + +void* metronome_worker(void* args) { + MetronomeArgs* m_args = (MetronomeArgs*)args; + + // Wait for manager to signal to start + pthread_mutex_lock(&mutex); + while (!start && !should_terminate) { + pthread_cond_wait(&cond, &mutex); + } + + if (start) { + ESP_ERROR_CHECK(esp_timer_start_periodic(m_args->metronome, m_args->bpm)); + } + pthread_mutex_unlock(&mutex); + + // handles the metronone callbacks + while (1) { + pthread_mutex_lock(&mutex); + + if (should_terminate) { + pthread_mutex_unlock(&mutex); + break; + } + + pthread_mutex_unlock(&mutex); + + vTaskDelay(pdMS_TO_TICKS(10)); + } + + ESP_ERROR_CHECK(esp_timer_stop(m_args->metronome)); + ESP_ERROR_CHECK(esp_timer_delete(m_args->metronome)); + ESP_LOGI(TAG, "Worker exited cleanly"); + + return NULL; +} + +//callback function, put espnow send true/false at end +static void metronome_callback(void* arg) { + if (correct_input == 0){ + //hahhshshdshdshdvsd + + } + else if (current_input == correct_input) { + scoreboard.hit++; + } else { + scoreboard.miss++; + // Vibrate code here + } + + + ESP_LOGI(TAG, "Status of input: %d", current_input == correct_input); //prints to serial 1 if match 0 if not +} + +void* manager_thread(void* arg) { + ESP_LOGI(TAG, "Manager thread started"); + + vTaskDelay(pdMS_TO_TICKS(1000)); // 1 second + + pthread_mutex_lock(&mutex); + start = true; + pthread_cond_signal(&cond); + pthread_mutex_unlock(&mutex); + + ESP_LOGI(TAG, "Sent start signal to metronome"); + + vTaskDelay(pdMS_TO_TICKS(5000)); // 5 seconds + + pthread_mutex_lock(&mutex); + should_terminate = true; + pthread_cond_signal(&cond); + pthread_mutex_unlock(&mutex); + + ESP_LOGI(TAG, "Sent terminate signal to metronome"); + + return NULL; +} + + diff --git a/main/metronome.h b/main/metronome.h new file mode 100644 index 0000000..21b420b --- /dev/null +++ b/main/metronome.h @@ -0,0 +1,28 @@ +#ifndef METRONOME_H +#define METRONOME_H + +#include +#include "esp_timer.h" +#include +#include // for sleep() +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include + +// External variables (mutex/cond) +extern pthread_mutex_t mutex; +extern pthread_cond_t cond; + +// Struct to pass worker arguments +typedef struct { + uint64_t bpm; // timer period in microseconds + esp_timer_handle_t metronome; // timer handle +} MetronomeArgs; + + +// Functions you can call +esp_timer_handle_t create_metronome(void); +void* metronome_worker(void* bpm_ptr); +void* manager_thread(void* arg); + +#endif // METRONOME_H diff --git a/main/timer_task.h b/main/timer_task.h deleted file mode 100644 index 73d2308..0000000 --- a/main/timer_task.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" - -#include "esp_log.h" - -#define TASK_STACK_SIZE 2048 -#define TASK_DELAY_MS 500 -//declarations -//public - -void metronome(int *input_delay); - -//private -static const char *TAG_TASK = "Task"; - - -void metronome(int *input_delay){ - for (;;) - { - //metronome task - vTaskDelay(*input_delay/portTICK_PERIOD_MS); - } - -} \ No newline at end of file