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