updated 7seg controls for new 7 seg boards

Dependencies:   PixelArray WS2812 mbed

Fork of frdm_pong_table_controller by Demo Team

Committer:
benswindell
Date:
Mon Mar 20 12:12:46 2017 +0000
Revision:
1:24bc4d8ed2ae
Parent:
0:f2b739e846ae
Child:
2:ff409472bc08
Publishing the table controller code used at Embedded World;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benswindell 0:f2b739e846ae 1 #include "mbed.h"
benswindell 0:f2b739e846ae 2 #include "WS2812.h"
benswindell 0:f2b739e846ae 3 #include "PixelArray.h"
benswindell 0:f2b739e846ae 4
benswindell 0:f2b739e846ae 5 #define WS2812_BUF 112
benswindell 0:f2b739e846ae 6 #define NUM_COLORS 6
benswindell 0:f2b739e846ae 7 #define NUM_LEDS_PER_COLOR 1
benswindell 0:f2b739e846ae 8
benswindell 0:f2b739e846ae 9 //-------- Colours -----------
benswindell 0:f2b739e846ae 10 #define RED 0x2f0000
benswindell 0:f2b739e846ae 11 #define YELLOW 0x2f2f00
benswindell 0:f2b739e846ae 12 #define GREEN 0x002f00
benswindell 0:f2b739e846ae 13 #define LIGHTBLUE 0x002f2f
benswindell 0:f2b739e846ae 14 #define DARKBLUE 0x00002f
benswindell 0:f2b739e846ae 15 #define BLUE 0x0000ff // Player scored a goal
benswindell 0:f2b739e846ae 16 #define PINK 0x2f002f
benswindell 0:f2b739e846ae 17 #define OFF 0x000000
benswindell 1:24bc4d8ed2ae 18 #define WHITE 0xffffaa
benswindell 0:f2b739e846ae 19 #define ARMBLUE 0x128BAB
benswindell 0:f2b739e846ae 20 #define PURPLE 0xff0055 // Player has conceded a goal
benswindell 0:f2b739e846ae 21
benswindell 0:f2b739e846ae 22 // GPIO
benswindell 0:f2b739e846ae 23 AnalogIn robotBreakBeam(A0);
benswindell 0:f2b739e846ae 24 AnalogIn playerBreakBeam(A1);
benswindell 0:f2b739e846ae 25 DigitalOut led_green(LED_GREEN, 1);
benswindell 0:f2b739e846ae 26
benswindell 0:f2b739e846ae 27 // SERIAL
benswindell 0:f2b739e846ae 28 Serial pc(USBTX, USBRX); // tx, rx
benswindell 0:f2b739e846ae 29
benswindell 0:f2b739e846ae 30 // LED STRIPS
benswindell 0:f2b739e846ae 31 // See the program page for information on the timing numbers
benswindell 0:f2b739e846ae 32 // The given numbers are for the K64F
benswindell 0:f2b739e846ae 33 WS2812 robotScoreLED(D9, WS2812_BUF, 0, 5, 5, 0);
benswindell 0:f2b739e846ae 34 WS2812 playerScoreLED(D8,WS2812_BUF, 0, 5, 5, 0);
benswindell 1:24bc4d8ed2ae 35 WS2812 robotGoalLED(D5,WS2812_BUF, 0, 5, 5, 0);
benswindell 1:24bc4d8ed2ae 36 WS2812 playerGoalLED(D3,WS2812_BUF, 0, 5, 5, 0);
benswindell 0:f2b739e846ae 37
benswindell 0:f2b739e846ae 38 // LED Variables
benswindell 0:f2b739e846ae 39 bool seg1A, seg1B, seg1C, seg1D, seg1E, seg1F, seg1G;
benswindell 0:f2b739e846ae 40 int seg1Array[112];
benswindell 0:f2b739e846ae 41 int mainArray[11][122];
benswindell 1:24bc4d8ed2ae 42 PixelArray robotScorePx(WS2812_BUF);
benswindell 1:24bc4d8ed2ae 43 PixelArray playerScorePx(WS2812_BUF);
benswindell 1:24bc4d8ed2ae 44 PixelArray robotGoalPx(WS2812_BUF);
benswindell 1:24bc4d8ed2ae 45 PixelArray playerGoalPx(WS2812_BUF);
benswindell 1:24bc4d8ed2ae 46 const int PLAYERGOALLEDS = 55;
benswindell 1:24bc4d8ed2ae 47 const int ROBOTGOALLEDS = 55;
benswindell 0:f2b739e846ae 48
benswindell 0:f2b739e846ae 49 // Score counters
benswindell 0:f2b739e846ae 50 int robotScore;
benswindell 0:f2b739e846ae 51 int playerScore;
benswindell 0:f2b739e846ae 52 int scoreLimit = 3;
benswindell 0:f2b739e846ae 53 bool finishedGame = false;
benswindell 1:24bc4d8ed2ae 54 int endFlashes = 3;
benswindell 0:f2b739e846ae 55 int numFlashes;
benswindell 0:f2b739e846ae 56
benswindell 0:f2b739e846ae 57 // Robot Bream Beam value
benswindell 0:f2b739e846ae 58 double prevRbbValue; // Previous Robot break beam value
benswindell 0:f2b739e846ae 59 double prevPbbValue; // Previous player break beam value
benswindell 0:f2b739e846ae 60
benswindell 0:f2b739e846ae 61 // FUNCTION DECLERATIONS
benswindell 0:f2b739e846ae 62 void Write7Seg(int num);
benswindell 0:f2b739e846ae 63 void SetLEDArray(int x);
benswindell 0:f2b739e846ae 64 void WriteRobotGoal(bool scored);
benswindell 0:f2b739e846ae 65 void WritePlayerGoal(bool scored);
benswindell 0:f2b739e846ae 66 void WriteScores();
benswindell 0:f2b739e846ae 67 void HandleGoal(bool hasRobotScored);
benswindell 1:24bc4d8ed2ae 68 void WritePxScores(int line_num, bool isRobot);
benswindell 1:24bc4d8ed2ae 69 void WritePxGoal(unsigned int colour,bool isRobot);
benswindell 1:24bc4d8ed2ae 70 void HandleWin();
benswindell 1:24bc4d8ed2ae 71 void WriteGoalsOff();
benswindell 0:f2b739e846ae 72
benswindell 0:f2b739e846ae 73
benswindell 0:f2b739e846ae 74 void setup()
benswindell 0:f2b739e846ae 75 {
benswindell 0:f2b739e846ae 76
benswindell 0:f2b739e846ae 77 // Turn on green LED
benswindell 0:f2b739e846ae 78 led_green = 0;
benswindell 0:f2b739e846ae 79
benswindell 1:24bc4d8ed2ae 80 // Set brightness of the 4 LED strips
benswindell 1:24bc4d8ed2ae 81 robotScoreLED.setII(0x19);
benswindell 1:24bc4d8ed2ae 82 robotScoreLED.useII(WS2812::GLOBAL);
benswindell 1:24bc4d8ed2ae 83 playerScoreLED.setII(0x19);
benswindell 1:24bc4d8ed2ae 84 playerScoreLED.useII(WS2812::GLOBAL);
benswindell 1:24bc4d8ed2ae 85 robotGoalLED.setII(0x19);
benswindell 1:24bc4d8ed2ae 86 robotGoalLED.useII(WS2812::GLOBAL);
benswindell 1:24bc4d8ed2ae 87 playerGoalLED.setII(0x80);
benswindell 1:24bc4d8ed2ae 88 playerGoalLED.useII(WS2812::GLOBAL);
benswindell 1:24bc4d8ed2ae 89
benswindell 0:f2b739e846ae 90 // Fills 2D array with data
benswindell 0:f2b739e846ae 91 for(int i=0; i<10; i++) {
benswindell 0:f2b739e846ae 92 Write7Seg(i);
benswindell 0:f2b739e846ae 93 }
benswindell 0:f2b739e846ae 94
benswindell 0:f2b739e846ae 95 // Set scores to 0
benswindell 0:f2b739e846ae 96 robotScore = 0;
benswindell 0:f2b739e846ae 97 playerScore = 0;
benswindell 0:f2b739e846ae 98 numFlashes = 0;
benswindell 0:f2b739e846ae 99
benswindell 0:f2b739e846ae 100 // Set LEDS to start values
benswindell 0:f2b739e846ae 101 WriteScores();
benswindell 0:f2b739e846ae 102 wait(0.01);
benswindell 0:f2b739e846ae 103 WritePlayerGoal(true);
benswindell 0:f2b739e846ae 104 wait(0.01);
benswindell 0:f2b739e846ae 105 WriteRobotGoal(true);
benswindell 1:24bc4d8ed2ae 106
benswindell 1:24bc4d8ed2ae 107 wait(5);
benswindell 1:24bc4d8ed2ae 108
benswindell 1:24bc4d8ed2ae 109 WriteGoalsOff();
benswindell 0:f2b739e846ae 110 // Turn off green LED
benswindell 0:f2b739e846ae 111 led_green = 1;
benswindell 0:f2b739e846ae 112 }
benswindell 0:f2b739e846ae 113
benswindell 0:f2b739e846ae 114 // Set segment variables
benswindell 0:f2b739e846ae 115 void Write7Seg(int num)
benswindell 0:f2b739e846ae 116 {
benswindell 0:f2b739e846ae 117 switch (num) {
benswindell 0:f2b739e846ae 118 case 0 :
benswindell 0:f2b739e846ae 119 // Off
benswindell 0:f2b739e846ae 120 seg1A = 1;
benswindell 0:f2b739e846ae 121 seg1B = 1;
benswindell 0:f2b739e846ae 122 seg1C = 1;
benswindell 0:f2b739e846ae 123 seg1D = 1;
benswindell 0:f2b739e846ae 124 seg1E = 1;
benswindell 0:f2b739e846ae 125 seg1F = 1;
benswindell 0:f2b739e846ae 126 seg1G = 0;
benswindell 0:f2b739e846ae 127
benswindell 0:f2b739e846ae 128 SetLEDArray(0);
benswindell 0:f2b739e846ae 129
benswindell 0:f2b739e846ae 130 break;
benswindell 0:f2b739e846ae 131
benswindell 0:f2b739e846ae 132 case 1 :
benswindell 0:f2b739e846ae 133 // 1
benswindell 0:f2b739e846ae 134 seg1A = 0;
benswindell 0:f2b739e846ae 135 seg1B = 1;
benswindell 0:f2b739e846ae 136 seg1C = 1;
benswindell 0:f2b739e846ae 137 seg1D = 0;
benswindell 0:f2b739e846ae 138 seg1E = 0;
benswindell 0:f2b739e846ae 139 seg1F = 0;
benswindell 0:f2b739e846ae 140 seg1G = 0;
benswindell 0:f2b739e846ae 141
benswindell 0:f2b739e846ae 142 SetLEDArray(1);
benswindell 0:f2b739e846ae 143 break;
benswindell 0:f2b739e846ae 144
benswindell 0:f2b739e846ae 145 case 2 :
benswindell 0:f2b739e846ae 146 // 2
benswindell 0:f2b739e846ae 147 seg1A = 1;
benswindell 0:f2b739e846ae 148 seg1B = 1;
benswindell 0:f2b739e846ae 149 seg1C = 0;
benswindell 0:f2b739e846ae 150 seg1D = 1;
benswindell 0:f2b739e846ae 151 seg1E = 1;
benswindell 0:f2b739e846ae 152 seg1F = 0;
benswindell 0:f2b739e846ae 153 seg1G = 1;
benswindell 0:f2b739e846ae 154
benswindell 0:f2b739e846ae 155 SetLEDArray(2);
benswindell 0:f2b739e846ae 156
benswindell 0:f2b739e846ae 157 break;
benswindell 0:f2b739e846ae 158
benswindell 0:f2b739e846ae 159 case 3 :
benswindell 0:f2b739e846ae 160 // 3
benswindell 0:f2b739e846ae 161 seg1A = 1;
benswindell 0:f2b739e846ae 162 seg1B = 1;
benswindell 0:f2b739e846ae 163 seg1C = 1;
benswindell 0:f2b739e846ae 164 seg1D = 1;
benswindell 0:f2b739e846ae 165 seg1E = 0;
benswindell 0:f2b739e846ae 166 seg1F = 0;
benswindell 0:f2b739e846ae 167 seg1G = 1;
benswindell 0:f2b739e846ae 168
benswindell 0:f2b739e846ae 169 SetLEDArray(3);
benswindell 0:f2b739e846ae 170 break;
benswindell 0:f2b739e846ae 171
benswindell 0:f2b739e846ae 172 case 4:
benswindell 0:f2b739e846ae 173 // 4
benswindell 0:f2b739e846ae 174 seg1A = 0;
benswindell 0:f2b739e846ae 175 seg1B = 1;
benswindell 0:f2b739e846ae 176 seg1C = 1;
benswindell 0:f2b739e846ae 177 seg1D = 0;
benswindell 0:f2b739e846ae 178 seg1E = 0;
benswindell 0:f2b739e846ae 179 seg1F = 1;
benswindell 0:f2b739e846ae 180 seg1G = 1;
benswindell 0:f2b739e846ae 181
benswindell 0:f2b739e846ae 182 SetLEDArray(4);
benswindell 0:f2b739e846ae 183
benswindell 0:f2b739e846ae 184 break;
benswindell 0:f2b739e846ae 185
benswindell 0:f2b739e846ae 186 case 5:
benswindell 0:f2b739e846ae 187 // 5
benswindell 0:f2b739e846ae 188 seg1A = 1;
benswindell 0:f2b739e846ae 189 seg1B = 0;
benswindell 0:f2b739e846ae 190 seg1C = 1;
benswindell 0:f2b739e846ae 191 seg1D = 1;
benswindell 0:f2b739e846ae 192 seg1E = 0;
benswindell 0:f2b739e846ae 193 seg1F = 1;
benswindell 0:f2b739e846ae 194 seg1G = 1;
benswindell 0:f2b739e846ae 195
benswindell 0:f2b739e846ae 196 SetLEDArray(5);
benswindell 0:f2b739e846ae 197
benswindell 0:f2b739e846ae 198 break;
benswindell 0:f2b739e846ae 199
benswindell 0:f2b739e846ae 200 case 6:
benswindell 0:f2b739e846ae 201 // 6
benswindell 0:f2b739e846ae 202 seg1A = 1;
benswindell 0:f2b739e846ae 203 seg1B = 0;
benswindell 0:f2b739e846ae 204 seg1C = 1;
benswindell 0:f2b739e846ae 205 seg1D = 1;
benswindell 0:f2b739e846ae 206 seg1E = 1;
benswindell 0:f2b739e846ae 207 seg1F = 1;
benswindell 0:f2b739e846ae 208 seg1G = 1;
benswindell 0:f2b739e846ae 209
benswindell 0:f2b739e846ae 210 SetLEDArray(6);
benswindell 0:f2b739e846ae 211
benswindell 0:f2b739e846ae 212 break;
benswindell 0:f2b739e846ae 213
benswindell 0:f2b739e846ae 214 case 7:
benswindell 0:f2b739e846ae 215 // 7
benswindell 0:f2b739e846ae 216 seg1A = 1;
benswindell 0:f2b739e846ae 217 seg1B = 1;
benswindell 0:f2b739e846ae 218 seg1C = 1;
benswindell 0:f2b739e846ae 219 seg1D = 0;
benswindell 0:f2b739e846ae 220 seg1E = 0;
benswindell 0:f2b739e846ae 221 seg1F = 0;
benswindell 0:f2b739e846ae 222 seg1G = 0;
benswindell 0:f2b739e846ae 223
benswindell 0:f2b739e846ae 224 SetLEDArray(7);
benswindell 0:f2b739e846ae 225
benswindell 0:f2b739e846ae 226 break;
benswindell 0:f2b739e846ae 227
benswindell 0:f2b739e846ae 228 case 8:
benswindell 0:f2b739e846ae 229 // 8
benswindell 0:f2b739e846ae 230 seg1A = 1;
benswindell 0:f2b739e846ae 231 seg1B = 1;
benswindell 0:f2b739e846ae 232 seg1C = 1;
benswindell 0:f2b739e846ae 233 seg1D = 1;
benswindell 0:f2b739e846ae 234 seg1E = 1;
benswindell 0:f2b739e846ae 235 seg1F = 1;
benswindell 0:f2b739e846ae 236 seg1G = 1;
benswindell 0:f2b739e846ae 237
benswindell 0:f2b739e846ae 238 SetLEDArray(8);
benswindell 0:f2b739e846ae 239 break;
benswindell 0:f2b739e846ae 240
benswindell 0:f2b739e846ae 241 case 9:
benswindell 0:f2b739e846ae 242 // 9
benswindell 0:f2b739e846ae 243 seg1A = 1;
benswindell 0:f2b739e846ae 244 seg1B = 1;
benswindell 0:f2b739e846ae 245 seg1C = 1;
benswindell 0:f2b739e846ae 246 seg1D = 0;
benswindell 0:f2b739e846ae 247 seg1E = 0;
benswindell 0:f2b739e846ae 248 seg1F = 1;
benswindell 0:f2b739e846ae 249 seg1G = 1;
benswindell 0:f2b739e846ae 250
benswindell 0:f2b739e846ae 251 SetLEDArray(9);
benswindell 0:f2b739e846ae 252 break;
benswindell 0:f2b739e846ae 253
benswindell 0:f2b739e846ae 254 case 10:
benswindell 0:f2b739e846ae 255 // OFF
benswindell 0:f2b739e846ae 256 seg1A = 0;
benswindell 0:f2b739e846ae 257 seg1B = 0;
benswindell 0:f2b739e846ae 258 seg1C = 0;
benswindell 0:f2b739e846ae 259 seg1D = 0;
benswindell 0:f2b739e846ae 260 seg1E = 0;
benswindell 0:f2b739e846ae 261 seg1F = 0;
benswindell 0:f2b739e846ae 262 seg1G = 0;
benswindell 0:f2b739e846ae 263
benswindell 0:f2b739e846ae 264 SetLEDArray(10);
benswindell 0:f2b739e846ae 265 break;
benswindell 0:f2b739e846ae 266
benswindell 0:f2b739e846ae 267 default :
benswindell 0:f2b739e846ae 268
benswindell 0:f2b739e846ae 269 break;
benswindell 0:f2b739e846ae 270
benswindell 0:f2b739e846ae 271 }
benswindell 0:f2b739e846ae 272 }
benswindell 0:f2b739e846ae 273
benswindell 0:f2b739e846ae 274 // Write segment config to main array
benswindell 0:f2b739e846ae 275 void SetLEDArray(int x)
benswindell 0:f2b739e846ae 276 {
benswindell 0:f2b739e846ae 277 for (int i = 0; i < WS2812_BUF; i++) {
benswindell 0:f2b739e846ae 278 if (i < 16) {
benswindell 0:f2b739e846ae 279 mainArray[x][i] = seg1A;
benswindell 0:f2b739e846ae 280 }
benswindell 0:f2b739e846ae 281
benswindell 0:f2b739e846ae 282 if ( i >= 16 && i < 32) {
benswindell 0:f2b739e846ae 283 mainArray[x][i] = seg1B;
benswindell 0:f2b739e846ae 284 }
benswindell 0:f2b739e846ae 285
benswindell 0:f2b739e846ae 286 if (i >= 32 && i < 48) {
benswindell 0:f2b739e846ae 287 mainArray[x][i] = seg1C;
benswindell 0:f2b739e846ae 288 }
benswindell 0:f2b739e846ae 289
benswindell 0:f2b739e846ae 290 if (i >= 48 && i < 64) {
benswindell 0:f2b739e846ae 291 mainArray[x][i]= seg1D;
benswindell 0:f2b739e846ae 292 }
benswindell 0:f2b739e846ae 293
benswindell 0:f2b739e846ae 294 if ( i >= 64 && i < 80) {
benswindell 0:f2b739e846ae 295 mainArray[x][i] = seg1E;
benswindell 0:f2b739e846ae 296 }
benswindell 0:f2b739e846ae 297
benswindell 0:f2b739e846ae 298 if (i >= 80 && i < 96) {
benswindell 0:f2b739e846ae 299 mainArray[x][i] = seg1F;
benswindell 0:f2b739e846ae 300 }
benswindell 0:f2b739e846ae 301
benswindell 0:f2b739e846ae 302 if ( i >= 96 && i < 112) {
benswindell 0:f2b739e846ae 303 mainArray[x][i] = seg1G;
benswindell 0:f2b739e846ae 304 }
benswindell 0:f2b739e846ae 305 }// FOR LOOP
benswindell 0:f2b739e846ae 306 }
benswindell 0:f2b739e846ae 307
benswindell 0:f2b739e846ae 308 // Write to player 1 LED (ROBOT)
benswindell 0:f2b739e846ae 309 void WriteRobotGoal(bool scored)
benswindell 0:f2b739e846ae 310 {
benswindell 1:24bc4d8ed2ae 311 if(scored == true) {
benswindell 1:24bc4d8ed2ae 312 WritePxGoal(BLUE,true);
benswindell 0:f2b739e846ae 313 } else {
benswindell 1:24bc4d8ed2ae 314 WritePxGoal(PURPLE,true);
benswindell 0:f2b739e846ae 315 }
benswindell 1:24bc4d8ed2ae 316 robotGoalLED.write(robotGoalPx.getBuf());
benswindell 0:f2b739e846ae 317 }
benswindell 0:f2b739e846ae 318
benswindell 0:f2b739e846ae 319 // Write to player 1 LED (ROBOT)
benswindell 0:f2b739e846ae 320 void WritePlayerGoal(bool scored)
benswindell 0:f2b739e846ae 321 {
benswindell 1:24bc4d8ed2ae 322 if(scored == true) {
benswindell 1:24bc4d8ed2ae 323 WritePxGoal(BLUE,false);
benswindell 0:f2b739e846ae 324 } else {
benswindell 1:24bc4d8ed2ae 325 WritePxGoal(PURPLE,false);
benswindell 0:f2b739e846ae 326 }
benswindell 1:24bc4d8ed2ae 327 playerGoalLED.write(playerGoalPx.getBuf());
benswindell 0:f2b739e846ae 328 }
benswindell 0:f2b739e846ae 329
benswindell 1:24bc4d8ed2ae 330 // Write OFF to both goals
benswindell 0:f2b739e846ae 331 void WriteGoalsOff()
benswindell 0:f2b739e846ae 332 {
benswindell 1:24bc4d8ed2ae 333 WritePxGoal(OFF,true);
benswindell 1:24bc4d8ed2ae 334 robotGoalLED.write(robotGoalPx.getBuf());
benswindell 1:24bc4d8ed2ae 335 WritePxGoal(OFF,false);
benswindell 1:24bc4d8ed2ae 336 playerGoalLED.write(playerGoalPx.getBuf());
benswindell 0:f2b739e846ae 337 }
benswindell 0:f2b739e846ae 338
benswindell 1:24bc4d8ed2ae 339 // Update both Score LEDs with the current score
benswindell 0:f2b739e846ae 340 void WriteScores()
benswindell 0:f2b739e846ae 341 {
benswindell 1:24bc4d8ed2ae 342 WritePxScores(playerScore,false);
benswindell 1:24bc4d8ed2ae 343 playerScoreLED.write(playerScorePx.getBuf());
benswindell 0:f2b739e846ae 344 wait(0.01);
benswindell 1:24bc4d8ed2ae 345 WritePxScores(robotScore,true);
benswindell 1:24bc4d8ed2ae 346 robotScoreLED.write(robotScorePx.getBuf());
benswindell 0:f2b739e846ae 347 }
benswindell 0:f2b739e846ae 348
benswindell 0:f2b739e846ae 349 // Write pixel array
benswindell 1:24bc4d8ed2ae 350 void WritePxScores(int line_num,bool isRobot)
benswindell 0:f2b739e846ae 351 {
benswindell 1:24bc4d8ed2ae 352 if(isRobot == true) {
benswindell 1:24bc4d8ed2ae 353
benswindell 1:24bc4d8ed2ae 354 for (int i = 0; i < WS2812_BUF; i++) {
benswindell 1:24bc4d8ed2ae 355 if (mainArray[line_num][i] == 0) {
benswindell 1:24bc4d8ed2ae 356 robotScorePx.Set(i,OFF);
benswindell 1:24bc4d8ed2ae 357 }
benswindell 1:24bc4d8ed2ae 358
benswindell 1:24bc4d8ed2ae 359 if (mainArray[line_num][i] == 1) {
benswindell 1:24bc4d8ed2ae 360 robotScorePx.Set(i,WHITE);
benswindell 1:24bc4d8ed2ae 361 }
benswindell 0:f2b739e846ae 362 }
benswindell 1:24bc4d8ed2ae 363 } else {
benswindell 1:24bc4d8ed2ae 364 for (int i = 0; i < WS2812_BUF; i++) {
benswindell 1:24bc4d8ed2ae 365 if (mainArray[line_num][i] == 0) {
benswindell 1:24bc4d8ed2ae 366 playerScorePx.Set(i,OFF);
benswindell 1:24bc4d8ed2ae 367 }
benswindell 0:f2b739e846ae 368
benswindell 1:24bc4d8ed2ae 369 if (mainArray[line_num][i] == 1) {
benswindell 1:24bc4d8ed2ae 370 playerScorePx.Set(i,WHITE);
benswindell 1:24bc4d8ed2ae 371 }
benswindell 1:24bc4d8ed2ae 372 }
benswindell 1:24bc4d8ed2ae 373 }
benswindell 1:24bc4d8ed2ae 374
benswindell 1:24bc4d8ed2ae 375 }
benswindell 1:24bc4d8ed2ae 376
benswindell 1:24bc4d8ed2ae 377 // Write Goal pixel array, using input colour for the specified goal
benswindell 1:24bc4d8ed2ae 378 // INPUTS: colour, isRobot
benswindell 1:24bc4d8ed2ae 379 //
benswindell 1:24bc4d8ed2ae 380 // colour - This is the colour that you wish to set the array to
benswindell 1:24bc4d8ed2ae 381 // isRobot - This specifies which goal to set the colours of, based on if it is the Robot side goal or not
benswindell 1:24bc4d8ed2ae 382 //
benswindell 1:24bc4d8ed2ae 383 void WritePxGoal(unsigned int colour,bool isRobot)
benswindell 1:24bc4d8ed2ae 384 {
benswindell 1:24bc4d8ed2ae 385 if(isRobot == true) {
benswindell 1:24bc4d8ed2ae 386 for (int i = 0; i < WS2812_BUF; i++) {
benswindell 1:24bc4d8ed2ae 387 robotGoalPx.Set(i,colour);
benswindell 1:24bc4d8ed2ae 388 }
benswindell 1:24bc4d8ed2ae 389 } else {
benswindell 1:24bc4d8ed2ae 390 for (int i = 0; i < WS2812_BUF; i++) {
benswindell 1:24bc4d8ed2ae 391 playerGoalPx.Set(i,colour);
benswindell 0:f2b739e846ae 392 }
benswindell 0:f2b739e846ae 393 }
benswindell 0:f2b739e846ae 394 }
benswindell 0:f2b739e846ae 395
benswindell 1:24bc4d8ed2ae 396 // Write the goal LEDs and score LEDs when a goal is recorded. Also check to see if the goal being scored
benswindell 1:24bc4d8ed2ae 397 // is the final point of the game and the game is over.
benswindell 1:24bc4d8ed2ae 398 // INPUTS: hasRobotScored
benswindell 1:24bc4d8ed2ae 399 //
benswindell 1:24bc4d8ed2ae 400 // hasRobotScored - true if the goal being recorded is from the playerside goal, meaning the robot has scored a point
benswindell 1:24bc4d8ed2ae 401 //
benswindell 0:f2b739e846ae 402 void HandleGoal(bool hasRobotScored)
benswindell 0:f2b739e846ae 403 {
benswindell 0:f2b739e846ae 404 if(hasRobotScored == true) {
benswindell 0:f2b739e846ae 405 robotScore++;
benswindell 0:f2b739e846ae 406 WriteRobotGoal(true);
benswindell 0:f2b739e846ae 407 wait(0.01);
benswindell 0:f2b739e846ae 408 WritePlayerGoal(false);
benswindell 0:f2b739e846ae 409 wait(0.01);
benswindell 0:f2b739e846ae 410 WriteScores();
benswindell 0:f2b739e846ae 411 } else {
benswindell 0:f2b739e846ae 412 playerScore++;
benswindell 0:f2b739e846ae 413 WriteRobotGoal(false);
benswindell 0:f2b739e846ae 414 wait(0.01);
benswindell 0:f2b739e846ae 415 WritePlayerGoal(true);
benswindell 0:f2b739e846ae 416 wait(0.01);
benswindell 0:f2b739e846ae 417 WriteScores();
benswindell 0:f2b739e846ae 418 }
benswindell 1:24bc4d8ed2ae 419
benswindell 0:f2b739e846ae 420 wait(5);
benswindell 1:24bc4d8ed2ae 421
benswindell 1:24bc4d8ed2ae 422 // If either player reaches the score limit, then call HandleWin(). If not, then turn goal LEDs off
benswindell 1:24bc4d8ed2ae 423 if(robotScore == scoreLimit || playerScore == scoreLimit) {
benswindell 1:24bc4d8ed2ae 424 HandleWin();
benswindell 1:24bc4d8ed2ae 425 } else {
benswindell 1:24bc4d8ed2ae 426 WriteGoalsOff();
benswindell 1:24bc4d8ed2ae 427 }
benswindell 1:24bc4d8ed2ae 428 }
benswindell 1:24bc4d8ed2ae 429
benswindell 1:24bc4d8ed2ae 430 // Handle behaviour when either the player or robot has won a game.
benswindell 1:24bc4d8ed2ae 431 void HandleWin()
benswindell 1:24bc4d8ed2ae 432 {
benswindell 1:24bc4d8ed2ae 433 // Initalise variable as true
benswindell 1:24bc4d8ed2ae 434 bool isRobotWinner = true;
benswindell 1:24bc4d8ed2ae 435
benswindell 1:24bc4d8ed2ae 436 // Decide if the robot is the winner. If not, then change isRobotWinner to false
benswindell 1:24bc4d8ed2ae 437 if(playerScore == scoreLimit) {
benswindell 1:24bc4d8ed2ae 438 isRobotWinner = false;
benswindell 1:24bc4d8ed2ae 439 pc.printf("Player won!");
benswindell 1:24bc4d8ed2ae 440
benswindell 1:24bc4d8ed2ae 441 // Animation for goal LEDs. Turns off the robot Goal LEDs, working from in the middle point outwards.
benswindell 1:24bc4d8ed2ae 442 for(int i = 0; i < (ROBOTGOALLEDS / 2); i++) {
benswindell 1:24bc4d8ed2ae 443 robotGoalPx.Set((ROBOTGOALLEDS /2) - i, OFF);
benswindell 1:24bc4d8ed2ae 444 robotGoalPx.Set((ROBOTGOALLEDS /2) + i, OFF);
benswindell 1:24bc4d8ed2ae 445 robotGoalLED.write(robotGoalPx.getBuf());
benswindell 1:24bc4d8ed2ae 446 wait(0.15);
benswindell 1:24bc4d8ed2ae 447 }
benswindell 1:24bc4d8ed2ae 448
benswindell 1:24bc4d8ed2ae 449 WriteGoalsOff();
benswindell 1:24bc4d8ed2ae 450
benswindell 1:24bc4d8ed2ae 451 } else {
benswindell 1:24bc4d8ed2ae 452 pc.printf("Robot won!");
benswindell 1:24bc4d8ed2ae 453
benswindell 1:24bc4d8ed2ae 454 // Animation for goal LEDs. Turns off the player Goal LEDs, working from in the middle point outwards.
benswindell 1:24bc4d8ed2ae 455 for(int i = 0; i < (PLAYERGOALLEDS / 2); i++) {
benswindell 1:24bc4d8ed2ae 456 playerGoalPx.Set((PLAYERGOALLEDS /2) - i, OFF);
benswindell 1:24bc4d8ed2ae 457 playerGoalPx.Set((PLAYERGOALLEDS /2) + i, OFF);
benswindell 1:24bc4d8ed2ae 458 playerGoalLED.write(playerGoalPx.getBuf());
benswindell 1:24bc4d8ed2ae 459 wait(0.25);
benswindell 1:24bc4d8ed2ae 460 }
benswindell 1:24bc4d8ed2ae 461
benswindell 1:24bc4d8ed2ae 462 WriteGoalsOff();
benswindell 1:24bc4d8ed2ae 463 }
benswindell 1:24bc4d8ed2ae 464
benswindell 1:24bc4d8ed2ae 465 // Reset scores then write to LEDs
benswindell 1:24bc4d8ed2ae 466 robotScore = 0;
benswindell 1:24bc4d8ed2ae 467 playerScore = 0;
benswindell 1:24bc4d8ed2ae 468 WriteScores();
benswindell 0:f2b739e846ae 469 }
benswindell 0:f2b739e846ae 470
benswindell 0:f2b739e846ae 471 int main()
benswindell 0:f2b739e846ae 472 {
benswindell 1:24bc4d8ed2ae 473 setup();
benswindell 1:24bc4d8ed2ae 474
benswindell 0:f2b739e846ae 475 while(1) {
benswindell 0:f2b739e846ae 476 double rbbValue = robotBreakBeam; // Read Robot Break beam
benswindell 0:f2b739e846ae 477 double pbbValue = playerBreakBeam; // Read Player Break beam
benswindell 0:f2b739e846ae 478
benswindell 0:f2b739e846ae 479 // IF PLAYER HAS SCORED A GOAL
benswindell 1:24bc4d8ed2ae 480 if ((prevRbbValue - rbbValue)>0.03) {
benswindell 0:f2b739e846ae 481 pc.printf("Player has scored a goal \n\r");
benswindell 0:f2b739e846ae 482 HandleGoal(false);
benswindell 0:f2b739e846ae 483 }
benswindell 0:f2b739e846ae 484
benswindell 0:f2b739e846ae 485 // IF ROBOT HAS SCORED A GOAL
benswindell 1:24bc4d8ed2ae 486 if ((prevPbbValue - pbbValue) > 0.03) {
benswindell 0:f2b739e846ae 487 pc.printf("Robot has scored a goal \n\r");
benswindell 0:f2b739e846ae 488 HandleGoal(true);
benswindell 0:f2b739e846ae 489 }
benswindell 1:24bc4d8ed2ae 490
benswindell 0:f2b739e846ae 491 prevRbbValue = rbbValue;
benswindell 0:f2b739e846ae 492 prevPbbValue = pbbValue;
benswindell 1:24bc4d8ed2ae 493
benswindell 1:24bc4d8ed2ae 494 pc.printf("PlayerGoal: %f, RobotGoal: %f \r\n",pbbValue,rbbValue);
benswindell 1:24bc4d8ed2ae 495 pc.printf("Player: %i v %i : Robot \r\n",playerScore,robotScore);
benswindell 1:24bc4d8ed2ae 496 wait(0.02);
benswindell 0:f2b739e846ae 497
benswindell 0:f2b739e846ae 498 }
benswindell 0:f2b739e846ae 499 }