rgbyteclock-code/util.c

105 lines
1.5 KiB
C
Raw Permalink Normal View History

2014-03-15 05:47:41 +01:00
#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;
}