Some random attempts at programming the retro console
Fork of RETRO_Pong_Mod by
Revision 7:c0f12f624832, committed 2015-02-27
- Comitter:
- loop
- Date:
- Fri Feb 27 20:21:32 2015 +0000
- Parent:
- 6:20788dfdb7b8
- Child:
- 8:c63981a45c95
- Commit message:
- (Almost) ported to LCD_ST7735 lib - faster!
Changed in this revision
--- a/DisplayN18.cpp Thu Feb 26 20:29:02 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,484 +0,0 @@ -#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; - - ystep = y0 < y1 ? 1 : -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
--- a/DisplayN18.h Thu Feb 26 20:29:02 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -#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 short GREY = 0x8888; - - 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
--- a/Game.cpp Thu Feb 26 20:29:02 2015 +0000 +++ b/Game.cpp Fri Feb 27 20:21:32 2015 +0000 @@ -6,6 +6,10 @@ // - Adjusted collision detection to add ball speed every 10 paddle hits // - Added scoring // - Added mute function (not fully implemented...needs to be set up on a button). +// Further mods buy loop (loop23) +// Changes: +// - Removed useless stuff +// - Read accelerometer in game #include "Game.h" @@ -16,7 +20,18 @@ const char* Game::LIVES = "Lives: "; const char* Game::SCORE = "Score: "; -Game::Game() : left(P0_14, PullUp), right(P0_11, PullUp), down(P0_12, PullUp), up(P0_13, PullUp), square(P0_16, PullUp), circle(P0_1, PullUp), led1(P0_9), led2(P0_8), pwm(P0_18), ain(P0_15), i2c(P0_5, P0_4) { +Game::Game(): left(P0_14, PullUp), + right(P0_11, PullUp), + down(P0_12, PullUp), + up(P0_13, PullUp), + square(P0_16, PullUp), + circle(P0_1, PullUp), + led1(P0_9), + led2(P0_8), + pwm(P0_18), + ain(P0_15), + i2c(P0_5, P0_4), + disp(P0_19, P0_20, P0_7, P0_21, P0_22, P1_15, P0_2, LCD_ST7735::RGB) { srand(this->ain.read_u16()); this->lastUp = false; @@ -26,9 +41,9 @@ this->i2c.frequency(400); this->writeRegister(0x2A, 0x01); - this->colors[0] = DisplayN18::RED; - this->colors[1] = DisplayN18::GREEN; - this->colors[2] = DisplayN18::BLUE; + this->colors[0] = Color565::Red; + this->colors[1] = Color565::Green; + this->colors[2] = Color565::Blue; this->initialize(); } @@ -53,27 +68,34 @@ return val / 512.0; } +void Game::getXY(double& x, double& y) { + char buffer[4]; + this->readRegisters(0x01, buffer, 4); + x = this->convert(buffer); + y = this->convert(buffer + 2); +} + void Game::getXYZ(double& x, double& y, double& z) { char buffer[6]; - - this->readRegisters(0x01, buffer, 6); - + this->readRegisters(0x01, buffer, 4); x = this->convert(buffer); y = this->convert(buffer + 2); z = this->convert(buffer + 4); + } + void Game::printDouble(double value, int x, int y) { char buffer[10]; int len = sprintf(buffer, "%.1f", value); - this->disp.drawString(x, y, buffer, DisplayN18::WHITE, DisplayN18::BLACK); + this->disp.drawString(font_ibm, x, y, buffer); } void Game::drawAxes() { for (int i = 0; i < 3; i++) { - this->disp.drawLine(0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING), 0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT, DisplayN18::WHITE); - this->disp.drawLine(0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT / 2, DisplayN18::WIDTH, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT / 2, DisplayN18::WHITE); + this->disp.drawLine(0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING), 0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT, Color565::White); + this->disp.drawLine(0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT / 2, disp.getWidth(), i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT / 2, Color565::White); } } @@ -93,9 +115,9 @@ } void Game::checkGraphReset() { - if (this->graphX > DisplayN18::WIDTH) { + if (this->graphX > disp.getWidth()) { this->graphX = 0; - this->disp.clear(); + this->disp.clearScreen(); this->drawAxes(); } } @@ -105,17 +127,17 @@ this->pwmTicksLeft = 0; this->lives = 4; this->score = 0; - this->muted = true; + this->muted = false; this->pwm.period(1); this->pwm.write(0.00); - - this->disp.clear(); + this->disp.setOrientation(LCD_ST7735::Rotate270, false); + this->disp.clearScreen(); } void Game::initializeBall() { - this->ballX = DisplayN18::WIDTH / 2 - Game::BALL_RADIUS; - this->ballY = DisplayN18::HEIGHT / 4 - Game::BALL_RADIUS; + this->ballX = disp.getWidth() / 2 - Game::BALL_RADIUS; + this->ballY = disp.getHeight() / 4 - Game::BALL_RADIUS; this->ballSpeedX = 0; this->ballSpeedY = 0; this->ballAccelX = 0; @@ -123,8 +145,8 @@ } void Game::readAccel() { - double x, y, z; - this->getXYZ(x, y, z); + double x, y; + this->getXY(x, y); x = x * 8; y = y * 8; this->ballAccelX = (int)x; @@ -138,18 +160,15 @@ this->checkButtons(); if (this->mode) { - if ((tcount % 10) == 0) { + if ((tcount++ % 10) == 0) { this->readAccel(); }; this->clearBall(); this->updateBall(); this->checkCollision(); this->drawBall(); - this->checkPwm(); - this->checkLives(); - tcount++; - //wait_ms(1); - + // this->checkPwm(); + //this->checkLives(); } else { double x, y, z; @@ -169,7 +188,7 @@ //this->muted = !this->muted; this->mode = !this->mode; - this->disp.clear(); + this->disp.clearScreen(); if (!this->mode) { this->graphX = 0; @@ -215,31 +234,34 @@ } void Game::drawString(const char* str, int y) { - this->disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) * strlen(str) / 2, y, str, DisplayN18::WHITE, DisplayN18::BLACK); + this->disp.drawString(font_ibm, + this->disp.getWidth() / 2 - (CHAR_WIDTH + CHAR_SPACING) * strlen(str) / 2, + y, str); } - void Game::showSplashScreen() { - this->drawString(Game::SPLASH_1, DisplayN18::HEIGHT / 2 - DisplayN18::CHAR_HEIGHT / 2); - this->drawString(Game::SPLASH_2, DisplayN18::HEIGHT / 2 + DisplayN18::CHAR_HEIGHT / 2); + this->drawString(Game::SPLASH_1, this->disp.getHeight() / 2 - CHAR_HEIGHT / 2); + this->drawString(Game::SPLASH_2, this->disp.getHeight() / 2 + CHAR_HEIGHT / 2); while (this->circle.read()) - wait_ms(1); + wait_ms(5); - this->disp.clear(); + this->disp.clearScreen(); } void Game::clearBall() { - this->disp.fillRect(this->ballX - Game::BALL_RADIUS, ballY - Game::BALL_RADIUS, Game::BALL_RADIUS * 2, Game::BALL_RADIUS * 2, DisplayN18::GREY); - //this->disp.fillCircle(this->ballX - Game::BALL_RADIUS, this->ballY - Game::BALL_RADIUS, Game::BALL_RADIUS, DisplayN18::BLACK); - //this->disp.fillCircle(this->ballX, this->ballY, Game::BALL_RADIUS, DisplayN18::GREY); - //this->disp.setPixel(this->ballX, this->ballY, DisplayN18::GREY); + this->disp.fillRect(ballX - BALL_RADIUS, + ballY - BALL_RADIUS, + ballX + BALL_RADIUS, + ballY + BALL_RADIUS, + Color565::Black); } void Game::drawBall() { - this->disp.fillRect(this->ballX - Game::BALL_RADIUS, ballY - Game::BALL_RADIUS, Game::BALL_RADIUS * 2, Game::BALL_RADIUS * 2, DisplayN18::RED); - //this->disp.fillCircle(this->ballX - Game::BALL_RADIUS, ballY - Game::BALL_RADIUS, Game::BALL_RADIUS, DisplayN18::RED); - //this->disp.fillCircle(this->ballX, ballY, Game::BALL_RADIUS, DisplayN18::RED); - //this->disp.setPixel(this->ballX, this->ballY, DisplayN18::GREEN); + this->disp.fillRect(ballX - BALL_RADIUS, + ballY - BALL_RADIUS, + ballX + BALL_RADIUS, + ballY + BALL_RADIUS, + Color565::Red); } void Game::updateBall() { @@ -252,7 +274,7 @@ void Game::checkCollision() { // Does bounds checking first.. if ((this->ballX - Game::BALL_RADIUS * 2 <= 0 && this->ballSpeedX < 0) || - (this->ballX + Game::BALL_RADIUS * 2 >= DisplayN18::WIDTH && this->ballSpeedX > 0)) { + (this->ballX + Game::BALL_RADIUS * 2 >= disp.getWidth() && this->ballSpeedX > 0)) { this->ballSpeedX *= -1; if(!this->muted) { this->pwm.period_ms(2); @@ -260,7 +282,7 @@ } } if ((this->ballY - Game::BALL_RADIUS * 2 <= 0 && this->ballSpeedY < 0) || - (this->ballY + Game::BALL_RADIUS * 2 >= DisplayN18::HEIGHT && this->ballSpeedY > 0)){ + (this->ballY + Game::BALL_RADIUS * 2 >= disp.getHeight() && this->ballSpeedY > 0)){ this->ballSpeedY *= -1; if(!this->muted) { this->pwm.period_ms(2); @@ -303,12 +325,13 @@ } } } -*/ char buf[10]; int a = this->score; sprintf(buf, "%d", a); - this->disp.drawString(DisplayN18::WIDTH - (DisplayN18::CHAR_WIDTH * 12), 0, Game::SCORE, DisplayN18::WHITE, DisplayN18::BLACK); - this->disp.drawString(DisplayN18::WIDTH - (DisplayN18::CHAR_WIDTH * 4), 0, buf, DisplayN18::WHITE, DisplayN18::BLACK); + this->disp.drawString(LCDST7735.getWidth() - (DisplayN18::CHAR_WIDTH * 12), 0, Game::SCORE, Color565::White, DisplayN18::BLACK); + this->disp.drawString(LCDST7735.getWidth() - (DisplayN18::CHAR_WIDTH * 4), 0, buf, Color565::White, DisplayN18::BLACK); +*/ + } void Game::checkPwm() { @@ -323,10 +346,10 @@ void Game::checkLives() { if (this->lives == 0) { - this->disp.clear(); + this->disp.clearScreen(); - this->drawString(Game::LOSE_1, DisplayN18::HEIGHT / 2 - DisplayN18::CHAR_HEIGHT); - this->drawString(Game::LOSE_2, DisplayN18::HEIGHT / 2); + this->drawString(Game::LOSE_1, disp.getHeight() / 2 - CHAR_HEIGHT); + this->drawString(Game::LOSE_2, disp.getHeight() / 2); if(!this->muted) { this->pwm.period(1.0/220); @@ -351,7 +374,7 @@ this->initialize(); } else { - this->disp.drawString(0, 0, Game::LIVES, DisplayN18::WHITE, DisplayN18::BLACK); - this->disp.drawCharacter(DisplayN18::CHAR_WIDTH * 8, 0, static_cast<char>(this->lives + '0'), DisplayN18::WHITE, DisplayN18::BLACK); + this->disp.drawString(font_ibm, 0, 0, Game::LIVES); + //this->disp.drawCharacter(DisplayN18::CHAR_WIDTH * 8, 0, static_cast<char>(this->lives + '0'), Color565::White, DisplayN18::BLACK); } } \ No newline at end of file
--- a/Game.h Thu Feb 26 20:29:02 2015 +0000 +++ b/Game.h Fri Feb 27 20:21:32 2015 +0000 @@ -1,6 +1,7 @@ #include "mbed.h" - -#include "DisplayN18.h" +#include "Color565.h" +#include "font_IBM.h" +#include "LCD_ST7735.h" #pragma once @@ -21,6 +22,10 @@ static const int GRAPH_HEIGHT = 40; static const int GRAPH_SPACING = 2; static const char I2C_ADDR = 0x1C << 1; + + static const int CHAR_WIDTH = 8; + static const int CHAR_HEIGHT = 8; + static const int CHAR_SPACING = 0; // Start with a ball.. let's see what becomes int ballX; @@ -50,11 +55,13 @@ PwmOut pwm; AnalogIn ain; I2C i2c; - DisplayN18 disp; + LCD_ST7735 disp; void readRegisters(char address, char* buffer, int len); int writeRegister(char address, char value); + void getXY(double& x, double& y); void getXYZ(double& x, double& y, double& z); + double convert(char* buffer); void printDouble(double value, int x, int y);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LCD_ST7735.lib Fri Feb 27 20:21:32 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/taylorza/code/LCD_ST7735/#c94d0a2c2ba0