![](/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:
- 10:6f9624c7425d
- Parent:
- 9:c5086fe3c0cc
- Child:
- 11:fc80c39ba9f0
File content as of revision 10:6f9624c7425d:
#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); void HandleScore(uint8_t *, uint8_t *, bool, Timer *); 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 setSpeed(float speed); 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]; float m_speed; Ticker UpdatePosition; void PositionUpdater(void); }; Paddle::Paddle() { setColor(255,255,255); setSize(4); position = -getSize(); direction = 1; setSpeed(50); } void Paddle::setSpeed(float speed) { //truncate float time; if(speed > 100000) speed = 100000; if(speed <= 0) speed = 0.001; m_speed = speed; time = 1/m_speed; UpdatePosition.detach(); UpdatePosition.attach(this,&Paddle::PositionUpdater, time); } void Paddle::PositionUpdater(void) { if(direction == 1) position++; else position--; } 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; bool leftpushed = false, rightpushed = false; void left_pushed(void) { if(paddle.direction != 1) leftpushed = true; } void right_pushed(void) { if(paddle.direction == 1) rightpushed = true; } int main() { Ticker updater; Ticker demopaddlepos; Timer gametimer; InterruptIn buttonleft(PTD5); InterruptIn buttonright(PTD0); uint8_t ledcounter; uint8_t left_score = 0, right_score = 0; buttonleft.mode(PullUp); buttonright.mode(PullUp); buttonright.fall(right_pushed); buttonleft.fall(left_pushed); updater.attach(UpdateLEDstrip, .01); 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.setSpeed(30); paddle.setColor(255,0,255); gametimer.start(); while(1) { static uint8_t naglevel1 = 0, naglevel2 = 0; //PaddleDemo(2,255,10,100); DrawGamePaddle(); //wait(.05); //Score(3,2); //WinLoose(2.5, LEFT); if(paddle.direction == 1) { if(rightpushed) { rightpushed = false; if(paddle.position > (NUMBER_OF_PIXELS - paddle.getSize()) && paddle.position < NUMBER_OF_PIXELS) paddle.direction = 0; } } else { if(leftpushed) { leftpushed = false; if(paddle.position > -paddle.getSize() && (paddle.position < 0 ) ) paddle.direction = 1; } } if(gametimer.read()>4 && !naglevel1) { naglevel1 = 1; paddle.setSize(4); paddle.setSpeed(40); } if(gametimer.read()>8 && !naglevel2) { naglevel2 = 1; paddle.setSize(2); paddle.setSpeed(70); } //paddle.setSpeed(gametimer<4?30:gametimer.read()*10); if(paddle.position >= NUMBER_OF_PIXELS && (paddle.direction == 1)) { //left player scores! left_score++; HandleScore(&left_score,&right_score,false, &gametimer); paddle.position = -paddle.getSize(); naglevel1=naglevel2 = 0; } if(paddle.position < -paddle.getSize() && (paddle.direction == 0)) { //right player scores! right_score++; HandleScore(&left_score,&right_score,true, &gametimer); naglevel1=naglevel2 = 0; paddle.position = NUMBER_OF_PIXELS; } } } void HandleScore(uint8_t *leftscore, uint8_t *rightscore, bool last_won, Timer *gametimer) { WinLoose(1.5, last_won); Score(*leftscore, *rightscore); if(*leftscore + *rightscore == 11) { *leftscore = 0; *rightscore = 0; Randomblinks(2,5); } (*(mbed::Timer *)gametimer).reset(); paddle.setSize(6); paddle.setSpeed(30); } 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.4); } 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); // } //}