Bluetooth
Dependencies: 4DGL-uLCD-SE mbed MMA8452 PinDetect SDFileSystem wave_player
main.cpp@2:655c8a5c2ad8, 23 months ago (annotated)
- Committer:
- jwalker366
- Date:
- Sat Dec 03 17:30:14 2022 +0000
- Revision:
- 2:655c8a5c2ad8
- Parent:
- 1:f264a7ecd285
- Child:
- 3:2499b6bda80f
Updated some of what Ridwan did, I still need to add some integration to the main, but the functions should be ready to print dice and read the accelerometer.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ridwangs7 | 1:f264a7ecd285 | 1 | /* |
ridwangs7 | 1:f264a7ecd285 | 2 | Jared Walker |
ridwangs7 | 1:f264a7ecd285 | 3 | Ridwan Sadiq |
ridwangs7 | 1:f264a7ecd285 | 4 | Emanuel Abdul-Salaam |
ridwangs7 | 1:f264a7ecd285 | 5 | Juan Padilla |
ridwangs7 | 1:f264a7ecd285 | 6 | */ |
jwalker366 | 2:655c8a5c2ad8 | 7 | #include <time.h> /* time_t, struct tm, difftime, time, mktime */ |
jwalker366 | 2:655c8a5c2ad8 | 8 | #include <ctime> |
jwalker366 | 0:088af403d779 | 9 | #include "mbed.h" |
ridwangs7 | 1:f264a7ecd285 | 10 | #include "uLCD_4DGL.h" |
ridwangs7 | 1:f264a7ecd285 | 11 | |
jwalker366 | 2:655c8a5c2ad8 | 12 | MMA8452 acc(p28, p27, 40000); //instantiate an acc object |
ridwangs7 | 1:f264a7ecd285 | 13 | BusOut myled(LED1,LED2,LED3,LED4); |
ridwangs7 | 1:f264a7ecd285 | 14 | Serial blue(p9,p10); |
ridwangs7 | 1:f264a7ecd285 | 15 | Serial pc(USBTX, USBRX); |
ridwangs7 | 1:f264a7ecd285 | 16 | uLCD_4DGL ulcd(p13,p14,p11); |
ridwangs7 | 1:f264a7ecd285 | 17 | |
ridwangs7 | 1:f264a7ecd285 | 18 | |
ridwangs7 | 1:f264a7ecd285 | 19 | int but_pushed = -1; // ==1 -> pb1 pushed, == 2 -> pb2 pushed |
ridwangs7 | 1:f264a7ecd285 | 20 | bool selectedDiceArray[5]; |
jwalker366 | 2:655c8a5c2ad8 | 21 | int accFlag = 1; // this is gonna be the variable that the accelerometer flips to roll the dice |
ridwangs7 | 1:f264a7ecd285 | 22 | |
jwalker366 | 0:088af403d779 | 23 | |
ridwangs7 | 1:f264a7ecd285 | 24 | int selectDice() |
ridwangs7 | 1:f264a7ecd285 | 25 | { |
ridwangs7 | 1:f264a7ecd285 | 26 | char bnum=0; |
ridwangs7 | 1:f264a7ecd285 | 27 | char bhit=0; |
ridwangs7 | 1:f264a7ecd285 | 28 | int diceNum = 0; |
ridwangs7 | 1:f264a7ecd285 | 29 | |
ridwangs7 | 1:f264a7ecd285 | 30 | if (blue.getc()=='!') { |
ridwangs7 | 1:f264a7ecd285 | 31 | if (blue.getc()=='B') { //button data packet |
ridwangs7 | 1:f264a7ecd285 | 32 | bnum = blue.getc(); //button number |
ridwangs7 | 1:f264a7ecd285 | 33 | bhit = blue.getc(); //1=hit, 0=release |
ridwangs7 | 1:f264a7ecd285 | 34 | if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK? |
ridwangs7 | 1:f264a7ecd285 | 35 | if ((bnum>='1')&&(bnum<='5')){ //is a number button 1..4 |
ridwangs7 | 1:f264a7ecd285 | 36 | diceNum = bnum-48; |
ridwangs7 | 1:f264a7ecd285 | 37 | selectedDiceArray[diceNum]= 1; |
ridwangs7 | 1:f264a7ecd285 | 38 | if (bhit=='1') { |
ridwangs7 | 1:f264a7ecd285 | 39 | myled = bnum - '0'; |
ridwangs7 | 1:f264a7ecd285 | 40 | } else { |
ridwangs7 | 1:f264a7ecd285 | 41 | myled = '0'; |
ridwangs7 | 1:f264a7ecd285 | 42 | } |
ridwangs7 | 1:f264a7ecd285 | 43 | } |
ridwangs7 | 1:f264a7ecd285 | 44 | } |
ridwangs7 | 1:f264a7ecd285 | 45 | } |
ridwangs7 | 1:f264a7ecd285 | 46 | } |
ridwangs7 | 1:f264a7ecd285 | 47 | return diceNum; |
ridwangs7 | 1:f264a7ecd285 | 48 | } |
ridwangs7 | 1:f264a7ecd285 | 49 | |
ridwangs7 | 1:f264a7ecd285 | 50 | |
ridwangs7 | 1:f264a7ecd285 | 51 | void draw_start(){ |
ridwangs7 | 1:f264a7ecd285 | 52 | ulcd.text_width(2); |
ridwangs7 | 1:f264a7ecd285 | 53 | ulcd.text_height(2); |
ridwangs7 | 1:f264a7ecd285 | 54 | ulcd.locate(2,2); |
ridwangs7 | 1:f264a7ecd285 | 55 | ulcd.printf("\n YAHTZEE"); |
ridwangs7 | 1:f264a7ecd285 | 56 | |
ridwangs7 | 1:f264a7ecd285 | 57 | ulcd.text_width(1); |
ridwangs7 | 1:f264a7ecd285 | 58 | ulcd.text_height(1); |
ridwangs7 | 1:f264a7ecd285 | 59 | ulcd.locate(4,8); |
ridwangs7 | 1:f264a7ecd285 | 60 | ulcd.printf("\n Press to Roll"); |
ridwangs7 | 1:f264a7ecd285 | 61 | |
ridwangs7 | 1:f264a7ecd285 | 62 | // Processing |
ridwangs7 | 1:f264a7ecd285 | 63 | |
ridwangs7 | 1:f264a7ecd285 | 64 | |
ridwangs7 | 1:f264a7ecd285 | 65 | } |
ridwangs7 | 1:f264a7ecd285 | 66 | |
jwalker366 | 0:088af403d779 | 67 | |
ridwangs7 | 1:f264a7ecd285 | 68 | void draw_readyRoll(){ |
ridwangs7 | 1:f264a7ecd285 | 69 | ulcd.text_width(1); |
ridwangs7 | 1:f264a7ecd285 | 70 | ulcd.text_height(1); |
ridwangs7 | 1:f264a7ecd285 | 71 | ulcd.locate(4,1); |
ridwangs7 | 1:f264a7ecd285 | 72 | ulcd.printf("\n Ready to Roll"); |
ridwangs7 | 1:f264a7ecd285 | 73 | |
ridwangs7 | 1:f264a7ecd285 | 74 | ulcd.text_width(2); |
ridwangs7 | 1:f264a7ecd285 | 75 | ulcd.text_height(2); |
ridwangs7 | 1:f264a7ecd285 | 76 | ulcd.locate(2,1); |
ridwangs7 | 1:f264a7ecd285 | 77 | ulcd.printf("\n Press\n Roll\n Button\n to Roll"); |
ridwangs7 | 1:f264a7ecd285 | 78 | |
ridwangs7 | 1:f264a7ecd285 | 79 | // Processing |
ridwangs7 | 1:f264a7ecd285 | 80 | |
ridwangs7 | 1:f264a7ecd285 | 81 | |
ridwangs7 | 1:f264a7ecd285 | 82 | } |
ridwangs7 | 1:f264a7ecd285 | 83 | |
ridwangs7 | 1:f264a7ecd285 | 84 | |
ridwangs7 | 1:f264a7ecd285 | 85 | void draw_diceDisp(){ |
ridwangs7 | 1:f264a7ecd285 | 86 | ulcd.text_width(1); |
ridwangs7 | 1:f264a7ecd285 | 87 | ulcd.text_height(1); |
ridwangs7 | 1:f264a7ecd285 | 88 | ulcd.locate(1,1); |
ridwangs7 | 1:f264a7ecd285 | 89 | ulcd.printf("Do you want to select any dice?"); |
ridwangs7 | 1:f264a7ecd285 | 90 | |
ridwangs7 | 1:f264a7ecd285 | 91 | // Processing |
ridwangs7 | 1:f264a7ecd285 | 92 | |
ridwangs7 | 1:f264a7ecd285 | 93 | |
ridwangs7 | 1:f264a7ecd285 | 94 | } |
ridwangs7 | 1:f264a7ecd285 | 95 | |
ridwangs7 | 1:f264a7ecd285 | 96 | |
ridwangs7 | 1:f264a7ecd285 | 97 | void draw_diceSelect(){ |
ridwangs7 | 1:f264a7ecd285 | 98 | ulcd.text_width(1); |
ridwangs7 | 1:f264a7ecd285 | 99 | ulcd.text_height(1); |
ridwangs7 | 1:f264a7ecd285 | 100 | ulcd.locate(1,1); |
ridwangs7 | 1:f264a7ecd285 | 101 | ulcd.printf("Press button 1 to roll again"); |
ridwangs7 | 1:f264a7ecd285 | 102 | ulcd.printf("\n Press button 2 to break"); |
ridwangs7 | 1:f264a7ecd285 | 103 | |
ridwangs7 | 1:f264a7ecd285 | 104 | // Processing |
ridwangs7 | 1:f264a7ecd285 | 105 | |
ridwangs7 | 1:f264a7ecd285 | 106 | |
ridwangs7 | 1:f264a7ecd285 | 107 | } |
ridwangs7 | 1:f264a7ecd285 | 108 | |
ridwangs7 | 1:f264a7ecd285 | 109 | |
ridwangs7 | 1:f264a7ecd285 | 110 | void draw_scoreScreen(){ |
ridwangs7 | 1:f264a7ecd285 | 111 | ulcd.text_width(1); |
ridwangs7 | 1:f264a7ecd285 | 112 | ulcd.text_height(1); |
ridwangs7 | 1:f264a7ecd285 | 113 | ulcd.locate(1,1); |
ridwangs7 | 1:f264a7ecd285 | 114 | ulcd.printf("Please score your round and reset."); |
ridwangs7 | 1:f264a7ecd285 | 115 | |
ridwangs7 | 1:f264a7ecd285 | 116 | // Processing |
ridwangs7 | 1:f264a7ecd285 | 117 | |
ridwangs7 | 1:f264a7ecd285 | 118 | |
ridwangs7 | 1:f264a7ecd285 | 119 | } |
ridwangs7 | 1:f264a7ecd285 | 120 | |
jwalker366 | 2:655c8a5c2ad8 | 121 | // This fuctuion prints a single dice to the screen, but can be used over and over to print every dice |
jwalker366 | 2:655c8a5c2ad8 | 122 | int xarry[] = {22,22,64,64,105};//holds the center point for every dice on the X axis (for uLCD) |
jwalker366 | 2:655c8a5c2ad8 | 123 | int yarry[] = {32,96,32,96,32};//holds the center point for every dice on the Y axis (for uLCD) |
jwalker366 | 2:655c8a5c2ad8 | 124 | void diceLoc(int diceLoc,int diceFaceNum)//diceLoc is the location of the dice (1-5) and diceFaceNum is the number on the dice face (provided by a random number gen |
jwalker366 | 2:655c8a5c2ad8 | 125 | { |
jwalker366 | 2:655c8a5c2ad8 | 126 | |
jwalker366 | 2:655c8a5c2ad8 | 127 | int x1=xarry[diceLoc] + 20; |
jwalker366 | 2:655c8a5c2ad8 | 128 | int x2=xarry[diceLoc] - 20; |
jwalker366 | 2:655c8a5c2ad8 | 129 | int y1=yarry[diceLoc] + 20; |
jwalker366 | 2:655c8a5c2ad8 | 130 | int y2=yarry[diceLoc] - 20; |
jwalker366 | 2:655c8a5c2ad8 | 131 | uLCD.rectangle(x1,y1,x2,y2,WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 132 | switch(diceFaceNum){ |
jwalker366 | 2:655c8a5c2ad8 | 133 | case 1://if dice =1 |
jwalker366 | 2:655c8a5c2ad8 | 134 | uLCD.filled_circle(xarry[diceLoc] , yarry[diceLoc] ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 135 | num1++; |
jwalker366 | 2:655c8a5c2ad8 | 136 | break; |
jwalker366 | 2:655c8a5c2ad8 | 137 | |
jwalker366 | 2:655c8a5c2ad8 | 138 | case 2://if dice =2 |
jwalker366 | 2:655c8a5c2ad8 | 139 | |
jwalker366 | 2:655c8a5c2ad8 | 140 | uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10), 3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 141 | uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+10) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 142 | num2++; |
jwalker366 | 2:655c8a5c2ad8 | 143 | |
jwalker366 | 2:655c8a5c2ad8 | 144 | break; |
jwalker366 | 2:655c8a5c2ad8 | 145 | |
jwalker366 | 2:655c8a5c2ad8 | 146 | case 3://so on |
jwalker366 | 2:655c8a5c2ad8 | 147 | uLCD.filled_circle((xarry[diceLoc]) , (yarry[diceLoc]) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 148 | uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+10) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 149 | uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc] - 10) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 150 | num3++; |
jwalker366 | 2:655c8a5c2ad8 | 151 | |
jwalker366 | 2:655c8a5c2ad8 | 152 | break; |
jwalker366 | 2:655c8a5c2ad8 | 153 | |
jwalker366 | 2:655c8a5c2ad8 | 154 | case 4://so forth |
jwalker366 | 2:655c8a5c2ad8 | 155 | uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]- 10) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 156 | uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]+10) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 157 | uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+ 10) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 158 | uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 159 | num4++; |
jwalker366 | 2:655c8a5c2ad8 | 160 | |
jwalker366 | 2:655c8a5c2ad8 | 161 | break; |
jwalker366 | 2:655c8a5c2ad8 | 162 | |
jwalker366 | 2:655c8a5c2ad8 | 163 | case 5: |
jwalker366 | 2:655c8a5c2ad8 | 164 | |
jwalker366 | 2:655c8a5c2ad8 | 165 | uLCD.filled_circle((xarry[diceLoc]) , (yarry[diceLoc]) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 166 | uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]+ 10) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 167 | uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+10) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 168 | uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]- 10) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 169 | uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 170 | num5++; |
jwalker366 | 2:655c8a5c2ad8 | 171 | break; |
jwalker366 | 2:655c8a5c2ad8 | 172 | case 6: |
jwalker366 | 2:655c8a5c2ad8 | 173 | |
jwalker366 | 2:655c8a5c2ad8 | 174 | uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]- 10) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 175 | uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]+10) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 176 | uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]+ 10) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 177 | uLCD.filled_circle((xarry[diceLoc]- 10) , (yarry[diceLoc]- 10) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 178 | uLCD.filled_circle((xarry[diceLoc]+ 10) , (yarry[diceLoc]) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 179 | uLCD.filled_circle((xarry[diceLoc]- 10 ) , (yarry[diceLoc]) ,3, WHITE); |
jwalker366 | 2:655c8a5c2ad8 | 180 | num6++; |
jwalker366 | 2:655c8a5c2ad8 | 181 | break; |
jwalker366 | 2:655c8a5c2ad8 | 182 | |
jwalker366 | 2:655c8a5c2ad8 | 183 | default: |
jwalker366 | 2:655c8a5c2ad8 | 184 | break; |
jwalker366 | 2:655c8a5c2ad8 | 185 | } |
jwalker366 | 2:655c8a5c2ad8 | 186 | //end dice printing function |
jwalker366 | 2:655c8a5c2ad8 | 187 | |
jwalker366 | 2:655c8a5c2ad8 | 188 | |
jwalker366 | 2:655c8a5c2ad8 | 189 | int shakeDetect(int accFlag){ |
jwalker366 | 2:655c8a5c2ad8 | 190 | acc.setBitDepth(MMA8452::BIT_DEPTH_12); |
jwalker366 | 2:655c8a5c2ad8 | 191 | acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G); |
jwalker366 | 2:655c8a5c2ad8 | 192 | acc.setDataRate(MMA8452::RATE_100);// dont worry about this garbage. it just sets some internal values for the acc |
jwalker366 | 2:655c8a5c2ad8 | 193 | |
jwalker366 | 2:655c8a5c2ad8 | 194 | double x = 0; |
jwalker366 | 2:655c8a5c2ad8 | 195 | while(1){ |
jwalker366 | 2:655c8a5c2ad8 | 196 | acc.readXGravity(&x); //notice this is passed by reference use pointers |
jwalker366 | 2:655c8a5c2ad8 | 197 | |
jwalker366 | 2:655c8a5c2ad8 | 198 | if(x >= .6){ |
jwalker366 | 2:655c8a5c2ad8 | 199 | accFlag = 0; |
jwalker366 | 2:655c8a5c2ad8 | 200 | break; |
jwalker366 | 2:655c8a5c2ad8 | 201 | } |
jwalker366 | 2:655c8a5c2ad8 | 202 | } |
jwalker366 | 2:655c8a5c2ad8 | 203 | return accFlag;// this is to pass back the value of the acc flag so I dont do an extra run. |
jwalker366 | 2:655c8a5c2ad8 | 204 | } |
jwalker366 | 2:655c8a5c2ad8 | 205 | // end acc stuff |
jwalker366 | 2:655c8a5c2ad8 | 206 | |
ridwangs7 | 1:f264a7ecd285 | 207 | int main() |
ridwangs7 | 1:f264a7ecd285 | 208 | { |
ridwangs7 | 1:f264a7ecd285 | 209 | ulcd.cls(); |
ridwangs7 | 1:f264a7ecd285 | 210 | ulcd.baudrate(3000000); //jack up baud rate to max for fast display |
ridwangs7 | 1:f264a7ecd285 | 211 | int selectedDice = 0; |
jwalker366 | 0:088af403d779 | 212 | while(1) { |
ridwangs7 | 1:f264a7ecd285 | 213 | draw_start(); |
ridwangs7 | 1:f264a7ecd285 | 214 | selectedDice = selectDice(); |
ridwangs7 | 1:f264a7ecd285 | 215 | pc.printf("Selected Dice: '%d %d'\n", selectedDice, selectedDiceArray[selectedDice]); |
ridwangs7 | 1:f264a7ecd285 | 216 | |
ridwangs7 | 1:f264a7ecd285 | 217 | /* pseudocode to match flow block diagram |
ridwangs7 | 1:f264a7ecd285 | 218 | but_pushed = -1; |
ridwangs7 | 1:f264a7ecd285 | 219 | while(but_pushed != 2){ // enter while if 1 pressed (yes) -> (initially -1) |
ridwangs7 | 1:f264a7ecd285 | 220 | but_pushed = -1; |
ridwangs7 | 1:f264a7ecd285 | 221 | while (but_pushed != 1){ |
ridwangs7 | 1:f264a7ecd285 | 222 | draw_readyRoll(); |
ridwangs7 | 1:f264a7ecd285 | 223 | draw_diceDisp(); |
ridwangs7 | 1:f264a7ecd285 | 224 | } |
ridwangs7 | 1:f264a7ecd285 | 225 | draw_diceSelect(); |
ridwangs7 | 1:f264a7ecd285 | 226 | } |
ridwangs7 | 1:f264a7ecd285 | 227 | */ |
ridwangs7 | 1:f264a7ecd285 | 228 | // draw_scoreScreen(); |
jwalker366 | 0:088af403d779 | 229 | } |
jwalker366 | 0:088af403d779 | 230 | } |