new mods upon mods by devhammer: - Added paddle control using tilting of the console - Finished mute function - Reduced flickering See game.cpp for full info.
Fork of RETRO_Pong_Mod by
This is a mod of the official Pong game released with the RETRO game console.
Revision 0:21669ea33448, committed 2014-11-10
- Comitter:
- john_ghielec
- Date:
- Mon Nov 10 13:04:42 2014 +0000
- Child:
- 1:cd8a3926f263
- Commit message:
- Initial commit.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DisplayN18.cpp Mon Nov 10 13:04:42 2014 +0000 @@ -0,0 +1,521 @@ +#include "DisplayN18.h" + +DisplayN18::DisplayN18() : resetPin(P0_20), backlightPin(P0_19), rsPin(P0_7), csPin(P0_2), spi(P0_21, P0_22, P1_15) +{ + this->resetPin.write(false); + this->backlightPin.write(true); + this->rsPin.write(false); + + this->spi.format(8, 3); + this->spi.frequency(15000000); + + this->initialize(); +} + +void DisplayN18::writeCommand(unsigned char command) +{ + this->rsPin.write(false); + + this->csPin.write(false); + + this->spi.write(command); + + this->csPin.write(true); +} + +void DisplayN18::writeData(unsigned char data) +{ + this->writeData(&data, 1); +} + +void DisplayN18::writeData(const unsigned char* data, unsigned int length) +{ + this->rsPin.write(true); + + this->csPin.write(false); + + for (unsigned int i = 0; i < length; i++) + this->spi.write(data[i]); + + this->csPin.write(true); +} + +void DisplayN18::reset() +{ + this->resetPin.write(false); + wait_ms(300); + + this->resetPin.write(true); + wait_ms(500); +} + +void DisplayN18::initialize() +{ + this->reset(); + + this->writeCommand(0x11); + + wait_ms(120); + + this->writeCommand(0xB1); + this->writeData(0x01); this->writeData(0x2C); this->writeData(0x2D); + this->writeCommand(0xB2); + this->writeData(0x01); this->writeData(0x2C); this->writeData(0x2D); + this->writeCommand(0xB3); + this->writeData(0x01); this->writeData(0x2C); this->writeData(0x2D); + this->writeData(0x01); this->writeData(0x2C); this->writeData(0x2D); + + this->writeCommand(0xB4); + this->writeData(0x07); + + this->writeCommand(0xC0); + this->writeData(0xA2); this->writeData(0x02); this->writeData(0x84); + this->writeCommand(0xC1); this->writeData(0xC5); + this->writeCommand(0xC2); + this->writeData(0x0A); this->writeData(0x00); + this->writeCommand(0xC3); + this->writeData(0x8A); this->writeData(0x2A); + this->writeCommand(0xC4); + this->writeData(0x8A); this->writeData(0xEE); + + this->writeCommand(0xC5); + this->writeData(0x0E); + + this->writeCommand(0x36); + this->writeData(0xA8); + + this->writeCommand(0xe0); + this->writeData(0x0f); this->writeData(0x1a); + this->writeData(0x0f); this->writeData(0x18); + this->writeData(0x2f); this->writeData(0x28); + this->writeData(0x20); this->writeData(0x22); + this->writeData(0x1f); this->writeData(0x1b); + this->writeData(0x23); this->writeData(0x37); this->writeData(0x00); + this->writeData(0x07); + this->writeData(0x02); this->writeData(0x10); + + this->writeCommand(0xe1); + this->writeData(0x0f); this->writeData(0x1b); + this->writeData(0x0f); this->writeData(0x17); + this->writeData(0x33); this->writeData(0x2c); + this->writeData(0x29); this->writeData(0x2e); + this->writeData(0x30); this->writeData(0x30); + this->writeData(0x39); this->writeData(0x3f); + this->writeData(0x00); this->writeData(0x07); + this->writeData(0x03); this->writeData(0x10); + + this->writeCommand(0x2a); + this->writeData(0x00); this->writeData(0x00); + this->writeData(0x00); this->writeData(0x7f); + this->writeCommand(0x2b); + this->writeData(0x00); this->writeData(0x00); + this->writeData(0x00); this->writeData(0x9f); + + this->writeCommand(0xF0); + this->writeData(0x01); + this->writeCommand(0xF6); + this->writeData(0x00); + + this->writeCommand(0x3A); + this->writeData(0x05); + + this->writeCommand(0x29); + + this->clear(); +} + +void DisplayN18::setClippingArea(unsigned char x, unsigned char y, unsigned char width, unsigned char height) +{ + unsigned char data[4] = { 0x00, 0x00, 0x00, 0x00 }; + + data[1] = x; + data[3] = x + width; + this->writeCommand(0x2A); + this->writeData(data, 4); + + data[1] = y; + data[3] = y + height; + this->writeCommand(0x2B); + this->writeData(data, 4); +} + +unsigned short DisplayN18::rgbToShort(unsigned char r, unsigned char g, unsigned char b) +{ + unsigned short red = r; + unsigned short green = g; + unsigned short blue = b; + + red /= 8; + green /= 4; + blue /= 8; + + red &= 0x1F; + green &= 0x3F; + blue &= 0x1F; + + red <<= 3; + blue <<= 8; + green = ((green & 0x7) << 13) + ((green & 0x38) >> 3); + + return red | green | blue; +} + +void DisplayN18::clear(unsigned short backColor) +{ + for (unsigned int i = 0; i < DisplayN18::WIDTH; i += 10) + for (unsigned int j = 0; j < DisplayN18::HEIGHT; j += 8) + this->fillRect(i, j, 10, 8, backColor); +} + +void DisplayN18::draw(const unsigned short* data, int x, int y, int width, int height) +{ + this->setClippingArea(x, y, width - 1, height - 1); + this->writeCommand(0x2C); + this->writeData(reinterpret_cast<const unsigned char*>(data), width * height * 2); +} + +void DisplayN18::setPixel(int x, int y, unsigned short foreColor) +{ + this->draw(&foreColor, x, y, 1, 1); +} + +void DisplayN18::fillRect(int x, int y, int width, int height, unsigned short foreColor) +{ + this->setClippingArea(static_cast<unsigned char>(x), static_cast<unsigned char>(y), static_cast<unsigned char>(width - 1), static_cast<unsigned char>(height)); + + this->writeCommand(0x2C); + + unsigned short buffer[50]; + for (int j = 0; j < sizeof(buffer) / 2; j++) + buffer[j] = foreColor; + + this->rsPin.write(true); + + int i; + for (i = sizeof(buffer); i < height * width * 2; i += sizeof(buffer)) + this->writeData(reinterpret_cast<unsigned char*>(buffer), sizeof(buffer)); + + i -= sizeof(buffer); + if (i != height * width * 2) + this->writeData(reinterpret_cast<unsigned char*>(buffer), height * width * 2 - i); +} + +void DisplayN18::drawRect(int x, int y, int width, int height, unsigned short foreColor) +{ + this->drawLine(x, y, x + width, y, foreColor); + this->drawLine(x, y + height, x + width, y + height, foreColor); + this->drawLine(x, y, x, y + height, foreColor); + this->drawLine(x + width, y, x + width, y + height, foreColor); +} + +void DisplayN18::fillCircle(int x, int y, int radius, unsigned short foreColor) +{ + int f = 1 - radius; + int dd_f_x = 1; + int dd_f_y = -2 * radius; + int x1 = 0; + int y1 = radius; + + for (int i = y - radius; i <= y + radius; i++) + this->setPixel(x, i, foreColor); + + while (x1 < y1) + { + if (f >= 0) + { + y1--; + dd_f_y += 2; + f += dd_f_y; + } + + x1++; + dd_f_x += 2; + f += dd_f_x; + + for (int i = y - y1; i <= y + y1; i++) + { + this->setPixel(x + x1, i, foreColor); + this->setPixel(x - x1, i, foreColor); + } + + for (int i = y - x1; i <= y + x1; i++) + { + this->setPixel(x + y1, i, foreColor); + this->setPixel(x - y1, i, foreColor); + } + } +} + +void DisplayN18::drawCircle(int x, int y, int radius, unsigned short foreColor) +{ + int f = 1 - radius; + int dd_f_x = 1; + int dd_f_y = -2 * radius; + int x1 = 0; + int y1 = radius; + + this->setPixel(x, y + radius, foreColor); + this->setPixel(x, y - radius, foreColor); + this->setPixel(x + radius, y, foreColor); + this->setPixel(x - radius, y, foreColor); + + while (x1 < y1) + { + if (f >= 0) + { + y1--; + dd_f_y += 2; + f += dd_f_y; + } + + x1++; + dd_f_x += 2; + f += dd_f_x; + + this->setPixel(x + x1, y + y1, foreColor); + this->setPixel(x - x1, y + y1, foreColor); + this->setPixel(x + x1, y - y1, foreColor); + this->setPixel(x - x1, y - y1, foreColor); + + this->setPixel(x + y1, y + x1, foreColor); + this->setPixel(x - y1, y + x1, foreColor); + this->setPixel(x + y1, y - x1, foreColor); + this->setPixel(x - y1, y - x1, foreColor); + } +} + +void DisplayN18::drawLine(int x0, int y0, int x1, int y1, unsigned short foreColor) +{ + if (x0 == x1) + { + if (y1 < y0) + { + int temp = y0; + y0 = y1; + y1 = temp; + } + + this->setClippingArea(static_cast<unsigned char>(x0), static_cast<unsigned char>(y0), 0, static_cast<unsigned char>(y1 - y0 - 1)); + this->writeCommand(0x2C); + + unsigned short data[DisplayN18::STEP]; + for (int i = 0; i < DisplayN18::STEP; i++) + data[i] = foreColor; + + for (unsigned char thisY = y0; thisY < y1; thisY += DisplayN18::STEP) + this->writeData(reinterpret_cast<unsigned char*>(data), (thisY + DisplayN18::STEP <= y1 ? DisplayN18::STEP : y1 - thisY) * 2); + + return; + } + + if (y0 == y1) + { + if (x1 < x0) + { + int temp = x0; + x0 = x1; + x1 = temp; + } + + this->setClippingArea(static_cast<unsigned char>(x0), static_cast<unsigned char>(y0), static_cast<unsigned char>(x1 - x0 - 1), 0); + this->writeCommand(0x2C); + + unsigned short data[DisplayN18::STEP]; + for (int i = 0; i < DisplayN18::STEP; i++) + data[i] = foreColor; + + for (unsigned char thisX = x0; thisX < x1; thisX += DisplayN18::STEP) + this->writeData(reinterpret_cast<unsigned char*>(data), (thisX + DisplayN18::STEP <= x1 ? DisplayN18::STEP : x1 - thisX) * 2); + + return; + } + + int t; + bool steep = ((y1 - y0) < 0 ? -(y1 - y0) : (y1 - y0)) > ((x1 - x0) < 0 ? -(x1 - x0) : (x1 - x0)); + + if (steep) + { + t = x0; + x0 = y0; + y0 = t; + t = x1; + x1 = y1; + y1 = t; + } + + if (x0 > x1) + { + t = x0; + x0 = x1; + x1 = t; + + t = y0; + y0 = y1; + y1 = t; + } + + int dx, dy; + dx = x1 - x0; + dy = (y1 - y0) < 0 ? -(y1 - y0) : (y1 - y0); + + int err = (dx / 2); + int ystep; + + if (y0 < y1) + ystep = 1; + else + ystep = -1; + + for (; x0 < x1; x0++) + { + if (steep) + this->setPixel(y0, x0, foreColor); + else + this->setPixel(x0, y0, foreColor); + + err -= dy; + + if (err < 0) + { + y0 += (char)ystep; + err += dx; + } + } +} + +unsigned char characters[95 * 5] = { + 0x00, 0x00, 0x00, 0x00, 0x00, /* Space 0x20 */ + 0x00, 0x00, 0x4f, 0x00, 0x00, /* ! */ + 0x00, 0x07, 0x00, 0x07, 0x00, /* " */ + 0x14, 0x7f, 0x14, 0x7f, 0x14, /* # */ + 0x24, 0x2a, 0x7f, 0x2a, 0x12, /* $ */ + 0x23, 0x13, 0x08, 0x64, 0x62, /* % */ + 0x36, 0x49, 0x55, 0x22, 0x20, /* & */ + 0x00, 0x05, 0x03, 0x00, 0x00, /* ' */ + 0x00, 0x1c, 0x22, 0x41, 0x00, /* ( */ + 0x00, 0x41, 0x22, 0x1c, 0x00, /* ) */ + 0x14, 0x08, 0x3e, 0x08, 0x14, /* // */ + 0x08, 0x08, 0x3e, 0x08, 0x08, /* + */ + 0x50, 0x30, 0x00, 0x00, 0x00, /* , */ + 0x08, 0x08, 0x08, 0x08, 0x08, /* - */ + 0x00, 0x60, 0x60, 0x00, 0x00, /* . */ + 0x20, 0x10, 0x08, 0x04, 0x02, /* / */ + 0x3e, 0x51, 0x49, 0x45, 0x3e, /* 0 0x30 */ + 0x00, 0x42, 0x7f, 0x40, 0x00, /* 1 */ + 0x42, 0x61, 0x51, 0x49, 0x46, /* 2 */ + 0x21, 0x41, 0x45, 0x4b, 0x31, /* 3 */ + 0x18, 0x14, 0x12, 0x7f, 0x10, /* 4 */ + 0x27, 0x45, 0x45, 0x45, 0x39, /* 5 */ + 0x3c, 0x4a, 0x49, 0x49, 0x30, /* 6 */ + 0x01, 0x71, 0x09, 0x05, 0x03, /* 7 */ + 0x36, 0x49, 0x49, 0x49, 0x36, /* 8 */ + 0x06, 0x49, 0x49, 0x29, 0x1e, /* 9 */ + 0x00, 0x36, 0x36, 0x00, 0x00, /* : */ + 0x00, 0x56, 0x36, 0x00, 0x00, /* ; */ + 0x08, 0x14, 0x22, 0x41, 0x00, /* < */ + 0x14, 0x14, 0x14, 0x14, 0x14, /* = */ + 0x00, 0x41, 0x22, 0x14, 0x08, /* > */ + 0x02, 0x01, 0x51, 0x09, 0x06, /* ? */ + 0x3e, 0x41, 0x5d, 0x55, 0x1e, /* @ 0x40 */ + 0x7e, 0x11, 0x11, 0x11, 0x7e, /* A */ + 0x7f, 0x49, 0x49, 0x49, 0x36, /* B */ + 0x3e, 0x41, 0x41, 0x41, 0x22, /* C */ + 0x7f, 0x41, 0x41, 0x22, 0x1c, /* D */ + 0x7f, 0x49, 0x49, 0x49, 0x41, /* E */ + 0x7f, 0x09, 0x09, 0x09, 0x01, /* F */ + 0x3e, 0x41, 0x49, 0x49, 0x7a, /* G */ + 0x7f, 0x08, 0x08, 0x08, 0x7f, /* H */ + 0x00, 0x41, 0x7f, 0x41, 0x00, /* I */ + 0x20, 0x40, 0x41, 0x3f, 0x01, /* J */ + 0x7f, 0x08, 0x14, 0x22, 0x41, /* K */ + 0x7f, 0x40, 0x40, 0x40, 0x40, /* L */ + 0x7f, 0x02, 0x0c, 0x02, 0x7f, /* M */ + 0x7f, 0x04, 0x08, 0x10, 0x7f, /* N */ + 0x3e, 0x41, 0x41, 0x41, 0x3e, /* O */ + 0x7f, 0x09, 0x09, 0x09, 0x06, /* P 0x50 */ + 0x3e, 0x41, 0x51, 0x21, 0x5e, /* Q */ + 0x7f, 0x09, 0x19, 0x29, 0x46, /* R */ + 0x26, 0x49, 0x49, 0x49, 0x32, /* S */ + 0x01, 0x01, 0x7f, 0x01, 0x01, /* T */ + 0x3f, 0x40, 0x40, 0x40, 0x3f, /* U */ + 0x1f, 0x20, 0x40, 0x20, 0x1f, /* V */ + 0x3f, 0x40, 0x38, 0x40, 0x3f, /* W */ + 0x63, 0x14, 0x08, 0x14, 0x63, /* X */ + 0x07, 0x08, 0x70, 0x08, 0x07, /* Y */ + 0x61, 0x51, 0x49, 0x45, 0x43, /* Z */ + 0x00, 0x7f, 0x41, 0x41, 0x00, /* [ */ + 0x02, 0x04, 0x08, 0x10, 0x20, /* \ */ + 0x00, 0x41, 0x41, 0x7f, 0x00, /* ] */ + 0x04, 0x02, 0x01, 0x02, 0x04, /* ^ */ + 0x40, 0x40, 0x40, 0x40, 0x40, /* _ */ + 0x00, 0x00, 0x03, 0x05, 0x00, /* ` 0x60 */ + 0x20, 0x54, 0x54, 0x54, 0x78, /* a */ + 0x7F, 0x44, 0x44, 0x44, 0x38, /* b */ + 0x38, 0x44, 0x44, 0x44, 0x44, /* c */ + 0x38, 0x44, 0x44, 0x44, 0x7f, /* d */ + 0x38, 0x54, 0x54, 0x54, 0x18, /* e */ + 0x04, 0x04, 0x7e, 0x05, 0x05, /* f */ + 0x08, 0x54, 0x54, 0x54, 0x3c, /* g */ + 0x7f, 0x08, 0x04, 0x04, 0x78, /* h */ + 0x00, 0x44, 0x7d, 0x40, 0x00, /* i */ + 0x20, 0x40, 0x44, 0x3d, 0x00, /* j */ + 0x7f, 0x10, 0x28, 0x44, 0x00, /* k */ + 0x00, 0x41, 0x7f, 0x40, 0x00, /* l */ + 0x7c, 0x04, 0x7c, 0x04, 0x78, /* m */ + 0x7c, 0x08, 0x04, 0x04, 0x78, /* n */ + 0x38, 0x44, 0x44, 0x44, 0x38, /* o */ + 0x7c, 0x14, 0x14, 0x14, 0x08, /* p 0x70 */ + 0x08, 0x14, 0x14, 0x14, 0x7c, /* q */ + 0x7c, 0x08, 0x04, 0x04, 0x00, /* r */ + 0x48, 0x54, 0x54, 0x54, 0x24, /* s */ + 0x04, 0x04, 0x3f, 0x44, 0x44, /* t */ + 0x3c, 0x40, 0x40, 0x20, 0x7c, /* u */ + 0x1c, 0x20, 0x40, 0x20, 0x1c, /* v */ + 0x3c, 0x40, 0x30, 0x40, 0x3c, /* w */ + 0x44, 0x28, 0x10, 0x28, 0x44, /* x */ + 0x0c, 0x50, 0x50, 0x50, 0x3c, /* y */ + 0x44, 0x64, 0x54, 0x4c, 0x44, /* z */ + 0x08, 0x36, 0x41, 0x41, 0x00, /* { */ + 0x00, 0x00, 0x77, 0x00, 0x00, /* | */ + 0x00, 0x41, 0x41, 0x36, 0x08, /* } */ + 0x08, 0x08, 0x2a, 0x1c, 0x08 /* ~ */ +}; + +void DisplayN18::drawCharacter(int x, int y, const char character, unsigned short foreColor, unsigned short backColor, unsigned char fontSize) +{ + if (character > 126 || character < 32) + return; + + unsigned short* horizontal = new unsigned short[DisplayN18::CHAR_HEIGHT * fontSize]; + for (int i = 0; i < DisplayN18::CHAR_WIDTH; i++) + { + for (int j = 0; j < DisplayN18::CHAR_HEIGHT; j++) + for (int k = 0; k < fontSize; k++) + horizontal[j * fontSize + k] = characters[(character - 32) * 5 + i] & (1 << j) ? foreColor : backColor; + + for (int k = 0; k < fontSize; k++) + this->draw(horizontal, x + i * fontSize + k, y, 1, DisplayN18::CHAR_HEIGHT * fontSize); + } + + for (int i = 0; i < DisplayN18::CHAR_HEIGHT; i++) + for (int k = 0; k < fontSize; k++) + horizontal[i * fontSize + k] = backColor; + + for (int k = 0; k < fontSize; k++) + this->draw(horizontal, x + DisplayN18::CHAR_WIDTH * fontSize + k, y, 1, DisplayN18::CHAR_HEIGHT * fontSize); + + delete[] horizontal; +} + +void DisplayN18::drawString(int x, int y, const char* str, unsigned short foreColor, unsigned short backColor, unsigned char fontSize) +{ + if (*str == '\0') + return; + + do + { + this->drawCharacter(x, y, *str, foreColor, backColor, fontSize); + + x += (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) * fontSize; + } while (*(++str) != '\0'); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DisplayN18.h Mon Nov 10 13:04:42 2014 +0000 @@ -0,0 +1,54 @@ +#include "mbed.h" + +#pragma once + +class DisplayN18 +{ + static const unsigned char STEP = 4; + + DigitalOut resetPin; + DigitalOut backlightPin; + DigitalOut rsPin; + DigitalOut csPin; + SPI spi; + + void writeCommand(unsigned char command); + void writeData(unsigned char data); + void writeData(const unsigned char* data, unsigned int length); + + void reset(); + void initialize(); + void setClippingArea(unsigned char x, unsigned char y, unsigned char width, unsigned char height); + + public: + DisplayN18(); + + static const unsigned short BLUE = 0x00F8; + static const unsigned short GREEN = 0xE007; + static const unsigned short RED = 0x1F00; + static const unsigned short WHITE = 0xFFFF; + static const unsigned short BLACK = 0x0000; + + static const unsigned int WIDTH = 160; + static const unsigned int HEIGHT = 128; + static const unsigned char CHAR_WIDTH = 5; + static const unsigned char CHAR_HEIGHT = 8; + static const unsigned char CHAR_SPACING = 1; + + static unsigned short rgbToShort(unsigned char r, unsigned char g, unsigned char b); + + void clear(unsigned short backColor = 0x0000); + void draw(const unsigned short* data, int x, int y, int width, int height); + void setPixel(int x, int y, unsigned short foreColor); + + void fillRect(int x, int y, int width, int height, unsigned short foreColor); + void drawRect(int x, int y, int width, int height, unsigned short foreColor); + + void fillCircle(int x, int y, int radius, unsigned short foreColor); + void drawCircle(int x, int y, int radius, unsigned short foreColor); + + void drawLine(int x0, int y0, int x1, int y1, unsigned short foreColor); + + void drawCharacter(int x, int y, const char character, unsigned short foreColor, unsigned short backColor, unsigned char fontSize = 1); + void drawString(int x, int y, const char* str, unsigned short foreColor, unsigned short backColor, unsigned char fontSize = 1); +}; \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Main.cpp Mon Nov 10 13:04:42 2014 +0000 @@ -0,0 +1,225 @@ +#include "mbed.h" + +#include "DisplayN18.h" + +DigitalIn left(P0_14, PullUp); +DigitalIn right(P0_11, PullUp); +DigitalIn down(P0_12, PullUp); +DigitalIn up(P0_13, PullUp); +DigitalIn square(P0_16, PullUp); +DigitalIn circle(P0_1, PullUp); +DigitalOut led1(P0_9); +DigitalOut led2(P0_8); +PwmOut pwm(P0_18); +AnalogIn ain(P0_15); +I2C i2c(P0_5, P0_4); +DisplayN18 disp; + +int ballX, ballY, ballSpeedX, ballSpeedY, paddleX, pwmTicksLeft, lives; + +const char* LOSE_1 = "You lose."; +const char* LOSE_2 = "Press circle to restart."; +const char* SPLASH = "Press circle to start."; + +const char I2C_ADDR = 0x1C << 1; +const int BALL_RADIUS = 4; +const int MIN_BALL_SPEED = 2; +const int MAX_BALL_SPEED = 4; +const int PADDLE_WIDTH = 40; +const int PADDLE_HEIGHT = 5; +const int PADDLE_SPEED = 5; +const int BOUNCE_SOUND_TICKS = 2; + +void initializeBall() { + ballX = DisplayN18::WIDTH / 2 - BALL_RADIUS; + ballY = DisplayN18::HEIGHT / 4 - BALL_RADIUS; + + ballSpeedX = rand() % (MAX_BALL_SPEED - MIN_BALL_SPEED) + MIN_BALL_SPEED; + ballSpeedY = rand() % (MAX_BALL_SPEED - MIN_BALL_SPEED) + MIN_BALL_SPEED; + + if (rand() % 2) + ballSpeedX *= -1; + + if (rand() % 2) + ballSpeedY *= -1; + + if (ballSpeedX == 0) + ballSpeedX++; + + if (ballSpeedY == 0) + ballSpeedY++; +} + +void initializeGame() { + initializeBall(); + + paddleX = DisplayN18::WIDTH / 2 - PADDLE_WIDTH / 2; + pwmTicksLeft = 0; + lives = 4; + + pwm.period_ms(1); + pwm.write(0.00); + + disp.clear(); +} + +void drawString(const char* str, int y) { + disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) * strlen(str) / 2, y, str, DisplayN18::WHITE, DisplayN18::BLACK); +} + +void doSplash() { + drawString(SPLASH, DisplayN18::HEIGHT / 2 - DisplayN18::CHAR_HEIGHT / 2); + + while (circle) + wait_ms(1); +} + +void clearPaddle() { + disp.fillRect(paddleX, DisplayN18::HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT, DisplayN18::BLACK); +} + +void drawPaddle() { + disp.fillRect(paddleX, DisplayN18::HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT, DisplayN18::BLUE); +} + +void updatePaddle() { + if (left) + paddleX += PADDLE_SPEED; + + if (right) + paddleX -= PADDLE_SPEED; +} + +void clearBall() { + disp.fillRect(ballX - BALL_RADIUS, ballY - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2, DisplayN18::BLACK); +} + +void drawBall() { + disp.fillRect(ballX - BALL_RADIUS, ballY - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2, DisplayN18::RED); +} + +void updateBall() { + ballX += ballSpeedX; + ballY += ballSpeedY; +} + +void checkCollision() { + if (paddleX < 0) + paddleX = 0; + + if (paddleX + PADDLE_WIDTH > DisplayN18::WIDTH) + paddleX = DisplayN18::WIDTH - PADDLE_WIDTH; + + if ((ballX - BALL_RADIUS < 0 && ballSpeedX < 0) || (ballX + BALL_RADIUS >= DisplayN18::WIDTH && ballSpeedX > 0)) + ballSpeedX *= -1; + + if (ballY - BALL_RADIUS < 0 && ballSpeedY < 0) + ballSpeedY *= -1; + + if (ballY + BALL_RADIUS >= DisplayN18::HEIGHT - PADDLE_HEIGHT && ballSpeedY > 0) { + if (ballY + BALL_RADIUS >= DisplayN18::HEIGHT) { + initializeBall(); + + lives--; + } + else if (ballX > paddleX && ballX < paddleX + PADDLE_WIDTH) { + ballSpeedY *= -1; + + pwmTicksLeft = BOUNCE_SOUND_TICKS; + } + } +} + +void checkPwm() { + if (pwmTicksLeft == 0) { + pwm.write(0.0); + } + else { + pwmTicksLeft--; + pwm.write(0.5); + } +} + +void checkLives() { + if (lives == 0) { + disp.clear(); + + drawString(LOSE_1, DisplayN18::HEIGHT / 2 - DisplayN18::CHAR_HEIGHT); + drawString(LOSE_2, DisplayN18::HEIGHT / 2); + + while (circle) + wait_ms(1); + + initializeGame(); + } + else { + disp.drawCharacter(0, 0, static_cast<char>(lives + '0'), DisplayN18::WHITE, DisplayN18::BLACK); + } +} + +int read_short(char address) { + char buffer[2]; + + i2c.write(I2C_ADDR, &address, 1, true); + i2c.read(I2C_ADDR | 1, buffer, 2); + + return ((buffer[0] << 2) | (buffer[1] >> 6)) & 0x3F; +} + +int write_register(char address, char value) { + char buffer[2] = { address, value }; + return i2c.write(I2C_ADDR, buffer, 2); +} + +void get_xyz(int& x, int& y, int& z) { + x = read_short(0x01); + y = read_short(0x03); + z = read_short(0x05); + + if (x > 511) x -= 1024; + if (y > 511) y -= 1024; + if (z > 511) z -= 1024; +} + +void print_int(int value, int x, int y) { + char buffer[10]; + int len = sprintf(buffer, "%d", value); + + disp.drawString(x, y, buffer, DisplayN18::WHITE, DisplayN18::BLACK); +} + +int main() { + int x, y, z; + + write_register(0x2A, 0x01); + + //srand(ain.read_u16()); + + //doSplash(); + + //initializeGame(); + + while (true) { + /*clearPaddle(); + clearBall(); + + updatePaddle(); + updateBall(); + + checkCollision(); + + drawPaddle(); + drawBall(); + + checkPwm(); + checkLives();*/ + + get_xyz(x, y, z); + + print_int(x, 0, 0); + print_int(y, 0, 15); + print_int(z, 0, 30); + + wait_ms(100); + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Nov 10 13:04:42 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/552587b429a1 \ No newline at end of file