Rock, Paper, Scissors game coordinator

Dependencies:   fsl_phy_mcr20a fsl_smac mbed-rtos mbed

Fork of mcr20_RPS_Coordinator by Freescale

Committer:
mnorman4
Date:
Tue Nov 17 17:14:10 2015 +0000
Revision:
0:9c8c234fd5ae
Initial commit of Rock, Paper, Scissors game coordinator

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mnorman4 0:9c8c234fd5ae 1 #include "mbed.h"
mnorman4 0:9c8c234fd5ae 2 #include "rtos.h"
mnorman4 0:9c8c234fd5ae 3 #include "Phy.h"
mnorman4 0:9c8c234fd5ae 4 #include "SMAC_Interface.h"
mnorman4 0:9c8c234fd5ae 5 #include "SMAC_Config.h"
mnorman4 0:9c8c234fd5ae 6 #include "MemManager.h"
mnorman4 0:9c8c234fd5ae 7 #include "circular_buffer.h"
mnorman4 0:9c8c234fd5ae 8
mnorman4 0:9c8c234fd5ae 9 /* Constant Strings */
mnorman4 0:9c8c234fd5ae 10 char const StartupMessage[]= "\rGame Controller Bridge is online.\r\n\n";
mnorman4 0:9c8c234fd5ae 11 char const LetsPlay[] = "Let's play Rock, Paper, Scissors!\r\n";
mnorman4 0:9c8c234fd5ae 12 char const PlayerAString[] = "Player A";
mnorman4 0:9c8c234fd5ae 13 char const PlayerBString[] = "Player B";
mnorman4 0:9c8c234fd5ae 14 char const StrRock[] = "Rock";
mnorman4 0:9c8c234fd5ae 15 char const StrPaper[] = "Paper";
mnorman4 0:9c8c234fd5ae 16 char const StrScissors[] = "Scissors";
mnorman4 0:9c8c234fd5ae 17
mnorman4 0:9c8c234fd5ae 18 /* Hardware Resources */
mnorman4 0:9c8c234fd5ae 19 /* LEDs */
mnorman4 0:9c8c234fd5ae 20 DigitalOut k64f_led_red(LED_RED, 1);
mnorman4 0:9c8c234fd5ae 21 DigitalOut k64f_led_green(LED_GREEN, 1);
mnorman4 0:9c8c234fd5ae 22 DigitalOut k64f_led_blue(LED_BLUE, 1);
mnorman4 0:9c8c234fd5ae 23 DigitalOut cr20a_led_red(PTC11, 1);
mnorman4 0:9c8c234fd5ae 24 DigitalOut cr20a_led_green(PTC10, 1);
mnorman4 0:9c8c234fd5ae 25 DigitalOut cr20a_led_blue(PTB11, 1);
mnorman4 0:9c8c234fd5ae 26
mnorman4 0:9c8c234fd5ae 27 #define LED_ON (0)
mnorman4 0:9c8c234fd5ae 28 #define LED_OFF (1)
mnorman4 0:9c8c234fd5ae 29
mnorman4 0:9c8c234fd5ae 30 /* Pushbuttons */
mnorman4 0:9c8c234fd5ae 31 InterruptIn k64f_sw2(SW2);
mnorman4 0:9c8c234fd5ae 32 InterruptIn k64f_sw3(SW3);
mnorman4 0:9c8c234fd5ae 33 InterruptIn cr20a_sw1(PTB23);
mnorman4 0:9c8c234fd5ae 34 InterruptIn cr20a_sw2(PTA1);
mnorman4 0:9c8c234fd5ae 35 InterruptIn cr20a_sw3(PTC4);
mnorman4 0:9c8c234fd5ae 36
mnorman4 0:9c8c234fd5ae 37 #define gPushbutton_K64F_SW2 (1<<1)
mnorman4 0:9c8c234fd5ae 38 #define gPushbutton_K64F_SW3 (1<<2)
mnorman4 0:9c8c234fd5ae 39 #define gPushbutton_CR20A_SW1 (1<<3)
mnorman4 0:9c8c234fd5ae 40 #define gPushbutton_CR20A_SW2 (1<<4)
mnorman4 0:9c8c234fd5ae 41 #define gPushbutton_CR20A_SW3 (1<<5)
mnorman4 0:9c8c234fd5ae 42
mnorman4 0:9c8c234fd5ae 43 /* OpenSDA Serial Port (UART) */
mnorman4 0:9c8c234fd5ae 44 Serial uart(USBTX, USBRX);
mnorman4 0:9c8c234fd5ae 45 CircularBuffer uartBuf;
mnorman4 0:9c8c234fd5ae 46 #define gDefaultBaudRate_UART_c 115200UL
mnorman4 0:9c8c234fd5ae 47
mnorman4 0:9c8c234fd5ae 48 /* Event Flags */
mnorman4 0:9c8c234fd5ae 49 #define gMcps_Cnf_EVENT_c (1<<1)
mnorman4 0:9c8c234fd5ae 50 #define gMcps_Ind_EVENT_c (1<<2)
mnorman4 0:9c8c234fd5ae 51 #define gMlme_EdCnf_EVENT_c (1<<3)
mnorman4 0:9c8c234fd5ae 52 #define gMlme_CcaCnf_EVENT_c (1<<4)
mnorman4 0:9c8c234fd5ae 53 #define gMlme_TimeoutInd_EVENT_c (1<<5)
mnorman4 0:9c8c234fd5ae 54 #define gWUSelf_EVENT_c (1<<6)
mnorman4 0:9c8c234fd5ae 55
mnorman4 0:9c8c234fd5ae 56 #ifdef VERBOSE
mnorman4 0:9c8c234fd5ae 57 static bool_t bCCAFailed;
mnorman4 0:9c8c234fd5ae 58 static bool_t bACKFailed;
mnorman4 0:9c8c234fd5ae 59 #endif
mnorman4 0:9c8c234fd5ae 60
mnorman4 0:9c8c234fd5ae 61 uint32_t gTaskEventFlags;
mnorman4 0:9c8c234fd5ae 62 static uint8_t gau8TxDataBuffer[gMaxSmacSDULength_c + sizeof(rxPacket_t)];
mnorman4 0:9c8c234fd5ae 63 txPacket_t *gAppTxPacket;
mnorman4 0:9c8c234fd5ae 64 rxPacket_t *gAppRxPacket;
mnorman4 0:9c8c234fd5ae 65 static txContextConfig_t txConfigContext;
mnorman4 0:9c8c234fd5ae 66
mnorman4 0:9c8c234fd5ae 67 void InitProject(void);
mnorman4 0:9c8c234fd5ae 68 void InitApp(void);
mnorman4 0:9c8c234fd5ae 69
mnorman4 0:9c8c234fd5ae 70 extern smacErrors_t smacToAppMlmeSap(smacToAppMlmeMessage_t* pMsg, instanceId_t instance);
mnorman4 0:9c8c234fd5ae 71 extern smacErrors_t smacToAppMcpsSap(smacToAppDataMessage_t* pMsg, instanceId_t instance);
mnorman4 0:9c8c234fd5ae 72
mnorman4 0:9c8c234fd5ae 73 osThreadId PushbuttonThreadID;
mnorman4 0:9c8c234fd5ae 74 osThreadId EventsThreadID;
mnorman4 0:9c8c234fd5ae 75
mnorman4 0:9c8c234fd5ae 76 void k64f_sw2_press(void) { osSignalSet(PushbuttonThreadID, gPushbutton_K64F_SW2); }
mnorman4 0:9c8c234fd5ae 77 void k64f_sw3_press(void) { osSignalSet(PushbuttonThreadID, gPushbutton_K64F_SW3); }
mnorman4 0:9c8c234fd5ae 78 void cr20a_sw1_press(void) { osSignalSet(PushbuttonThreadID, gPushbutton_CR20A_SW1); }
mnorman4 0:9c8c234fd5ae 79 void cr20a_sw2_press(void) { osSignalSet(PushbuttonThreadID, gPushbutton_CR20A_SW2); }
mnorman4 0:9c8c234fd5ae 80 void cr20a_sw3_press(void) { osSignalSet(PushbuttonThreadID, gPushbutton_CR20A_SW3); }
mnorman4 0:9c8c234fd5ae 81
mnorman4 0:9c8c234fd5ae 82 /* Constants used to build the single byte game selection */
mnorman4 0:9c8c234fd5ae 83 char const PlayerA = 0xA0;
mnorman4 0:9c8c234fd5ae 84 char const PlayerB = 0xB0;
mnorman4 0:9c8c234fd5ae 85 char const Rock = 0x01;
mnorman4 0:9c8c234fd5ae 86 char const Paper = 0x02;
mnorman4 0:9c8c234fd5ae 87 char const Scissors = 0x03;
mnorman4 0:9c8c234fd5ae 88 char const Winner = 0x08;
mnorman4 0:9c8c234fd5ae 89 char const Loser = 0x0F;
mnorman4 0:9c8c234fd5ae 90 char const Draw = 0xFF;
mnorman4 0:9c8c234fd5ae 91
mnorman4 0:9c8c234fd5ae 92 /* RPS States */
mnorman4 0:9c8c234fd5ae 93 #define GameStateWaitingBoth (1<<0)
mnorman4 0:9c8c234fd5ae 94 #define GameStateWaitingA (1<<1)
mnorman4 0:9c8c234fd5ae 95 #define GameStateWaitingB (1<<2)
mnorman4 0:9c8c234fd5ae 96 #define GameStateReady (1<<3)
mnorman4 0:9c8c234fd5ae 97 #define GameStateAWins (1<<4)
mnorman4 0:9c8c234fd5ae 98 #define GameStateBWins (1<<5)
mnorman4 0:9c8c234fd5ae 99 #define GameStateDraw (1<<6)
mnorman4 0:9c8c234fd5ae 100
mnorman4 0:9c8c234fd5ae 101 int GameState = GameStateWaitingBoth;
mnorman4 0:9c8c234fd5ae 102 uint8_t Aselection = 0;
mnorman4 0:9c8c234fd5ae 103 uint8_t Bselection = 0;
mnorman4 0:9c8c234fd5ae 104
mnorman4 0:9c8c234fd5ae 105 /* Stats */
mnorman4 0:9c8c234fd5ae 106 int GameNum = 0;
mnorman4 0:9c8c234fd5ae 107 int NumWinsA = 0;
mnorman4 0:9c8c234fd5ae 108 int NumWinsB = 0;
mnorman4 0:9c8c234fd5ae 109 int NumDraws = 0;
mnorman4 0:9c8c234fd5ae 110
mnorman4 0:9c8c234fd5ae 111 void HeartbeatThread(void const *argument)
mnorman4 0:9c8c234fd5ae 112 {
mnorman4 0:9c8c234fd5ae 113 while (true) {
mnorman4 0:9c8c234fd5ae 114 k64f_led_green = LED_ON;
mnorman4 0:9c8c234fd5ae 115 Thread::wait(50);
mnorman4 0:9c8c234fd5ae 116 k64f_led_green = LED_OFF;
mnorman4 0:9c8c234fd5ae 117 Thread::wait(1950);
mnorman4 0:9c8c234fd5ae 118 }
mnorman4 0:9c8c234fd5ae 119 }
mnorman4 0:9c8c234fd5ae 120
mnorman4 0:9c8c234fd5ae 121 void PushbuttonThread(void const *argument)
mnorman4 0:9c8c234fd5ae 122 {
mnorman4 0:9c8c234fd5ae 123 PushbuttonThreadID = Thread::gettid();
mnorman4 0:9c8c234fd5ae 124 osEvent event;
mnorman4 0:9c8c234fd5ae 125
mnorman4 0:9c8c234fd5ae 126 while (true) {
mnorman4 0:9c8c234fd5ae 127 event = Thread::signal_wait(0, osWaitForever);
mnorman4 0:9c8c234fd5ae 128
mnorman4 0:9c8c234fd5ae 129 //TODO: Consider using pushbuttons on bridge to set game mode (single/dual player, etc.)
mnorman4 0:9c8c234fd5ae 130
mnorman4 0:9c8c234fd5ae 131 if (event.value.signals & gPushbutton_K64F_SW2) {
mnorman4 0:9c8c234fd5ae 132 uart.printf("K64F_SW2\r\n");
mnorman4 0:9c8c234fd5ae 133 }
mnorman4 0:9c8c234fd5ae 134 if (event.value.signals & gPushbutton_K64F_SW3) {
mnorman4 0:9c8c234fd5ae 135 uart.printf("K64F_SW3\r\n");
mnorman4 0:9c8c234fd5ae 136 }
mnorman4 0:9c8c234fd5ae 137 if (event.value.signals & gPushbutton_CR20A_SW1) {
mnorman4 0:9c8c234fd5ae 138 uart.printf("CR20A_SW1\r\n");
mnorman4 0:9c8c234fd5ae 139 }
mnorman4 0:9c8c234fd5ae 140 if (event.value.signals & gPushbutton_CR20A_SW2) {
mnorman4 0:9c8c234fd5ae 141 uart.printf("CR20A_SW2\r\n");
mnorman4 0:9c8c234fd5ae 142 }
mnorman4 0:9c8c234fd5ae 143 if (event.value.signals & gPushbutton_CR20A_SW3) {
mnorman4 0:9c8c234fd5ae 144 uart.printf("CR20A_SW3\r\n");
mnorman4 0:9c8c234fd5ae 145 }
mnorman4 0:9c8c234fd5ae 146 }
mnorman4 0:9c8c234fd5ae 147 }
mnorman4 0:9c8c234fd5ae 148
mnorman4 0:9c8c234fd5ae 149 void PrintSelection (uint8_t selection)
mnorman4 0:9c8c234fd5ae 150 {
mnorman4 0:9c8c234fd5ae 151 const char *PlayerString;
mnorman4 0:9c8c234fd5ae 152 const char *WeaponString;
mnorman4 0:9c8c234fd5ae 153 uint8_t player = ((selection & 0xF0) >> 4);
mnorman4 0:9c8c234fd5ae 154 uint8_t weapon = ((selection & 0x0F) >> 0);
mnorman4 0:9c8c234fd5ae 155
mnorman4 0:9c8c234fd5ae 156 if (player == 0xA)
mnorman4 0:9c8c234fd5ae 157 PlayerString = PlayerAString;
mnorman4 0:9c8c234fd5ae 158 else if (player == 0xB)
mnorman4 0:9c8c234fd5ae 159 PlayerString = PlayerBString;
mnorman4 0:9c8c234fd5ae 160 else {
mnorman4 0:9c8c234fd5ae 161 uart.printf("Invalid Player\r\n");
mnorman4 0:9c8c234fd5ae 162 return;
mnorman4 0:9c8c234fd5ae 163 }
mnorman4 0:9c8c234fd5ae 164
mnorman4 0:9c8c234fd5ae 165 switch (weapon)
mnorman4 0:9c8c234fd5ae 166 {
mnorman4 0:9c8c234fd5ae 167 case Rock:
mnorman4 0:9c8c234fd5ae 168 WeaponString = StrRock;
mnorman4 0:9c8c234fd5ae 169 break;
mnorman4 0:9c8c234fd5ae 170 case Paper:
mnorman4 0:9c8c234fd5ae 171 WeaponString = StrPaper;
mnorman4 0:9c8c234fd5ae 172 break;
mnorman4 0:9c8c234fd5ae 173 case Scissors:
mnorman4 0:9c8c234fd5ae 174 WeaponString = StrScissors;
mnorman4 0:9c8c234fd5ae 175 break;
mnorman4 0:9c8c234fd5ae 176 default:
mnorman4 0:9c8c234fd5ae 177 uart.printf("Invalid Weapon: 0x%d\r\n", weapon);
mnorman4 0:9c8c234fd5ae 178 return;
mnorman4 0:9c8c234fd5ae 179 }
mnorman4 0:9c8c234fd5ae 180
mnorman4 0:9c8c234fd5ae 181 uart.printf("%s: %s\r\n", PlayerString, WeaponString);
mnorman4 0:9c8c234fd5ae 182 }
mnorman4 0:9c8c234fd5ae 183
mnorman4 0:9c8c234fd5ae 184 void RockPaperScissors (uint8_t selection)
mnorman4 0:9c8c234fd5ae 185 {
mnorman4 0:9c8c234fd5ae 186 uint8_t player = ((selection & 0xF0) >> 4);
mnorman4 0:9c8c234fd5ae 187 uint8_t weapon = ((selection & 0x0F) >> 0);
mnorman4 0:9c8c234fd5ae 188
mnorman4 0:9c8c234fd5ae 189 switch (GameState) {
mnorman4 0:9c8c234fd5ae 190 case GameStateWaitingBoth:
mnorman4 0:9c8c234fd5ae 191 if (player == 0xA) {
mnorman4 0:9c8c234fd5ae 192 Aselection = weapon;
mnorman4 0:9c8c234fd5ae 193 GameState = GameStateWaitingB;
mnorman4 0:9c8c234fd5ae 194 uart.printf("Player A: Ready\r\n");
mnorman4 0:9c8c234fd5ae 195 }
mnorman4 0:9c8c234fd5ae 196 if (player == 0xB) {
mnorman4 0:9c8c234fd5ae 197 Bselection = weapon;
mnorman4 0:9c8c234fd5ae 198 GameState = GameStateWaitingA;
mnorman4 0:9c8c234fd5ae 199 uart.printf("Player B: Ready\r\n");
mnorman4 0:9c8c234fd5ae 200 }
mnorman4 0:9c8c234fd5ae 201 break;
mnorman4 0:9c8c234fd5ae 202 case GameStateWaitingA:
mnorman4 0:9c8c234fd5ae 203 if (player == 0xA) {
mnorman4 0:9c8c234fd5ae 204 Aselection = weapon;
mnorman4 0:9c8c234fd5ae 205 uart.printf("Player A: Ready\r\n");
mnorman4 0:9c8c234fd5ae 206 GameState = GameStateReady;
mnorman4 0:9c8c234fd5ae 207 }
mnorman4 0:9c8c234fd5ae 208 break;
mnorman4 0:9c8c234fd5ae 209 case GameStateWaitingB:
mnorman4 0:9c8c234fd5ae 210 if (player == 0xB) {
mnorman4 0:9c8c234fd5ae 211 Bselection = weapon;
mnorman4 0:9c8c234fd5ae 212 uart.printf("Player B: Ready\r\n");
mnorman4 0:9c8c234fd5ae 213 GameState = GameStateReady;
mnorman4 0:9c8c234fd5ae 214 }
mnorman4 0:9c8c234fd5ae 215 break;
mnorman4 0:9c8c234fd5ae 216 }
mnorman4 0:9c8c234fd5ae 217
mnorman4 0:9c8c234fd5ae 218 if (GameState == GameStateReady)
mnorman4 0:9c8c234fd5ae 219 {
mnorman4 0:9c8c234fd5ae 220 if (Aselection == Rock)
mnorman4 0:9c8c234fd5ae 221 {
mnorman4 0:9c8c234fd5ae 222 uart.printf("\nRock vs. ");
mnorman4 0:9c8c234fd5ae 223 if (Bselection == Rock)
mnorman4 0:9c8c234fd5ae 224 {
mnorman4 0:9c8c234fd5ae 225 uart.printf("Rock\r\n");
mnorman4 0:9c8c234fd5ae 226 GameState = GameStateDraw;
mnorman4 0:9c8c234fd5ae 227 }
mnorman4 0:9c8c234fd5ae 228 if (Bselection == Paper)
mnorman4 0:9c8c234fd5ae 229 {
mnorman4 0:9c8c234fd5ae 230 uart.printf("Paper\r\n");
mnorman4 0:9c8c234fd5ae 231 GameState = GameStateBWins;
mnorman4 0:9c8c234fd5ae 232 }
mnorman4 0:9c8c234fd5ae 233 if (Bselection == Scissors)
mnorman4 0:9c8c234fd5ae 234 {
mnorman4 0:9c8c234fd5ae 235 uart.printf("Scissors\r\n");
mnorman4 0:9c8c234fd5ae 236 GameState = GameStateAWins;
mnorman4 0:9c8c234fd5ae 237 }
mnorman4 0:9c8c234fd5ae 238 }
mnorman4 0:9c8c234fd5ae 239 if (Aselection == Paper)
mnorman4 0:9c8c234fd5ae 240 {
mnorman4 0:9c8c234fd5ae 241 uart.printf("\nPaper vs. ");
mnorman4 0:9c8c234fd5ae 242 if (Bselection == Rock)
mnorman4 0:9c8c234fd5ae 243 {
mnorman4 0:9c8c234fd5ae 244 uart.printf("Rock\r\n");
mnorman4 0:9c8c234fd5ae 245 GameState = GameStateAWins;
mnorman4 0:9c8c234fd5ae 246 }
mnorman4 0:9c8c234fd5ae 247 if (Bselection == Paper)
mnorman4 0:9c8c234fd5ae 248 {
mnorman4 0:9c8c234fd5ae 249 uart.printf("Paper\r\n");
mnorman4 0:9c8c234fd5ae 250 GameState = GameStateDraw;
mnorman4 0:9c8c234fd5ae 251 }
mnorman4 0:9c8c234fd5ae 252 if (Bselection == Scissors)
mnorman4 0:9c8c234fd5ae 253 {
mnorman4 0:9c8c234fd5ae 254 uart.printf("Scissors\r\n");
mnorman4 0:9c8c234fd5ae 255 GameState = GameStateBWins;
mnorman4 0:9c8c234fd5ae 256 }
mnorman4 0:9c8c234fd5ae 257 }
mnorman4 0:9c8c234fd5ae 258 if (Aselection == Scissors)
mnorman4 0:9c8c234fd5ae 259 {
mnorman4 0:9c8c234fd5ae 260 uart.printf("\nScissors vs. ");
mnorman4 0:9c8c234fd5ae 261 if (Bselection == Rock)
mnorman4 0:9c8c234fd5ae 262 {
mnorman4 0:9c8c234fd5ae 263 uart.printf("Rock\r\n");
mnorman4 0:9c8c234fd5ae 264 GameState = GameStateBWins;
mnorman4 0:9c8c234fd5ae 265 }
mnorman4 0:9c8c234fd5ae 266 if (Bselection == Paper)
mnorman4 0:9c8c234fd5ae 267 {
mnorman4 0:9c8c234fd5ae 268 uart.printf("Paper\r\n");
mnorman4 0:9c8c234fd5ae 269 GameState = GameStateAWins;
mnorman4 0:9c8c234fd5ae 270 }
mnorman4 0:9c8c234fd5ae 271 if (Bselection == Scissors)
mnorman4 0:9c8c234fd5ae 272 {
mnorman4 0:9c8c234fd5ae 273 uart.printf("Scissors\r\n");
mnorman4 0:9c8c234fd5ae 274 GameState = GameStateDraw;
mnorman4 0:9c8c234fd5ae 275 }
mnorman4 0:9c8c234fd5ae 276 }
mnorman4 0:9c8c234fd5ae 277 }
mnorman4 0:9c8c234fd5ae 278 if (GameState == GameStateAWins)
mnorman4 0:9c8c234fd5ae 279 {
mnorman4 0:9c8c234fd5ae 280 uart.printf("Player A Wins!\r\n\n");
mnorman4 0:9c8c234fd5ae 281 (void)uartBuf.addToBuffer(uint8_t(PlayerA | Winner));
mnorman4 0:9c8c234fd5ae 282 ++NumWinsA;
mnorman4 0:9c8c234fd5ae 283 GameState = GameStateWaitingBoth;
mnorman4 0:9c8c234fd5ae 284 }
mnorman4 0:9c8c234fd5ae 285 if (GameState == GameStateBWins)
mnorman4 0:9c8c234fd5ae 286 {
mnorman4 0:9c8c234fd5ae 287 uart.printf("Player B Wins!\r\n\n");
mnorman4 0:9c8c234fd5ae 288 (void)uartBuf.addToBuffer(uint8_t(PlayerB | Winner));
mnorman4 0:9c8c234fd5ae 289 ++NumWinsB;
mnorman4 0:9c8c234fd5ae 290 GameState = GameStateWaitingBoth;
mnorman4 0:9c8c234fd5ae 291 }
mnorman4 0:9c8c234fd5ae 292 if (GameState == GameStateDraw)
mnorman4 0:9c8c234fd5ae 293 {
mnorman4 0:9c8c234fd5ae 294 uart.printf("It's a draw.\r\n\n");
mnorman4 0:9c8c234fd5ae 295 (void)uartBuf.addToBuffer(uint8_t(Draw));
mnorman4 0:9c8c234fd5ae 296 ++NumDraws;
mnorman4 0:9c8c234fd5ae 297 GameState = GameStateWaitingBoth;
mnorman4 0:9c8c234fd5ae 298 }
mnorman4 0:9c8c234fd5ae 299 if (GameState == GameStateWaitingBoth)
mnorman4 0:9c8c234fd5ae 300 {
mnorman4 0:9c8c234fd5ae 301 uart.printf("%s", LetsPlay);
mnorman4 0:9c8c234fd5ae 302 }
mnorman4 0:9c8c234fd5ae 303 if (uartBuf.getCount())
mnorman4 0:9c8c234fd5ae 304 {
mnorman4 0:9c8c234fd5ae 305 gTaskEventFlags |= gWUSelf_EVENT_c;
mnorman4 0:9c8c234fd5ae 306 osSignalSet(EventsThreadID, 0x1);
mnorman4 0:9c8c234fd5ae 307 }
mnorman4 0:9c8c234fd5ae 308 }
mnorman4 0:9c8c234fd5ae 309
mnorman4 0:9c8c234fd5ae 310 void EventsThread(void const *argument)
mnorman4 0:9c8c234fd5ae 311 {
mnorman4 0:9c8c234fd5ae 312 EventsThreadID = Thread::gettid();
mnorman4 0:9c8c234fd5ae 313 uint8_t rcvd = 0, c = 0;
mnorman4 0:9c8c234fd5ae 314
mnorman4 0:9c8c234fd5ae 315 while (true)
mnorman4 0:9c8c234fd5ae 316 {
mnorman4 0:9c8c234fd5ae 317 Thread::signal_wait(0x1);
mnorman4 0:9c8c234fd5ae 318 if(gMcps_Cnf_EVENT_c == (gTaskEventFlags & gMcps_Cnf_EVENT_c))
mnorman4 0:9c8c234fd5ae 319 {
mnorman4 0:9c8c234fd5ae 320 MLMERXEnableRequest(gAppRxPacket, 0);
mnorman4 0:9c8c234fd5ae 321 }
mnorman4 0:9c8c234fd5ae 322 if(gMcps_Ind_EVENT_c == (gTaskEventFlags & gMcps_Ind_EVENT_c))
mnorman4 0:9c8c234fd5ae 323 {
mnorman4 0:9c8c234fd5ae 324 rcvd = gAppRxPacket->smacPdu.smacPdu[0];
mnorman4 0:9c8c234fd5ae 325 RockPaperScissors(rcvd);
mnorman4 0:9c8c234fd5ae 326 MLMERXEnableRequest(gAppRxPacket, 0);
mnorman4 0:9c8c234fd5ae 327 }
mnorman4 0:9c8c234fd5ae 328 if(gMlme_TimeoutInd_EVENT_c == (gTaskEventFlags & gMlme_TimeoutInd_EVENT_c))
mnorman4 0:9c8c234fd5ae 329 {
mnorman4 0:9c8c234fd5ae 330 uart.printf("MlmeTimeoutInd: \r\n");
mnorman4 0:9c8c234fd5ae 331 }
mnorman4 0:9c8c234fd5ae 332 if(gMlme_EdCnf_EVENT_c == (gTaskEventFlags & gMlme_EdCnf_EVENT_c))
mnorman4 0:9c8c234fd5ae 333 {
mnorman4 0:9c8c234fd5ae 334 uart.printf("EdCnf: \r\n");
mnorman4 0:9c8c234fd5ae 335 }
mnorman4 0:9c8c234fd5ae 336 if(gMlme_CcaCnf_EVENT_c == (gTaskEventFlags & gMlme_CcaCnf_EVENT_c))
mnorman4 0:9c8c234fd5ae 337 {
mnorman4 0:9c8c234fd5ae 338 uart.printf("CcaCnf: \r\n");
mnorman4 0:9c8c234fd5ae 339 }
mnorman4 0:9c8c234fd5ae 340 if(gWUSelf_EVENT_c == (gTaskEventFlags & gWUSelf_EVENT_c))
mnorman4 0:9c8c234fd5ae 341 {
mnorman4 0:9c8c234fd5ae 342 if (buffer_Ok_c == uartBuf.getFromBuffer(&c))
mnorman4 0:9c8c234fd5ae 343 {
mnorman4 0:9c8c234fd5ae 344 gAppTxPacket->smacPdu.smacPdu[0] = c;
mnorman4 0:9c8c234fd5ae 345 gAppTxPacket->u8DataLength = 1;
mnorman4 0:9c8c234fd5ae 346 (void)MLMERXDisableRequest();
mnorman4 0:9c8c234fd5ae 347 (void)MCPSDataRequest(gAppTxPacket);
mnorman4 0:9c8c234fd5ae 348 }
mnorman4 0:9c8c234fd5ae 349 }
mnorman4 0:9c8c234fd5ae 350 gTaskEventFlags = 0;
mnorman4 0:9c8c234fd5ae 351 }
mnorman4 0:9c8c234fd5ae 352 }
mnorman4 0:9c8c234fd5ae 353
mnorman4 0:9c8c234fd5ae 354 int main()
mnorman4 0:9c8c234fd5ae 355 {
mnorman4 0:9c8c234fd5ae 356 MEM_Init();
mnorman4 0:9c8c234fd5ae 357 Thread heartbeat(HeartbeatThread);
mnorman4 0:9c8c234fd5ae 358 Thread pushbuttons(PushbuttonThread);
mnorman4 0:9c8c234fd5ae 359 Thread events(EventsThread);
mnorman4 0:9c8c234fd5ae 360 Phy_Init();
mnorman4 0:9c8c234fd5ae 361 InitSmac();
mnorman4 0:9c8c234fd5ae 362
mnorman4 0:9c8c234fd5ae 363 //Tell SMAC who to call when it needs to pass a message to the application thread.
mnorman4 0:9c8c234fd5ae 364 Smac_RegisterSapHandlers((SMAC_APP_MCPS_SapHandler_t)smacToAppMcpsSap,(SMAC_APP_MLME_SapHandler_t)smacToAppMlmeSap,0);
mnorman4 0:9c8c234fd5ae 365
mnorman4 0:9c8c234fd5ae 366 InitApp();
mnorman4 0:9c8c234fd5ae 367
mnorman4 0:9c8c234fd5ae 368 //uart.printf(StartupMessage);
mnorman4 0:9c8c234fd5ae 369 uart.printf("%s", LetsPlay);
mnorman4 0:9c8c234fd5ae 370
mnorman4 0:9c8c234fd5ae 371 while (true)
mnorman4 0:9c8c234fd5ae 372 {
mnorman4 0:9c8c234fd5ae 373 Thread::yield();
mnorman4 0:9c8c234fd5ae 374 }
mnorman4 0:9c8c234fd5ae 375 }
mnorman4 0:9c8c234fd5ae 376
mnorman4 0:9c8c234fd5ae 377 void InitApp()
mnorman4 0:9c8c234fd5ae 378 {
mnorman4 0:9c8c234fd5ae 379 gAppTxPacket = (txPacket_t*)gau8TxDataBuffer; //Map TX packet to buffer
mnorman4 0:9c8c234fd5ae 380 gAppRxPacket = (rxPacket_t*)MEM_BufferAlloc(gMaxSmacSDULength_c + sizeof(rxPacket_t));
mnorman4 0:9c8c234fd5ae 381
mnorman4 0:9c8c234fd5ae 382 InitProject();
mnorman4 0:9c8c234fd5ae 383
mnorman4 0:9c8c234fd5ae 384 SMACFillHeader(&(gAppTxPacket->smacHeader), gDefaultAddress_c);
mnorman4 0:9c8c234fd5ae 385
mnorman4 0:9c8c234fd5ae 386 (void)MLMEPAOutputAdjust(gDefaultOutputPower_c);
mnorman4 0:9c8c234fd5ae 387 (void)MLMESetChannelRequest(gDefaultChannelNumber_c);
mnorman4 0:9c8c234fd5ae 388 (void)MLMEConfigureTxContext(&txConfigContext);
mnorman4 0:9c8c234fd5ae 389 //AppDelayTmr = TMR_AllocateTimer();
mnorman4 0:9c8c234fd5ae 390 gAppRxPacket->u8MaxDataLength = gMaxSmacSDULength_c;
mnorman4 0:9c8c234fd5ae 391 (void)MLMERXEnableRequest(gAppRxPacket, 0);
mnorman4 0:9c8c234fd5ae 392 }
mnorman4 0:9c8c234fd5ae 393
mnorman4 0:9c8c234fd5ae 394 /* (Management) Sap handler for managing timeout indication and ED confirm
mnorman4 0:9c8c234fd5ae 395 This is running in INTERRUPT context, so need to send messages to one of the task */
mnorman4 0:9c8c234fd5ae 396 smacErrors_t smacToAppMlmeSap(smacToAppMlmeMessage_t* pMsg, instanceId_t instance)
mnorman4 0:9c8c234fd5ae 397 {
mnorman4 0:9c8c234fd5ae 398 switch(pMsg->msgType)
mnorman4 0:9c8c234fd5ae 399 {
mnorman4 0:9c8c234fd5ae 400 case gMlmeEdCnf_c:
mnorman4 0:9c8c234fd5ae 401 gTaskEventFlags |= gMlme_EdCnf_EVENT_c;
mnorman4 0:9c8c234fd5ae 402 break;
mnorman4 0:9c8c234fd5ae 403 case gMlmeCcaCnf_c:
mnorman4 0:9c8c234fd5ae 404 gTaskEventFlags |= gMlme_CcaCnf_EVENT_c;
mnorman4 0:9c8c234fd5ae 405 break;
mnorman4 0:9c8c234fd5ae 406 case gMlmeTimeoutInd_c:
mnorman4 0:9c8c234fd5ae 407 gTaskEventFlags |= gMlme_TimeoutInd_EVENT_c;
mnorman4 0:9c8c234fd5ae 408 break;
mnorman4 0:9c8c234fd5ae 409 default:
mnorman4 0:9c8c234fd5ae 410 break;
mnorman4 0:9c8c234fd5ae 411 }
mnorman4 0:9c8c234fd5ae 412 osSignalSet(EventsThreadID, 0x1);
mnorman4 0:9c8c234fd5ae 413 MEM_BufferFree(pMsg);
mnorman4 0:9c8c234fd5ae 414 return gErrorNoError_c;
mnorman4 0:9c8c234fd5ae 415 }
mnorman4 0:9c8c234fd5ae 416
mnorman4 0:9c8c234fd5ae 417 /* (Data) Sap handler for managing data confirm and data indication
mnorman4 0:9c8c234fd5ae 418 This is running in INTERRUPT context, so need to send messages to one of the task */
mnorman4 0:9c8c234fd5ae 419 smacErrors_t smacToAppMcpsSap(smacToAppDataMessage_t* pMsg, instanceId_t instance)
mnorman4 0:9c8c234fd5ae 420 {
mnorman4 0:9c8c234fd5ae 421 switch(pMsg->msgType)
mnorman4 0:9c8c234fd5ae 422 {
mnorman4 0:9c8c234fd5ae 423 case gMcpsDataInd_c:
mnorman4 0:9c8c234fd5ae 424 if(pMsg->msgData.dataInd.pRxPacket->rxStatus == rxSuccessStatus_c)
mnorman4 0:9c8c234fd5ae 425 {
mnorman4 0:9c8c234fd5ae 426 gTaskEventFlags |= gMcps_Ind_EVENT_c;
mnorman4 0:9c8c234fd5ae 427 }
mnorman4 0:9c8c234fd5ae 428 break;
mnorman4 0:9c8c234fd5ae 429
mnorman4 0:9c8c234fd5ae 430 case gMcpsDataCnf_c:
mnorman4 0:9c8c234fd5ae 431 #ifdef VERBOSE
mnorman4 0:9c8c234fd5ae 432 if(pMsg->msgData.dataCnf.status == gErrorChannelBusy_c)
mnorman4 0:9c8c234fd5ae 433 {
mnorman4 0:9c8c234fd5ae 434 bCCAFailed = TRUE;
mnorman4 0:9c8c234fd5ae 435 }
mnorman4 0:9c8c234fd5ae 436
mnorman4 0:9c8c234fd5ae 437 if(pMsg->msgData.dataCnf.status == gErrorNoAck_c)
mnorman4 0:9c8c234fd5ae 438 {
mnorman4 0:9c8c234fd5ae 439 bACKFailed = TRUE;
mnorman4 0:9c8c234fd5ae 440 }
mnorman4 0:9c8c234fd5ae 441 #endif
mnorman4 0:9c8c234fd5ae 442 gTaskEventFlags |= gMcps_Cnf_EVENT_c;
mnorman4 0:9c8c234fd5ae 443 break;
mnorman4 0:9c8c234fd5ae 444 default:
mnorman4 0:9c8c234fd5ae 445 break;
mnorman4 0:9c8c234fd5ae 446 }
mnorman4 0:9c8c234fd5ae 447 osSignalSet(EventsThreadID, 0x1);
mnorman4 0:9c8c234fd5ae 448 MEM_BufferFree(pMsg);
mnorman4 0:9c8c234fd5ae 449
mnorman4 0:9c8c234fd5ae 450 return gErrorNoError_c;
mnorman4 0:9c8c234fd5ae 451 }
mnorman4 0:9c8c234fd5ae 452
mnorman4 0:9c8c234fd5ae 453 void InitProject(void)
mnorman4 0:9c8c234fd5ae 454 {
mnorman4 0:9c8c234fd5ae 455 /*Global Data init*/
mnorman4 0:9c8c234fd5ae 456 #ifdef VERBOSE
mnorman4 0:9c8c234fd5ae 457 bACKFailed = FALSE;
mnorman4 0:9c8c234fd5ae 458 bCCAFailed = FALSE;
mnorman4 0:9c8c234fd5ae 459 #endif
mnorman4 0:9c8c234fd5ae 460
mnorman4 0:9c8c234fd5ae 461 gTaskEventFlags = 0;
mnorman4 0:9c8c234fd5ae 462
mnorman4 0:9c8c234fd5ae 463 txConfigContext.autoAck = FALSE;
mnorman4 0:9c8c234fd5ae 464 txConfigContext.ccaBeforeTx = FALSE;
mnorman4 0:9c8c234fd5ae 465 txConfigContext.retryCountAckFail = 0;
mnorman4 0:9c8c234fd5ae 466 txConfigContext.retryCountCCAFail = 0;
mnorman4 0:9c8c234fd5ae 467
mnorman4 0:9c8c234fd5ae 468 /* Setup UART */
mnorman4 0:9c8c234fd5ae 469 uart.baud(gDefaultBaudRate_UART_c);
mnorman4 0:9c8c234fd5ae 470
mnorman4 0:9c8c234fd5ae 471 /* Setup Pushbutton Interrupt Callbacks */
mnorman4 0:9c8c234fd5ae 472 k64f_sw2.fall(&k64f_sw2_press);
mnorman4 0:9c8c234fd5ae 473 k64f_sw3.fall(&k64f_sw3_press);
mnorman4 0:9c8c234fd5ae 474 cr20a_sw1.fall(&cr20a_sw1_press);
mnorman4 0:9c8c234fd5ae 475 cr20a_sw2.fall(&cr20a_sw2_press);
mnorman4 0:9c8c234fd5ae 476 cr20a_sw3.fall(&cr20a_sw3_press);
mnorman4 0:9c8c234fd5ae 477 }