Finished Firmware for Vacum

This commit is contained in:
AlexanderHD27
2025-01-02 03:55:45 +01:00
parent ad61e7e9b7
commit 0bb125fad9
39 changed files with 1701 additions and 2 deletions

View File

@@ -0,0 +1,100 @@
#include "vacum.hpp"
#include "pinConfig.hpp"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "pico/stdlib.h"
void vInterruptionTaskFn(void *pvParameters) {
VacumControl *vacumControl = (VacumControl *)pvParameters;
vacumControl->interruptionTaskFn();
}
void vButtonTaskFn(void *pvParameters) {
VacumControl *vacumControl = (VacumControl *)pvParameters;
vacumControl->buttonTaskFn();
}
VacumControl::VacumControl() {
initVacumPins();
state = false;
vacumMutex = xSemaphoreCreateMutex();
interruptionSemaphore = xSemaphoreCreateBinary();
xTaskCreate(vInterruptionTaskFn, "Interruption Task", 1024, this, 3, &interruptionTask);
xTaskCreate(vButtonTaskFn, "Button Task", 1024, this, 2, &buttonTask);
}
void VacumControl::initVacumPins() {
gpio_init(LED1_PIN);
gpio_set_dir(LED1_PIN, GPIO_OUT);
gpio_put(LED1_PIN, 0);
gpio_init(LED2_PIN);
gpio_set_dir(LED2_PIN, GPIO_OUT);
gpio_put(LED2_PIN, 0);
gpio_init(RELAY_PIN);
gpio_set_dir(RELAY_PIN, GPIO_OUT);
gpio_put(RELAY_PIN, 0);
gpio_init(BUTTON_PIN);
gpio_set_dir(BUTTON_PIN, GPIO_IN);
gpio_pull_up(BUTTON_PIN);
}
void VacumControl::setState(bool on) {
xSemaphoreTake(vacumMutex, portMAX_DELAY);
if(on) {
gpio_put(RELAY_PIN, 1);
gpio_put(LED1_PIN, 1);
state = true;
xSemaphoreGive(interruptionSemaphore);
} else {
gpio_put(RELAY_PIN, 0);
gpio_put(LED1_PIN, 0);
state = false;
}
xSemaphoreGive(vacumMutex);
}
void VacumControl::interruptionTaskFn() {
while(true) {
xSemaphoreTake(interruptionSemaphore, portMAX_DELAY);
if(state) {
while(state) {
vTaskDelay(INTERRUPTION_DELAY / portTICK_PERIOD_MS);
if(state) {
gpio_put(RELAY_PIN, 0);
gpio_put(LED1_PIN, 0);
} else
break;
vTaskDelay(INTERRUPTION_DURATION / portTICK_PERIOD_MS);
if(state) {
gpio_put(RELAY_PIN, 1);
gpio_put(LED1_PIN, 1);
}
}
}
}
}
void VacumControl::buttonTaskFn() {
bool buttonLastState = gpio_get(BUTTON_PIN);
while(true) {
bool buttonState = gpio_get(BUTTON_PIN);
if(!buttonState && buttonLastState) {
setState(true);
vTaskDelay(250 / portTICK_PERIOD_MS);
} else if(buttonState && !buttonLastState) {
setState(false);
}
buttonLastState = buttonState;
vTaskDelay(250 / portTICK_PERIOD_MS);
}
}

View File

@@ -0,0 +1,30 @@
#include <stdio.h>
#include "pico/stdlib.h"
#include "vacum.hpp"
#include "FreeRTOSConfig.h"
#include "FreeRTOS.h"
#include "task.h"
void vTaskFunction(void *pvParameters) {
while (true) {
printf("Button: %d\n", gpio_get(BUTTON_PIN));
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
int main() {
stdio_init_all();
printf("Hello, world!\n");
VacumControl vacumControl;
vacumControl.setState(false);
xTaskCreate(vTaskFunction, "Main Task", 1024, NULL, tskIDLE_PRIORITY, NULL);
vTaskStartScheduler();
while (true) {
tight_loop_contents();
}
}