hw-rollerderby-scoreboard/main.c

286 lines
4.1 KiB
C
Raw Normal View History

2013-03-07 23:55:05 -07:00
#include <msp430.h>
2013-02-26 22:49:02 -07:00
#include <stdint.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-11 23:14:30 -06:00
uint16_t period_clock = 600 * 30;
uint16_t jam_clock = 600 * 2;
enum {
SETUP,
JAM,
LINEUP,
TIMEOUT
} state = JAM;
2013-02-26 22:49:02 -07:00
2013-03-15 23:17:38 -06:00
uint8_t controller = 0;
2013-02-26 22:49:02 -07:00
2013-03-11 17:14:29 -06:00
#define MODE BIT0
#define SIN BIT1
#define SCLK BIT2
2013-03-15 23:17:38 -06:00
#define XLAT BIT3
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-15 23:17:38 -06:00
#define NESCLK BIT4
#define NESLTCH BIT5
#define NESSOUT BIT7
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-15 23:17:38 -06:00
#if defined(WIKIPEDIA)
2013-03-11 23:14:30 -06:00
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71
2013-03-15 23:17:38 -06:00
#elseif defined(TOPDOWN)
2013-03-11 23:14:30 -06:00
0x7e, 0x48, 0x3d, 0x6d, 0x4b, 0x67, 0x77, 0x4c, 0x7f, 0x6f,
2013-03-15 23:17:38 -06:00
#else
0x7b, 0x09, 0xb3, 0x9b, 0xc9, 0xda, 0xfa, 0x0b, 0xfb, 0xdb
2013-03-11 23:14:30 -06:00
#endif
};
2013-03-11 20:17:09 -06:00
2013-03-11 17:14:29 -06:00
#define mode(on) bit(P1OUT, MODE, on)
#define sin(on) bit(P1OUT, SIN, on)
#define sclk(on) bit(P1OUT, SCLK, on)
#define xlat(on) bit(P1OUT, XLAT, on)
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));
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
}
}
2013-03-11 17:14:29 -06:00
void
blip()
{
int i;
2013-03-15 23:17:38 -06:00
for (i = 0; i < 12; i += 1) {
__delay_cycles(1);
2013-03-11 17:14:29 -06:00
}
}
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
#if 1
uint16_t clk;
write_num(score_a, 3);
clk = ((period_clock / 10) / 60) * 100;
clk = clk + ((period_clock / 10) % 60);
write_num(clk, 4);
2013-03-11 23:14:30 -06:00
2013-03-15 23:17:38 -06:00
clk = ((jam_clock / 600) % 10) * 1000;
clk = clk + (jam_clock % 600);
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);
#else
int i;
2013-03-11 23:14:30 -06:00
2013-03-15 23:17:38 -06:00
for (i = 0; i < 12; i += 1) {
write_num(sizeof(seven_segment_digits) - 1, 1);
}
#endif
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;
P1OUT |= NESLTCH;
P1OUT &= ~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;
if (P1IN & NESSOUT) {
// Button not pressed
} else {
state |= 1;
}
P1OUT |= NESCLK;
P1OUT &= ~NESCLK;
2013-03-11 23:14:30 -06:00
}
2013-03-15 23:17:38 -06:00
return state;
}
void
update_controller()
{
uint8_t val = nesprobe();
if (val & 0x80) {
2013-03-11 23:14:30 -06:00
switch (state) {
case JAM:
jam_clock = 300;
state = LINEUP;
break;
default:
jam_clock = 600 * 2;
state = JAM;
break;
}
}
2013-03-15 23:17:38 -06:00
}
/*
* Run logic for this decisecond
*/
void
loop()
{
if (jam_clock) {
jam_clock -= 1;
}
if (period_clock) {
period_clock -= 1;
}
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-07 23:55:05 -07:00
WDTCTL = WDTPW + WDTHOLD; // Disable Watchdog Timer
2013-03-15 23:17:38 -06:00
P1DIR |= MODE + SIN + SCLK + XLAT + NESCLK + NESLTCH + BIT6; // P1 output bits
P1DIR &= ~(NESSOUT);
2013-03-11 17:14:29 -06:00
P1OUT = 0;
2013-03-11 18:28:25 -06:00
setup_gs();
setup_dc();
2013-03-11 17:14:29 -06:00
2013-03-11 18:28:25 -06:00
// Enable interrupts
2013-03-15 23:17:38 -06:00
CCTL0 |= CCIE; // Trigger interrupt on A checkpoint
2013-03-07 23:55:05 -07:00
TACTL = TASSEL_2 + MC_1; // Set timer A to SMCLCK, up mode
2013-03-11 18:28:25 -06:00
TACCR0 = 0x4444; // Interrupt 60 times per second
2013-03-07 23:55:05 -07:00
__enable_interrupt();
2013-03-11 17:14:29 -06:00
// Now actually run
2013-02-26 22:49:02 -07:00
for (;;) {
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();
P1OUT ^= BIT6;
}
2013-03-07 23:55:05 -07:00
}
2013-02-26 22:49:02 -07:00
}
}
2013-03-07 23:55:05 -07:00
// Timer A0 interrupt service routine
__attribute__((interrupt(TIMER0_A0_VECTOR)))
void timer_a(void)
{
2013-03-11 23:14:30 -06:00
tick = true;
2013-03-07 23:55:05 -07:00
}