Skip to content

Instantly share code, notes, and snippets.

@rubyist
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubyist/9315605 to your computer and use it in GitHub Desktop.
Save rubyist/9315605 to your computer and use it in GitHub Desktop.
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "light_ws2812.h"
#include <string.h>
#define LEDSPERSEG 1
#define LEDSEGMENTS 7
#define DIGITS 4
#define LEDSPERDIGIT LEDSPERSEG * LEDSEGMENTS
#define LRED 0
#define LBLU 100
#define LGRN 0
// 4 digits * 7 segments * 2 LEDs per segment + 2 serve indicators = 58 LEDs
struct cRGB led[DIGITS * LEDSPERDIGIT]; // just one digit for now
// - A -
// | |
// F B
// | |
// - G -
// | |
// E C
// | |
// - D -
const int segments[10][7] = {
{1, 1, 1, 1, 1, 1, 0}, // 0
{0, 1, 1, 0, 0, 0, 0}, // 1
{1, 1, 0, 1, 1, 0, 1}, // 2
{1, 1, 1, 1, 0, 0, 1}, // 3
{0, 1, 1, 0, 0, 1, 1}, // 4
{1, 0, 1, 1, 0, 1, 1}, // 5
{1, 0, 1, 1, 1, 1, 1}, // 6
{1, 1, 1, 0, 0, 0, 0}, // 7
{1, 1, 1, 1, 1, 1, 1}, // 8
{1, 1, 1, 1, 0, 1, 1} // 9
};
void display_digit(int digit, int pos) {
const int *d = segments[digit];
int start = pos * LEDSPERDIGIT;
int end = start + LEDSPERDIGIT;
int x, c;
for (x = start, c=0; x < end; x += LEDSPERSEG, c++) {
if (d[c]) {
int i;
for (i = 0; i < LEDSPERSEG; i++) {
led[x+i].r=LRED; led[x+i].g=LGRN; led[x+i].b=LBLU;
}
}
}
}
void display_score(int p1, int p2) {
int p1_ones = p1 % 10;
int p1_tens = p1 / 10;
int p2_ones = p2 % 10;
int p2_tens = p2 / 10;
memset(&led, 0, sizeof led);
display_digit(p1_tens, 0);
display_digit(p1_ones, 1);
display_digit(p2_tens, 2);
display_digit(p2_ones, 3);
ws2812_setleds(led, DIGITS*LEDSPERDIGIT);
}
int main(void) {
CLKPR=_BV(CLKPCE);
CLKPR=0;
int n;
while (1) {
for (n = 0; n < 12; n++) {
display_score(n, n);
_delay_ms(1000);
}
}
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment