From 1a3acb79e114bc1272ecbf389eb9cd58b952b6f8 Mon Sep 17 00:00:00 2001 From: angrycompany16 Date: Mon, 19 Feb 2024 15:53:06 +0100 Subject: [PATCH 1/3] started working on input detection --- skeleton_project/.vscode/settings.json | 4 ++- skeleton_project/source/button.c | 24 +++++++++++++++++ skeleton_project/source/button.h | 15 +++++++++++ skeleton_project/source/main.c | 29 +++++++++----------- skeleton_project/source/run.c | 36 +++++++++++++++++++++++++ skeleton_project/source/run.h | 10 +++++++ skeleton_project/source/state_machine.c | 2 +- 7 files changed, 102 insertions(+), 18 deletions(-) create mode 100644 skeleton_project/source/button.c create mode 100644 skeleton_project/source/button.h create mode 100644 skeleton_project/source/run.c create mode 100644 skeleton_project/source/run.h diff --git a/skeleton_project/.vscode/settings.json b/skeleton_project/.vscode/settings.json index a585ed3..4ac892f 100644 --- a/skeleton_project/.vscode/settings.json +++ b/skeleton_project/.vscode/settings.json @@ -1,5 +1,7 @@ { "files.associations": { - "elevio.h": "c" + "elevio.h": "c", + "run.h": "c", + "time.h": "c" } } \ No newline at end of file diff --git a/skeleton_project/source/button.c b/skeleton_project/source/button.c new file mode 100644 index 0000000..fa8c8a6 --- /dev/null +++ b/skeleton_project/source/button.c @@ -0,0 +1,24 @@ +#include "button.h" + +Button* init_button() { + return &(Button) {false, false, false}; +} + +void button_update(Button* button, bool value) { + button->is_pressed = value; + + if (button->is_pressed && !button->was_pressed) { + button->was_just_pressed = true; + } else if (!button->is_pressed && button->was_pressed) { + button->was_just_released = true; + } else { + button->was_just_pressed = false; + button->was_just_released = false; + } + + button->was_pressed = button->is_pressed; +} + + + + diff --git a/skeleton_project/source/button.h b/skeleton_project/source/button.h new file mode 100644 index 0000000..aff90a7 --- /dev/null +++ b/skeleton_project/source/button.h @@ -0,0 +1,15 @@ +#pragma once +#include +#include +#include + +typedef struct Button { + bool is_pressed; + bool was_pressed; + bool was_just_pressed; + bool was_just_released; +} Button; + +Button* init_button(); + +void button_update(Button* button, bool value); \ No newline at end of file diff --git a/skeleton_project/source/main.c b/skeleton_project/source/main.c index a41a6bf..7e6de1e 100644 --- a/skeleton_project/source/main.c +++ b/skeleton_project/source/main.c @@ -6,31 +6,28 @@ #include "driver/elevio.h" #include "state_machine.h" #include "queue.h" +#include "run.h" +#include "button.h" - -int main(){ - - /* +int main() { elevio_init(); Queue* p_main_queue = queue_init(); + int target_floor = 0; - queue_add(p_main_queue, (Request){2, true, true}); - queue_add(p_main_queue, (Request){1, false, false}); - queue_add(p_main_queue, (Request){1, true, false}); - queue_add(p_main_queue, (Request){4, true, false}); - queue_add(p_main_queue, (Request){4, true, false}); - - printf(queue_has_off_requests(p_main_queue) ? "has off requests\n" : "has no off requests\n"); - printf(queue_query(p_main_queue, true, false) ? "has query matches\n" : "has no query matches\n"); + Button* p_button_2_up = init_button(); - sleep(1); - - queue_print(p_main_queue); + while(1) { + run(&target_floor, p_main_queue); + if (elevio_stopButton()) { + break; + } + nanosleep(&(struct timespec){0, 20*1000*1000}, NULL); } - */ + + queue_deinit(p_main_queue); return 0; diff --git a/skeleton_project/source/run.c b/skeleton_project/source/run.c new file mode 100644 index 0000000..0b72bb6 --- /dev/null +++ b/skeleton_project/source/run.c @@ -0,0 +1,36 @@ +#include "run.h" + +void run(int* target_floor, Queue* p_main_queue, Button* p_button_2_up) { + // Setup elevator + + button_update(p_button_2_up, elevio_callButton(2, BUTTON_HALL_UP)); + + if (p_button_2_up->was_just_pressed) { + // queue_add(p_main_queue, (Request) {2, false, false}); + printf("Button just pressed\n"); + // queue_print(p_main_queue); + } + /* + for (int i = 0; i < 4; i++) { + if (elevio_callButton(i, BUTTON_HALL_DOWN)) { + queue_add(p_main_queue, (Request) {i, false, false}); + queue_print(p_main_queue); + } + + if (elevio_callButton(i, BUTTON_HALL_UP)) { + queue_add(p_main_queue, (Request) {i, true, false}); + queue_print(p_main_queue); + } + + if (elevio_callButton(i, BUTTON_CAB)) { + if (i > elevio_floorSensor()) { + queue_add(p_main_queue, (Request) {i, true, true}); + queue_print(p_main_queue); + } else if (i < elevio_floorSensor()) { + queue_add(p_main_queue, (Request) {i, true, false}); + queue_print(p_main_queue); + } + } + } + */ +} \ No newline at end of file diff --git a/skeleton_project/source/run.h b/skeleton_project/source/run.h new file mode 100644 index 0000000..358c96b --- /dev/null +++ b/skeleton_project/source/run.h @@ -0,0 +1,10 @@ +#include +#include +#include +#include +#include +#include "queue.h" +#include "driver/elevio.h" +#include "button.h" + +void run(); \ No newline at end of file diff --git a/skeleton_project/source/state_machine.c b/skeleton_project/source/state_machine.c index f8cf1d6..704da33 100644 --- a/skeleton_project/source/state_machine.c +++ b/skeleton_project/source/state_machine.c @@ -1,7 +1,7 @@ #include "state_machine.h" void behaviour(State* p_current_state){ - + switch(*p_current_state) { // RETNING FULLHET DØR OBSTRUKSJON case UP_EMPTY: From 7894a0a4b55efb801f39cdcce5dde5a3f8f03439 Mon Sep 17 00:00:00 2001 From: angrycompany16 Date: Wed, 21 Feb 2024 11:36:27 +0100 Subject: [PATCH 2/3] seemingly working button system --- skeleton_project/.vscode/settings.json | 4 +- skeleton_project/Makefile | 2 +- skeleton_project/source/button.c | 17 ++++++--- skeleton_project/source/button.h | 5 +-- skeleton_project/source/main.c | 8 ++-- skeleton_project/source/queue.h | 2 +- skeleton_project/source/request.h | 2 +- skeleton_project/source/run.c | 51 ++++++++++++++++++++++--- skeleton_project/source/state_machine.h | 4 +- 9 files changed, 71 insertions(+), 24 deletions(-) diff --git a/skeleton_project/.vscode/settings.json b/skeleton_project/.vscode/settings.json index 4ac892f..c7f2ee0 100644 --- a/skeleton_project/.vscode/settings.json +++ b/skeleton_project/.vscode/settings.json @@ -2,6 +2,8 @@ "files.associations": { "elevio.h": "c", "run.h": "c", - "time.h": "c" + "time.h": "c", + "button.h": "c", + "stdio.h": "c" } } \ No newline at end of file diff --git a/skeleton_project/Makefile b/skeleton_project/Makefile index b8f28b8..77a6d48 100644 --- a/skeleton_project/Makefile +++ b/skeleton_project/Makefile @@ -2,7 +2,7 @@ EXECUTABLE = elevator COMPILER = clang -CFLAGS = -Wall -g -std=gnu11 -fsanitize=address +CFLAGS = -Wall -g -std=gnu11 -fsanitize=address -O0 LDFLAGS = -fsanitize=address EXCLUDE = '*test*' diff --git a/skeleton_project/source/button.c b/skeleton_project/source/button.c index fa8c8a6..34c5261 100644 --- a/skeleton_project/source/button.c +++ b/skeleton_project/source/button.c @@ -1,22 +1,27 @@ #include "button.h" Button* init_button() { - return &(Button) {false, false, false}; + Button* p_button = (Button*)malloc(sizeof(Button)); + if (p_button != NULL) { + p_button->pressed = false; + p_button->was_just_pressed = true; + p_button->was_just_released = false; + } + + return p_button; } void button_update(Button* button, bool value) { - button->is_pressed = value; - - if (button->is_pressed && !button->was_pressed) { + if (!button->pressed && value) { button->was_just_pressed = true; - } else if (!button->is_pressed && button->was_pressed) { + } else if (button->pressed && !value) { button->was_just_released = true; } else { button->was_just_pressed = false; button->was_just_released = false; } - button->was_pressed = button->is_pressed; + button->pressed = value; } diff --git a/skeleton_project/source/button.h b/skeleton_project/source/button.h index aff90a7..de9fe92 100644 --- a/skeleton_project/source/button.h +++ b/skeleton_project/source/button.h @@ -3,9 +3,8 @@ #include #include -typedef struct Button { - bool is_pressed; - bool was_pressed; +typedef struct { + bool pressed; bool was_just_pressed; bool was_just_released; } Button; diff --git a/skeleton_project/source/main.c b/skeleton_project/source/main.c index 7e6de1e..a9de4f5 100644 --- a/skeleton_project/source/main.c +++ b/skeleton_project/source/main.c @@ -14,11 +14,13 @@ int main() { Queue* p_main_queue = queue_init(); int target_floor = 0; - - Button* p_button_2_up = init_button(); + // Button* p_button_2_up = init_button(); + Button* up_buttons[] = { init_button(), init_button(), init_button(), init_button() }; + Button* down_buttons[] = { init_button(), init_button(), init_button(), init_button() }; + Button* cab_buttons[] = { init_button(), init_button(), init_button(), init_button() }; while(1) { - run(&target_floor, p_main_queue); + run(&target_floor, p_main_queue, up_buttons, down_buttons, cab_buttons); if (elevio_stopButton()) { break; diff --git a/skeleton_project/source/queue.h b/skeleton_project/source/queue.h index b0d717e..3373482 100644 --- a/skeleton_project/source/queue.h +++ b/skeleton_project/source/queue.h @@ -6,7 +6,7 @@ #define MAX_QUEUE_SIZE 100 -typedef struct Queue { +typedef struct { Request queue[MAX_QUEUE_SIZE]; size_t last_queue_element; } Queue; diff --git a/skeleton_project/source/request.h b/skeleton_project/source/request.h index 6bdb93a..efab0d6 100644 --- a/skeleton_project/source/request.h +++ b/skeleton_project/source/request.h @@ -3,7 +3,7 @@ #include #include -typedef struct Request { +typedef struct { int floor; bool up; bool off; diff --git a/skeleton_project/source/run.c b/skeleton_project/source/run.c index 0b72bb6..58e7bdf 100644 --- a/skeleton_project/source/run.c +++ b/skeleton_project/source/run.c @@ -1,15 +1,54 @@ #include "run.h" -void run(int* target_floor, Queue* p_main_queue, Button* p_button_2_up) { +void run( + int* target_floor, + Queue* p_main_queue, + Button** pp_up_buttons, + Button** pp_down_buttons, + Button** pp_cab_buttons +) { // Setup elevator - button_update(p_button_2_up, elevio_callButton(2, BUTTON_HALL_UP)); + // button_update(p_button_2_up, false); - if (p_button_2_up->was_just_pressed) { - // queue_add(p_main_queue, (Request) {2, false, false}); - printf("Button just pressed\n"); - // queue_print(p_main_queue); + // printf("%d\n", pp_up_buttons[0]->pressed); + // printf("%d\n", pp_up_buttons[0]->was_just_pressed); + // printf("%d\n", pp_up_buttons[0]->was_just_released); + + for (size_t i = 0; i < 4; i++) { + button_update(pp_up_buttons[i], elevio_callButton(i, BUTTON_HALL_UP)); + button_update(pp_down_buttons[i], elevio_callButton(i, BUTTON_HALL_DOWN)); + button_update(pp_cab_buttons[i], elevio_callButton(i, BUTTON_CAB)); + } + + + // printf("%d\n", pp_up_buttons[0]->was_just_pressed); + + if (pp_cab_buttons[3]->was_just_pressed) { + printf("4th floor cab button pressed\n"); } + + // button_update(p_button_2_up, elevio_callButton(2, BUTTON_HALL_UP)); + // if (elevio_callButton(2, BUTTON_HALL_UP)) { + // } + // sleep(0.001); + + // printf("%d\n", elevio_callButton(2, BUTTON_HALL_UP)); + + // if (p_button_2_up->was_just_pressed) { + + // // queue_add(p_main_queue, (Request) {2, false, false}); + // printf("Button just pressed\n"); + // // queue_print(p_main_queue); + // } + + // if (p_button_2_up->was_just_released) { + + // // queue_add(p_main_queue, (Request) {2, false, false}); + // printf("Button just released\n"); + // // queue_print(p_main_queue); + // } + /* for (int i = 0; i < 4; i++) { if (elevio_callButton(i, BUTTON_HALL_DOWN)) { diff --git a/skeleton_project/source/state_machine.h b/skeleton_project/source/state_machine.h index c431de5..9287479 100644 --- a/skeleton_project/source/state_machine.h +++ b/skeleton_project/source/state_machine.h @@ -6,7 +6,7 @@ #include "driver/elevio.h" // enums -typedef enum State { +typedef enum { UP_EMPTY, UP_UNEMPTY, @@ -23,7 +23,7 @@ typedef enum State { CLOSED_UNEMPTY } State; -typedef enum Trigger { +typedef enum { STOP, ENTERED_FLOOR, OBSTRUCTION, From 7133e6bad3140440b0d14906c8fce1454c7489cc Mon Sep 17 00:00:00 2001 From: angrycompany16 Date: Wed, 21 Feb 2024 11:43:27 +0100 Subject: [PATCH 3/3] ooop :P i created a memory leak XD --- skeleton_project/source/button.c | 6 +++++- skeleton_project/source/button.h | 4 +++- skeleton_project/source/main.c | 13 +++++++++---- skeleton_project/source/run.c | 4 ---- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/skeleton_project/source/button.c b/skeleton_project/source/button.c index 34c5261..bfc439a 100644 --- a/skeleton_project/source/button.c +++ b/skeleton_project/source/button.c @@ -1,6 +1,6 @@ #include "button.h" -Button* init_button() { +Button* button_init() { Button* p_button = (Button*)malloc(sizeof(Button)); if (p_button != NULL) { p_button->pressed = false; @@ -11,6 +11,10 @@ Button* init_button() { return p_button; } +void button_deinit(Button* button) { + free(button); +} + void button_update(Button* button, bool value) { if (!button->pressed && value) { button->was_just_pressed = true; diff --git a/skeleton_project/source/button.h b/skeleton_project/source/button.h index de9fe92..ad0a683 100644 --- a/skeleton_project/source/button.h +++ b/skeleton_project/source/button.h @@ -9,6 +9,8 @@ typedef struct { bool was_just_released; } Button; -Button* init_button(); +Button* button_init(); + +void button_deinit(Button* button); void button_update(Button* button, bool value); \ No newline at end of file diff --git a/skeleton_project/source/main.c b/skeleton_project/source/main.c index a9de4f5..130e70e 100644 --- a/skeleton_project/source/main.c +++ b/skeleton_project/source/main.c @@ -14,10 +14,10 @@ int main() { Queue* p_main_queue = queue_init(); int target_floor = 0; - // Button* p_button_2_up = init_button(); - Button* up_buttons[] = { init_button(), init_button(), init_button(), init_button() }; - Button* down_buttons[] = { init_button(), init_button(), init_button(), init_button() }; - Button* cab_buttons[] = { init_button(), init_button(), init_button(), init_button() }; + + Button* up_buttons[] = { button_init(), button_init(), button_init(), button_init() }; + Button* down_buttons[] = { button_init(), button_init(), button_init(), button_init() }; + Button* cab_buttons[] = { button_init(), button_init(), button_init(), button_init() }; while(1) { run(&target_floor, p_main_queue, up_buttons, down_buttons, cab_buttons); @@ -30,6 +30,11 @@ int main() { } queue_deinit(p_main_queue); + for (int i = 0; i < 4; i++) { + button_deinit(up_buttons[i]); + button_deinit(down_buttons[i]); + button_deinit(cab_buttons[i]); + } return 0; diff --git a/skeleton_project/source/run.c b/skeleton_project/source/run.c index 58e7bdf..1ca4458 100644 --- a/skeleton_project/source/run.c +++ b/skeleton_project/source/run.c @@ -11,10 +11,6 @@ void run( // button_update(p_button_2_up, false); - // printf("%d\n", pp_up_buttons[0]->pressed); - // printf("%d\n", pp_up_buttons[0]->was_just_pressed); - // printf("%d\n", pp_up_buttons[0]->was_just_released); - for (size_t i = 0; i < 4; i++) { button_update(pp_up_buttons[i], elevio_callButton(i, BUTTON_HALL_UP)); button_update(pp_down_buttons[i], elevio_callButton(i, BUTTON_HALL_DOWN));