Revised code. The original has been removed.

Dependencies:   USBDevice mbed

Files at this revision

API Documentation at this revision

Comitter:
willgeorge
Date:
Tue Feb 03 22:11:06 2015 +0000
Commit message:
Updated code. I have removed the original code

Changed in this revision

DisplayN18.cpp Show annotated file Show diff for this revision Revisions of this file
DisplayN18.h Show annotated file Show diff for this revision Revisions of this file
Game.cpp Show annotated file Show diff for this revision Revisions of this file
USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DisplayN18.cpp	Tue Feb 03 22:11:06 2015 +0000
@@ -0,0 +1,485 @@
+
+#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');
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DisplayN18.h	Tue Feb 03 22:11:06 2015 +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/Game.cpp	Tue Feb 03 22:11:06 2015 +0000
@@ -0,0 +1,414 @@
+
+
+//I have removed Library LCD_ST7735 that was in the original post.
+//Chris Taylor (taylorza) has reminded me that I was not using his library as I had stated.
+
+//I know nothing about payouts on a 'real' slot machine so I just made some up...
+//Be creative and make your own!
+
+//There is no calculated payback percentage (Odds).
+//Fruits displayed and payout values for two of a kind are chosen by random numbers.
+//LUCKY7 - All payouts are fixed values
+
+
+#include "mbed.h"
+#include <stdarg.h>
+#include <stdlib.h>
+#include "USBSerial.h"
+#include <string>
+
+#include "DisplayN18.h"
+
+ bool spin = false;
+ 
+ AnalogIn ain(P0_15);  //One of the user header pins
+ 
+ DigitalOut led1(P0_9, 0);
+ DigitalOut led2(P0_8, 1);
+ DigitalOut Buzzer(P0_18);
+ 
+ DigitalIn up(P0_13, PullUp);
+ DigitalIn down(P0_12, PullUp);
+ DigitalIn left(P0_14, PullUp);
+ DigitalIn right(P0_11, PullUp);
+
+ DigitalIn pbRobot(P0_16, PullUp);
+ 
+ //Falling edge (hit) generates an interrupt
+ //Just wanted to try an interrupt
+ InterruptIn pbSpaceship(P0_1);
+ 
+ //Random numbers for reels()
+ int r1 = 0;
+ int r2 = 0;
+ int r3 = 0;
+ int r4 = 0;
+ int r5 = 0;
+ int r6 = 0;
+ 
+ //Virtual serial port over USB.
+ //Used for debug output only
+ USBSerial serial;
+
+ DisplayN18 disp;
+ 
+ bool doublepay = false;
+ bool win = false;
+ 
+ int cash = 100; //Start with
+ int cashpay = 0; //Win or loss
+ 
+ const char * const bar[] = {
+ "BAR ",
+ "BELL ",
+ "ORANGE ",
+ "LEMON ",
+ "LUCKY7 ",
+ "CHERRY " };
+
+ const char* msg1 = "";
+ const char* msg2 = "";
+ const char* msg3 = "";
+ 
+ //Screen width using spaces
+ const char* szErase = "                          "; //26 spaces
+
+//From E-Dice
+void soundBuzzer(int Hz)
+{
+    int ticks = Hz/64;
+    int tickCount = 0;
+    float frequency = 1/(float)Hz;
+    
+    while(tickCount < ticks)
+    {
+        wait(frequency);
+        Buzzer = true;
+        wait(frequency);
+        Buzzer = false;
+        tickCount++;
+    }
+}
+//
+
+void reels(void)
+{
+    //Erase current text (-val = position from bottom of LCD)
+    //Fruit bars
+    disp.drawString(0, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT -58, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
+    
+    //Erase current text 
+    //DOUBLE PAY (May not be used but we will erase that position anyway)
+    disp.drawString(0, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT + 73, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
+        
+    while(spin) {
+        
+        r1 = (rand() % 6);  //Fruit 1
+        r2 = (rand() % 6);  //Fruit 2
+        r3 = (rand() % 6);  //Fruit 3
+        r4 = (rand() % 50); //Double Pay if 34,13,7
+        r5 = (rand() % 2);  //LOSE! 1 or 2 dollars
+        r6 = (rand() % 4) +1;  //cashpay on two of a kind. (+1 for no zero wanted)
+        
+        //Use if wanted to show the random number values
+        serial.printf(" %d %d %d %d %d %d\r\n",r1,r2,r3,r4,r5,r6);
+                        
+        msg1 = bar[r1];
+        msg2 = bar[r2];
+        msg3 = bar[r3];
+        
+        char buf[27] = "";
+        strncat(buf, msg1, sizeof buf);
+        strncat(buf, msg2, sizeof buf - strlen(buf));
+        strncat(buf, msg3, sizeof buf - strlen(buf));
+    
+        //Erase current text then draw new text centered
+        //Fruit bars
+        disp.drawString(0, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT -58, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
+        disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
+            strlen(buf) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 58, buf, DisplayN18::GREEN, DisplayN18::BLACK);
+        
+        //Robot button is Not pressed
+        if(pbRobot) {
+            //YOUR PLAY
+            disp.drawString(0, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT -78, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
+        }
+        //
+        wait(0.1);
+        
+        //Robot button was pressed
+        if(!pbRobot) {
+            led2 = 1;
+            led1 = 0;
+            
+            char buf[27] = "";
+            sprintf(buf, "YOUR PLAY - Cash %d ", cash);
+            
+            //Erase current text then draw new text centered
+            //YOUR PLAY
+            disp.drawString(0, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT -78, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
+            disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
+                strlen(buf) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 78, buf, DisplayN18::GREEN, DisplayN18::BLACK);
+            
+            //Erase current text
+            //WIN/LOSS Cash at hand
+            disp.drawString(0, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT -16, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
+            
+            spin = false;
+            
+            //Show fruit bars left to right one at a time
+            //1 second interval
+            for(int i =0; i <= 2; i++)
+            {  
+                msg1 = bar[r1];
+                msg2 = bar[r2];
+                msg3 = bar[r3];
+                char buf[27] = "";
+                
+                if(i == 0) {
+                    strncat(buf, msg1, sizeof buf);    
+                }
+                if(i == 1) {
+                    strncat(buf, msg1, sizeof buf);
+                    strncat(buf, msg2, sizeof buf - strlen(buf));  
+                }
+                if(i == 2) {
+                    strncat(buf, msg1, sizeof buf);
+                    strncat(buf, msg2, sizeof buf - strlen(buf));
+                    strncat(buf, msg3, sizeof buf - strlen(buf));     
+                }
+                
+                //Erase current text then draw new text centered
+                //Fruit bars
+                disp.drawString(0, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT -58, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
+                disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
+                    strlen(buf) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 58, buf, DisplayN18::RED, DisplayN18::BLACK);
+                    
+                wait(1);
+                
+                //Erase current text then draw new text centered
+                //Fruit bars
+                disp.drawString(0, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT -58, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
+                disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
+                    strlen(buf) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 58, buf, DisplayN18::GREEN, DisplayN18::BLACK);                
+            } //for
+            
+            win = false;
+
+            //bar[2] = ORANGE
+            if ((msg1 == bar[2]) && (msg2 == bar[2]) && (msg3 == bar[2]))
+            {
+                cash = cash + 4;
+                cashpay = 4;
+                win = true;
+            }
+            if ((msg1 == bar[2]) && (msg2 == bar[2]))
+            {
+                cash = cash + r6;
+                cashpay = r6;
+                win = true;
+            }
+            if ((msg2 == bar[2]) && (msg3 == bar[2]))
+            {
+                cash = cash + r6;
+                cashpay = r6;
+                win = true;
+            }
+            //
+            
+            //bar[3] = LEMON
+            if ((msg1 == bar[3]) && (msg2 == bar[3]) && (msg3 == bar[3]))
+            {
+                cash = cash + 4;
+                cashpay = 4;
+                win = true;
+            }
+            if ((msg1 == bar[3]) && (msg2 == bar[3]))
+            {
+                cash = cash + r6;
+                cashpay = r6;
+                win = true;
+            }
+            if ((msg2 == bar[3]) && (msg3 == bar[3]))
+            {
+                cash = cash + r6;
+                cashpay = r6;
+                win = true;
+            }
+            //
+                       
+            //bar[5] = CHERRY
+            if ((msg1 == bar[5]) && (msg2 == bar[5]) && (msg3 == bar[5]))
+            {
+                cash = cash + 6;
+                cashpay = 6;
+                win = true;
+            }
+            if ((msg1 == bar[5]) && (msg2 == bar[5]))
+            {
+                cash = cash + r6;
+                cashpay = r6;
+                win = true;
+            }
+            if ((msg2 == bar[5]) && (msg3 == bar[5]))
+            {
+                cash = cash + r6;
+                cashpay = r6;
+                win = true;
+            }
+            //
+            
+            //bar[1] = BELL
+            if ((msg1 == bar[1]) && (msg2 == bar[1]) && (msg3 == bar[1]))
+            {
+                cash = cash + 6;
+                cashpay = 6;
+                win = true;
+            }
+            if ((msg1 == bar[1]) && (msg2 == bar[1]))
+            {
+                cash = cash + r6;
+                cashpay = r6;
+                win = true;
+            }
+            if ((msg2 == bar[1]) && (msg3 == bar[1]))
+            {
+                cash = cash + r6;
+                cashpay = r6;
+                win = true;
+            }
+            //
+            
+            //bar[0] = BAR
+            if ((msg1 == bar[0]) && (msg2 == bar[0]) && (msg3 == bar[0]))
+            {
+                cash = cash + 10;
+                cashpay = 10;
+                win = true;
+            }
+            if ((msg1 == bar[0]) && (msg2 == bar[0]))
+            {
+                cash = cash + r6;
+                cashpay = r6;
+                win = true;
+            }
+            if ((msg2 == bar[0]) && (msg3 == bar[0]))
+            {
+                cash = cash + r6;
+                cashpay = r6;
+                win = true;
+            }
+            //
+            
+            //bar[4] = LUCKY7
+            if ((msg1 == bar[4]) && (msg2 == bar[4]) && (msg3 == bar[4]))
+            {
+                cash = cash + 20;
+                cashpay = 20;
+                win = true;
+            }
+            if ((msg1 == bar[4]) && (msg2 == bar[4]))
+            {
+                cash = cash + 10;
+                cashpay = 10;
+                win = true;
+            }
+            if ((msg2 == bar[4]) && (msg3 == bar[4]))
+            {
+                cash = cash + 8;
+                cashpay = 8;
+                win = true;
+            }
+            //
+            
+            if(win)
+            {                
+                if(r4 == 35 || r4 == 13 || r4 == 7) {
+                    sprintf(buf, "DOUBLE PAY %d  %d ", cashpay * 2, cash + cashpay);
+                }
+                else {
+                    sprintf(buf, "WIN! Pay: %d Cash: %d ", cashpay, cash);
+                }   
+            }
+            //
+            
+            if(!win)
+            {
+                if(r5 == 0) {
+                    cash = cash - 1;
+                    sprintf(buf, "LOSE! 1 dollar - %d ", cash);
+                }
+                else if(r5 == 1) {
+                    cash = cash - 2;
+                    sprintf(buf, "LOSE! 2 dollars - %d ", cash);
+                }
+                
+            }
+            //
+            
+            //Erase current text then draw new text centered
+            //WIN/LOSS Cash at hand
+            disp.drawString(0, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT -16, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
+            disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
+                strlen(buf) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 16, buf, DisplayN18::GREEN, DisplayN18::BLACK);
+
+            if(win) {
+               for(int i = 0; i < 5; i++) {
+                    soundBuzzer(800);
+                    wait(.1);
+                    soundBuzzer(600);
+                    wait(.1);
+                    soundBuzzer(400);
+                    wait(.1);
+                }
+            }
+        } //if
+    } //while
+}
+//
+
+//Spaceship/RETRO button begins random pick of fruit bars
+void pbSpaceship_hit_interrupt (void) {
+    led1 = 1;
+    led2 = 0;
+    
+    spin = true;
+    reels();
+}
+//
+
+int main() {
+    
+    //For read_u16() - See: http://developer.mbed.org/handbook/AnalogIn
+    srand(ain.read_u16());
+    
+    led1 = 0;
+    led2 = 0;
+    pbRobot.mode(PullUp);
+    //Delay for initial pullup to take effect
+    wait(.01);
+    
+    pbSpaceship.mode(PullUp);
+    //Delay for initial pullup to take effect
+    wait(.01);
+    
+    //Attach address of interrupt handler routine for pushbutton
+    pbSpaceship.fall(&pbSpaceship_hit_interrupt);
+     
+    disp.clear();
+    wait(.5);
+    
+    //Splash
+    char buf[27] = "";
+    sprintf(buf,"   OUTRAGEOUS RETRO Slot");
+    disp.drawString(0, 0, buf, DisplayN18::GREEN, DisplayN18::BLACK);
+    sprintf(buf,"Press RETRO to pull Lever");
+    disp.drawString(0, DisplayN18::CHAR_HEIGHT + 3, buf, DisplayN18::RED, DisplayN18::BLACK);
+    sprintf(buf,"Press ROBOT Lever up");
+    disp.drawString(0, DisplayN18::CHAR_HEIGHT + 13, buf, DisplayN18::RED, DisplayN18::BLACK);
+        
+    while (true) {     
+        wait(.1);
+    } //while
+}
+//
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Tue Feb 03 22:11:06 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/USBDevice/#3b1c43ac045c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Feb 03 22:11:06 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5
\ No newline at end of file