TTK4155 Lab Node 1
Term project in the NTNU course TTK4155 Embedded and Industrial Computer Systems Design
Loading...
Searching...
No Matches
controller.c
Go to the documentation of this file.
1/*
2 * controller.c
3 *
4 * Created: 15.11.2025 13:52:49
5 * Author: wighu
6 */
7
8#include "controller.h"
9#include "can.h"
10#include "io.h"
11#include "log.h"
12
13LOG_MODULE_DEFINE("Controller");
14
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}
33
34int8_t tx_joy_btn(struct io_joystick_device *joy_dev,
35 struct io_avr_device *avr_dev, struct can_device *can_dev) {
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}
57
58int8_t tx_gamestart(struct can_device *can_dev) {
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}
67
68int8_t tx_reset(struct can_device *can_dev) {
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}
77
78int8_t tx_error(struct can_device *can_dev) {
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}
CAN driver.
Handles CAN communication for game control, joystick, and buttons.
#define CAN_ID_GAMEOVER
Definition can.h:19
#define CAN_ID_RESET
Definition can.h:22
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_GAMESTART
Definition can.h:20
#define CAN_ID_JOYPOS
Definition can.h:21
int can_rxq_pull(struct CAN_frame *out)
Pull a CAN frame from the internal receive queue.
Definition can.c:171
#define CAN_ID_ERROR
Definition can.h:18
int8_t tx_error(struct can_device *can_dev)
Sends an error message via CAN.
Definition controller.c:78
uint8_t process_can_frame(struct control_state *ctrl)
Processes an incoming CAN frame.
Definition controller.c:15
int8_t tx_reset(struct can_device *can_dev)
Sends a reset message via CAN.
Definition controller.c:68
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.
Definition controller.c:34
int8_t tx_gamestart(struct can_device *can_dev)
Sends a game start message via CAN.
Definition controller.c:58
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
#define LOG_CRITICAL(msg)
Definition log.h:46
#define LOG_MODULE_DEFINE(name)
Definition log.h:39
Driver for joystick, buttons, and OLED.
Logging interface for debug messages.
struct control_state ctrl
Definition main.c:91
struct io_avr_buttons btn
Definition main.c:59
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
Represents a CAN device, storing the SPI slave controlling the MCP2515.
Definition can.h:43
Stores current control state.
Definition controller.h:18
Represents an SPI-connected AVR-I/O board.
Definition io.h:39
Represents a joystick input device.
Definition io.h:55
Represents the position of a joystick relative to center.
Definition io.h:70