format
This commit is contained in:
+2
-1
@@ -1,2 +1,3 @@
|
||||
[*.c]
|
||||
indent_style = tab
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
|
||||
#define OSCILLATOR_CALIBRATION 0x76
|
||||
|
||||
#define MAIN_TIME 30*1000L
|
||||
#define MAIN_TIME 30 * 1000L
|
||||
// #define TIME_INCREMENT 30*1000L
|
||||
#define INCREMENT_MODE MODE_FISCHER
|
||||
#define RESET_DELAY 3000
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/io.h>
|
||||
|
||||
#include "i2c.h"
|
||||
|
||||
@@ -15,7 +15,7 @@ void i2c_init() {
|
||||
#define LOW 0
|
||||
|
||||
void port_a_put(uint8_t pin, bool value) {
|
||||
if(value) {
|
||||
if (value) {
|
||||
PORTA |= _BV(pin);
|
||||
DDRA &= ~_BV(pin);
|
||||
} else {
|
||||
@@ -47,7 +47,7 @@ int i2c_write_byte(uint8_t byte) {
|
||||
return ack;
|
||||
}
|
||||
|
||||
int i2c_write(uint8_t addr, const uint8_t* data, uint8_t len) {
|
||||
int i2c_write(uint8_t addr, const uint8_t *data, uint8_t len) {
|
||||
// START condition
|
||||
port_a_put(SDA_BIT, LOW);
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <stdint.h>
|
||||
|
||||
void i2c_init();
|
||||
int i2c_write(uint8_t addr, const uint8_t* data, uint8_t len);
|
||||
int i2c_write(uint8_t addr, const uint8_t *data, uint8_t len);
|
||||
|
||||
+31
-29
@@ -1,11 +1,11 @@
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/io.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "as1115.h"
|
||||
#include "config.h"
|
||||
#include "i2c.h"
|
||||
|
||||
#if MAIN_TIME > 5999000
|
||||
@@ -17,11 +17,7 @@
|
||||
|
||||
volatile uint32_t ms_since_boot = 0;
|
||||
|
||||
typedef enum {
|
||||
STOPPED,
|
||||
PLAYER_A,
|
||||
PLAYER_B
|
||||
} state_type;
|
||||
typedef enum { STOPPED, PLAYER_A, PLAYER_B } state_type;
|
||||
|
||||
volatile state_type state = STOPPED;
|
||||
|
||||
@@ -31,16 +27,21 @@ volatile uint32_t move_started_at = MAIN_TIME;
|
||||
|
||||
volatile uint32_t finished_at = 0;
|
||||
|
||||
ISR(TIM1_COMPA_vect)
|
||||
{
|
||||
ISR(TIM1_COMPA_vect) {
|
||||
ms_since_boot++;
|
||||
|
||||
if(state == PLAYER_A) {
|
||||
if (state == PLAYER_A) {
|
||||
player_a_timer--;
|
||||
if(player_a_timer == 0) { state = STOPPED; finished_at = ms_since_boot; }
|
||||
} else if(state == PLAYER_B) {
|
||||
if (player_a_timer == 0) {
|
||||
state = STOPPED;
|
||||
finished_at = ms_since_boot;
|
||||
}
|
||||
} else if (state == PLAYER_B) {
|
||||
player_b_timer--;
|
||||
if(player_b_timer == 0) { state = STOPPED; finished_at = ms_since_boot; }
|
||||
if (player_b_timer == 0) {
|
||||
state = STOPPED;
|
||||
finished_at = ms_since_boot;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,9 +62,7 @@ void setup_clock(void) {
|
||||
TIMSK1 = 0x02;
|
||||
}
|
||||
|
||||
bool button_pressed(uint8_t pin) {
|
||||
return (PINA & (1 << pin)) == 0;
|
||||
}
|
||||
bool button_pressed(uint8_t pin) { return (PINA & (1 << pin)) == 0; }
|
||||
|
||||
#define DISPLAY_LEFT 0
|
||||
#define DISPLAY_RIGHT 1
|
||||
@@ -103,7 +102,7 @@ void render_timer(unsigned long ms, uint8_t display) {
|
||||
}
|
||||
|
||||
render:
|
||||
switch(display) {
|
||||
switch (display) {
|
||||
case DISPLAY_LEFT:
|
||||
as1115_send_command(0x01, digits[0]);
|
||||
as1115_send_command(0x02, digits[1]);
|
||||
@@ -124,20 +123,20 @@ uint32_t get_increment(uint32_t player_timer) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef TIME_INCREMENT
|
||||
#if INCREMENT_MODE == MODE_FISCHER
|
||||
#ifdef TIME_INCREMENT
|
||||
#if INCREMENT_MODE == MODE_FISCHER
|
||||
return TIME_INCREMENT;
|
||||
#elif INCREMENT_MODE == MODE_BRONSTEIN
|
||||
#elif INCREMENT_MODE == MODE_BRONSTEIN
|
||||
uint32_t difference = move_started_at - player_timer;
|
||||
if(difference < TIME_INCREMENT) {
|
||||
if (difference < TIME_INCREMENT) {
|
||||
return difference;
|
||||
} else {
|
||||
return TIME_INCREMENT;
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
#endif
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
void reset() {
|
||||
@@ -167,25 +166,28 @@ int main() {
|
||||
as1115_send_command(GLOBAL_INTENSITY_REG, 0x06); // set brightness
|
||||
as1115_send_command(SHUTDOWN_REG, 0x01); // turn on
|
||||
|
||||
while(true) {
|
||||
while (true) {
|
||||
render_timer(player_a_timer, DISPLAY_LEFT);
|
||||
render_timer(player_b_timer, DISPLAY_RIGHT);
|
||||
|
||||
cli();
|
||||
if(button_pressed(PLAYER_A_BUTTON) && state != PLAYER_B && player_b_timer != 0 && player_a_timer != 0) {
|
||||
if (button_pressed(PLAYER_A_BUTTON) && state != PLAYER_B &&
|
||||
player_b_timer != 0 && player_a_timer != 0) {
|
||||
player_a_timer += get_increment(player_a_timer);
|
||||
state = PLAYER_B;
|
||||
move_started_at = player_b_timer;
|
||||
}
|
||||
|
||||
if(button_pressed(PLAYER_B_BUTTON) && state != PLAYER_A && player_b_timer != 0 && player_a_timer != 0) {
|
||||
if (button_pressed(PLAYER_B_BUTTON) && state != PLAYER_A &&
|
||||
player_b_timer != 0 && player_a_timer != 0) {
|
||||
player_b_timer += get_increment(player_b_timer);
|
||||
state = PLAYER_A;
|
||||
move_started_at = player_a_timer;
|
||||
}
|
||||
sei();
|
||||
|
||||
if (state == STOPPED && (player_a_timer == 0 || player_b_timer == 0) && (ms_since_boot > finished_at + RESET_DELAY)) {
|
||||
if (state == STOPPED && (player_a_timer == 0 || player_b_timer == 0) &&
|
||||
(ms_since_boot > finished_at + RESET_DELAY)) {
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user