hw-rollerderby-scoreboard/main.c

286 lines
3.9 KiB
C
Raw Normal View History

2013-02-26 22:49:02 -07:00
#include <stdint.h>
2013-03-17 23:42:17 -06:00
#include <stdlib.h>
2013-03-11 17:14:29 -06:00
#include <stdbool.h>
2013-04-01 21:17:18 -06:00
#include <avr/io.h>
#include <util/delay.h>
2013-05-04 19:46:18 -06:00
#if ARCH == avr
2013-04-01 21:17:18 -06:00
# include "avr.h"
#else
# include "ti.h"
#endif
2013-02-26 22:49:02 -07:00
2013-03-11 18:28:25 -06:00
// Number of shift registers in your scoreboard
// If you want scores to go over 199, you need 8
const int nsr = 6;
2013-03-30 20:03:10 -06:00
//
// Timing stuff
//
2013-03-31 20:10:04 -06:00
2013-03-30 20:03:10 -06:00
volatile uint32_t jiffies = 0;
volatile bool tick = false; // Set high when jiffy clock ticks
2013-03-11 23:14:30 -06:00
// Clocks are in deciseconds
2013-03-15 23:17:38 -06:00
uint16_t score_a = 0;
uint16_t score_b = 0;
2013-03-31 16:25:43 -06:00
int16_t period_clock = -(30 * 60 * 10);
int16_t jam_duration = -(2 * 60 * 10);
int16_t lineup_duration = (-30 * 10);
2013-03-30 20:03:10 -06:00
int16_t jam_clock = 0;
2013-03-11 23:14:30 -06:00
enum {
SETUP,
JAM,
LINEUP,
TIMEOUT
2013-03-17 23:42:17 -06:00
} state = SETUP;
2013-02-26 22:49:02 -07:00
2013-03-15 23:17:38 -06:00
2013-03-17 23:42:17 -06:00
// NES Controller buttons
2013-03-27 22:47:17 -06:00
#define BTN_A _BV(7)
#define BTN_B _BV(6)
#define BTN_SELECT _BV(5)
#define BTN_START _BV(4)
#define BTN_UP _BV(3)
#define BTN_DOWN _BV(2)
#define BTN_LEFT _BV(1)
#define BTN_RIGHT _BV(0)
2013-03-11 17:14:29 -06:00
2013-03-11 23:14:30 -06:00
const uint8_t seven_segment_digits[] = {
2013-03-30 20:03:10 -06:00
0x7b, 0x60, 0x37, 0x76, 0x6c, 0x5e, 0x5f, 0x70, 0x7f, 0x7e
};
const uint8_t setup_digits[] = {
0x1b, 0x12, 0x72
2013-03-11 23:14:30 -06:00
};
2013-03-11 20:17:09 -06:00
2013-03-11 17:14:29 -06:00
void
latch()
{
xlat(true);
xlat(false);
}
void
2013-03-11 18:28:25 -06:00
pulse()
{
sclk(true);
sclk(false);
}
void
write(uint8_t number)
2013-03-11 17:14:29 -06:00
{
int i;
int j;
// MSB first
2013-03-11 18:28:25 -06:00
for (i = 7; i >= 0; i -= 1) {
2013-03-11 17:14:29 -06:00
sin(number & (1 << i));
2013-03-17 23:42:17 -06:00
2013-03-11 17:14:29 -06:00
for (j = 0; j < 12; j += 1) {
2013-03-11 18:28:25 -06:00
pulse();
2013-03-11 17:14:29 -06:00
}
}
}
2013-03-11 18:28:25 -06:00
void
write_num(uint16_t number, int digits)
{
2013-03-15 23:17:38 -06:00
uint16_t divisor = 1;
2013-03-11 18:28:25 -06:00
int i;
2013-03-30 20:03:10 -06:00
2013-03-11 18:28:25 -06:00
for (i = 0; i < digits; i += 1) {
2013-03-15 23:17:38 -06:00
uint16_t n = (number / divisor) % 10;
write(seven_segment_digits[n]);
2013-03-30 20:03:10 -06:00
divisor *= 10;
2013-03-11 18:28:25 -06:00
}
}
2013-03-11 23:14:30 -06:00
/*
* Update all the digits
*/
void
draw()
{
2013-03-15 23:17:38 -06:00
uint16_t clk;
2013-03-29 22:39:44 -06:00
//XXX testing
2013-03-11 23:14:30 -06:00
write_num(score_b, 2);
2013-03-30 20:03:10 -06:00
if (state == SETUP) {
write(setup_digits[2]);
write(setup_digits[1]);
write(setup_digits[1]);
write(setup_digits[0]);
} else {
clk = (abs(jam_clock / 600) % 10) * 1000;
clk += abs(jam_clock) % 600;
write_num(clk, 4);
}
2013-03-11 23:14:30 -06:00
write_num(score_a, 2);
2013-03-11 23:14:30 -06:00
if ((state == TIMEOUT) && (jam_clock % 8 == 0)) {
// Blank it out
for (clk = 0; clk < 4; clk += 1) {
write(0);
}
} else {
clk = (abs(period_clock / 10) / 60) * 100;
clk += abs(period_clock / 10) % 60;
write_num(clk, 4);
}
2013-03-11 23:14:30 -06:00
latch();
pulse();
}
/*
2013-03-15 23:17:38 -06:00
* Probe the NES controller
2013-03-11 23:14:30 -06:00
*/
2013-03-15 23:17:38 -06:00
uint8_t
nesprobe()
2013-03-11 23:14:30 -06:00
{
2013-03-15 23:17:38 -06:00
int i;
uint8_t state = 0;
2013-03-31 20:10:04 -06:00
nesltch(true);
nesltch(false);
2013-03-11 23:14:30 -06:00
2013-03-15 23:17:38 -06:00
for (i = 0; i < 8; i += 1) {
state <<= 1;
2013-03-31 20:10:04 -06:00
if (nesout()) {
2013-03-15 23:17:38 -06:00
// Button not pressed
} else {
state |= 1;
}
2013-03-31 20:10:04 -06:00
nesclk(true);
nesclk(false);
2013-03-11 23:14:30 -06:00
}
2013-03-17 23:42:17 -06:00
// Only report button down events.
return state;
2013-03-15 23:17:38 -06:00
}
void
update_controller()
{
static uint8_t last_val = 0;
uint8_t cur;
uint8_t pressed;
2013-03-30 20:03:10 -06:00
int inc = 1;
cur = nesprobe();
pressed = (last_val ^ cur) & cur;
last_val = cur;
2013-03-15 23:17:38 -06:00
if ((pressed & BTN_A) && ((state != JAM) || (jam_clock == 0))) {
2013-03-30 20:03:10 -06:00
state = JAM;
2013-03-31 16:25:43 -06:00
jam_clock = jam_duration;
2013-03-11 23:14:30 -06:00
}
2013-03-17 23:42:17 -06:00
if ((pressed & BTN_B) && ((state != LINEUP) || (jam_clock == 0))) {
2013-03-30 20:03:10 -06:00
state = LINEUP;
2013-03-31 16:25:43 -06:00
jam_clock = lineup_duration;
2013-03-30 20:03:10 -06:00
}
if ((pressed & BTN_START) && (state != TIMEOUT)) {
2013-03-30 20:03:10 -06:00
state = TIMEOUT;
jam_clock = 1;
}
if (cur & BTN_SELECT) {
2013-03-30 20:03:10 -06:00
inc = -1;
// XXX: if in timeout, select digit to adjust
2013-03-17 23:42:17 -06:00
}
2013-03-27 21:02:14 -06:00
if (pressed & BTN_LEFT) {
2013-03-30 20:03:10 -06:00
score_a += inc;
2013-03-27 21:02:14 -06:00
}
if (pressed & BTN_RIGHT) {
2013-03-30 20:03:10 -06:00
score_b += inc;
}
2013-03-15 23:17:38 -06:00
}
/*
2013-03-30 20:03:10 -06:00
*
* Main program
*
2013-03-15 23:17:38 -06:00
*/
2013-03-30 20:03:10 -06:00
2013-03-11 23:14:30 -06:00
2013-03-30 20:03:10 -06:00
void
setup()
2013-02-26 22:49:02 -07:00
{
2013-03-30 20:03:10 -06:00
int i;
2013-03-11 23:14:30 -06:00
2013-03-30 20:03:10 -06:00
// Datasheet says you have to do this before DC initialization.
// In practice it doesn't seem to matter, but what the hey.
draw();
2013-03-11 17:14:29 -06:00
2013-03-30 20:03:10 -06:00
// Initialize dot correction logic
mode(true);
sin(true);
for (i = 0; i < nsr * 96; i += 1) {
pulse();
}
latch();
mode(false);
}
void
loop()
{
uint32_t i;
2013-03-29 22:39:44 -06:00
2013-03-30 20:03:10 -06:00
if (tick) {
tick = false;
2013-03-27 22:47:17 -06:00
2013-03-29 22:39:44 -06:00
update_controller();
2013-03-15 23:17:38 -06:00
2013-03-30 20:03:10 -06:00
if (jiffies % (JIFFIES_PER_SECOND / 10) == 0) {
2013-05-04 19:46:18 -06:00
PORTB ^= 0xff;
2013-03-30 20:03:10 -06:00
switch (state) {
case SETUP:
break;
case JAM:
case LINEUP:
2013-03-30 20:03:10 -06:00
if (period_clock) {
period_clock += 1;
}
// fall through
case TIMEOUT:
2013-03-30 20:03:10 -06:00
if (jam_clock) {
jam_clock += 1;
}
2013-03-11 23:14:30 -06:00
}
2013-03-30 20:03:10 -06:00
draw();
2013-03-07 23:55:05 -07:00
}
2013-02-26 22:49:02 -07:00
}
}
2013-03-30 20:03:10 -06:00
int
main(void)
2013-03-07 23:55:05 -07:00
{
2013-03-30 20:03:10 -06:00
init();
setup();
for (;;) {
loop();
2013-03-29 22:39:44 -06:00
}
2013-03-30 20:03:10 -06:00
return 0;
2013-03-07 23:55:05 -07:00
}
2013-03-30 20:03:10 -06:00