![](/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-22
- Revision:
- 13:e96098b6660d
- Parent:
- 12:97c71b84c984
- Child:
- 14:5b588ea6bd8f
File content as of revision 13:e96098b6660d:
#include "mbed.h" #include "MODSERIAL.h" #define NUMBER_OF_PIXELS 50 #define PADDLE_LENGTH 5 #define STARTING_SPEED 20 #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); MODSERIAL pc(USBTX,USBRX); class Paddle { public: Paddle();//constructor // ~Paddle();//deconstructor int16_t position; uint8_t direction; void setSpeed(float speed); //pixels per second 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; int16_t leftpushpos= 0, rightpushpos = 0; void left_pushed(void) { leftpushpos = paddle.position; if(paddle.direction != 1) leftpushed = true; } void right_pushed(void) { rightpushpos = paddle.position; 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; pc.baud(115200); buttonleft.mode(PullUp); buttonright.mode(PullUp); buttonright.fall(right_pushed); buttonleft.fall(left_pushed); updater.attach(UpdateLEDstrip, .03); //demopaddlepos.attach(UpdateDemoPaddle, .03); ledstrip.format(8,0); //15 bits, mode '0' ledstrip.frequency(1000000); //1MHz clock for(ledcounter = 0; ledcounter < NUMBER_OF_PIXELS; ledcounter++) {//turn off leds write_led(&totalstrip[ledcounter], 0,0,0); } paddle.setSize(6); paddle.setSpeed(STARTING_SPEED); paddle.setColor(255,0,255); gametimer.start(); while(1) { static uint8_t naglevel1 = 0, naglevel2 = 0; //paddle.position = 48; //while(1); if(leftpushed || rightpushed) { if(paddle.direction == 1) { if(rightpushed) { //printf("\n\rright pushed"); rightpushed = false; if(rightpushpos >= NUMBER_OF_PIXELS ) { paddle.direction = 0; paddle.position = NUMBER_OF_PIXELS; } pc.printf("\n\rright pushed. Paddle position: %d, registered: %d", paddle.position, rightpushpos); } } else { if(leftpushed) { //printf("\n\rleft pushed"); leftpushed = false; if(leftpushpos <= 0 ) { paddle.direction = 1; paddle.position = 0; } pc.printf("\n\rleft pushed. Paddle position: %d, registered: %d", paddle.position, leftpushpos); } } } else { if(paddle.position > ( NUMBER_OF_PIXELS + paddle.getSize() ) && (paddle.direction == 1)) { pc.printf("\n\rleft player score. Paddle position: %d", paddle.position); //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)) { pc.printf("\n\rlright player score. Paddle position: %d", paddle.position); //right player scores! right_score++; HandleScore(&left_score,&right_score,true, &gametimer); naglevel1=naglevel2 = 0; paddle.position = NUMBER_OF_PIXELS; } } if(gametimer.read()>6 && !naglevel1) { naglevel1 = 1; paddle.setSize(4); paddle.setSpeed(40); } if(gametimer.read()>10 && !naglevel2) { naglevel2 = 1; paddle.setSize(2); paddle.setSpeed(70); } DrawGamePaddle(); } } 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(STARTING_SPEED); } void DrawGamePaddle(void) { uint8_t ledcounter; uint8_t colorpos; for(ledcounter = 0; ledcounter< NUMBER_OF_PIXELS; ledcounter++) { if(paddle.direction == 1) { if(ledcounter > paddle.position-paddle.getSize() && ledcounter <= paddle.position) { colorpos = paddle.getSize()-(paddle.position - ledcounter);//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); } else { if(ledcounter >= paddle.position && ledcounter <= paddle.position+paddle.getSize()) { 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; }