Elements used in the Balls and Things games for the RETRO.
Dependents: RETRO_BallsAndPaddle RETRO_BallAndHoles
Revision 0:3d0db4e183ee, committed 2015-02-06
- Comitter:
- maxint
- Date:
- Fri Feb 06 09:51:06 2015 +0000
- Child:
- 1:71185a0aadfc
- Commit message:
- Put Balls and Things objects into a library
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Accelerometer.cpp Fri Feb 06 09:51:06 2015 +0000 @@ -0,0 +1,102 @@ +#include "Accelerometer.h" + +Accelerometer::Accelerometer(int nI2cAddress, LCD_ST7735* pDisp) : i2cAddress(nI2cAddress), i2c(P0_5, P0_4) +{ // constructor + this->i2c.frequency(400000); // fast I2C is 400 KHz, not 400 Hz. Default frequency is 100 KHz + this->writeRegister(0x2A, 0x01); // initialize accelerometer (set CTRL_REG1 bit ACTIVE) + this->pDisp=pDisp; + + this->colors[0] = Color565::Red; + this->colors[1] = Color565::Green; + this->colors[2] = Color565::Blue; +} + +void Accelerometer::readRegisters(char address, char* buffer, int len) { +// int nStart=this->tWait.read_ms(); + this->i2c.write(i2cAddress, &address, 1, true); + this->i2c.read(i2cAddress | 1, buffer, len); +// printDouble((double)this->tWait.read_ms()-nStart, 10, 10); +} + +int Accelerometer::writeRegister(char address, char value) { + char buffer[2] = { address, value }; + + return this->i2c.write(i2cAddress, buffer, 2); +} + +double Accelerometer::convert(char* buffer) { + double val = ((buffer[0] << 2) | (buffer[1] >> 6)); + + if (val > 511.0) + val -= 1024.0; + + return val / 512.0; +} + +void Accelerometer::getXYZ(double& x, double& y, double& z) { + char buffer[6]; + + this->readRegisters(0x01, buffer, 6); + + x = this->convert(buffer); + y = this->convert(buffer + 2); + z = this->convert(buffer + 4); +} + +// +// Accellerator graph for debug purposes +// + +void Accelerometer::drawAxes() +{ + for (int i = 0; i < 3; i++) { + this->pDisp->fillRect(0, i * (Accelerometer::GRAPH_HEIGHT + Accelerometer::GRAPH_SPACING), this->pDisp->getWidth(), i * (Accelerometer::GRAPH_HEIGHT + Accelerometer::GRAPH_SPACING) + Accelerometer::GRAPH_HEIGHT, Color565::fromRGB(i==0?0x22:0, i==1?0x22:0, i==2?0x22:0)); + this->pDisp->drawLine(0, i * (Accelerometer::GRAPH_HEIGHT + Accelerometer::GRAPH_SPACING), 0, i * (Accelerometer::GRAPH_HEIGHT + Accelerometer::GRAPH_SPACING) + Accelerometer::GRAPH_HEIGHT, Color565::White); + this->pDisp->drawLine(0, i * (Accelerometer::GRAPH_HEIGHT + Accelerometer::GRAPH_SPACING) + Accelerometer::GRAPH_HEIGHT / 2, this->pDisp->getWidth(), i * (Accelerometer::GRAPH_HEIGHT + Accelerometer::GRAPH_SPACING) + Accelerometer::GRAPH_HEIGHT / 2, Color565::White); + } +} + +void Accelerometer::drawPoint(int axis, double value) +{ + if (value < -1.0) + value = -1.0; + + if (value > 1.0) + value = 1.0; + + value += 1.0; + value /= 2.0; + value = 1.0 - value; + value *= Accelerometer::GRAPH_HEIGHT; + + this->pDisp->setPixel(this->graphX, axis * (Accelerometer::GRAPH_HEIGHT + Accelerometer::GRAPH_SPACING) + (int)value, this->colors[axis]); +} + +void Accelerometer::resetGraph() +{ + this->graphX = 0; + this->pDisp->clearScreen(); + //this->pDisp->fillRect(0, 0, this->pDisp->getWidth(), this->pDisp->getHeight(), Color565::fromRGB(0x11, 0x33, 0x22)); + this->drawAxes(); +} + +void Accelerometer::checkGraphReset() +{ + if (this->graphX > this->pDisp->getWidth()) + { + this->resetGraph(); + } +} + +void Accelerometer::updateGraph() +{ + double x, y, z; + this->getXYZ(x, y, z); + + this->checkGraphReset(); + this->drawPoint(0, x); + this->drawPoint(1, y); + this->drawPoint(2, z); + this->graphX++; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Accelerometer.h Fri Feb 06 09:51:06 2015 +0000 @@ -0,0 +1,32 @@ +#pragma once +#include "mbed.h" + +#include "Color565.h" +#include "LCD_ST7735.h" + +class Accelerometer +{ + public: + Accelerometer(int nI2cAddress, LCD_ST7735* pDisp); + void getXYZ(double& x, double& y, double& z); + void resetGraph(); + void updateGraph(); + + private: + static const int GRAPH_HEIGHT = 40; + static const int GRAPH_SPACING = 2; + + void readRegisters(char address, char* buffer, int len); + int writeRegister(char address, char value); + double convert(char* buffer); + + int i2cAddress; + I2C i2c; + LCD_ST7735* pDisp; + unsigned short colors[3]; + int graphX; + + void drawAxes(); + void drawPoint(int axis, double value); + void checkGraphReset(); +}; \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Ball.cpp Fri Feb 06 09:51:06 2015 +0000 @@ -0,0 +1,134 @@ +#include "Ball.h" + +Ball::Ball() : vSpeed() +{ // constructor +} + +Ball::Ball(LCD_ST7735* pDisp) : vSpeed() +{ // constructor + this->pDisp=pDisp; + this->fActive=false; +} + +uint16_t Ball::dimmedColor(uint16_t uColor) +{ + uint16_t r, g, b; + + r=(uColor >> 11) <<3; + g=((uColor >> 5) & 0x3F) <<2; + b=(uColor & 0x1F) << 3; + r=r*2/3; + g=g*2/3; + b=b*2/3; +// r=r/2; +// g=g/2; +// b=b/2; + + return(Color565::fromRGB((uint16_t)r,(uint16_t)g,(uint16_t)b)); +} + +void Ball::initialize(int nX, int nY, int nR, uint16_t uColor) +{ + this->pos.set(nX, nY); + this->nRadius=nR; + this->uColor=uColor; + this->uColorHigh=uColor; + this->uColorMid=dimmedColor(uColorHigh); + this->uColorLow=dimmedColor(uColorMid); + +} + +void Ball::setSpeed(int X, int Y) +{ + this->vSpeed.set(X, Y); +} + +void Ball::changeSpeed(bool fUp) +{ + if(fUp) + { + if(this->vSpeed.getSize()<=4.0) this->vSpeed.add(1.0); + } + else + { + if(this->vSpeed.getSize()>=1.0) + this->vSpeed.add(-1.0); + else // TODO: added code below to allow zero speed pause of ball + this->vSpeed.set(0,0); + } +} + +void Ball::unmove() +{ // move back to previous position + pos.set(pos.getPrev()); +} + +void Ball::update() +{ + this->pos.move(this->vSpeed); +} + +Circle Ball::getBoundingCircle() +{ + return(Circle(this->pos.getX(), this->pos.getY(), this->nRadius)); +} + +bool Ball::collides(Rectangle r) +{ + Circle cBall=this->getBoundingCircle(); + Rectangle rBall=cBall.getBoundingRectangle(); + +/* +char szBuffer[256]; +sprintf(szBuffer, "c:%d,%d ", cBall.getX(), cBall.getY()); +this->pDisp->drawString(font_oem, 0, 0, szBuffer); +*/ + return(rBall.collides(r)); +} + +void Ball::Bounce(Vector vBounce) +{ // change the direction in a certain direction + this->unmove(); // undo move to pre-bouncing position to avoid drawing on colliding position + + this->vSpeed.multiply(vBounce); + + // check speed w/max + if(this->vSpeed.y>5.0) this->vSpeed.y=5; +} + + + +void Ball::clear() +{ + Point p=pos.getCur(); + this->pDisp->fillCircle(p.getX(),p.getY(), this->nRadius, Color565::Black, Color565::Black); +} + +void Ball::clearPrev() +{ + Point p=pos.getPrev(); + this->pDisp->fillCircle(p.getX(),p.getY(), this->nRadius, Color565::Black, Color565::Black); +} + + +void Ball::draw() +{ // render the object on the screen, based on its current position + Point p=pos.getCur(); + if(this->nRadius>3) + { + this->pDisp->fillCircle(p.getX(), p.getY(), this->nRadius, this->uColorLow, this->uColorLow); + this->pDisp->fillCircle(p.getX()-this->nRadius/3, p.getY()-this->nRadius/3, this->nRadius/2, this->uColorMid, this->uColorMid); + this->pDisp->setPixel(p.getX()-this->nRadius/2, p.getY()-this->nRadius/2, this->uColorHigh); + } + else + this->pDisp->fillCircle(p.getX(), p.getY(), this->nRadius, this->uColorMid, this->uColorMid); +} + +void Ball::redraw() +{ // redraw the ball if its position has changed + if(pos.hasChanged()) + { + clearPrev(); + draw(); + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Ball.h Fri Feb 06 09:51:06 2015 +0000 @@ -0,0 +1,47 @@ +#pragma once +#include "mbed.h" + +#include "Color565.h" +#include "font_OEM.h" +#include "LCD_ST7735.h" + +#include "Shapes.h" +#include "Vector.h" +#include "Physics.h" + +class Ball +{ + public: + static const bool fFixed=false; + bool fActive; + + Ball(); + Ball(LCD_ST7735* pDisp); + void initialize(int X, int Y, int R, uint16_t uColor); + void setSpeed(int X, int Y); + void changeSpeed(bool fUp); + void unmove(); + void update(); + void clear(); + void clearPrev(); + void draw(); + void redraw(); + + Position pos; + int nRadius; + Vector vSpeed; + + Circle getBoundingCircle(); + bool collides(Rectangle r); + void Bounce(Vector vBounce); + + private: + uint16_t uColor; + uint16_t uColorHigh; + uint16_t uColorMid; + uint16_t uColorLow; + LCD_ST7735* pDisp; + + uint16_t dimmedColor(uint16_t uColor); +}; +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Paddle.cpp Fri Feb 06 09:51:06 2015 +0000 @@ -0,0 +1,87 @@ +#include "Paddle.h" + +Paddle::Paddle(LCD_ST7735* pDisp) +{ // constructor + this->pDisp=pDisp; +} + +Paddle::Paddle(LCD_ST7735* pDisp, int nX, int nY, int nWidth, int nHeight) +{ // constructor + this->pos.set(nX, nY); + this->dim.nWidth=nWidth; + this->dim.nHeight=nHeight; + this->pDisp=pDisp; +} + +void Paddle::initialize(int nX, int nY, int nWidth, int nHeight) +{ + this->pos.set(nX, nY); + this->dim.nWidth=nWidth; + this->dim.nHeight=nHeight; +} + +void Paddle::checkBoundary(Rectangle rBoundary) +{ + if(pos.getX()<rBoundary.getX1()) + pos.setX(rBoundary.getX1()); + if(pos.getX()+dim.nWidth>rBoundary.getX2()) + pos.setX(rBoundary.getX2()-dim.nWidth); +} + +bool Paddle::hasChanged() +{ + return(pos.hasChanged()); +} + +void Paddle::move(Vector vDiff) +{ + this->pos.move(vDiff); +// this->rPaddle.move(vDiff); + +/* +char szBuffer[256]; +sprintf(szBuffer, "p:%d,%d ", pos.getX(), pos.getY()); +this->pDisp->drawString(font_oem, 0, 0, szBuffer); +*/ + +} + + +void Paddle::clearPrev() +{ + Point p=pos.getPrev(); + this->pDisp->fillRect(p.getX(), p.getY(), p.getX()+dim.nWidth, p.getY()+dim.nHeight, Color565::Black); +} + +void Paddle::clear() +{ + Point p=pos.getCur(); + this->pDisp->fillRect(p.getX(), p.getY(), p.getX()+dim.nWidth, p.getY()+dim.nHeight, Color565::Black); +} + +void Paddle::draw() +{ + Point p=pos.getCur(); + this->pDisp->drawLine(p.getX(), p.getY()+dim.nHeight, p.getX()+dim.nWidth/3, p.getY(), Color565::Blue); + this->pDisp->fillRect(p.getX()+dim.nWidth/3, p.getY(), p.getX()+dim.nWidth/3+dim.nWidth/3, p.getY()+dim.nHeight, Color565::Blue); + this->pDisp->drawLine(p.getX()+dim.nWidth/3+dim.nWidth/3, p.getY(), p.getX()+dim.nWidth, p.getY()+dim.nHeight, Color565::Blue); +} + +void Paddle::redraw(bool fForceDraw) // fForceDraw=false +{ // redraw the paddle if its position has changed + //static int n=0; +//char szBuffer[256]; + + if(pos.hasChanged() || fForceDraw) + { + Point pPrev=pos.getPrev(); + Point pCur=pos.getCur(); +/* +n++; +sprintf(szBuffer, "%d %d,%d - %d,%d", n, pPrev.getX(), pPrev.getY(), pCur.getX(), pCur.getY()); +this->pDisp->drawString(font_oem, 20, 0, szBuffer); +*/ + clearPrev(); + draw(); + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Paddle.h Fri Feb 06 09:51:06 2015 +0000 @@ -0,0 +1,37 @@ +#pragma once +#include "mbed.h" + +#include "Color565.h" +#include "font_OEM.h" +#include "LCD_ST7735.h" + +#include "Shapes.h" +#include "Vector.h" +#include "Physics.h" + +class Paddle +{ + public: + Paddle(LCD_ST7735* pDisp); + Paddle(LCD_ST7735* pDisp, int nX, int nY, int nWidth, int nHeight); + void initialize(int nX, int nY, int nWidth, int nHeight); + + + void move(Vector vDiff); + void checkBoundary(Rectangle rBoundary); + bool hasChanged(); + + void clearPrev(); + void clear(); + void draw(); + void redraw(bool fForceDraw=false); + + Position pos; + Dimension dim; + Vector vSpeed; + + + + private: + LCD_ST7735* pDisp; +}; \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Physics.cpp Fri Feb 06 09:51:06 2015 +0000 @@ -0,0 +1,91 @@ +#include "Physics.h" + +Position::Position() : vPos(), pPrev(), pCur() +{ // constructor + +} + +Point Position::getPrev() +{ + return(pPrev); +} + +Point Position::getCur() +{ + return(pCur); +} + +int Position::getX() +{ + return(pCur.getX()); +} +int Position::getY() +{ + return(pCur.getY()); +} + +void Position::set(float x, float y) +{ + //nPrevX=nCurX; + //nPrevY=nCurY; + pPrev=pCur; + vPos.x=x; + vPos.y=y; + pCur.set(rint(vPos.x), rint(vPos.y)); + //nCurX=rint(vPos.x); + //nCurY=rint(vPos.y); +} + +void Position::set(int x, int y) +{ + set((float)x, (float)y); +} + +void Position::set(Point ptNew) +{ + set(ptNew.getX(), ptNew.getY()); +} + +void Position::setX(int x) +{ + set((float)x, vPos.y); +} + +void Position::setY(int y) +{ + set(vPos.x, (float)y); +} + +void Position::move(float fDiffX, float fDiffY) +{ + //nPrevX=nCurX; + //nPrevY=nCurY; + pPrev=pCur; + vPos.x+=fDiffX; + vPos.y+=fDiffY; + pCur.set(rint(vPos.x), rint(vPos.y)); + //nCurX=rint(vPos.x); + //nCurY=rint(vPos.y); +} + +void Position::move(int nDiffX, int nDiffY) +{ + move((float)nDiffX, (float)nDiffY); +} + +void Position::move(Vector vDiff) +{ + move(vDiff.x, vDiff.y); +} + +bool Position::hasChanged() +{ + //return(nPrevX!=nCurX || nPrevy!=nCury); + return(pPrev.getX()!=pCur.getX() || pPrev.getY()!=pCur.getY()); +} + +Dimension::Dimension() +{ + nWidth=0; + nHeight=0; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Physics.h Fri Feb 06 09:51:06 2015 +0000 @@ -0,0 +1,37 @@ +#pragma once +#include "mbed.h" +#include "Vector.h" +#include "Shapes.h" + +class Position +{ + public: + Position(); + Point getPrev(); + Point getCur(); + int getX(); + int getY(); + + void set(float x, float y); + void set(int x, int y); + void set(Point ptNew); + void setX(int x); + void setY(int y); + + void move(float fDiffX, float fDiffY); + void move(int nDiffX, int nDiffY); + void move(Vector vDiff); + bool hasChanged(); + private: + Vector vPos; + Point pPrev; + Point pCur; +}; + +class Dimension +{ + public: + Dimension(); + int nWidth; + int nHeight; +}; \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Shapes.cpp Fri Feb 06 09:51:06 2015 +0000 @@ -0,0 +1,227 @@ +#include "mbed.h" +#include "Shapes.h" + + + +/////////////////// +// POINT +/////////////////// +Point::Point() +{ // constructor + x1=0; + y1=0; +} + +Point::Point(int x, int y) +{ // constructor + x1=x; + y1=y; +} + +int Point::getX() +{ + return x1; +} + +int Point::getY() +{ + return y1; +} + +void Point::set(int x, int y) +{ + x1=x; + y1=y; +} + + +/////////////////// +// RECTANGLE +/////////////////// + +Rectangle::Rectangle(int x,int y, int xx, int yy) +{ + x1 = x; + x2 = xx; + y1 = y; + y2 = yy; +} + +Rectangle::Rectangle(Point pt1, Point pt2) +{ + x1 = pt1.getX(); + x2 = pt2.getX(); + y1 = pt1.getY(); + y2 = pt2.getY(); +} + + +bool Rectangle::collides(Point pt) +{ + if(pt.getX() >= x1 && pt.getX() <= x2) { + if(pt.getY() >= y1 && pt.getY() <= y2) { + return true; + } + } + return false; +} + +/* +Rectangle Rectangle::intersection(Rectangle r) +{ // return the intersection of two rectangles or null + Rectangle rResult; + rResult=Rectangle() + if(this->x2 >= +} +*/ + +bool Rectangle::collides(Rectangle r) +{ // check if two rectangles collide + // method 1: check if corners of eithter rectangle collide + // method 2: compare sides + if(this->collides(r.get1()) || this->collides(r.get2())) return(true); + if(this->collides(r.get3()) || this->collides(r.get4())) return(true); + if(r.collides(this->get1()) || r.collides(this->get2())) return(true); + if(r.collides(this->get3()) || r.collides(this->get4())) return(true); + // TODO: check other corners + return(false); +} + + +Point Rectangle::get1() +{ + return(Point(x1, y1)); +} + +Point Rectangle::get2() +{ + return(Point(x2, y2)); +} + +Point Rectangle::get3() +{ + return(Point(x2, y1)); +} + +Point Rectangle::get4() +{ + return(Point(x1, y2)); +} + + + +int Rectangle::getX1() +{ + return x1; +} + +int Rectangle::getX2() +{ + return x2; +} + +int Rectangle::getY1() +{ + return y1; +} + +int Rectangle::getY2() +{ + return y2; +} + +int Rectangle::getCenterX() +{ + return x1 + (x2-x1)/2; +} + +int Rectangle::getCenterY() +{ + return y1 + (y2-y1)/2; +} + +Point Rectangle::getCenter() +{ + return(Point(x1 + (x2-x1)/2, y1 + (y2-y1)/2)); +} + +void Rectangle::set(Rectangle rNew) +{ + x1=rNew.getX1(); + y1=rNew.getY1(); + x2=rNew.getX2(); + y2=rNew.getY2(); +} + +void Rectangle::move(Vector v) +{ + x1+=rint(v.x); + y1+=rint(v.y); + x2+=rint(v.x); + y2+=rint(v.y); +} + + +/////////////////// +// CIRCLE +/////////////////// +Circle::Circle(int x,int y, int r) +{ + x1=x; + y1=y; + r1=r; +} + +Point Circle::getCenter() +{ + return(Point(x1, y1)); +} + +int Circle::getRadius() +{ + return(r1); +} + +int Circle::getX() +{ + return(x1); +} + +int Circle::getY() +{ + return(y1); +} + +void Circle::setX(int x) +{ + x1=x; +} + +void Circle::setY(int y) +{ + y1=y; +} + +void Circle::setXY(int x, int y) +{ + x1=x; + y1=y; +} + +void Circle::move(int x, int y) +{ + x1+=x; + y1+=y; +} + +void Circle::move(Vector v) +{ + x1+=rint(v.x); + y1+=rint(v.y); +} + + +Rectangle Circle::getBoundingRectangle() +{ + return(Rectangle(x1-r1, y1-r1, x1+r1, y1+r1)); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Shapes.h Fri Feb 06 09:51:06 2015 +0000 @@ -0,0 +1,64 @@ +#pragma once +#include "mbed.h" +#include "Vector.h" + +class Point +{ +protected : + int x1, y1; + +public : + Point(); + Point(int x, int y); + int getX(); + int getY(); + void set(int x, int y); +}; + +class Rectangle +{ +protected : + int x1, x2, y1, y2; + +public : + Rectangle(int x,int y, int x2, int y2); + Rectangle(Point pt1, Point pt2); + bool collides(Point pt); + bool collides(Rectangle r); + + void set(Rectangle rNew); + + Point get1(); + Point get2(); + Point get3(); + Point get4(); + Point getCenter(); + + int getX1(); + int getX2(); + int getY1(); + int getY2(); + int getCenterX(); + int getCenterY(); + void move(Vector v); +}; + +class Circle +{ +protected : + int x1, y1, r1; + +public : + Circle(int x,int y, int r); + Point getCenter(); + int getRadius(); + int getX(); + int getY(); + void setX(int x); + void setY(int y); + void setXY(int x, int y); + void move(int x, int y); + void move(Vector v); + Rectangle getBoundingRectangle(); +}; +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SoundFX.h Fri Feb 06 09:51:06 2015 +0000 @@ -0,0 +1,31 @@ +#pragma once +#include "mbed.h" +#include "MusicEngine.h" + +class SoundFX +{ + public: + SoundFX(); + void checkPwm(); + void reset(); + void beep(int nDuration=1); + void beepShort(); + void beepLong(); + void beepLow(); + void play(char *szPlay); + void playTune(); + void setMute(bool fMute); + bool getMute(); + + void musicCompleted(void); + + private: + static const int BOUNCE1_SOUND_TICKS = 1; + static const int BOUNCE2_SOUND_TICKS = 2; + +// PwmOut pwm; +// int pwmTicksLeft; + bool fMute; + MusicEngine music; + +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SoundFx.cpp Fri Feb 06 09:51:06 2015 +0000 @@ -0,0 +1,135 @@ +#include "SoundFX.h" + +SoundFX::SoundFX() : +// pwm(P0_18), + music(P0_18) +{ +// this->pwmTicksLeft=0; + this->fMute=false; + music.setCompletionCallback(this, &SoundFX::musicCompleted); +} + +void SoundFX::checkPwm() +{ +/* + if(this->fMute || this->pwmTicksLeft == 0) + this->pwm.write(0.0); + else + { + this->pwmTicksLeft--; + this->pwm.write(0.5); + } +*/ +} + +void SoundFX::musicCompleted(void) +{ +/* + PwmOut pwm(P0_18); + pwm = 0.0; + pwm.period(0.1); + pwm.write(0.00); +*/ +} + +void SoundFX::reset() +{ +/* + this->pwmTicksLeft=0; + + this->pwm.period_ms(1); + this->pwm.write(0.00); +*/ +/* + PwmOut pwm(P0_18); + pwm.write(0.00); +*/ +} + +void SoundFX::setMute(bool fMute) +{ + this->fMute=fMute; +} + + +bool SoundFX::getMute() +{ + return(this->fMute); +} + + +void SoundFX::beep(int nDuration) //nDuration=1 +{ + if(this->fMute) + return; +// this->pwmTicksLeft = nDuration; +} + +void SoundFX::beepShort() +{ + if(this->fMute) + return; +/* + this->pwm.period_ms(2); + this->pwmTicksLeft = SoundFX::BOUNCE1_SOUND_TICKS; +*/ + music.play("T240 L32 O6 C"); +} + +void SoundFX::beepLong() +{ + if(this->fMute) + return; + +/* + this->pwm.period_ms(1); + this->pwmTicksLeft = SoundFX::BOUNCE2_SOUND_TICKS; +*/ + music.play("T240 L8 O5 C"); +} + +void SoundFX::beepLow() +{ + if(this->fMute) + return; +/* + this->pwm.period(1.0/220); + this->pwm.write(0.5); + wait_ms(150); + this->pwm.write(0.0); +*/ + music.play("T180 L4 O3 C"); +} + +void SoundFX::play(char *szPlay) +{ + if(this->fMute) + return; + music.play(szPlay); +} + +void SoundFX::playTune() +{ + if(this->fMute) + return; +/* + this->pwm.period(1.0/220); + this->pwm.write(0.5); + wait_ms(150); + this->pwm.write(0.0); + + this->pwm.period(1.0/196); + this->pwm.write(0.5); + wait_ms(150); + this->pwm.write(0.0); + + this->pwm.period(1.0/164.81); + this->pwm.write(0.5); + wait_ms(150); + this->pwm.write(0.0); +*/ +// music.setCompletionCallback(this, &SoundFX::musicCompleted); + //music.play("T224L8O5CL16>C<P16GP16L8EL16P16>C<GP16L8E.L16P16L8C#L16>C#<P16G#P16L8FL16P16>C#<G#P16L8F.L16P16L8CL16>C<P16GP16L8EL16P16>C<GP16L8E.L16P16D#EFP16FF#GP16GG#AP16L8>C<P8L4>C"); + music.play("T120 O3 L16 F L4 C F L8 C"); + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Vector.cpp Fri Feb 06 09:51:06 2015 +0000 @@ -0,0 +1,60 @@ +#include "Vector.h" + +Vector::Vector() +{ // constructor + this->set((float)0,(float)0); +} + +Vector::Vector(float fX, float fY) +{ + this->set(fX, fY); +} + +void Vector::set(float fX, float fY) +{ + x=fX; + y=fY; +} +void Vector::set(int nX, int nY) +{ + this->set((float)nX, (float)nY); +} + +float Vector::getSize() +{ // get the size of the vector + return(sqrt(x*x+y*y)); +} + +bool Vector::isLeft() { return(x<0); } +bool Vector::isRight() { return(x>0); } +bool Vector::isUp() { return(y<0); } +bool Vector::isDown() { return(y>0); } + + +void Vector::add(float fAdd) +{ // add the size of the vector by adding to its size + float fSize=getSize(); + float fSizeNew=fSize+fAdd; + if(fSize!=0) + { + x=x * (fSizeNew/fSize); + y=y * (fSizeNew/fSize); + } + else + { // in case of zero lenght, default to addition in first quadrant. + x=sqrt(0.5 * fAdd * fAdd); + y=x; + } +} + +void Vector::add(Vector vAdd) +{ + x+=vAdd.x; + y+=vAdd.y; +} + +void Vector::multiply(Vector vMult) +{ // multiply the vector by means of the other vector + x*=vMult.x; + y*=vMult.y; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Vector.h Fri Feb 06 09:51:06 2015 +0000 @@ -0,0 +1,22 @@ +#pragma once +#include "mbed.h" + +class Vector +{ + public: + float x; + float y; + + Vector(); + Vector(float fX, float fY); + void set(float dX, float fY); + void set(int nX, int nY); + float getSize(); + bool isLeft(); + bool isRight(); + bool isUp(); + bool isDown(); + void add(float fAdd); + void add(Vector vAdd); + void multiply(Vector vMult); +};