hw-rollerderby-scoreboard/main.c

317 lines
4.3 KiB
C
Raw Normal View History

2013-03-27 22:47:17 -06:00
#include <avr/io.h>
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-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-11 23:14:30 -06:00
volatile bool tick = false; // Set high when clock ticks
uint16_t time = 0; // Tenths of a second elapsed since boot
// Clocks are in deciseconds
2013-03-15 23:17:38 -06:00
uint16_t score_a = 0;
uint16_t score_b = 0;
2013-03-17 23:42:17 -06:00
int16_t period_clock = -600 * 30;
int16_t jam_clock = -600 * 2;
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-17 23:42:17 -06:00
uint8_t last_controller = 0;
2013-03-15 23:17:38 -06:00
2013-02-26 22:49:02 -07:00
2013-03-27 22:47:17 -06:00
#define MODE _BV(0)
#define SIN _BV(1)
#define SCLK _BV(2)
#define XLAT _BV(3)
2013-03-11 23:14:30 -06:00
// Connect GSCLK to SCLK
// Connect BLANK to XLAT
// TRUST ME, THIS TOTALLY WORKS
2013-03-11 17:14:29 -06:00
2013-03-27 22:47:17 -06:00
#define NESCLK _BV(4)
#define NESLTCH _BV(5)
#define NESSOUT _BV(6)
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-17 23:42:17 -06:00
2013-03-11 17:14:29 -06:00
#define bit(pin, bit, on) pin = (on ? (pin | bit) : (pin & ~bit))
2013-03-11 23:14:30 -06:00
const uint8_t seven_segment_digits[] = {
2013-03-17 23:42:17 -06:00
0x7b, 0x09, 0xb3, 0x9b, 0xc9, 0xda, 0xfa, 0x0b, 0xfb, 0xdb,
2013-03-11 23:14:30 -06:00
};
2013-03-11 20:17:09 -06:00
2013-03-27 22:47:17 -06:00
#define mode(on) bit(PORTD, MODE, on)
#define sin(on) bit(PORTD, SIN, on)
#define sclk(on) bit(PORTD, SCLK, on)
#define xlat(on) bit(PORTD, XLAT, on)
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-15 23:17:38 -06:00
for (i = 1; i < digits; i += 1) {
divisor *= 10;
}
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]);
divisor /= 10;
2013-03-11 18:28:25 -06:00
}
}
/* Set up grayscale */
void
setup_gs()
{
int i;
for (i = 0; i < nsr; i += 1) {
write(0);
}
latch();
}
/*
* Set up dot correction.
*
* We don't use dot correction so this is easy: set everything to full brightness.
*/
void
setup_dc()
{
int i;
mode(true);
sin(true);
for (i = 0; i < nsr * 96; i += 1) {
pulse();
}
latch();
mode(false);
}
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;
write_num(score_a, 3);
2013-03-17 23:42:17 -06:00
if ((state == TIMEOUT) && (jam_clock % 8 == 0)) {
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
2013-03-17 23:42:17 -06:00
clk = (abs(jam_clock / 600) % 10) * 1000;
clk += abs(jam_clock) % 600;
2013-03-15 23:17:38 -06:00
write_num(clk, 4);
2013-03-11 23:14:30 -06:00
2013-03-15 23:17:38 -06:00
write_num(score_b, 2);
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-17 23:42:17 -06:00
uint8_t ret = 0;
2013-03-15 23:17:38 -06:00
2013-03-27 22:47:17 -06:00
PORTD |= NESLTCH;
PORTD &= ~NESLTCH;
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-27 22:47:17 -06:00
if (PIND & NESSOUT) {
2013-03-15 23:17:38 -06:00
// Button not pressed
} else {
state |= 1;
}
2013-03-27 22:47:17 -06:00
PORTD |= NESCLK;
PORTD &= ~NESCLK;
2013-03-11 23:14:30 -06:00
}
2013-03-17 23:42:17 -06:00
// Only report button down events.
ret = (last_controller ^ state) & state;
last_controller = state;
return ret;
2013-03-15 23:17:38 -06:00
}
void
update_controller()
{
uint8_t val = nesprobe();
2013-03-17 23:42:17 -06:00
if (val & BTN_A) {
2013-03-11 23:14:30 -06:00
switch (state) {
case JAM:
2013-03-17 23:42:17 -06:00
jam_clock = -300;
2013-03-11 23:14:30 -06:00
state = LINEUP;
break;
default:
2013-03-17 23:42:17 -06:00
jam_clock = -600 * 2;
2013-03-11 23:14:30 -06:00
state = JAM;
break;
}
}
2013-03-17 23:42:17 -06:00
if (val & BTN_START) {
switch (state) {
case TIMEOUT:
break;
default:
state = TIMEOUT;
jam_clock = 1;
}
}
2013-03-27 21:02:14 -06:00
if (val & BTN_LEFT) {
score_a += 1;
}
if (val & BTN_RIGHT) {
score_b += 1;
}
2013-03-15 23:17:38 -06:00
}
/*
* Run logic for this decisecond
*/
void
loop()
{
2013-03-17 23:42:17 -06:00
switch (state) {
case SETUP:
break;
default:
if (jam_clock) {
jam_clock += 1;
}
2013-03-15 23:17:38 -06:00
}
2013-03-17 23:42:17 -06:00
switch (state) {
case SETUP:
case TIMEOUT:
break;
default:
if (period_clock) {
period_clock += 1;
}
2013-03-15 23:17:38 -06:00
}
2013-03-11 23:14:30 -06:00
draw();
}
2013-02-26 22:49:02 -07:00
int
2013-03-07 23:55:05 -07:00
main(void)
2013-02-26 22:49:02 -07:00
{
2013-03-15 23:17:38 -06:00
uint16_t jiffies = 0;
2013-03-11 23:14:30 -06:00
2013-03-27 22:47:17 -06:00
DDRD = ~(NESSOUT);
DDRB = 0xff;
2013-03-11 17:14:29 -06:00
2013-03-27 22:47:17 -06:00
PORTD = 0;
2013-03-11 17:14:29 -06:00
2013-03-17 23:42:17 -06:00
//setup_gs();
2013-03-11 18:28:25 -06:00
setup_dc();
2013-03-11 17:14:29 -06:00
// Now actually run
2013-02-26 22:49:02 -07:00
for (;;) {
2013-03-27 22:47:17 -06:00
uint32_t i;
2013-03-15 23:17:38 -06:00
update_controller();
2013-03-11 23:14:30 -06:00
if (tick) {
tick = false;
jiffies += 1;
if (jiffies == 6) {
jiffies = 0;
time += 1;
loop();
2013-03-27 22:47:17 -06:00
PORTB ^= 0xff;
2013-03-11 23:14:30 -06:00
}
2013-03-07 23:55:05 -07:00
}
2013-03-27 22:47:17 -06:00
// XXX: fix timer_a
for (i = 0; i < 20000; i += 1) {
tick = !tick;
}
tick = true;
2013-02-26 22:49:02 -07:00
}
}
2013-03-27 22:47:17 -06:00
// XXX: reenable this
2013-03-07 23:55:05 -07:00
// Timer A0 interrupt service routine
2013-03-27 22:47:17 -06:00
//__attribute__((interrupt(TIMER0_A0_VECTOR)))
2013-03-07 23:55:05 -07:00
void timer_a(void)
{
2013-03-11 23:14:30 -06:00
tick = true;
2013-03-07 23:55:05 -07:00
}