A Stock Exchange Game using IO devices, the 128x128 uLCD, and other components
Dependencies: 4DGL-uLCD-SE SDFileSystem ShiftBrite mbed wave_player
Fork of WavePlayer_HelloWorld by
main.cpp@2:3585cd51c765, 2016-11-03 (annotated)
- Committer:
- RohanIyengar
- Date:
- Thu Nov 03 19:35:44 2016 +0000
- Revision:
- 2:3585cd51c765
- Parent:
- 1:5b8e223e983d
Created project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 1:5b8e223e983d | 1 | #include "mbed.h" |
4180_1 | 1:5b8e223e983d | 2 | #include "SDFileSystem.h" |
4180_1 | 1:5b8e223e983d | 3 | #include "wave_player.h" |
RohanIyengar | 2:3585cd51c765 | 4 | #include "ShiftBrite.h" |
RohanIyengar | 2:3585cd51c765 | 5 | #include "uLCD_4DGL.h" |
RohanIyengar | 2:3585cd51c765 | 6 | #include <mpr121.h> |
4180_1 | 1:5b8e223e983d | 7 | |
RohanIyengar | 2:3585cd51c765 | 8 | // Stock exchange game |
RohanIyengar | 2:3585cd51c765 | 9 | // Rohan Kumar Iyengar & Sai Sathiesh Rajan |
RohanIyengar | 2:3585cd51c765 | 10 | |
RohanIyengar | 2:3585cd51c765 | 11 | // Set up Mbed LEDs |
RohanIyengar | 2:3585cd51c765 | 12 | DigitalOut led1(LED1); |
RohanIyengar | 2:3585cd51c765 | 13 | DigitalOut led2(LED2); |
RohanIyengar | 2:3585cd51c765 | 14 | DigitalOut led3(LED3); |
RohanIyengar | 2:3585cd51c765 | 15 | DigitalOut led4(LED4); |
RohanIyengar | 2:3585cd51c765 | 16 | |
RohanIyengar | 2:3585cd51c765 | 17 | // Create the interrupt receiver object on pin 26 |
RohanIyengar | 2:3585cd51c765 | 18 | InterruptIn interrupt(p26); |
RohanIyengar | 2:3585cd51c765 | 19 | |
RohanIyengar | 2:3585cd51c765 | 20 | // Setup the i2c bus on pins 9 and 10 |
RohanIyengar | 2:3585cd51c765 | 21 | I2C i2c(p9, p10); |
RohanIyengar | 2:3585cd51c765 | 22 | |
RohanIyengar | 2:3585cd51c765 | 23 | // Setup the Mpr121: |
RohanIyengar | 2:3585cd51c765 | 24 | // constructor(i2c object, i2c address of the mpr121) |
RohanIyengar | 2:3585cd51c765 | 25 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
RohanIyengar | 2:3585cd51c765 | 26 | |
RohanIyengar | 2:3585cd51c765 | 27 | uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin; |
RohanIyengar | 2:3585cd51c765 | 28 | |
RohanIyengar | 2:3585cd51c765 | 29 | SPI spi(p11, p12, p13); |
RohanIyengar | 2:3585cd51c765 | 30 | |
RohanIyengar | 2:3585cd51c765 | 31 | ShiftBrite myBrite(p15,p16,spi); //latch, enable, spi |
4180_1 | 1:5b8e223e983d | 32 | |
4180_1 | 1:5b8e223e983d | 33 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
4180_1 | 1:5b8e223e983d | 34 | |
4180_1 | 1:5b8e223e983d | 35 | AnalogOut DACout(p18); |
4180_1 | 1:5b8e223e983d | 36 | |
4180_1 | 1:5b8e223e983d | 37 | wave_player waver(&DACout); |
4180_1 | 1:5b8e223e983d | 38 | |
RohanIyengar | 2:3585cd51c765 | 39 | int stockPrices [7] = {0,0,0,0,0,0,0}; // array to store trend data for current stock |
RohanIyengar | 2:3585cd51c765 | 40 | int maxEle = -1; // easily accessible max value of current trend data |
RohanIyengar | 2:3585cd51c765 | 41 | int minEle = 200; // easily accessible min value of current trend data |
RohanIyengar | 2:3585cd51c765 | 42 | int gameState = 0; // used to transition between game states |
RohanIyengar | 2:3585cd51c765 | 43 | int stocksChosen = 0; // number of stocks user has chosen already |
RohanIyengar | 2:3585cd51c765 | 44 | char* currentName; // current stock name |
RohanIyengar | 2:3585cd51c765 | 45 | char* chosenName1; // first chosen stock name |
RohanIyengar | 2:3585cd51c765 | 46 | char* chosenName2; // second chosen stock name |
RohanIyengar | 2:3585cd51c765 | 47 | int stockGain1 = 0; // first stock's profit/loss |
RohanIyengar | 2:3585cd51c765 | 48 | int stockGain2 = 0; // second stock's profit/loss |
RohanIyengar | 2:3585cd51c765 | 49 | int stockNamePos = 0; // Used to generate next stock name |
RohanIyengar | 2:3585cd51c765 | 50 | int stocksScrolled = 0; // Used to loop around stock names |
RohanIyengar | 2:3585cd51c765 | 51 | |
RohanIyengar | 2:3585cd51c765 | 52 | // Function to get next stock whenever user selects / rejects a stock |
RohanIyengar | 2:3585cd51c765 | 53 | void getNewStock() |
RohanIyengar | 2:3585cd51c765 | 54 | { |
RohanIyengar | 2:3585cd51c765 | 55 | // Select stock name first |
RohanIyengar | 2:3585cd51c765 | 56 | switch (stockNamePos) { |
RohanIyengar | 2:3585cd51c765 | 57 | |
RohanIyengar | 2:3585cd51c765 | 58 | case 0: |
RohanIyengar | 2:3585cd51c765 | 59 | currentName = "Amazon"; |
RohanIyengar | 2:3585cd51c765 | 60 | break; |
RohanIyengar | 2:3585cd51c765 | 61 | case 1: |
RohanIyengar | 2:3585cd51c765 | 62 | currentName = "Google"; |
RohanIyengar | 2:3585cd51c765 | 63 | break; |
RohanIyengar | 2:3585cd51c765 | 64 | case 2: |
RohanIyengar | 2:3585cd51c765 | 65 | currentName = "Microsoft"; |
RohanIyengar | 2:3585cd51c765 | 66 | break; |
RohanIyengar | 2:3585cd51c765 | 67 | case 3: |
RohanIyengar | 2:3585cd51c765 | 68 | currentName = "Apple"; |
RohanIyengar | 2:3585cd51c765 | 69 | break; |
RohanIyengar | 2:3585cd51c765 | 70 | case 4: |
RohanIyengar | 2:3585cd51c765 | 71 | currentName = "IBM"; |
RohanIyengar | 2:3585cd51c765 | 72 | break; |
RohanIyengar | 2:3585cd51c765 | 73 | case 5: |
RohanIyengar | 2:3585cd51c765 | 74 | currentName = "Intel"; |
RohanIyengar | 2:3585cd51c765 | 75 | break; |
RohanIyengar | 2:3585cd51c765 | 76 | case 6: |
RohanIyengar | 2:3585cd51c765 | 77 | currentName = "Cisco"; |
RohanIyengar | 2:3585cd51c765 | 78 | break; |
RohanIyengar | 2:3585cd51c765 | 79 | case 7: |
RohanIyengar | 2:3585cd51c765 | 80 | currentName = "Mathworks"; |
RohanIyengar | 2:3585cd51c765 | 81 | break; |
RohanIyengar | 2:3585cd51c765 | 82 | case 8: |
RohanIyengar | 2:3585cd51c765 | 83 | currentName = "Uber"; |
RohanIyengar | 2:3585cd51c765 | 84 | break; |
RohanIyengar | 2:3585cd51c765 | 85 | case 9: |
RohanIyengar | 2:3585cd51c765 | 86 | currentName = "Facebook"; |
RohanIyengar | 2:3585cd51c765 | 87 | break; |
RohanIyengar | 2:3585cd51c765 | 88 | } |
RohanIyengar | 2:3585cd51c765 | 89 | |
RohanIyengar | 2:3585cd51c765 | 90 | // Generate trend data for stock |
RohanIyengar | 2:3585cd51c765 | 91 | srand(time(NULL)); |
RohanIyengar | 2:3585cd51c765 | 92 | float currentPrice = (rand() % 100) + 50; // initialize price |
RohanIyengar | 2:3585cd51c765 | 93 | int trend = 0; |
RohanIyengar | 2:3585cd51c765 | 94 | int range = 20; |
RohanIyengar | 2:3585cd51c765 | 95 | int bound = 10; |
RohanIyengar | 2:3585cd51c765 | 96 | stockPrices[0] = currentPrice; |
RohanIyengar | 2:3585cd51c765 | 97 | maxEle = currentPrice; |
RohanIyengar | 2:3585cd51c765 | 98 | minEle = currentPrice; |
RohanIyengar | 2:3585cd51c765 | 99 | // Generate random data for first six days |
RohanIyengar | 2:3585cd51c765 | 100 | for (int i = 1; i < 6; i++) { |
RohanIyengar | 2:3585cd51c765 | 101 | float change = rand() % range - bound; |
RohanIyengar | 2:3585cd51c765 | 102 | currentPrice = currentPrice + change; |
RohanIyengar | 2:3585cd51c765 | 103 | stockPrices[i] = currentPrice; |
RohanIyengar | 2:3585cd51c765 | 104 | if (currentPrice > maxEle) { |
RohanIyengar | 2:3585cd51c765 | 105 | maxEle = currentPrice; |
RohanIyengar | 2:3585cd51c765 | 106 | } |
RohanIyengar | 2:3585cd51c765 | 107 | if (currentPrice < minEle) { |
RohanIyengar | 2:3585cd51c765 | 108 | minEle = currentPrice; |
RohanIyengar | 2:3585cd51c765 | 109 | } |
RohanIyengar | 2:3585cd51c765 | 110 | int wildCard = rand() % 10 - 5; |
RohanIyengar | 2:3585cd51c765 | 111 | if (change >= 0) { |
RohanIyengar | 2:3585cd51c765 | 112 | trend++; |
RohanIyengar | 2:3585cd51c765 | 113 | range = 15; |
RohanIyengar | 2:3585cd51c765 | 114 | bound = 5 + wildCard; |
RohanIyengar | 2:3585cd51c765 | 115 | } else { |
RohanIyengar | 2:3585cd51c765 | 116 | trend--; |
RohanIyengar | 2:3585cd51c765 | 117 | range = 15; |
RohanIyengar | 2:3585cd51c765 | 118 | bound = 10 + wildCard; |
RohanIyengar | 2:3585cd51c765 | 119 | } |
RohanIyengar | 2:3585cd51c765 | 120 | } |
RohanIyengar | 2:3585cd51c765 | 121 | |
RohanIyengar | 2:3585cd51c765 | 122 | // Add more random data for 7th day |
RohanIyengar | 2:3585cd51c765 | 123 | srand(time(NULL)); |
RohanIyengar | 2:3585cd51c765 | 124 | int finalWildCard = rand() % 8 - 4 + trend; |
RohanIyengar | 2:3585cd51c765 | 125 | float finalPrice = (1 + (finalWildCard / 34.0)) * currentPrice; |
RohanIyengar | 2:3585cd51c765 | 126 | stockPrices[6] = finalPrice; |
RohanIyengar | 2:3585cd51c765 | 127 | |
RohanIyengar | 2:3585cd51c765 | 128 | // Increment counters to allow next stock retrieval to be smooth |
RohanIyengar | 2:3585cd51c765 | 129 | stockNamePos = (stockNamePos + 1) % 10; |
RohanIyengar | 2:3585cd51c765 | 130 | stocksScrolled++; |
RohanIyengar | 2:3585cd51c765 | 131 | if (stocksScrolled > 10 && stocksChosen != 2) { |
RohanIyengar | 2:3585cd51c765 | 132 | stocksScrolled = 0; |
RohanIyengar | 2:3585cd51c765 | 133 | stocksChosen = 0; |
RohanIyengar | 2:3585cd51c765 | 134 | stockNamePos = rand() % 10; |
RohanIyengar | 2:3585cd51c765 | 135 | } |
RohanIyengar | 2:3585cd51c765 | 136 | } |
RohanIyengar | 2:3585cd51c765 | 137 | |
RohanIyengar | 2:3585cd51c765 | 138 | // Uses current stock data to print a connected graph of points for that stock |
RohanIyengar | 2:3585cd51c765 | 139 | void printGraph() { |
RohanIyengar | 2:3585cd51c765 | 140 | int x = 4; |
RohanIyengar | 2:3585cd51c765 | 141 | // draw graph axes |
RohanIyengar | 2:3585cd51c765 | 142 | uLCD.line(2, 20, 2, 126, LGREY); |
RohanIyengar | 2:3585cd51c765 | 143 | uLCD.line(2, 126, 126, 126, LGREY); |
RohanIyengar | 2:3585cd51c765 | 144 | float arrayRange = maxEle - minEle; |
RohanIyengar | 2:3585cd51c765 | 145 | float graphRange = 110 - 30; |
RohanIyengar | 2:3585cd51c765 | 146 | float scaleFactor = graphRange / arrayRange; |
RohanIyengar | 2:3585cd51c765 | 147 | int offset = 1; |
RohanIyengar | 2:3585cd51c765 | 148 | for (int i = 0; i < 5; i++) { |
RohanIyengar | 2:3585cd51c765 | 149 | int currDist = stockPrices[i] - minEle; |
RohanIyengar | 2:3585cd51c765 | 150 | int y = (int) (110 - scaleFactor * currDist); |
RohanIyengar | 2:3585cd51c765 | 151 | int nextDist = stockPrices[i + 1] - minEle; |
RohanIyengar | 2:3585cd51c765 | 152 | int y2 = (int) (110 - scaleFactor * nextDist); |
RohanIyengar | 2:3585cd51c765 | 153 | // Draw lines between each pair of points w/ color based on trend |
RohanIyengar | 2:3585cd51c765 | 154 | if (y2 - y > 0) { |
RohanIyengar | 2:3585cd51c765 | 155 | uLCD.line(x, y, x + 20, y2, RED); |
RohanIyengar | 2:3585cd51c765 | 156 | } else if (y2 - y < 0) { |
RohanIyengar | 2:3585cd51c765 | 157 | uLCD.line(x, y, x + 20, y2, GREEN); |
RohanIyengar | 2:3585cd51c765 | 158 | } else { |
RohanIyengar | 2:3585cd51c765 | 159 | uLCD.line(x, y, x + 20, y2, WHITE); |
RohanIyengar | 2:3585cd51c765 | 160 | } |
RohanIyengar | 2:3585cd51c765 | 161 | uLCD.filled_circle(x, y, 2, BLUE); |
RohanIyengar | 2:3585cd51c765 | 162 | uLCD.filled_circle(x + 20, y2, 2, BLUE); |
RohanIyengar | 2:3585cd51c765 | 163 | // Scale to row, col format of uLCD |
RohanIyengar | 2:3585cd51c765 | 164 | int point1row = (int) ((x / 7.1) - .5); |
RohanIyengar | 2:3585cd51c765 | 165 | int point1col = (int) ((y /8.0) - .5); |
RohanIyengar | 2:3585cd51c765 | 166 | int point2col = (int) ((y2 / 8.0) - .5); |
RohanIyengar | 2:3585cd51c765 | 167 | int point2row = (int) (((x + 20) / 7.1) - .5); |
RohanIyengar | 2:3585cd51c765 | 168 | char buf1 [10]; |
RohanIyengar | 2:3585cd51c765 | 169 | char buf2 [10]; |
RohanIyengar | 2:3585cd51c765 | 170 | // NEED sprintf because printf on LCD has inexplicable behavior |
RohanIyengar | 2:3585cd51c765 | 171 | sprintf(buf1, "%d", stockPrices[i]); |
RohanIyengar | 2:3585cd51c765 | 172 | sprintf(buf2, "%d", stockPrices[i + 1]); |
RohanIyengar | 2:3585cd51c765 | 173 | uLCD.text_string(buf1, point1row + 1, point1col + 1, FONT_12X16, WHITE); |
RohanIyengar | 2:3585cd51c765 | 174 | offset*=-1; |
RohanIyengar | 2:3585cd51c765 | 175 | //uLCD.locate(point2row + offset, point2col + offset); |
RohanIyengar | 2:3585cd51c765 | 176 | //uLCD.printf("%3i", stockPrices[i+1]); |
RohanIyengar | 2:3585cd51c765 | 177 | uLCD.text_string(buf2, point2row + 1, point2col + 1, FONT_12X16, WHITE); |
RohanIyengar | 2:3585cd51c765 | 178 | x+=20; |
RohanIyengar | 2:3585cd51c765 | 179 | } |
RohanIyengar | 2:3585cd51c765 | 180 | } |
RohanIyengar | 2:3585cd51c765 | 181 | |
RohanIyengar | 2:3585cd51c765 | 182 | // Interrupt when touch sensor is pressed |
RohanIyengar | 2:3585cd51c765 | 183 | void fallInterrupt() { |
RohanIyengar | 2:3585cd51c765 | 184 | int key_code=0; |
RohanIyengar | 2:3585cd51c765 | 185 | int i=0; |
RohanIyengar | 2:3585cd51c765 | 186 | int value=mpr121.read(0x00); |
RohanIyengar | 2:3585cd51c765 | 187 | value +=mpr121.read(0x01)<<8; |
RohanIyengar | 2:3585cd51c765 | 188 | // LED demo mod code inspired by J. Hamblen |
RohanIyengar | 2:3585cd51c765 | 189 | i=0; |
RohanIyengar | 2:3585cd51c765 | 190 | // puts key number out to LEDs for demo |
RohanIyengar | 2:3585cd51c765 | 191 | for (i=0; i<12; i++) { |
RohanIyengar | 2:3585cd51c765 | 192 | if (((value>>i)&0x01)==1) key_code=i+1; |
RohanIyengar | 2:3585cd51c765 | 193 | } |
RohanIyengar | 2:3585cd51c765 | 194 | led4=key_code & 0x01; |
RohanIyengar | 2:3585cd51c765 | 195 | led3=(key_code>>1) & 0x01; |
RohanIyengar | 2:3585cd51c765 | 196 | led2=(key_code>>2) & 0x01; |
RohanIyengar | 2:3585cd51c765 | 197 | led1=(key_code>>3) & 0x01; |
RohanIyengar | 2:3585cd51c765 | 198 | if (key_code == 9 && stocksChosen < 2) { |
RohanIyengar | 2:3585cd51c765 | 199 | gameState = 2; |
RohanIyengar | 2:3585cd51c765 | 200 | } |
RohanIyengar | 2:3585cd51c765 | 201 | if (key_code == 4 && stocksChosen < 2) { |
RohanIyengar | 2:3585cd51c765 | 202 | gameState = 1; |
RohanIyengar | 2:3585cd51c765 | 203 | } |
RohanIyengar | 2:3585cd51c765 | 204 | } |
RohanIyengar | 2:3585cd51c765 | 205 | |
4180_1 | 1:5b8e223e983d | 206 | int main() |
4180_1 | 1:5b8e223e983d | 207 | { |
RohanIyengar | 2:3585cd51c765 | 208 | myBrite.Write(1023, 1023 ,0); |
RohanIyengar | 2:3585cd51c765 | 209 | bool finished = false; |
RohanIyengar | 2:3585cd51c765 | 210 | // Do title sequence one time |
RohanIyengar | 2:3585cd51c765 | 211 | // Welcome Sequence on Game Startup |
RohanIyengar | 2:3585cd51c765 | 212 | uLCD.cls(); |
RohanIyengar | 2:3585cd51c765 | 213 | uLCD.text_string("Welcome to the\n Stock Exchange\n Challenge!" , 1, 1, FONT_12X16, GREEN); |
RohanIyengar | 2:3585cd51c765 | 214 | wait(2); |
RohanIyengar | 2:3585cd51c765 | 215 | uLCD.cls(); |
RohanIyengar | 2:3585cd51c765 | 216 | srand(time(NULL)); |
RohanIyengar | 2:3585cd51c765 | 217 | stockNamePos = rand() % 10; |
RohanIyengar | 2:3585cd51c765 | 218 | // Title Screen Image |
RohanIyengar | 2:3585cd51c765 | 219 | uLCD.media_init(); |
RohanIyengar | 2:3585cd51c765 | 220 | uLCD.set_sector_address(0x003A, 0x7C26); |
RohanIyengar | 2:3585cd51c765 | 221 | uLCD.display_image(0,15); |
RohanIyengar | 2:3585cd51c765 | 222 | wait(3); |
RohanIyengar | 2:3585cd51c765 | 223 | uLCD.cls(); |
RohanIyengar | 2:3585cd51c765 | 224 | uLCD.text_string("Look at stock \n trends then -" , 1, 1, FONT_12X16, BLUE); |
RohanIyengar | 2:3585cd51c765 | 225 | uLCD.text_string("Pick two stocks \nthat will make\n you win BIG!", 1, 5, FONT_12X16, GREEN); |
RohanIyengar | 2:3585cd51c765 | 226 | uLCD.text_string("Press button 8\n to choose the\n current stock", 1, 9, FONT_12X16, RED); |
RohanIyengar | 2:3585cd51c765 | 227 | uLCD.text_string("Press button 3\n to move onto\n the next stock", 1, 13, FONT_12X16, WHITE); |
RohanIyengar | 2:3585cd51c765 | 228 | wait(4); |
RohanIyengar | 2:3585cd51c765 | 229 | uLCD.cls(); |
RohanIyengar | 2:3585cd51c765 | 230 | uLCD.text_string("Press 3 to begin!", 1, 7, FONT_12X16, GREEN); |
RohanIyengar | 2:3585cd51c765 | 231 | // Now allow interrupts from touch sensor to trigger |
RohanIyengar | 2:3585cd51c765 | 232 | interrupt.fall(&fallInterrupt); |
RohanIyengar | 2:3585cd51c765 | 233 | interrupt.mode(PullUp); |
RohanIyengar | 2:3585cd51c765 | 234 | |
RohanIyengar | 2:3585cd51c765 | 235 | while (!finished) { |
RohanIyengar | 2:3585cd51c765 | 236 | // State to move to next stock |
RohanIyengar | 2:3585cd51c765 | 237 | if (gameState == 1) { |
RohanIyengar | 2:3585cd51c765 | 238 | getNewStock(); |
RohanIyengar | 2:3585cd51c765 | 239 | uLCD.cls(); |
RohanIyengar | 2:3585cd51c765 | 240 | uLCD.text_string("Stock: ", 1, 1, FONT_12X16, GREEN); |
RohanIyengar | 2:3585cd51c765 | 241 | uLCD.text_string(currentName, 7, 1, FONT_12X16, GREEN); |
RohanIyengar | 2:3585cd51c765 | 242 | printGraph(); |
RohanIyengar | 2:3585cd51c765 | 243 | gameState = 0; |
RohanIyengar | 2:3585cd51c765 | 244 | } |
RohanIyengar | 2:3585cd51c765 | 245 | // State when stock is picked |
RohanIyengar | 2:3585cd51c765 | 246 | if (gameState == 2) { |
RohanIyengar | 2:3585cd51c765 | 247 | if (stocksChosen == 0) { |
RohanIyengar | 2:3585cd51c765 | 248 | chosenName1 = currentName; |
RohanIyengar | 2:3585cd51c765 | 249 | stockGain1 = stockPrices[6] - stockPrices[5]; |
RohanIyengar | 2:3585cd51c765 | 250 | } else { |
RohanIyengar | 2:3585cd51c765 | 251 | chosenName2 = currentName; |
RohanIyengar | 2:3585cd51c765 | 252 | stockGain2 = stockPrices[6] - stockPrices[5]; |
RohanIyengar | 2:3585cd51c765 | 253 | } |
RohanIyengar | 2:3585cd51c765 | 254 | stocksChosen++; |
RohanIyengar | 2:3585cd51c765 | 255 | if (stocksChosen > 1) { |
RohanIyengar | 2:3585cd51c765 | 256 | finished = true; |
RohanIyengar | 2:3585cd51c765 | 257 | } else { |
RohanIyengar | 2:3585cd51c765 | 258 | gameState = 1; |
RohanIyengar | 2:3585cd51c765 | 259 | } |
RohanIyengar | 2:3585cd51c765 | 260 | } |
RohanIyengar | 2:3585cd51c765 | 261 | wait(.5); |
RohanIyengar | 2:3585cd51c765 | 262 | } |
RohanIyengar | 2:3585cd51c765 | 263 | // End game summary + statistics |
RohanIyengar | 2:3585cd51c765 | 264 | int totalGain = stockGain1 + stockGain2; |
RohanIyengar | 2:3585cd51c765 | 265 | uLCD.cls(); |
RohanIyengar | 2:3585cd51c765 | 266 | if (totalGain <= 0) { |
RohanIyengar | 2:3585cd51c765 | 267 | // Print losing image |
RohanIyengar | 2:3585cd51c765 | 268 | myBrite.Write(1023 ,0,0); |
RohanIyengar | 2:3585cd51c765 | 269 | uLCD.media_init(); |
RohanIyengar | 2:3585cd51c765 | 270 | uLCD.set_sector_address(0x003A, 0x7C01); |
RohanIyengar | 2:3585cd51c765 | 271 | uLCD.display_image(0,22); |
RohanIyengar | 2:3585cd51c765 | 272 | } else { |
RohanIyengar | 2:3585cd51c765 | 273 | myBrite.Write(0, 1023, 0); |
RohanIyengar | 2:3585cd51c765 | 274 | // Print winning image |
RohanIyengar | 2:3585cd51c765 | 275 | uLCD.media_init(); |
RohanIyengar | 2:3585cd51c765 | 276 | uLCD.set_sector_address(0x003A, 0x7C57); |
RohanIyengar | 2:3585cd51c765 | 277 | uLCD.display_image(0,15); |
RohanIyengar | 2:3585cd51c765 | 278 | } |
RohanIyengar | 2:3585cd51c765 | 279 | wait(1); |
RohanIyengar | 2:3585cd51c765 | 280 | uLCD.cls(); |
RohanIyengar | 2:3585cd51c765 | 281 | // Print picks and statistics |
RohanIyengar | 2:3585cd51c765 | 282 | uLCD.locate(0,0); |
RohanIyengar | 2:3585cd51c765 | 283 | if (stockGain1 < 0) { |
RohanIyengar | 2:3585cd51c765 | 284 | uLCD.color(RED); |
RohanIyengar | 2:3585cd51c765 | 285 | } else if (stockGain1 > 0) { |
RohanIyengar | 2:3585cd51c765 | 286 | uLCD.color(GREEN); |
RohanIyengar | 2:3585cd51c765 | 287 | } else { |
RohanIyengar | 2:3585cd51c765 | 288 | uLCD.color(WHITE); |
RohanIyengar | 2:3585cd51c765 | 289 | } |
RohanIyengar | 2:3585cd51c765 | 290 | uLCD.printf("Pick 1: %s\n\r", chosenName1); |
RohanIyengar | 2:3585cd51c765 | 291 | uLCD.printf("Stock Gain 1: $%d\n\r\n", stockGain1); |
RohanIyengar | 2:3585cd51c765 | 292 | if (stockGain2 < 0) { |
RohanIyengar | 2:3585cd51c765 | 293 | uLCD.color(RED); |
RohanIyengar | 2:3585cd51c765 | 294 | } else if (stockGain2 > 0) { |
RohanIyengar | 2:3585cd51c765 | 295 | uLCD.color(GREEN); |
RohanIyengar | 2:3585cd51c765 | 296 | } else { |
RohanIyengar | 2:3585cd51c765 | 297 | uLCD.color(WHITE); |
RohanIyengar | 2:3585cd51c765 | 298 | } |
RohanIyengar | 2:3585cd51c765 | 299 | uLCD.printf("Pick 2: %s\n\r", chosenName2); |
RohanIyengar | 2:3585cd51c765 | 300 | uLCD.printf("Stock Gain 2: $%d\n\r\n", stockGain2); |
RohanIyengar | 2:3585cd51c765 | 301 | if (totalGain < 0) { |
RohanIyengar | 2:3585cd51c765 | 302 | uLCD.color(RED); |
RohanIyengar | 2:3585cd51c765 | 303 | } else if (totalGain > 0) { |
RohanIyengar | 2:3585cd51c765 | 304 | uLCD.color(GREEN); |
RohanIyengar | 2:3585cd51c765 | 305 | } else { |
RohanIyengar | 2:3585cd51c765 | 306 | uLCD.color(WHITE); |
RohanIyengar | 2:3585cd51c765 | 307 | } |
RohanIyengar | 2:3585cd51c765 | 308 | uLCD.printf("\nTotal Gain: $%d\n\r", totalGain); |
RohanIyengar | 2:3585cd51c765 | 309 | |
RohanIyengar | 2:3585cd51c765 | 310 | // Play appropriate winning/losing sound |
RohanIyengar | 2:3585cd51c765 | 311 | if (totalGain > 0) { |
RohanIyengar | 2:3585cd51c765 | 312 | FILE* wave_file; |
RohanIyengar | 2:3585cd51c765 | 313 | wave_file = fopen("/sd/Win.wav", "r"); |
RohanIyengar | 2:3585cd51c765 | 314 | waver.play(wave_file); |
RohanIyengar | 2:3585cd51c765 | 315 | fclose(wave_file); |
RohanIyengar | 2:3585cd51c765 | 316 | } else { |
RohanIyengar | 2:3585cd51c765 | 317 | FILE* wave_file2; |
RohanIyengar | 2:3585cd51c765 | 318 | wave_file2 = fopen("/sd/Lose.wav", "r"); |
RohanIyengar | 2:3585cd51c765 | 319 | waver.play(wave_file2); |
RohanIyengar | 2:3585cd51c765 | 320 | fclose(wave_file2); |
RohanIyengar | 2:3585cd51c765 | 321 | } |
4180_1 | 1:5b8e223e983d | 322 | } |