Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 18:789e9fafa5f6, committed 2013-08-23
- Comitter:
- vsluiter
- Date:
- Fri Aug 23 20:18:40 2013 +0000
- Parent:
- 17:c5a38d01dfbe
- Child:
- 19:bb0179b9043f
- Commit message:
- With button hit indication. Some bug in paddle speed, have to work that out;
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Aug 23 17:04:50 2013 +0000
+++ b/main.cpp Fri Aug 23 20:18:40 2013 +0000
@@ -17,6 +17,7 @@
void DrawLine(uint8_t start, uint8_t end, uint8_t red, uint8_t green, uint8_t blue);
uint16_t totalstrip[NUMBER_OF_PIXELS];
volatile int8_t paddlestart= 0;
+bool strip_drawable = true;
SPI ledstrip(PTD2,NC,PTD1);
MODSERIAL pc(USBTX,USBRX);
@@ -30,6 +31,7 @@
void setSpeed(float speed); //pixels per second
void setColor(uint8_t red, uint8_t green, uint8_t blue);
uint8_t getSize(void);
+ uint8_t getSpeed(void){return m_speed;};
void setSize(uint8_t size);
uint8_t getColor(uint8_t pixel, uint8_t color);
private:
@@ -115,17 +117,19 @@
class GameButton
{
public:
+ GameButton(){};
GameButton(PinName pin, float time);
bool pushflag;
bool getTimeoutActive(void);
- // void SetTimeout(float time);
+ protected:
+ virtual void pushhandlercallback(void){};
private:
InterruptIn *intpin;
Timeout timeout;
float m_time;
bool m_timeoutactive;
void TimeOutHandler(void);
- void PushHandler(void);
+ virtual void PushHandler(void);
};
GameButton::GameButton(PinName pin, float time=0.5)
@@ -135,6 +139,7 @@
(*intpin).mode(PullUp);
(*intpin).fall(this, &GameButton::PushHandler);
m_time = time;
+ m_timeoutactive = false;
//timeout = new Timeout(m_time);
}
@@ -143,6 +148,7 @@
pushflag = true;
m_timeoutactive = true;
timeout.attach(this, &GameButton::TimeOutHandler, m_time);
+ pushhandlercallback();
}
void GameButton::TimeOutHandler(void)
@@ -155,24 +161,41 @@
return m_timeoutactive;
}
+Paddle paddle;
+
class PongGameButton : public GameButton
{
public:
- uint8_t paddlepos;
+ PongGameButton(PinName name, float time);
+ void pushhandlercallback(void);
+ int16_t paddlepos;
};
+PongGameButton::PongGameButton(PinName name, float time) : GameButton(name)
+{
+ paddlepos = 0;
+}
+
+void PongGameButton::pushhandlercallback(void)
+{
+ paddlepos = paddle.position;
+}
+
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);
- }
+ if(strip_drawable)
+ {
+ /*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)
@@ -180,15 +203,9 @@
*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;
-}
+
+bool rightpushed = false;
+int16_t rightpushpos = 0;
void right_pushed(void)
{
@@ -202,15 +219,15 @@
Ticker updater;
//Ticker demopaddlepos;
Timer gametimer;
- InterruptIn buttonleft(PTD5);
- InterruptIn buttonright(PTD0);
+ PongGameButton buttonleft(PTD5,0.5);
+ PongGameButton buttonright(PTD0, 0.5);
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);
+ // 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'
@@ -226,40 +243,46 @@
static uint8_t naglevel1 = 0, naglevel2 = 0;
//paddle.position = 48;
//while(1);
+ strip_drawable = false;
DrawGamePaddle();
- if(leftpushed || rightpushed)
+ if(buttonleft.getTimeoutActive())
+ DrawLine(5,10,255,0,0);
+ if(buttonright.getTimeoutActive())
+ DrawLine(NUMBER_OF_PIXELS-5, NUMBER_OF_PIXELS-1,255,0,0);
+ strip_drawable = true;
+ if(buttonleft.pushflag || buttonright.pushflag)
{
if(paddle.direction == 1)
{
- if(rightpushed)
+ if(buttonright.pushflag)
{
//printf("\n\rright pushed");
- rightpushed = false;
- if(rightpushpos >= NUMBER_OF_PIXELS-1 ) //also count when hit at last pixel = NUMBER_OF_PIXELS-1
+ buttonright.pushflag = false;
+ if(buttonright.paddlepos >= NUMBER_OF_PIXELS-1 ) //also count when hit at last pixel = NUMBER_OF_PIXELS-1
{
paddle.direction = 0;
- paddle.setSpeed(25+(rightpushpos-(NUMBER_OF_PIXELS-1))*4);
- paddle.position = NUMBER_OF_PIXELS-1;
- DrawLine(NUMBER_OF_PIXELS-10, NUMBER_OF_PIXELS-1, 255,255,255);
+ paddle.setSpeed(25+(buttonright.paddlepos-(NUMBER_OF_PIXELS-1))*4);
+ paddle.position = NUMBER_OF_PIXELS-2;
}
- pc.printf("\n\rright pushed. Paddle position: %d, registered: %d", paddle.position, rightpushpos);
+ pc.printf("\n\rright pushed. Paddle position: %d, registered: %d, speed: %", paddle.position, buttonright.paddlepos,paddle.getSpeed());
}
+ buttonleft.pushflag = false;
}
else
{
- if(leftpushed)
+ if(buttonleft.pushflag)
{
//printf("\n\rleft pushed");
- leftpushed = false;
- if(leftpushpos <= 0 )
+ buttonleft.pushflag = false;
+ if(buttonleft.paddlepos <= 0 )
{
paddle.direction = 1;
- paddle.setSpeed(25+(-leftpushpos)*4);
+ paddle.setSpeed(25+(-buttonleft.paddlepos)*4);
paddle.position = 0;
- DrawLine(0,9, 255,255,255);
}
- pc.printf("\n\rleft pushed. Paddle position: %d, registered: %d", paddle.position, leftpushpos);
+ pc.printf("\n\rleft pushed. Paddle position: %d, registered: %d, speed %d", paddle.position, buttonleft.paddlepos, paddle.getSpeed());
}
+ buttonright.pushflag = false;
}
}
else
@@ -297,6 +320,7 @@
paddle.setSize(2);
//paddle.setSpeed(70);
}
+ wait(0.05);
}
}
@@ -403,9 +427,14 @@
void DrawLine(uint8_t start, uint8_t end, uint8_t red, uint8_t green, uint8_t blue)
{
uint8_t ledcounter;
- for(ledcounter = start; ledcounter < end ; ledcounter++);
+ if(end >= NUMBER_OF_PIXELS)
+ end = NUMBER_OF_PIXELS-1;
+ if(start < end)
{
- write_led(&totalstrip[ledcounter], red, green, blue);
+ for(ledcounter = start; ledcounter < end ; ledcounter++)
+ {
+ write_led(&totalstrip[ledcounter], red, green, blue);
+ }
}
}