1D-Pong game based on a LED strip with 150 LPD6803-controlled pixels. Game keeps score for 'best-of-21' game. Written for KL25Z
Diff: main.cpp
- Revision:
- 5:d2e819018807
- Parent:
- 4:119537f0ff8e
- Child:
- 6:c2040b4cf589
--- a/main.cpp Wed Aug 21 18:35:43 2013 +0000 +++ b/main.cpp Wed Aug 21 20:21:43 2013 +0000 @@ -4,15 +4,83 @@ #define PADDLE_LENGTH 5 #define LEFT false #define RIGHT true + +#define MAX_PADDLE_SIZE 10 void Randomblinks(float seconds, bool colored = false); void PaddleDemo(float seconds, uint8_t red, uint8_t green, uint8_t blue); void WinLoose(float seconds, bool side); -void UpdatePaddle(void); +void UpdateDemoPaddle(void); void Score(uint8_t left, uint8_t right); +void DrawGamePaddle(void); + uint16_t totalstrip[NUMBER_OF_PIXELS]; volatile int8_t paddlestart= 0; SPI ledstrip(PTD2,NC,PTD1); +class Paddle +{ + public: + Paddle();//constructor +// ~Paddle();//deconstructor + int8_t position; + uint8_t direction; + void setColor(uint8_t red, uint8_t green, uint8_t blue); + uint8_t getSize(void); + void setSize(uint8_t size); + uint8_t getColor(uint8_t pixel, uint8_t color); + private: + uint8_t m_red ,m_green ,m_blue; + uint8_t m_size; + uint8_t m_paddle[MAX_PADDLE_SIZE][3]; +}; + +Paddle::Paddle() +{ + setColor(255,255,255); + setSize(4); + position = -getSize(); + direction = 1; +} + +uint8_t Paddle::getColor(uint8_t pixel, uint8_t color) +{ + if(pixel<MAX_PADDLE_SIZE && color < 3) + return m_paddle[pixel][color]; + else + return 0; +} + +uint8_t Paddle::getSize(void) +{ + return m_size; +} + +void Paddle::setSize(uint8_t size) +{ + if(size > MAX_PADDLE_SIZE) + size = MAX_PADDLE_SIZE; + m_size = size; + setColor(m_red, m_green, m_blue); +} + +void Paddle::setColor(uint8_t red, uint8_t green, uint8_t blue) +{ + uint8_t paddlepixel; + m_red = red; + m_green = green; + m_blue = blue; + for(paddlepixel = 0 ; paddlepixel < MAX_PADDLE_SIZE ; paddlepixel++) + { + float factor; + factor = 1.0*paddlepixel/(m_size*1.0); + if (factor > 1) + factor = 1; + factor = factor*factor*factor;// make the effect more dramatic + m_paddle[paddlepixel][0] = (float)m_red * factor; + m_paddle[paddlepixel][1] = (float)m_green * factor; + m_paddle[paddlepixel][2] = (float)m_blue * factor; + } +} void UpdateLEDstrip(void) { @@ -32,33 +100,67 @@ { *led = (1<<15) | ((green >> 3)<<10) | ((red >>3)<< 5) | (blue >>3); } + +Paddle paddle; + int main() { Ticker updater; - Ticker paddlepos; + Ticker demopaddlepos; uint8_t ledcounter; updater.attach(UpdateLEDstrip, .02); - paddlepos.attach(UpdatePaddle, .03); + demopaddlepos.attach(UpdateDemoPaddle, .03); ledstrip.format(8,0); //15 bits, mode '0' ledstrip.frequency(1000000); for(ledcounter = 0; ledcounter < NUMBER_OF_PIXELS; ledcounter++) {//turn off leds write_led(&totalstrip[ledcounter], 0,0,0); } + paddle.setSize(6); + paddle.setColor(255,0,255); while(1) { //PaddleDemo(2,255,10,100); - - Score(0,0); - Score(1,1); - Score(0,1); - Score(3,2); - WinLoose(2.5, LEFT); - Score(3,4); - WinLoose(1.5, RIGHT); + DrawGamePaddle(); + wait(.05); + //Score(3,2); + //WinLoose(2.5, LEFT); + if(paddle.direction == 1) + paddle.position++; + else + paddle.position--; + if(paddle.position == NUMBER_OF_PIXELS) + paddle.direction = 0; + if(paddle.position == -7) + paddle.direction = 1; + // paddle.position = -paddle.getSize(); //Randomblinks(5, true); //WinLoose(3, false); } } +void DrawGamePaddle(void) +{ + uint8_t ledcounter; + for(ledcounter = 0; ledcounter< NUMBER_OF_PIXELS; ledcounter++) + { + if(ledcounter >= paddle.position && ledcounter <= paddle.position+paddle.getSize()) + { + uint8_t colorpos; + if(paddle.direction ==1) + { + colorpos = ledcounter-paddle.position; + write_led(&totalstrip[ledcounter],paddle.getColor(colorpos,0),paddle.getColor(colorpos,1),paddle.getColor(colorpos,2)); + } + else + { + colorpos = paddle.getSize()-(ledcounter-paddle.position); + write_led(&totalstrip[ledcounter],paddle.getColor(colorpos,0),paddle.getColor(colorpos,1),paddle.getColor(colorpos,2)); + } + } + else + write_led(&totalstrip[ledcounter], 0,0,0); + } +} + void Score(uint8_t left, uint8_t right) { uint8_t maxscore; @@ -175,7 +277,7 @@ } } -void UpdatePaddle(void) +void UpdateDemoPaddle(void) { static uint8_t direction = 1; if(direction) {