Kill the bit game for Real Time Assignment
Dependencies: mbed C12832 mbed-rtos
main.cpp@2:7309680cbb76, 2022-04-10 (annotated)
- Committer:
- spearson93
- Date:
- Sun Apr 10 12:50:40 2022 +0000
- Revision:
- 2:7309680cbb76
- Parent:
- 1:617a3879630b
Rev2 for Draw added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:ecbcef1609d9 | 1 | #include "mbed.h" |
spearson93 | 1:617a3879630b | 2 | #include "rtos.h" |
spearson93 | 1:617a3879630b | 3 | #include "Timer.h" |
spearson93 | 1:617a3879630b | 4 | #include "C12832.h" |
spearson93 | 1:617a3879630b | 5 | #include "stdio.h" |
spearson93 | 1:617a3879630b | 6 | #include "stdlib.h" |
spearson93 | 1:617a3879630b | 7 | // Samuel Pearson 19064819 |
4180_1 | 0:ecbcef1609d9 | 8 | // Joystick push kills LED bit on left, if it is on |
4180_1 | 0:ecbcef1609d9 | 9 | // If bit is not on, another is created |
4180_1 | 0:ecbcef1609d9 | 10 | // Goal is to kill off all of the bits |
4180_1 | 0:ecbcef1609d9 | 11 | // LEDs flash and speaker beeps on a win |
4180_1 | 0:ecbcef1609d9 | 12 | |
4180_1 | 0:ecbcef1609d9 | 13 | DigitalOut myled1(LED1); |
4180_1 | 0:ecbcef1609d9 | 14 | DigitalOut myled2(LED2); |
4180_1 | 0:ecbcef1609d9 | 15 | DigitalOut myled3(LED3); |
4180_1 | 0:ecbcef1609d9 | 16 | DigitalOut myled4(LED4); |
spearson93 | 1:617a3879630b | 17 | DigitalIn pb(p14); //the player hits the switch connected here to respond |
4180_1 | 0:ecbcef1609d9 | 18 | PwmOut spkr(p26); |
spearson93 | 1:617a3879630b | 19 | PwmOut RGBLED_r(p23); |
spearson93 | 1:617a3879630b | 20 | PwmOut RGBLED_g(p24); |
spearson93 | 1:617a3879630b | 21 | PwmOut RGBLED_b(p25); |
spearson93 | 1:617a3879630b | 22 | C12832 lcd(p5, p7, p6, p8, p11); |
spearson93 | 1:617a3879630b | 23 | |
spearson93 | 1:617a3879630b | 24 | // mutex to make the lcd lib thread safe |
spearson93 | 1:617a3879630b | 25 | Mutex lcd_mutex; |
spearson93 | 1:617a3879630b | 26 | Mutex mtx; |
spearson93 | 1:617a3879630b | 27 | |
spearson93 | 1:617a3879630b | 28 | Timer t1, t2; //used to measure the response time |
spearson93 | 1:617a3879630b | 29 | int player1, player2, player2Ready, StartGame, GameFinished = 0; |
spearson93 | 1:617a3879630b | 30 | float Player1Time, Player2Time = 0; |
4180_1 | 0:ecbcef1609d9 | 31 | |
4180_1 | 0:ecbcef1609d9 | 32 | void display(int number) |
4180_1 | 0:ecbcef1609d9 | 33 | { |
4180_1 | 0:ecbcef1609d9 | 34 | myled1 = (number) & 0x01; |
4180_1 | 0:ecbcef1609d9 | 35 | myled2 = (number>>1) & 0x01; |
4180_1 | 0:ecbcef1609d9 | 36 | myled3 = (number>>2) & 0x01; |
4180_1 | 0:ecbcef1609d9 | 37 | myled4 = (number>>3) & 0x01; |
4180_1 | 0:ecbcef1609d9 | 38 | } |
spearson93 | 1:617a3879630b | 39 | |
spearson93 | 1:617a3879630b | 40 | void measure (int value) // calls when the joystick is pressed |
spearson93 | 1:617a3879630b | 41 | { |
spearson93 | 1:617a3879630b | 42 | if (pb) |
spearson93 | 1:617a3879630b | 43 | { //Check if button is held |
spearson93 | 1:617a3879630b | 44 | lcd_mutex.lock(); |
spearson93 | 1:617a3879630b | 45 | lcd.cls(); |
spearson93 | 1:617a3879630b | 46 | lcd.printf("Button Press!\n"); |
spearson93 | 1:617a3879630b | 47 | lcd_mutex.unlock(); |
spearson93 | 1:617a3879630b | 48 | } |
spearson93 | 1:617a3879630b | 49 | } |
spearson93 | 1:617a3879630b | 50 | |
spearson93 | 1:617a3879630b | 51 | void calculate (float T1, float T2) |
spearson93 | 1:617a3879630b | 52 | { |
spearson93 | 1:617a3879630b | 53 | mtx.lock(); |
spearson93 | 1:617a3879630b | 54 | if (T1 < T2) |
spearson93 | 1:617a3879630b | 55 | { |
spearson93 | 1:617a3879630b | 56 | lcd.printf("Player 1 Wins!!\n"); |
spearson93 | 1:617a3879630b | 57 | } |
spearson93 | 1:617a3879630b | 58 | if (T1 > T2) |
spearson93 | 1:617a3879630b | 59 | { |
spearson93 | 1:617a3879630b | 60 | lcd.printf("Player 2 Wins!\n"); |
spearson93 | 1:617a3879630b | 61 | } |
spearson93 | 2:7309680cbb76 | 62 | if (T1 == T2) |
spearson93 | 2:7309680cbb76 | 63 | { |
spearson93 | 2:7309680cbb76 | 64 | lcd.printf("Game was a Draw, Try Again\n"); |
spearson93 | 2:7309680cbb76 | 65 | } |
spearson93 | 1:617a3879630b | 66 | mtx.unlock(); |
spearson93 | 1:617a3879630b | 67 | } |
spearson93 | 1:617a3879630b | 68 | |
spearson93 | 1:617a3879630b | 69 | void thread3(void const *args) |
spearson93 | 1:617a3879630b | 70 | { |
spearson93 | 1:617a3879630b | 71 | while(true) // thread loop |
spearson93 | 1:617a3879630b | 72 | { |
spearson93 | 1:617a3879630b | 73 | RGBLED_r = 0.7 + (rand() % 11)/20.0; |
spearson93 | 1:617a3879630b | 74 | RGBLED_g = 0.7 + (rand() % 11)/20.0; |
spearson93 | 1:617a3879630b | 75 | RGBLED_b = 0.7 + (rand() % 11)/20.0; |
spearson93 | 1:617a3879630b | 76 | Thread::wait(1667); // wait 1.5s |
spearson93 | 1:617a3879630b | 77 | } |
spearson93 | 1:617a3879630b | 78 | } |
spearson93 | 1:617a3879630b | 79 | |
4180_1 | 0:ecbcef1609d9 | 80 | int main() |
4180_1 | 0:ecbcef1609d9 | 81 | { |
spearson93 | 1:617a3879630b | 82 | while(1) |
spearson93 | 1:617a3879630b | 83 | { |
spearson93 | 1:617a3879630b | 84 | Thread t3(thread3); //RGB LED |
spearson93 | 1:617a3879630b | 85 | lcd.printf("Destroy the Bit - \n\r"); |
spearson93 | 1:617a3879630b | 86 | lcd.printf("Player 1 Start\n\r"); |
spearson93 | 1:617a3879630b | 87 | lcd.printf("Press Joystick to Start\n\r"); |
4180_1 | 0:ecbcef1609d9 | 88 | unsigned int value = 0x12; |
4180_1 | 0:ecbcef1609d9 | 89 | spkr.period(1.0/2000.0); |
spearson93 | 1:617a3879630b | 90 | while (StartGame == 0) |
spearson93 | 1:617a3879630b | 91 | { |
spearson93 | 1:617a3879630b | 92 | if (pb) |
spearson93 | 1:617a3879630b | 93 | { |
spearson93 | 1:617a3879630b | 94 | StartGame = 1; |
spearson93 | 1:617a3879630b | 95 | player2 = 1; |
spearson93 | 1:617a3879630b | 96 | player1 = 0; |
spearson93 | 1:617a3879630b | 97 | t1.start(); |
spearson93 | 1:617a3879630b | 98 | t2.start(); |
spearson93 | 1:617a3879630b | 99 | } |
spearson93 | 1:617a3879630b | 100 | } |
spearson93 | 1:617a3879630b | 101 | while(StartGame == 1) |
spearson93 | 1:617a3879630b | 102 | { |
spearson93 | 1:617a3879630b | 103 | if (player1 == 0) |
spearson93 | 1:617a3879630b | 104 | { |
spearson93 | 1:617a3879630b | 105 | lcd.cls(); |
spearson93 | 1:617a3879630b | 106 | lcd.printf("Press Joystick to kill bit\n\r"); |
spearson93 | 1:617a3879630b | 107 | value = value ^ pb; |
spearson93 | 1:617a3879630b | 108 | if (value == 0) { |
spearson93 | 1:617a3879630b | 109 | for (int i=0; i<5; ++i) { |
spearson93 | 1:617a3879630b | 110 | spkr = 0.5; |
spearson93 | 1:617a3879630b | 111 | lcd.cls(); |
spearson93 | 1:617a3879630b | 112 | display(0x0F); |
spearson93 | 1:617a3879630b | 113 | wait(.5); |
spearson93 | 1:617a3879630b | 114 | display(0); |
spearson93 | 1:617a3879630b | 115 | spkr = 0.0; |
spearson93 | 1:617a3879630b | 116 | wait(.25); |
spearson93 | 1:617a3879630b | 117 | } |
spearson93 | 1:617a3879630b | 118 | t1.stop(); |
spearson93 | 1:617a3879630b | 119 | value = 0x012; |
spearson93 | 1:617a3879630b | 120 | lcd_mutex.lock(); |
spearson93 | 1:617a3879630b | 121 | lcd.printf("Player 1 Finished!!!!!!!\n\r"); |
spearson93 | 1:617a3879630b | 122 | lcd.printf("Finish time - %f secs\n\r", t1.read()); |
spearson93 | 1:617a3879630b | 123 | wait(2); |
spearson93 | 1:617a3879630b | 124 | lcd_mutex.unlock(); |
spearson93 | 1:617a3879630b | 125 | Player1Time = t1.read(); |
spearson93 | 1:617a3879630b | 126 | player1 = 1; |
spearson93 | 1:617a3879630b | 127 | player2 = 0; |
spearson93 | 1:617a3879630b | 128 | t1.reset(); |
spearson93 | 1:617a3879630b | 129 | t2.reset(); |
spearson93 | 1:617a3879630b | 130 | } |
spearson93 | 1:617a3879630b | 131 | value = ((value & 0x01)<<3) | value >> 1; |
spearson93 | 1:617a3879630b | 132 | display(value); |
spearson93 | 1:617a3879630b | 133 | measure(value); |
spearson93 | 1:617a3879630b | 134 | wait(.25); |
spearson93 | 1:617a3879630b | 135 | } |
spearson93 | 1:617a3879630b | 136 | if (player2 == 0) |
spearson93 | 1:617a3879630b | 137 | { |
spearson93 | 1:617a3879630b | 138 | if (player2Ready == 0) |
spearson93 | 1:617a3879630b | 139 | { |
spearson93 | 1:617a3879630b | 140 | lcd.cls(); |
spearson93 | 1:617a3879630b | 141 | lcd.printf("Player 2 Start\n\r"); |
spearson93 | 1:617a3879630b | 142 | lcd.printf("Press JoyStick to Start\n\r"); |
spearson93 | 1:617a3879630b | 143 | } |
spearson93 | 1:617a3879630b | 144 | while(player2Ready == 0) |
spearson93 | 1:617a3879630b | 145 | { |
spearson93 | 1:617a3879630b | 146 | if (pb) |
spearson93 | 1:617a3879630b | 147 | { |
spearson93 | 1:617a3879630b | 148 | t2.start(); |
spearson93 | 1:617a3879630b | 149 | player2Ready = 1; |
spearson93 | 1:617a3879630b | 150 | } |
spearson93 | 1:617a3879630b | 151 | } |
spearson93 | 1:617a3879630b | 152 | lcd.cls(); |
spearson93 | 1:617a3879630b | 153 | lcd.printf("Press Joystick to kill bit\n\r"); |
spearson93 | 1:617a3879630b | 154 | value = value ^ pb; |
spearson93 | 1:617a3879630b | 155 | if (value == 0) { |
spearson93 | 1:617a3879630b | 156 | for (int i=0; i<5; ++i) { |
4180_1 | 0:ecbcef1609d9 | 157 | spkr = 0.5; |
4180_1 | 0:ecbcef1609d9 | 158 | display(0x0F); |
4180_1 | 0:ecbcef1609d9 | 159 | wait(.5); |
4180_1 | 0:ecbcef1609d9 | 160 | display(0); |
4180_1 | 0:ecbcef1609d9 | 161 | spkr = 0.0; |
4180_1 | 0:ecbcef1609d9 | 162 | wait(.25); |
4180_1 | 0:ecbcef1609d9 | 163 | } |
spearson93 | 1:617a3879630b | 164 | t2.stop(); |
4180_1 | 0:ecbcef1609d9 | 165 | value = 0x012; |
spearson93 | 1:617a3879630b | 166 | lcd_mutex.lock(); |
spearson93 | 1:617a3879630b | 167 | lcd.printf("Player 2 Finished!!!!!!!\n\r"); |
spearson93 | 1:617a3879630b | 168 | lcd.printf("Finish time - %f secs\n\r", t2.read()); |
spearson93 | 1:617a3879630b | 169 | wait(2); |
spearson93 | 1:617a3879630b | 170 | lcd_mutex.unlock(); |
spearson93 | 1:617a3879630b | 171 | Player2Time = t2.read(); |
spearson93 | 1:617a3879630b | 172 | player2 = 1; |
spearson93 | 1:617a3879630b | 173 | StartGame = 0; |
spearson93 | 1:617a3879630b | 174 | GameFinished = 1; |
spearson93 | 1:617a3879630b | 175 | t2.reset(); |
4180_1 | 0:ecbcef1609d9 | 176 | } |
4180_1 | 0:ecbcef1609d9 | 177 | value = ((value & 0x01)<<3) | value >> 1; |
4180_1 | 0:ecbcef1609d9 | 178 | display(value); |
spearson93 | 1:617a3879630b | 179 | measure(value); |
4180_1 | 0:ecbcef1609d9 | 180 | wait(.25); |
spearson93 | 1:617a3879630b | 181 | } |
spearson93 | 1:617a3879630b | 182 | } |
spearson93 | 1:617a3879630b | 183 | if (GameFinished == 1) |
spearson93 | 1:617a3879630b | 184 | { |
spearson93 | 1:617a3879630b | 185 | lcd.cls(); |
spearson93 | 1:617a3879630b | 186 | wait(1); |
spearson93 | 1:617a3879630b | 187 | lcd.printf("Game has Ended\n"); |
spearson93 | 1:617a3879630b | 188 | calculate(Player1Time, Player2Time); |
spearson93 | 1:617a3879630b | 189 | wait(1); |
spearson93 | 1:617a3879630b | 190 | lcd.cls(); |
spearson93 | 1:617a3879630b | 191 | lcd.printf("Game will reset in 5 secs\n"); |
spearson93 | 1:617a3879630b | 192 | wait(5); |
spearson93 | 1:617a3879630b | 193 | GameFinished = 0; |
spearson93 | 1:617a3879630b | 194 | Player1Time, Player2Time = 0; |
spearson93 | 1:617a3879630b | 195 | lcd.cls(); |
4180_1 | 0:ecbcef1609d9 | 196 | } |
4180_1 | 0:ecbcef1609d9 | 197 | } |
spearson93 | 1:617a3879630b | 198 | } |