TTK4155 Lab Node 1
Term project in the NTNU course TTK4155 Embedded and Industrial Computer Systems Design
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
26
27#include <stddef.h>
28#include <stdlib.h>
29#include <util/delay.h>
30
31#include "can.h"
32#include "controller.h"
33#include "io.h"
34#include "log.h"
35#include "menu.h"
36#include "spi.h"
37#include "timer.h"
38#include "uart.h"
39#include "utils.h"
40#include "xmem.h"
41
42#define BAUD 9600
43
45
46// ---------------------------------------------------------------------------
47// Device Configs
48// ---------------------------------------------------------------------------
49struct USART_config config = {BAUD, F_CPU};
50struct io_joystick_device joy = {0, 1, 67, 69, 247, 247};
53struct can_device can = {SSE2};
54
55// ---------------------------------------------------------------------------
56// Data Containers
57// ---------------------------------------------------------------------------
59struct io_avr_buttons btn;
61
62// Define settings sub menu
63static struct menu_item settings_menu[] = {
64 {"Adjust brightness", PAGE_ADJUST_BRIGHTNESS, NULL, 0},
65 {"Calibrate joystick", PAGE_CALIBRATE_JOYSTICK, NULL, 0},
66};
67
68// Define main menu
69static struct menu_item main_menu[] = {
70 {"Start game", PAGE_PLAY_GAME, NULL, 0},
71 {"High scores", PAGE_HIGH_SCORES, NULL, 0},
72 {"Settings", PAGE_SETTINGS, settings_menu, 2},
73};
74
75// Initialize menu_cfg struct with menus defined above
76struct menu_cfg menu = {
77 .oled = &oled,
78 .items = main_menu,
79 .length = 3,
80 .cursor_pos = 0,
81 .current_page = PAGE_WELCOME,
82 .root_items = main_menu,
83 .root_length = 1,
84 .parent_menu = NULL,
85 .parent_length = 0,
86};
87
88// ---------------------------------------------------------------------------
89// Game state
90// ---------------------------------------------------------------------------
91struct control_state ctrl = {0};
93
94// ---------------------------------------------------------------------------
95// Main
96// ---------------------------------------------------------------------------
97int main() {
98 int status = 0;
99
100 // ---------------------------------------------------------------------------
101 // Initializations
102 // ---------------------------------------------------------------------------
103
104 status = USART_init(&config);
105 STATUS_ASSERT(status);
106
107 status = xmem_init();
108 STATUS_ASSERT(status)
109
110 status = SRAM_test();
111 STATUS_ASSERT(status)
112
113 status = tim1_CTC_init();
114 STATUS_ASSERT(status)
115
116 status = io_avr_init(&avr);
117 STATUS_ASSERT(status)
118
119 status = menu_init(&menu);
120 STATUS_ASSERT(status)
121
122 status = can_init(&can);
123 STATUS_ASSERT(status)
124
125 LOG_CRITICAL("---Init complete---");
126
127 static enum page_id last_state = PAGE_WELCOME;
128
129 while (1) {
130
131 // ---------------------------------------------------------------------------
132 // Input/Output Control
133 // ---------------------------------------------------------------------------
134
135 io_avr_buttons_read(&avr, &btn); // Read button inputs from IO board
136 process_can_frame(&ctrl); // Parse CAN
137
138 // ---------------------------------------------------------------------------
139 // Game Control
140 // ---------------------------------------------------------------------------
141
142 menu_handler(&menu, &btn); // Handle menu state
143 if (menu.current_page != last_state) { // Check for game start
144 if (menu.current_page == PAGE_PLAY_GAME) {
146 }
147 last_state = menu.current_page;
148 } else {
149 tx_joy_btn(&joy, &avr, &can); // Transmit input states over CAN
150 }
151
152 if (ctrl.game_over) { // State game-over
153 ctrl.game_over = 0;
154 if (menu.current_page == PAGE_PLAY_GAME) {
155 LOG_CRITICAL("Game over");
156 menu.current_page = PAGE_GAME_OVER;
157 }
158 }
159
160 _delay_ms(100);
161 }
162 return 0;
163}
164
CAN driver.
Handles CAN communication for game control, joystick, and buttons.
int8_t can_init(struct can_device *dev)
Initialize a CAN device.
Definition can.c:22
uint8_t process_can_frame(struct control_state *ctrl)
Processes an incoming CAN frame.
Definition controller.c:15
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_avr_init(struct io_avr_device *dev)
Definition io.c:106
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
int menu_init(struct menu_cfg *menu)
Initializes menu and OLED display.
Definition menu.c:16
int menu_handler(struct menu_cfg *menu, struct io_avr_buttons *btn)
Handles menu logic (navigation, selection, page timers).
Definition menu.c:340
page_id
Menu page identifiers.
Definition menu.h:19
@ PAGE_PLAY_GAME
Definition menu.h:21
@ PAGE_HIGH_SCORES
Definition menu.h:22
@ PAGE_SETTINGS
Definition menu.h:23
@ PAGE_ADJUST_BRIGHTNESS
Definition menu.h:24
@ PAGE_GAME_OVER
Definition menu.h:26
@ PAGE_WELCOME
Definition menu.h:20
@ PAGE_CALIBRATE_JOYSTICK
Definition menu.h:25
@ SSB2
Definition spi.h:16
@ SSB3
Definition spi.h:17
@ SSE2
Definition spi.h:18
int tim1_CTC_init(void)
Initialize Timer1 in CTC mode on OC1A.
Definition timer.c:26
int USART_init(struct USART_config *config)
Initialize USART0 with the given configuration.
Definition uart.c:15
#define STATUS_ASSERT(status)
Macro to assert a status and flash a debug LED.
Definition utils.h:26
int SRAM_test(void)
Tests external SRAM and prints a report.
Definition xmem.c:43
int xmem_init(void)
Initializes external SRAM (2KB or 3KB depending on XMEM_3KB).
Definition xmem.c:12
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
struct io_joystick_position pos
Definition main.c:58
static enum page_id last_state
Definition main.c:92
#define BAUD
Definition main.c:42
struct CAN_frame dummy_msg
Definition main.c:60
struct io_joystick_device joy
Definition main.c:50
static struct menu_item settings_menu[]
Definition main.c:63
struct menu_cfg menu
Definition main.c:76
static struct menu_item main_menu[]
Definition main.c:69
struct can_device can
Definition main.c:53
struct io_avr_device avr
Definition main.c:52
int main()
Definition main.c:97
struct io_oled_device oled
Definition main.c:51
struct USART_config config
Definition main.c:49
Hierarchical OLED menu system with navigation and page handling.
SPI driver.
Represents a full CAN frame for sending/receiving.
Definition can.h:53
USART configuration structure.
Definition uart.h:22
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
Represents an OLED display device controlled via SPI.
Definition io.h:46
Menu configuration and state.
Definition menu.h:49
Menu item struct.
Definition menu.h:35
Timer driver (for ADC clock).
UART communication driver.
Utility and helper functions for joystick, LED debugging, and mapping.
External SRAM interface for ATmega microcontrollers.