![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
1D-Pong game based on a LED strip with 150 LPD6803-controlled pixels. Game keeps score for 'best-of-21' game. Written for KL25Z
main.cpp
- Committer:
- vsluiter
- Date:
- 2013-08-21
- Revision:
- 5:d2e819018807
- Parent:
- 4:119537f0ff8e
- Child:
- 6:c2040b4cf589
File content as of revision 5:d2e819018807:
#include "mbed.h" #define NUMBER_OF_PIXELS 50 #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 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) { uint8_t pixelcounter; /*start by writing 32 zeroes */ ledstrip.write(0); ledstrip.write(0); ledstrip.write(0); ledstrip.write(0); for(pixelcounter = 0 ; pixelcounter < NUMBER_OF_PIXELS; pixelcounter++) { ledstrip.write( uint8_t(totalstrip[pixelcounter]>>8));//uint8_t(temp16));//(totalstrip.ledcounter[pixelcounter].red << 2) | (totalstrip.ledcounter[pixelcounter].high << 7) |(totalstrip.ledcounter[pixelcounter].green & 0x << 2) ); ledstrip.write( uint8_t(totalstrip[pixelcounter]));//(*(uint16_t *)(&totalstrip[pixelcounter]))>>8); } } void write_led(uint16_t * led, uint8_t red, uint8_t green, uint8_t blue) { *led = (1<<15) | ((green >> 3)<<10) | ((red >>3)<< 5) | (blue >>3); } Paddle paddle; int main() { Ticker updater; Ticker demopaddlepos; uint8_t ledcounter; updater.attach(UpdateLEDstrip, .02); 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); 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; int8_t ledcounter; uint8_t scorecounter; typedef struct ledcolor { uint8_t red; uint8_t green; uint8_t blue; }ledcolor_t; ledcolor_t rightled={0,0,0}, leftled={0,0,0}; if(left >= right) { maxscore = left; leftled.green = 255; rightled.red = 255; } if(right >= left) { maxscore = right; leftled.red = 255; rightled.green = 255; } for(scorecounter = 0 ; scorecounter <= maxscore ; scorecounter++) { uint8_t templeft,tempright; templeft = left>scorecounter?scorecounter:left; tempright = right>scorecounter?scorecounter:right; uint8_t sidecounter; for(ledcounter = 0; ledcounter < NUMBER_OF_PIXELS; ledcounter++) { write_led(&totalstrip[ledcounter], 0, 0 ,0); } for(sidecounter = 0 ; sidecounter < templeft; sidecounter++) { write_led(&totalstrip[sidecounter*2],leftled.red,leftled.green,leftled.blue); } for(sidecounter = 0 ; sidecounter < tempright ; sidecounter++) { write_led(&totalstrip[(NUMBER_OF_PIXELS-1)-(sidecounter*2)],rightled.red,rightled.green,rightled.blue); } wait(0.7); } wait(0.5); } void WinLoose(float seconds, bool side) { uint8_t ledcounter; Timer timer; timer.start(); while( timer.read() < seconds) { uint8_t redvalue = 255-(255.0*(timer.read()/(seconds/2))); for(ledcounter = 0; ledcounter < NUMBER_OF_PIXELS; ledcounter++) { if(ledcounter < NUMBER_OF_PIXELS / 2) { if(side) write_led(&totalstrip[ledcounter], redvalue,0,0); else write_led(&totalstrip[ledcounter], 0,255,0); } else { if(side) write_led(&totalstrip[ledcounter], 0,255,0); else write_led(&totalstrip[ledcounter], redvalue ,0,0); } } } } void PaddleDemo(float seconds, uint8_t red, uint8_t green, uint8_t blue) { uint8_t ledcounter; Timer timer; timer.start(); while( timer.read() < seconds) { for(ledcounter = 0; ledcounter < NUMBER_OF_PIXELS; ledcounter++) { if((ledcounter >= paddlestart) && ( ledcounter <= paddlestart+PADDLE_LENGTH)) write_led(&totalstrip[ledcounter], red,green,blue); else write_led(&totalstrip[ledcounter], 0,0,0); } } } void Randomblinks(float seconds, bool colored) { uint8_t ledcounter; uint8_t test; Timer timer; timer.start(); while( timer.read() < seconds ) { test = 50.0*rand()/(RAND_MAX*1.0); for(ledcounter = 0; ledcounter < NUMBER_OF_PIXELS; ledcounter++) { if(ledcounter == test) { if(colored) write_led(&totalstrip[ledcounter], test*5,(test%10)*25,(test%15)*15); else write_led(&totalstrip[ledcounter], 255,255,255); } else write_led(&totalstrip[ledcounter], 0,0,0); } } } void UpdateDemoPaddle(void) { static uint8_t direction = 1; if(direction) { paddlestart++; } else { paddlestart--; } if(paddlestart > (NUMBER_OF_PIXELS - PADDLE_LENGTH)) direction = 0; if(paddlestart < 0) direction = 1; } //DigitalOut myled(LED1); //int main() { // while(1) { // myled = 1; // wait(0.2); // myled = 0; // wait(0.2); // } //}