Skip to content

Commit

Permalink
Resolved merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
elisaakj committed Feb 26, 2024
2 parents 708e10c + f5aa47a commit 7a3183a
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 48 deletions.
6 changes: 5 additions & 1 deletion skeleton_project/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"files.associations": {
"elevio.h": "c"
"elevio.h": "c",
"run.h": "c",
"time.h": "c",
"button.h": "c",
"stdio.h": "c"
}
}
2 changes: 1 addition & 1 deletion skeleton_project/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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*'

Expand Down
5 changes: 3 additions & 2 deletions skeleton_project/source/FSM.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include "queue.h"
#include "timer.h"

typedef enum FSM_State {
// enums
typedef enum {
UP_EMPTY,
UP_UNEMPTY,

Expand All @@ -23,7 +24,7 @@ typedef enum FSM_State {
CLOSED_UNEMPTY
} FSM_State;

typedef enum FSM_Trigger {
typedef enum {
STOP,
ENTERED_FLOOR,
OBSTRUCTION,
Expand Down
33 changes: 33 additions & 0 deletions skeleton_project/source/button.c
Original file line number Diff line number Diff line change
@@ -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;
}




16 changes: 16 additions & 0 deletions skeleton_project/source/button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

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);
58 changes: 16 additions & 42 deletions skeleton_project/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,36 @@
#include "driver/elevio.h"
#include "FSM.h"
#include "queue.h"
#include "timer.h"
#include "run.h"
#include "button.h"

int main() {

int main(){

/*
// test av timer
time_t start_time;
printf("Starting timer");
start_timer(&start_time);
while(!time_limit(&start_time)){
printf("ikke enda");
}
printf("der ja");
*/

/*
elevio_init();

Queue* p_main_queue = queue_init();
int target_floor = 0;

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();
nanosleep(&(struct timespec){0, 20*1000*1000}, NULL);
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);
*/

/*
elevio_init();
Queue* p_main_queue = queue_init();
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);
queue_print(p_main_queue);
nanosleep(&(struct timespec){0, 20*1000*1000}, NULL);
*/
for (int i = 0; i < 4; i++) {
button_deinit(up_buttons[i]);
button_deinit(down_buttons[i]);
button_deinit(cab_buttons[i]);
}

return 0;

Expand Down
2 changes: 1 addition & 1 deletion skeleton_project/source/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion skeleton_project/source/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stdlib.h>
#include <stdbool.h>

typedef struct Request {
typedef struct {
int floor;
bool up;
bool off;
Expand Down
71 changes: 71 additions & 0 deletions skeleton_project/source/run.c
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
*/
}
10 changes: 10 additions & 0 deletions skeleton_project/source/run.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include "queue.h"
#include "driver/elevio.h"
#include "button.h"

void run();

0 comments on commit 7a3183a

Please sign in to comment.