Skip to content

Commit

Permalink
Merge branch 'master' of github.com:c3d2/PentaLight
Browse files Browse the repository at this point in the history
  • Loading branch information
sebseb7 committed Oct 31, 2011
2 parents 7848a00 + 33783d6 commit 03e34f4
Showing 1 changed file with 60 additions and 28 deletions.
88 changes: 60 additions & 28 deletions firmware/apps/replicate.c
@@ -1,43 +1,41 @@
#include <main.h>
#include <stdlib.h>
#include "../libs/font.h"

#define CHAIN_LENGTH 128
#define MAX_CHAIN_LENGTH 128

typedef enum { INIT, SHOW, LISTEN, QUIT } State;
typedef enum { INIT, SHOW, BREAK, LISTEN, SCORE, QUIT } State;

static State state = INIT;
static uint8_t chain[CHAIN_LENGTH];
static uint8_t length = 3;
static uint8_t level = 0;
static uint8_t chain[MAX_CHAIN_LENGTH];
static uint8_t length = 1;
static uint8_t step = 0;
static uint8_t sub_step = 0;

static char score[5] = { ' ', ' ', '0', '0', '0' };

static uint8_t tick() {
uint8_t x, y;

if(state == INIT) {
uint8_t i;
for(i = 0; i < length; i++) chain[i] = rand() & 1;

state = SHOW;
}

if(state == SHOW) {
++sub_step;
if(sub_step < 10) {

uint8_t side = chain[step];
uint8_t color = sub_step < 8 ? 8 - sub_step : 0;

uint8_t x, y;
for(x = 0; x < LED_WIDTH; x++) {
uint8_t c = (x >= LED_WIDTH / 2) ^ side;
for(y = 0; y < LED_HEIGHT; y++)
{
setLedXY(x, y, c ? color : 0);
}
}

uint8_t side = chain[step];
uint8_t color = sub_step < 7 ? 7 - sub_step : 0;

for(x = 0; x < LED_WIDTH; ++x) {
uint8_t c = (x >= LED_WIDTH / 2) ^ side ? color : 0;
for(y = 0; y < LED_HEIGHT; ++y)
setLedXY(x, y, c);
}
else {

++sub_step;
if(sub_step == 10) {
sub_step = 0;
++step;
if(step == length) {
Expand All @@ -46,28 +44,63 @@ static uint8_t tick() {
}
}

}
else if(state == BREAK) {
++sub_step;
if(sub_step == 5) {
sub_step = 0;
state = SHOW;
}
}
else if(state == SCORE) {

for(x = 0; x < LED_WIDTH; ++x) {

uint16_t p = (sub_step / 4) + x;
char c = score[p / 4 % 5];
uint8_t bits = 0;
if((p & 3) < 3) bits = font[c - 32][p & 3];

for(y = 0; y < LED_HEIGHT; ++y) {
setLedXY(x, y, 7 * (bits & 1));
bits >>= 1;
}
}
++sub_step;

}

return state == QUIT;
}

static void key(key_type key, event_type event) {
if(state != LISTEN || event != DOWN)
return;
if(event != DOWN) return;
if(state == SCORE) state = QUIT;
if(state != LISTEN) return;

if(key == chain[step]) {
++step;
sub_step = 0;
if(step == length) {

// wow, you just beat the game!
if(length == MAX_CHAIN_LENGTH) goto DO_SCORE;

chain[step] = rand() & 1;
step = 0;
++length;
++level;
state = SHOW;
state = BREAK;
}
}
else {
// TODO: print level before exiting
state = QUIT;
DO_SCORE:
state = SCORE;
sub_step = 12;
uint8_t i;
for(i = 4; i > 1; --i) {
score[i] = '0' + length % 10;
length /= 10;
}
}
}

Expand All @@ -76,4 +109,3 @@ void init(void) {
registerApp(tick, key, 7);
}


0 comments on commit 03e34f4

Please sign in to comment.