Skip to content

Instantly share code, notes, and snippets.

@leyyce
Last active November 20, 2023 21:09
Show Gist options
  • Save leyyce/e54a659915e0b5d2eed11bc90ffbc140 to your computer and use it in GitHub Desktop.
Save leyyce/e54a659915e0b5d2eed11bc90ffbc140 to your computer and use it in GitHub Desktop.
IoT_Uebung2
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include <limits.h>
#define STACK_SIZE 1024 * 2
#define GPIO_LED_ONBOARD 5
#define GPIO_LED_26 26
#define GPIO_LED_4 4
SemaphoreHandle_t mutex;
QueueHandle_t queue;
typedef struct data
{
gpio_num_t gpio;
int freq;
} Data;
void print_prompt() {
printf("> ");
fflush(stdout);
}
void blink_x(gpio_num_t gpio, int freq) {
int period_ms = 1. / freq * 1000;
int delay_ticks = pdMS_TO_TICKS(period_ms);
gpio_set_level(gpio, 0);
vTaskDelay(delay_ticks);
gpio_set_level(gpio, 1);
vTaskDelay(delay_ticks);
}
void blink(void *pvParams) {
Data *data = (Data*) pvParams;
gpio_num_t owned_gpio = data->gpio;
int freq = data->freq;
gpio_set_direction(owned_gpio, GPIO_MODE_OUTPUT);
while (1) {
if (xSemaphoreTake(mutex, 0) == pdTRUE) {
// Check if new data is available and addressed to the current task...
if (xQueuePeek (queue, data, 0) == pdTRUE) {
// ... and if so take it out of the queque and set new values
if (data->gpio == owned_gpio) {
xQueueReceive(queue, data, 0);
freq = data->freq;
}
}
xSemaphoreGive(mutex);
}
blink_x(owned_gpio, freq);
}
}
void get_data(void *pvParams) {
char buffer[12];
int buffer_index = 0;
Data data;
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
uart_driver_install(UART_NUM_0, 256, 0, 0, NULL, 0);
uart_param_config(UART_NUM_0, &uart_config);
printf("\n\nEnter pin number [1 -> Onboard, 2 -> GPIO26, 3 -> GPIO4] and new blink frequency (between 1 and 999 999 999) in the format '{pin_num}:{freq}' and hit enter!\n\n");
print_prompt();
while (1) {
uint8_t byte;
int len = uart_read_bytes(UART_NUM_0, &byte, 1, 0); // Non-blocking read
if (len > 0) {
if ( ((byte != '\b') && (buffer_index < sizeof(buffer) - 1)) || byte == '\n' ) { // Echo recived char back to the terminal so the user can see what he entered
printf("%c", byte);
fflush(stdout);
}
if (byte == '\b' && buffer_index > 0) { // Handle backspace to erase a character from console
printf("\b \b");
fflush(stdout);
buffer_index--;
} else if (byte == '\n') { // End of input line, parse the buffered data
buffer[buffer_index] = 0;
int gpio, freq;
if (sscanf(buffer, "%d:%d", &gpio, &freq) == 2) {
data.gpio = (gpio == 1 ? GPIO_LED_ONBOARD : (gpio == 2 ? GPIO_LED_26 : GPIO_LED_4));
if (freq > 0) data.freq = freq;
else {
data.freq = 1;
puts("Frequency can't be smaller than 1!");
}
printf("Setting GPIO%d to a frequency of %d Hz.\n", data.gpio, data.freq);
xQueueSend(queue, &data, 0);
} else {
puts("Wrong format!");
}
// Reset the buffer index
buffer_index = 0;
print_prompt();
} else if (byte != '\b') { // Collect input character by character
if (buffer_index < sizeof(buffer) - 1) {
buffer[buffer_index++] = byte;
}
}
}
vTaskDelay(1); // Add a small delay to prevent busy-waiting
}
}
void app_main() {
mutex = xSemaphoreCreateMutex();
queue = xQueueCreate(10, sizeof(Data));
Data *onboard_data = malloc(sizeof(Data));
onboard_data->gpio = GPIO_LED_ONBOARD;
onboard_data->freq = 1;
Data *gpio_26_data = malloc(sizeof(Data));
gpio_26_data->gpio = GPIO_LED_26;
gpio_26_data->freq = 2;
Data *gpio_4_data = malloc(sizeof(Data));
gpio_4_data->gpio = GPIO_LED_4;
gpio_4_data->freq = 3;
TaskHandle_t blink_onboard_task = NULL;
TaskHandle_t blink_gpio_26_task = NULL;
TaskHandle_t blink_gpio_4_task = NULL;
TaskHandle_t get_data_task = NULL;
xTaskCreate(blink, "Onboard Blink", STACK_SIZE, onboard_data , tskIDLE_PRIORITY, &blink_onboard_task);
xTaskCreate(blink, "GPIO 26 Blink", STACK_SIZE, gpio_26_data , tskIDLE_PRIORITY, &blink_gpio_26_task);
xTaskCreate(blink, "GPIO 4 Blink", STACK_SIZE, gpio_4_data, tskIDLE_PRIORITY, &blink_gpio_4_task);
xTaskCreate(get_data, "Get Data", STACK_SIZE, NULL, tskIDLE_PRIORITY, &get_data_task);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment