TTK4155 Lab Node 1
Term project in the NTNU course TTK4155 Embedded and Industrial Computer Systems Design
Loading...
Searching...
No Matches
main.c File Reference

Main entry point for the TTK4155 Node 1 firmware. More...

#include <stddef.h>
#include <stdlib.h>
#include <util/delay.h>
#include "can.h"
#include "controller.h"
#include "io.h"
#include "log.h"
#include "menu.h"
#include "spi.h"
#include "timer.h"
#include "uart.h"
#include "utils.h"
#include "xmem.h"

Go to the source code of this file.

Macros

#define BAUD   9600

Functions

 LOG_MODULE_DEFINE ("main")
int main ()

Variables

struct USART_config config = {BAUD, F_CPU}
struct io_joystick_device joy = {0, 1, 67, 69, 247, 247}
struct io_oled_device oled = {SSB2}
struct io_avr_device avr = {SSB3}
struct can_device can = {SSE2}
struct io_joystick_position pos
struct io_avr_buttons btn
struct CAN_frame dummy_msg
static struct menu_item settings_menu []
static struct menu_item main_menu []
struct menu_cfg menu
struct control_state ctrl = {0}
static enum page_id last_state = PAGE_WELCOME

Detailed Description

Main entry point for the TTK4155 Node 1 firmware.

This file initializes all peripherals and modules, then enters the main game loop. The loop handles:

  • Reading input from the I/O board
  • Sending and receiving state-information over CAN
  • Updating menu states and page transitions
  • Managing game start and game over conditions

Modules used:

  • IO
  • USART
  • SPI
  • TIMER
  • CAN
  • CONTROLLER
  • MENU
  • XMEM
  • UTILS
  • LOG

Definition in file main.c.

Macro Definition Documentation

◆ BAUD

#define BAUD   9600

Definition at line 42 of file main.c.

Function Documentation

◆ LOG_MODULE_DEFINE()

LOG_MODULE_DEFINE ( "main" )

◆ main()

int main ( )

Definition at line 97 of file main.c.

97 {
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}
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
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_GAME_OVER
Definition menu.h:26
@ PAGE_WELCOME
Definition menu.h:20
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
struct control_state ctrl
Definition main.c:91
struct io_avr_buttons btn
Definition main.c:59
static enum page_id last_state
Definition main.c:92
struct io_joystick_device joy
Definition main.c:50
struct menu_cfg menu
Definition main.c:76
struct can_device can
Definition main.c:53
struct io_avr_device avr
Definition main.c:52
struct USART_config config
Definition main.c:49

Variable Documentation

◆ avr

struct io_avr_device avr = {SSB3}

Definition at line 52 of file main.c.

52{SSB3};
@ SSB3
Definition spi.h:17

Referenced by main().

◆ btn

struct io_avr_buttons btn

◆ can

struct can_device can = {SSE2}

Definition at line 53 of file main.c.

53{SSE2};
@ SSE2
Definition spi.h:18

Referenced by main().

◆ config

struct USART_config config = {BAUD, F_CPU}

Definition at line 49 of file main.c.

49{BAUD, F_CPU};
#define BAUD
Definition main.c:42

Referenced by main(), and USART_init().

◆ ctrl

struct control_state ctrl = {0}

Definition at line 91 of file main.c.

91{0};

Referenced by main(), and process_can_frame().

◆ dummy_msg

struct CAN_frame dummy_msg

Definition at line 60 of file main.c.

◆ joy

struct io_joystick_device joy = {0, 1, 67, 69, 247, 247}

Definition at line 50 of file main.c.

50{0, 1, 67, 69, 247, 247};

Referenced by main().

◆ last_state

enum page_id last_state = PAGE_WELCOME
static

Definition at line 92 of file main.c.

Referenced by main().

◆ main_menu

struct menu_item main_menu[]
static
Initial value:
= {
{"Start game", PAGE_PLAY_GAME, NULL, 0},
{"High scores", PAGE_HIGH_SCORES, NULL, 0},
{"Settings", PAGE_SETTINGS, settings_menu, 2},
}
@ PAGE_HIGH_SCORES
Definition menu.h:22
@ PAGE_SETTINGS
Definition menu.h:23
static struct menu_item settings_menu[]
Definition main.c:63

Definition at line 69 of file main.c.

69 {
70 {"Start game", PAGE_PLAY_GAME, NULL, 0},
71 {"High scores", PAGE_HIGH_SCORES, NULL, 0},
72 {"Settings", PAGE_SETTINGS, settings_menu, 2},
73};

◆ menu

struct menu_cfg menu
Initial value:
= {
.oled = &oled,
.items = main_menu,
.length = 3,
.cursor_pos = 0,
.current_page = PAGE_WELCOME,
.root_items = main_menu,
.root_length = 1,
.parent_menu = NULL,
.parent_length = 0,
}
static struct menu_item main_menu[]
Definition main.c:69
struct io_oled_device oled
Definition main.c:51

Definition at line 76 of file main.c.

76 {
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};

Referenced by brightness_display(), calibrate_joystick_display(), cursor_update(), draw_cursor(), game_over_display(), high_scores_display(), main(), menu_handler(), menu_init(), page_back(), page_dispatch(), page_select(), play_game_display(), set_page(), settings_display(), and welcome_display().

◆ oled

struct io_oled_device oled = {SSB2}

Definition at line 51 of file main.c.

51{SSB2};
@ SSB2
Definition spi.h:16

◆ pos

Definition at line 58 of file main.c.

Referenced by io_joystick_read_direction(), and utils_joystick_print_pos().

◆ settings_menu

struct menu_item settings_menu[]
static
Initial value:
= {
{"Adjust brightness", PAGE_ADJUST_BRIGHTNESS, NULL, 0},
{"Calibrate joystick", PAGE_CALIBRATE_JOYSTICK, NULL, 0},
}
@ PAGE_ADJUST_BRIGHTNESS
Definition menu.h:24
@ PAGE_CALIBRATE_JOYSTICK
Definition menu.h:25

Definition at line 63 of file main.c.

63 {
64 {"Adjust brightness", PAGE_ADJUST_BRIGHTNESS, NULL, 0},
65 {"Calibrate joystick", PAGE_CALIBRATE_JOYSTICK, NULL, 0},
66};