-
Notifications
You must be signed in to change notification settings - Fork 1
/
comms.c
121 lines (105 loc) · 3.04 KB
/
comms.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
Comms module source code
@team 128
@author Ambrose Ledbrook - 79172462
@author Josh Jarvis - 28803714
@date 09-oct-2018
@brief This module provides the ir communication functionality used by the
game to communicate between the two boards.
*/
// Including modules used for the communication
#include "ir_uart.h"
#include "comms.h"
#include "boing.h"
/**
Intialising the communications for the game
*/
void comms_init(void) {
ir_uart_init();
}
/**
Sends a code to start the game to the other board
*/
void send_start(void) {
ir_uart_putc(START_CODE);
}
/**
Sending a code to the other board informing it that it won the game
*/
void send_won(void) {
ir_uart_putc(WIN_CODE);
}
/**
Takes a boing_dir_t of the ball and converts it to a direction code to be
sent to the other board.
@param ball_dir the boing_dir_t of the ball
@return A uint8_t code to send corresponding to the passed boing_dir_t
*/
uint8_t get_dir(boing_dir_t ball_dir) {
uint8_t to_send = 0;
// Finding what direction was passed
switch (ball_dir) {
case DIR_SW :
to_send = NORTH_EAST;
break;
case DIR_W :
to_send = EAST;
break;
case DIR_NW :
to_send = SOUTH_EAST;
break;
default :
break;
}
// Returning the direction code to be sent
return to_send;
}
/**
Sends the ball to the other board, first a BALL_CODE is sent which is then
followed by the y-coordinate and then direction of the ball.
@param The game ball
*/
void send_ball(boing_state_t ball) {
// Sending a BALL_CODE
ir_uart_putc(BALL_CODE);
// Sending the ball y-coordinate
ir_uart_putc(ball.pos.y);
// Sending the balls direction
ir_uart_putc(get_dir(ball.dir));
}
/**
Reciving data from the other board, returns the data received in the form of
a Data struct variable, which holds the type of data that was received as well
as a ball y-coordinate and drection if a ball was received.
@return The data received for the other board
*/
Data receive_data(void) {
// Initializing variable used to receive data
Data data_received = {0, 0, 0};
// Checking if ready to recieve data
if (ir_uart_read_ready_p()) {
// Receiving data into the dataReceived variable
data_received.type = ir_uart_getc();
// Checking if a ball has been recived
if (data_received.type == BALL_CODE) {
// Getting the y-coordinate and direction of the ball
data_received.ball_pos = ir_uart_getc();
data_received.ball_dir = ir_uart_getc();
}
if (data_received.type == SPEED_CODE) {
data_received.ball_pos = ir_uart_getc();
}
}
// Returning the data that was received
return data_received;
}
/**
Sending the chosen ball speed to the other board
@param The speed of the ball
*/
void send_speed(uint8_t speed) {
// Sending SPEED_CODE
ir_uart_putc(SPEED_CODE);
// Sending the ball speed
ir_uart_putc(speed);
}