Skip to content

Commit

Permalink
Merge pull request #3 from angrycompany16/david_work
Browse files Browse the repository at this point in the history
David work
  • Loading branch information
angrycompany16 authored Feb 26, 2024
2 parents c4a76de + 7133e6b commit f5aa47a
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 23 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
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);
36 changes: 20 additions & 16 deletions skeleton_project/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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();
2 changes: 1 addition & 1 deletion skeleton_project/source/state_machine.c
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
4 changes: 2 additions & 2 deletions skeleton_project/source/state_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "driver/elevio.h"

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

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

typedef enum Trigger {
typedef enum {
STOP,
ENTERED_FLOOR,
OBSTRUCTION,
Expand Down

0 comments on commit f5aa47a

Please sign in to comment.