diff --git a/skeleton_project/.vscode/settings.json b/skeleton_project/.vscode/settings.json index a585ed3..c7f2ee0 100644 --- a/skeleton_project/.vscode/settings.json +++ b/skeleton_project/.vscode/settings.json @@ -1,5 +1,9 @@ { "files.associations": { - "elevio.h": "c" + "elevio.h": "c", + "run.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 new file mode 100644 index 0000000..bfc439a --- /dev/null +++ b/skeleton_project/source/button.c @@ -0,0 +1,33 @@ +#include "button.h" + +Button* button_init() { + 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_deinit(Button* button) { + free(button); +} + +void button_update(Button* button, bool value) { + if (!button->pressed && value) { + button->was_just_pressed = true; + } else if (button->pressed && !value) { + button->was_just_released = true; + } else { + button->was_just_pressed = false; + button->was_just_released = false; + } + + button->pressed = value; +} + + + + diff --git a/skeleton_project/source/button.h b/skeleton_project/source/button.h new file mode 100644 index 0000000..ad0a683 --- /dev/null +++ b/skeleton_project/source/button.h @@ -0,0 +1,16 @@ +#pragma once +#include +#include +#include + +typedef struct { + bool pressed; + bool was_just_pressed; + bool was_just_released; +} 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 a41a6bf..130e70e 100644 --- a/skeleton_project/source/main.c +++ b/skeleton_project/source/main.c @@ -6,31 +6,35 @@ #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"); - - sleep(1); + 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() }; - queue_print(p_main_queue); + while(1) { + run(&target_floor, p_main_queue, up_buttons, down_buttons, cab_buttons); + if (elevio_stopButton()) { + break; + } + nanosleep(&(struct timespec){0, 20*1000*1000}, NULL); } - */ + + 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/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 new file mode 100644 index 0000000..1ca4458 --- /dev/null +++ b/skeleton_project/source/run.c @@ -0,0 +1,71 @@ +#include "run.h" + +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, false); + + 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)) { + 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: 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,