Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 "")
12 changes: 12 additions & 0 deletions main/global.c
Original file line number Diff line number Diff line change
@@ -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};
22 changes: 22 additions & 0 deletions main/global.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// global.h
#ifndef GLOBAL_H
#define GLOBAL_H

#include <stdbool.h>

// 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
92 changes: 73 additions & 19 deletions main/hello_world_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pthread.h>
#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.");
}
103 changes: 103 additions & 0 deletions main/metronome.c
Original file line number Diff line number Diff line change
@@ -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;
}


28 changes: 28 additions & 0 deletions main/metronome.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef METRONOME_H
#define METRONOME_H

#include <stdint.h>
#include "esp_timer.h"
#include <pthread.h>
#include <unistd.h> // for sleep()
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <stdbool.h>

// 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
26 changes: 0 additions & 26 deletions main/timer_task.h

This file was deleted.