-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
113 lines (101 loc) · 1.88 KB
/
main.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
#include <avr/io.h>
#include <util/twi.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <i2c.h>
#include <uart.h>
#include "rush00.h"
volatile game_state_t game_state = IDLE;
volatile uint8_t is_slave = 0;
volatile uint8_t slave_button_state = 0;
void light_led(uint8_t index)
{
const uint8_t led_index[4] = {PB0, PB1, PB2, PB4};
PORTB = 0;
if (index > 3)
return;
PORTB ^= (1 << led_index[index]);
}
void light_rgb(game_state_t status)
{
if (status == NOT_READY)
{
PORTD &= ~((1 << PD6) | (1 << PD5));
}
else if (status == WIN)
{
PORTD = (1 << PD6);
}
else if (status == LOOSE)
{
PORTD = (1 << PD5);
}
}
ISR(PCINT2_vect)
{
static uint8_t sw_pressed = 0;
// uart_printstr("SW2 ");
// uart_printhex(sw_pressed);
// uart_printstr(" | ");
// uart_printhex(is_slave);
// uart_printstr(" | ");
// uart_printhex(game_state);
// uart_printstr("\n\r");
if (!sw_pressed)
{
sw_pressed = 1;
if (is_slave)
{
slave_button_state = 1;
}
else
{
// Only on master OR if not in game
// NOBODY IS SLAVE
if (game_state == IDLE)
{
// Ask to be a slave
i2c_start(0x10, I2C_WRITE);
i2c_write(BE_A_SLAVE);
i2c_stop();
// Wait for slave to be ready
i2c_start(0x10, I2C_WRITE);
i2c_write((NOT_READY << 4) | CHANGE_GAME_STATUS);
i2c_stop();
game_state = NOT_READY;
}
else if (game_state == INGAME)
{
game_state = WIN;
}
}
}
else
{
slave_button_state = 0;
sw_pressed = 0;
}
}
int main()
{
DDRB = (1 << PINB0) | (1 << PINB1) | (1 << PINB2) | (1 << PINB4);
DDRD = (1 << PD6) | (1 << PD5);
uart_init(UART_TX);
i2c_init();
i2c_init_slave(0x10);
PCMSK2 |= (0b1 << PCINT20); // Set PCINT20 (SW2) to be interrupted
PCICR |= (0b1 << PCIE2); // Enable interrupt for PCINT[23:16] to PCINT2_vect
sei();
_delay_ms(200);
while (1)
{
if (is_slave)
{
slave_loop();
}
else
{
master_loop();
}
}
}