TTK4155 Lab Node 1
Term project in the NTNU course TTK4155 Embedded and Industrial Computer Systems Design
Loading...
Searching...
No Matches
Game Controller / CAN

Data Structures

struct  control_state
 Stores current control state. More...

Functions

uint8_t process_can_frame (struct control_state *ctrl)
 Processes an incoming CAN frame.
int8_t tx_joy_btn (struct io_joystick_device *joy_dev, struct io_avr_device *avr_dev, struct can_device *can_dev)
 Transmits joystick positions and button states via CAN.
int8_t tx_gamestart (struct can_device *can_dev)
 Sends a game start message via CAN.
int8_t tx_reset (struct can_device *can_dev)
 Sends a reset message via CAN.
int8_t tx_error (struct can_device *can_dev)
 Sends an error message via CAN.

Detailed Description

Function Documentation

◆ process_can_frame()

uint8_t process_can_frame ( struct control_state * ctrl)

#include <controller.h>

Processes an incoming CAN frame.

Parameters
ctrlPointer to control state struct
Returns
0 on success

Definition at line 15 of file controller.c.

15 {
16
17 // We only recive one type of message
18
19 struct CAN_frame msg = {0};
20
21 if (can_rxq_pull(&msg)) {
22
23 LOG_CRITICAL("Recv");
24
25 if (msg.id == CAN_ID_GAMEOVER) {
26 LOG_CRITICAL("Game over");
27
28 ctrl->game_over = msg.data[0];
29 }
30 }
31 return 0;
32}
#define CAN_ID_GAMEOVER
Definition can.h:19
int can_rxq_pull(struct CAN_frame *out)
Pull a CAN frame from the internal receive queue.
Definition can.c:171
#define LOG_CRITICAL(msg)
Definition log.h:46
struct control_state ctrl
Definition main.c:91
Represents a full CAN frame for sending/receiving.
Definition can.h:53
uint32_t id
Definition can.h:54
char data[8]
Definition can.h:56

Referenced by main().

◆ tx_error()

int8_t tx_error ( struct can_device * can_dev)

#include <controller.h>

Sends an error message via CAN.

Parameters
can_devPointer to CAN device
Returns
0 on success, negative on error

Definition at line 78 of file controller.c.

78 {
79
80 // Frame Data
81 struct CAN_frame msg = {CAN_ID_ERROR, 0x08, {0}, 0, 0};
82
83 // Transmit data
84 return can_write(can_dev, msg);
85}
int8_t can_write(struct can_device *dev, struct CAN_frame msg)
Write a CAN frame to the MCP2515 transmit buffer.
Definition can.c:49
#define CAN_ID_ERROR
Definition can.h:18

◆ tx_gamestart()

int8_t tx_gamestart ( struct can_device * can_dev)

#include <controller.h>

Sends a game start message via CAN.

Parameters
can_devPointer to CAN device
Returns
0 on success

Definition at line 58 of file controller.c.

58 {
59
60 // Frame Data
61 struct CAN_frame msg = {CAN_ID_GAMESTART, 0x01, {1}, 0, 0};
62
63 // Transmit data
64 can_write(can_dev, msg);
65 return 0;
66}
#define CAN_ID_GAMESTART
Definition can.h:20

Referenced by main().

◆ tx_joy_btn()

int8_t tx_joy_btn ( struct io_joystick_device * joy_dev,
struct io_avr_device * avr_dev,
struct can_device * can_dev )

#include <controller.h>

Transmits joystick positions and button states via CAN.

Parameters
joy_devPointer to joystick device
avr_devPointer to I/O board device
can_devPointer to CAN device
Returns
0 on success, negative on error

Definition at line 34 of file controller.c.

35 {
36
37 // Read ADC
38 struct io_joystick_position joy_pos;
39 io_joystick_read_position(joy_dev, &joy_pos);
40
41 struct io_avr_buttons btn;
42 io_avr_buttons_read(avr_dev, &btn);
43
44 // Frame Data
45 struct CAN_frame msg = {
47 0x08,
48 {joy_pos.x, joy_pos.y, btn.right, btn.left, btn.nav, 0, 0, 0},
49 0,
50 0};
51
52 // Transmit data
53 can_write(can_dev, msg);
54
55 return 0;
56}
#define CAN_ID_JOYPOS
Definition can.h:21
int io_joystick_read_position(struct io_joystick_device *dev, struct io_joystick_position *buffer)
Read the position of a joystick as a percentage from 0 in each axis.
Definition io.c:38
int io_avr_buttons_read(struct io_avr_device *dev, struct io_avr_buttons *btn)
Definition io.c:115
struct io_avr_buttons btn
Definition main.c:59
Represents the position of a joystick relative to center.
Definition io.h:70

Referenced by main().

◆ tx_reset()

int8_t tx_reset ( struct can_device * can_dev)

#include <controller.h>

Sends a reset message via CAN.

Parameters
can_devPointer to CAN device
Returns
0 on success

Definition at line 68 of file controller.c.

68 {
69
70 // Frame Data
71 struct CAN_frame msg = {CAN_ID_RESET, 0x01, {0}, 0, 0};
72
73 // Transmit data
74 can_write(can_dev, msg);
75 return 0;
76}
#define CAN_ID_RESET
Definition can.h:22