Simon the game
This commit is contained in:
		
							
								
								
									
										4
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								Makefile
									
									
									
									
									
								
							@@ -1,5 +1,5 @@
 | 
				
			|||||||
PRG            = rgbyteclock
 | 
					PRG            = rgbyteclock
 | 
				
			||||||
OBJ            = rgbyteclock.o timer.o lcd.o main.o rtc.o spi.o ringbuffer.o crc.o ledcontroller.o
 | 
					OBJ            = rgbyteclock.o timer.o lcd.o main.o rtc.o spi.o ringbuffer.o crc.o ledcontroller.o util.o
 | 
				
			||||||
MCU_TARGET     = atmega164a
 | 
					MCU_TARGET     = atmega164a
 | 
				
			||||||
OPTIMIZE       = -Os
 | 
					OPTIMIZE       = -Os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -25,7 +25,7 @@ $(PRG).elf: $(OBJ)
 | 
				
			|||||||
	@echo
 | 
						@echo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# dependency:
 | 
					# dependency:
 | 
				
			||||||
rgbyteclock.o: timer.o lcd.o rgbyteclock.h rtc.o spi.o ledcontroller.o
 | 
					rgbyteclock.o: timer.o lcd.o rgbyteclock.h rtc.o spi.o ledcontroller.o util.o
 | 
				
			||||||
main.o: rgbyteclock.o lcd.o spi.o ledcontroller.o rtc.o timer.o 
 | 
					main.o: rgbyteclock.o lcd.o spi.o ledcontroller.o rtc.o timer.o 
 | 
				
			||||||
timer.o: timer.h
 | 
					timer.o: timer.h
 | 
				
			||||||
lcd.o: lcd.h timer.o
 | 
					lcd.o: lcd.h timer.o
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										409
									
								
								rgbyteclock.c
									
									
									
									
									
								
							
							
						
						
									
										409
									
								
								rgbyteclock.c
									
									
									
									
									
								
							@@ -1,4 +1,3 @@
 | 
				
			|||||||
 | 
					 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 * ----------------------------------------------------------------------------
 | 
					 * ----------------------------------------------------------------------------
 | 
				
			||||||
 * "THE BEER-WARE LICENSE" (Revision 42):
 | 
					 * "THE BEER-WARE LICENSE" (Revision 42):
 | 
				
			||||||
@@ -13,53 +12,391 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include "rgbyteclock.h"
 | 
					#include "rgbyteclock.h"
 | 
				
			||||||
#include "timer.h"
 | 
					#include "timer.h"
 | 
				
			||||||
#include "lcd.h"
 | 
					 | 
				
			||||||
#include "ledcontroller.h"
 | 
					#include "ledcontroller.h"
 | 
				
			||||||
 | 
					#include "util.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define SEED_RNG  0
 | 
				
			||||||
 | 
					#define INIT      1
 | 
				
			||||||
 | 
					#define INTRO     2
 | 
				
			||||||
 | 
					#define INTRODUCE_COLORS  3
 | 
				
			||||||
 | 
					#define SHOW_SEQUENCE     4
 | 
				
			||||||
 | 
					#define GET_USER_SEQUENCE 5
 | 
				
			||||||
 | 
					#define USER_GUESS_OK   6
 | 
				
			||||||
 | 
					#define USER_GUESS_NOK  7
 | 
				
			||||||
 | 
					#define EXTEND_SEQUENCE 8
 | 
				
			||||||
 | 
					#define WIN 9
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int8_t show_intro( uint32_t now, uint32_t animation_start );
 | 
				
			||||||
 | 
					int8_t show_introduce_colors( uint32_t now, uint32_t animation_start );
 | 
				
			||||||
 | 
					void show_anim_wait_seed( uint32_t now, uint32_t animation_start );
 | 
				
			||||||
 | 
					int8_t show_anim_ok( uint32_t now, uint32_t animation_start );
 | 
				
			||||||
 | 
					int8_t show_anim_nok( uint32_t now, uint32_t animation_start );
 | 
				
			||||||
 | 
					int8_t get_sequence( uint32_t now, uint32_t animation_start );
 | 
				
			||||||
 | 
					int8_t show_anim_win( uint32_t now, uint32_t animation_start );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void rgbyteclock(void)
 | 
					static uint8_t sequence[12];
 | 
				
			||||||
{
 | 
					static uint8_t sequence_index;
 | 
				
			||||||
 | 
					static uint8_t input_sequence_index;
 | 
				
			||||||
 | 
					static uint8_t colorScheme[2];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void rgbyteclock(void) {
 | 
				
			||||||
 | 
						uint32_t now, t1;
 | 
				
			||||||
 | 
						uint16_t r,g,b; // colors
 | 
				
			||||||
 | 
						int8_t end;
 | 
				
			||||||
 | 
						uint8_t button = BUTTON_NONE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						uint8_t gameState = SEED_RNG;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ledcontroller_turn_all_on();
 | 
						ledcontroller_turn_all_on();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						DDRC &= ~(1<<PC5); // use pullup
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	while ( 1 ) {
 | 
						while ( 1 ) {
 | 
				
			||||||
    //PORTC ^= (1<<PC5);
 | 
							now = timer_get( );
 | 
				
			||||||
    for (int i=0; i<14; i++) {
 | 
					
 | 
				
			||||||
      led_set(i, 0x7ff, 0x00, 0x00);
 | 
							switch( gameState ) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								case SEED_RNG:
 | 
				
			||||||
 | 
									// get seed for RNG from button press
 | 
				
			||||||
 | 
									show_anim_wait_seed( now, t1 );
 | 
				
			||||||
 | 
									button = get_button( );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if( button != BUTTON_NONE ) {
 | 
				
			||||||
 | 
										init_random( (uint16_t)(now - t1));
 | 
				
			||||||
 | 
										gameState = INIT;
 | 
				
			||||||
 | 
										t1 = now;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								case INIT:
 | 
				
			||||||
 | 
									PORTC &= ~(1<<PC5);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									sequence_index = 0;
 | 
				
			||||||
 | 
									input_sequence_index = 0;
 | 
				
			||||||
 | 
									gameState = INTRO;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									colorScheme[0] = (random()%6) +1;
 | 
				
			||||||
 | 
									colorScheme[1] = colorScheme[0];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									while( colorScheme[0] == colorScheme[1] ) {
 | 
				
			||||||
 | 
										colorScheme[1] = (random()%6) +1;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									// all buttons dark
 | 
				
			||||||
 | 
									for( int i=12; i<14; i++ ) {
 | 
				
			||||||
 | 
										led_set( i, 0,0,0 );
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									t1 = now;
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								case INTRO:
 | 
				
			||||||
 | 
									// show a nice animation to indicate the start of the game
 | 
				
			||||||
 | 
									end = show_intro( now, t1 );
 | 
				
			||||||
 | 
									if( end ) {
 | 
				
			||||||
 | 
										gameState = INTRODUCE_COLORS;
 | 
				
			||||||
 | 
										t1 = now;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								case INTRODUCE_COLORS:
 | 
				
			||||||
 | 
									end = show_introduce_colors( now, t1 );
 | 
				
			||||||
 | 
									if( end ) {
 | 
				
			||||||
 | 
										gameState = EXTEND_SEQUENCE;
 | 
				
			||||||
 | 
										t1 = now;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								case SHOW_SEQUENCE:
 | 
				
			||||||
 | 
									// TODO: animation that shows sequence color after color
 | 
				
			||||||
 | 
									for( int i=0; i<sequence_index; i++ ) {
 | 
				
			||||||
 | 
										get_color( &r, &g, &b, sequence[i] );
 | 
				
			||||||
 | 
										led_set( i, r, g, b );
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									// display last color for 2 seconds
 | 
				
			||||||
 | 
									if( now - t1 > 2000 || (get_button())) {
 | 
				
			||||||
 | 
										// hide sequence
 | 
				
			||||||
 | 
										for( int i=0; i<12; i++ ) {
 | 
				
			||||||
 | 
											get_color( &r, &g, &b, COLOR_BLACK );
 | 
				
			||||||
 | 
											led_set( i, r, g, b);
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
										gameState = GET_USER_SEQUENCE;
 | 
				
			||||||
 | 
										t1 = now;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								case GET_USER_SEQUENCE:
 | 
				
			||||||
 | 
									end = get_sequence( now, t1 );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if( end == 1 ) {
 | 
				
			||||||
 | 
										gameState = USER_GUESS_NOK;
 | 
				
			||||||
 | 
										t1 = now;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if( end == 2 ) {
 | 
				
			||||||
 | 
										gameState = USER_GUESS_OK;
 | 
				
			||||||
 | 
										t1 = now;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								case USER_GUESS_OK:
 | 
				
			||||||
 | 
									end = show_anim_ok( now, t1 );
 | 
				
			||||||
 | 
									if( end ) {
 | 
				
			||||||
 | 
										gameState = EXTEND_SEQUENCE;
 | 
				
			||||||
 | 
										t1 = now;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								case USER_GUESS_NOK:
 | 
				
			||||||
 | 
									end = show_anim_nok( now, t1 );
 | 
				
			||||||
 | 
									if( end ) {
 | 
				
			||||||
 | 
										gameState = INIT;
 | 
				
			||||||
 | 
										t1 = now;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								case EXTEND_SEQUENCE:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if(sequence_index >= 12) {
 | 
				
			||||||
 | 
										gameState = WIN;
 | 
				
			||||||
 | 
										continue;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									else {
 | 
				
			||||||
 | 
										gameState = SHOW_SEQUENCE;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if( random()&1 ) {
 | 
				
			||||||
 | 
										sequence[sequence_index] = colorScheme[0];
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									else {
 | 
				
			||||||
 | 
										sequence[sequence_index] = colorScheme[1];
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									sequence_index++;
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								case WIN:
 | 
				
			||||||
 | 
									end = show_anim_win( now, t1 );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if( end ) {
 | 
				
			||||||
 | 
										gameState = INIT;
 | 
				
			||||||
 | 
										t1 = now;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		led_flush( );
 | 
							led_flush( );
 | 
				
			||||||
      timer_wait(100);
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
    /* for (int i=0; i<14; i++) { */
 | 
					 | 
				
			||||||
    /*   led_set(i, 0x00, 0x00, 0x00); */
 | 
					 | 
				
			||||||
    /* } */
 | 
					 | 
				
			||||||
    /* led_flush(); */
 | 
					 | 
				
			||||||
    ledcontroller_clear();
 | 
					 | 
				
			||||||
    timer_wait(100);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for (int i=0; i<14; i++) {
 | 
					 | 
				
			||||||
      led_set(i, 0x00, 0x7ff, 0x00);
 | 
					 | 
				
			||||||
      led_flush();
 | 
					 | 
				
			||||||
      timer_wait(100);
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
    /* for (int i=0; i<14; i++) { */
 | 
					 | 
				
			||||||
    /*   led_set(i, 0x00, 0x00, 0x00); */
 | 
					 | 
				
			||||||
    /* } */
 | 
					 | 
				
			||||||
    /* led_flush(); */
 | 
					 | 
				
			||||||
    ledcontroller_clear();
 | 
					 | 
				
			||||||
    timer_wait(100);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (int i=0; i<14; i++) {
 | 
					
 | 
				
			||||||
      led_set(i, 0x00, 0x00, 0x7ff);
 | 
					
 | 
				
			||||||
      led_flush();
 | 
					int8_t get_sequence( uint32_t now, uint32_t animation_start ) {
 | 
				
			||||||
      timer_wait(100);
 | 
						uint16_t r,g,b;
 | 
				
			||||||
 | 
						uint8_t button;
 | 
				
			||||||
 | 
						uint8_t color;
 | 
				
			||||||
 | 
						uint8_t guessOK;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						button = get_button( );
 | 
				
			||||||
 | 
						color = sequence[input_sequence_index];
 | 
				
			||||||
 | 
						guessOK = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( button == BUTTON_RO ) {
 | 
				
			||||||
 | 
							if( color == colorScheme[0] ) {
 | 
				
			||||||
 | 
								guessOK = 1;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
    /* for (int i=0; i<14; i++) { */
 | 
						}
 | 
				
			||||||
    /*   led_set(i, 0x00, 0x00, 0x00); */
 | 
						else if( button == BUTTON_LO ) {
 | 
				
			||||||
    /* } */
 | 
							if( color == colorScheme[1] ) {
 | 
				
			||||||
    /* led_flush(); */
 | 
								guessOK = 1;
 | 
				
			||||||
    ledcontroller_clear();
 | 
					 | 
				
			||||||
    timer_wait(100);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						else {
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( guessOK ) {
 | 
				
			||||||
 | 
							// draw what user entered until now
 | 
				
			||||||
 | 
							get_color( &r, &g, &b, color );
 | 
				
			||||||
 | 
							led_set( input_sequence_index, r, g, b );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							input_sequence_index++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if( input_sequence_index >= sequence_index) {
 | 
				
			||||||
 | 
								input_sequence_index = 0;
 | 
				
			||||||
 | 
								return 2; // entire sequence right
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return 0; // right but not finished
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 1; // user guessed wrong
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
						##############
 | 
				
			||||||
 | 
						# ANIMATIONS #
 | 
				
			||||||
 | 
						##############
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int8_t show_introduce_colors( uint32_t now, uint32_t animation_start ) {
 | 
				
			||||||
 | 
						uint32_t t_diff = now - animation_start;
 | 
				
			||||||
 | 
						uint16_t r,g,b;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// all ring LEDs off
 | 
				
			||||||
 | 
						for( int i=0; i<12; i++ ) {
 | 
				
			||||||
 | 
							led_set( i, 0,0,0 );
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( t_diff%100 < 50 ) {
 | 
				
			||||||
 | 
							// put gamecolors on the buttons
 | 
				
			||||||
 | 
							get_color( &r, &g, &b, colorScheme[0] );
 | 
				
			||||||
 | 
							led_set( 12, r, g, b); // RO
 | 
				
			||||||
 | 
							get_color( &r, &g, &b, colorScheme[1] );
 | 
				
			||||||
 | 
							led_set( 13, r, g, b); // LO
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						else {
 | 
				
			||||||
 | 
							for( int i=12; i<14; i++ ) {
 | 
				
			||||||
 | 
								get_color( &r, &g, &b, COLOR_BLACK );
 | 
				
			||||||
 | 
								led_set( i, r,g,b );
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( t_diff > 1000 ) {
 | 
				
			||||||
 | 
							return 1;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void show_anim_wait_seed( uint32_t now, uint32_t animation_start ) {
 | 
				
			||||||
 | 
						uint32_t t_diff = (now - animation_start)%1000;
 | 
				
			||||||
 | 
						uint16_t w;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( t_diff < 500 ) {
 | 
				
			||||||
 | 
							// fade up
 | 
				
			||||||
 | 
							w = (MAX_BRIGHTNESS/500) * t_diff;
 | 
				
			||||||
 | 
							for( int i=0; i<12; i++ ) {
 | 
				
			||||||
 | 
								led_set( i, w, w, w);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						else {
 | 
				
			||||||
 | 
							// fade down
 | 
				
			||||||
 | 
							w = MAX_BRIGHTNESS - (MAX_BRIGHTNESS/500) * (t_diff-500);
 | 
				
			||||||
 | 
							for( int i=0; i<12; i++ ) {
 | 
				
			||||||
 | 
								led_set( i, w, w, w);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int8_t show_intro( uint32_t now, uint32_t animation_start ) {
 | 
				
			||||||
 | 
						// Animation: rotate a single LED once across the circle
 | 
				
			||||||
 | 
						uint32_t t_diff = now - animation_start;
 | 
				
			||||||
 | 
						uint16_t r,g,b;
 | 
				
			||||||
 | 
						uint8_t index;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						index = (t_diff%1200)/100;
 | 
				
			||||||
 | 
						if( !(t_diff % 100) ) {
 | 
				
			||||||
 | 
							// every 100ms
 | 
				
			||||||
 | 
							get_color( &r, &g, &b, (random()%6)+1 );
 | 
				
			||||||
 | 
							led_set( index, r, g, b );
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// next LED in line is dark to mark the update spot better
 | 
				
			||||||
 | 
						index++;
 | 
				
			||||||
 | 
						led_set( index%12, 0,0,0 );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( now - animation_start > 2400 ) {
 | 
				
			||||||
 | 
							return 1;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int8_t show_anim_ok( uint32_t now, uint32_t animation_start ) {
 | 
				
			||||||
 | 
						uint32_t t_diff = now - animation_start;
 | 
				
			||||||
 | 
						uint16_t g;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( t_diff < 500 ) {
 | 
				
			||||||
 | 
							g = (MAX_BRIGHTNESS/500) * t_diff;
 | 
				
			||||||
 | 
							for( int i=0; i<12; i++ ) {
 | 
				
			||||||
 | 
								led_set( i, 0x00, g, 0x00);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						else {
 | 
				
			||||||
 | 
							g = MAX_BRIGHTNESS - (MAX_BRIGHTNESS/500) * (t_diff-500);
 | 
				
			||||||
 | 
							for( int i=0; i<12; i++ ) {
 | 
				
			||||||
 | 
								led_set( i, 0x00, g, 0x00);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( t_diff > 1000 ) {
 | 
				
			||||||
 | 
							return 1;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int8_t show_anim_nok( uint32_t now, uint32_t animation_start ) {
 | 
				
			||||||
 | 
						uint32_t t_diff = now - animation_start;
 | 
				
			||||||
 | 
						uint16_t r,g,b;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( t_diff%100 < 50 ) {
 | 
				
			||||||
 | 
							for( int i=0; i<12; i++ ) {
 | 
				
			||||||
 | 
								get_color( &r, &g, &b, COLOR_RED );
 | 
				
			||||||
 | 
								led_set( i, r,g,b );
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						else {
 | 
				
			||||||
 | 
							for( int i=0; i<12; i++ ) {
 | 
				
			||||||
 | 
								get_color( &r, &g, &b, COLOR_BLACK );
 | 
				
			||||||
 | 
								led_set( i, r,g,b );
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( t_diff > 1000 ) {
 | 
				
			||||||
 | 
							return 1;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int8_t show_anim_win( uint32_t now, uint32_t animation_start ) {
 | 
				
			||||||
 | 
						uint32_t t_diff = now - animation_start;
 | 
				
			||||||
 | 
						uint16_t b;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( t_diff < 1000 ) {
 | 
				
			||||||
 | 
							b = (MAX_BRIGHTNESS/1000) * t_diff;
 | 
				
			||||||
 | 
							for( int i=0; i<12; i++ ) {
 | 
				
			||||||
 | 
								led_set( i, 0x00, 0x00, b);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						else {
 | 
				
			||||||
 | 
							b = MAX_BRIGHTNESS - (MAX_BRIGHTNESS/1000) * (t_diff-1000);
 | 
				
			||||||
 | 
							for( int i=0; i<12; i++ ) {
 | 
				
			||||||
 | 
								led_set( i, 0x00, 0x00, b);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( t_diff > 2000 ) {
 | 
				
			||||||
 | 
							return 1;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										104
									
								
								util.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								util.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,104 @@
 | 
				
			|||||||
 | 
					#include <avr/io.h>
 | 
				
			||||||
 | 
					#include <stdint.h>
 | 
				
			||||||
 | 
					#include "util.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static volatile uint16_t s_lfsr=0xACE1u;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void init_random( uint16_t lfsr ) {
 | 
				
			||||||
 | 
						s_lfsr = lfsr;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					uint16_t random( void ) {
 | 
				
			||||||
 | 
						uint8_t bit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						bit  = ((s_lfsr >> 0) ^ (s_lfsr >> 2) ^ (s_lfsr >> 3) ^ (s_lfsr >> 5) ) & 1;
 | 
				
			||||||
 | 
						s_lfsr =  (s_lfsr >> 1) | (bit << 15);
 | 
				
			||||||
 | 
						return s_lfsr;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void get_color( uint16_t *r, uint16_t *g, uint16_t *b, uint8_t colorIndex ) {
 | 
				
			||||||
 | 
						switch( colorIndex ) {
 | 
				
			||||||
 | 
							case COLOR_RED:
 | 
				
			||||||
 | 
								*r = MAX_BRIGHTNESS;
 | 
				
			||||||
 | 
								*g = 0x00;
 | 
				
			||||||
 | 
								*b = 0x00;
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							case COLOR_GREEN:
 | 
				
			||||||
 | 
								*r = 0x00;
 | 
				
			||||||
 | 
								*g = MAX_BRIGHTNESS;
 | 
				
			||||||
 | 
								*b = 0x00;
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							case COLOR_YELLOW:
 | 
				
			||||||
 | 
								*r = MAX_BRIGHTNESS;
 | 
				
			||||||
 | 
								*g = MAX_BRIGHTNESS;
 | 
				
			||||||
 | 
								*b = 0x00;
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							case COLOR_BLUE:
 | 
				
			||||||
 | 
								*r = 0x00;
 | 
				
			||||||
 | 
								*g = 0x00;
 | 
				
			||||||
 | 
								*b = MAX_BRIGHTNESS;
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							case COLOR_MAGENTA:
 | 
				
			||||||
 | 
								*r = MAX_BRIGHTNESS;
 | 
				
			||||||
 | 
								*g = 0x00;
 | 
				
			||||||
 | 
								*b = MAX_BRIGHTNESS;
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							case COLOR_CYAN:
 | 
				
			||||||
 | 
								*r = 0x00;
 | 
				
			||||||
 | 
								*g = MAX_BRIGHTNESS;
 | 
				
			||||||
 | 
								*b = MAX_BRIGHTNESS;
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							case COLOR_WHITE:
 | 
				
			||||||
 | 
								*r = MAX_BRIGHTNESS;
 | 
				
			||||||
 | 
								*g = MAX_BRIGHTNESS;
 | 
				
			||||||
 | 
								*b = MAX_BRIGHTNESS;
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							default: // black
 | 
				
			||||||
 | 
								*r = 0x00;
 | 
				
			||||||
 | 
								*g = 0x00;
 | 
				
			||||||
 | 
								*b = 0x00;
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					uint8_t get_button( void ) {
 | 
				
			||||||
 | 
						static uint8_t last_button = BUTTON_NONE;
 | 
				
			||||||
 | 
						uint8_t button = BUTTON_NONE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( !(PIND&(1<<PD5)) )
 | 
				
			||||||
 | 
							button = BUTTON_LU;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( !(PIND&(1<<PD6)) )
 | 
				
			||||||
 | 
							button = BUTTON_LO;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( !(PIND&(1<<PD7)) )
 | 
				
			||||||
 | 
							button = BUTTON_RO;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( !(PINB&(1<<PB0)) )
 | 
				
			||||||
 | 
							button = BUTTON_RU;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( button != last_button ) {
 | 
				
			||||||
 | 
							last_button = button;
 | 
				
			||||||
 | 
							return button;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return BUTTON_NONE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										22
									
								
								util.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								util.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
				
			|||||||
 | 
					#define MAX_BRIGHTNESS 0x0800
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define COLOR_BLACK 0
 | 
				
			||||||
 | 
					#define COLOR_RED 1
 | 
				
			||||||
 | 
					#define COLOR_GREEN 2
 | 
				
			||||||
 | 
					#define COLOR_YELLOW 3
 | 
				
			||||||
 | 
					#define COLOR_BLUE 4
 | 
				
			||||||
 | 
					#define COLOR_MAGENTA 5
 | 
				
			||||||
 | 
					#define COLOR_CYAN 6
 | 
				
			||||||
 | 
					#define COLOR_WHITE 7
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define BUTTON_NONE 0
 | 
				
			||||||
 | 
					#define BUTTON_LU 1
 | 
				
			||||||
 | 
					#define BUTTON_LO 2
 | 
				
			||||||
 | 
					#define BUTTON_RU 3
 | 
				
			||||||
 | 
					#define BUTTON_RO 4
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void init_random( uint16_t lfsr );
 | 
				
			||||||
 | 
					uint16_t random( void );
 | 
				
			||||||
 | 
					void get_color( uint16_t *r, uint16_t *g, uint16_t *b, uint8_t colorIndex );
 | 
				
			||||||
 | 
					uint8_t get_button( void );
 | 
				
			||||||
		Reference in New Issue
	
	Block a user