Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of QW-BinaryDices by
main.cpp
00001 /* This program demonstrates how to use the board to simulate a dual dice throw. 00002 * The result is displayed in binary on the leds and transmitted via Sigfox. 00003 * Open a serial console to the board to see extra info. 00004 */ 00005 00006 #include "mbed.h" 00007 00008 #include <stdio.h> 00009 #include <stdlib.h> 00010 00011 #define SER_BUFFER_SIZE 32 00012 00013 /* The 4 onboard LEDs */ 00014 DigitalOut LED_0 (PB_6); 00015 DigitalOut LED_1 (PA_7); 00016 DigitalOut LED_2 (PA_6); 00017 DigitalOut LED_3 (PA_5); 00018 00019 /* The 2 user buttons */ 00020 InterruptIn SW1(PA_8); 00021 InterruptIn SW2(PB_10); 00022 00023 00024 00025 /* Function prototypes */ 00026 void sw1interrupt(); 00027 void sw2interrupt(); 00028 void sertmout(); 00029 bool modem_command_check_ok(char * command); 00030 void modem_setup(); 00031 00032 bool ser_timeout = false; 00033 00034 00035 /* Serial port over USB */ 00036 Serial pc(USBTX, USBRX); 00037 00038 /* Serial connection to sigfox modem */ 00039 Serial modem(PA_9, PA_10); 00040 00041 bool dice1 = false; 00042 bool dice2 = false; 00043 uint8_t dice1val; 00044 uint8_t dice2val; 00045 uint8_t number; 00046 00047 00048 int main() { 00049 00050 time_t t; 00051 00052 /* Setup TD120x */ 00053 wait(3); 00054 modem_setup(); 00055 00056 /* Turn off all LED */ 00057 LED_0 = 1; 00058 LED_1 = 1; 00059 LED_2 = 1; 00060 LED_3 = 1; 00061 00062 /* Setup button interrupts */ 00063 SW1.fall(&sw1interrupt); 00064 SW2.fall(&sw2interrupt); 00065 00066 /* Intializes random number generator */ 00067 srand((unsigned) time(&t)); 00068 00069 while(1) { 00070 00071 if(dice1 == true && dice2 == true){ 00072 number = dice1val + dice2val; 00073 pc.printf("\nThe number is: %d\n\r", number); 00074 00075 switch(number) { 00076 case 2 : 00077 LED_0 = 1; 00078 LED_1 = 1; 00079 LED_2 = 0; 00080 LED_3 = 1; 00081 break; 00082 case 3 : 00083 LED_0 = 1; 00084 LED_1 = 1; 00085 LED_2 = 0; 00086 LED_3 = 0; 00087 break; 00088 case 4 : 00089 LED_0 = 1; 00090 LED_1 = 0; 00091 LED_2 = 1; 00092 LED_3 = 1; 00093 break; 00094 case 5 : 00095 LED_0 = 1; 00096 LED_1 = 0; 00097 LED_2 = 1; 00098 LED_3 = 0; 00099 break; 00100 case 6 : 00101 LED_0 = 1; 00102 LED_1 = 0; 00103 LED_2 = 0; 00104 LED_3 = 1; 00105 break; 00106 case 7 : 00107 LED_0 = 1; 00108 LED_1 = 0; 00109 LED_2 = 0; 00110 LED_3 = 0; 00111 break; 00112 case 8 : 00113 LED_0 = 0; 00114 LED_1 = 1; 00115 LED_2 = 1; 00116 LED_3 = 1; 00117 break; 00118 case 9 : 00119 LED_0 = 0; 00120 LED_1 = 1; 00121 LED_2 = 1; 00122 LED_3 = 0; 00123 break; 00124 case 10 : 00125 LED_0 = 0; 00126 LED_1 = 1; 00127 LED_2 = 0; 00128 LED_3 = 1; 00129 break; 00130 case 11 : 00131 LED_0 = 0; 00132 LED_1 = 1; 00133 LED_2 = 0; 00134 LED_3 = 0; 00135 break; 00136 case 12 : 00137 LED_0 = 0; 00138 LED_1 = 0; 00139 LED_2 = 1; 00140 LED_3 = 1; 00141 break; 00142 default : 00143 pc.printf("\nImpossible number!!\n\r"); 00144 LED_0 = 1; 00145 LED_1 = 1; 00146 LED_2 = 1; 00147 LED_3 = 1; 00148 } 00149 char command[SER_BUFFER_SIZE]; 00150 sprintf(command, "AT$SF=04%04x,2,0\n", (int) number ); 00151 pc.printf("Sending thrown number = %i over Sigfox using modem command: %s\n", number , command); 00152 modem_command_check_ok(command); 00153 //wait_ms(5000); 00154 LED_0 = 1; 00155 LED_1 = 1; 00156 LED_2 = 1; 00157 LED_3 = 1; 00158 dice1 = false; 00159 dice2 = false; 00160 } 00161 00162 } 00163 } 00164 void modem_setup() 00165 { 00166 /* Reset to factory defaults */ 00167 if(modem_command_check_ok("AT&F")) 00168 { 00169 pc.printf("Factory reset succesfull\r\n"); 00170 } 00171 else 00172 { 00173 pc.printf("Factory reset TD120x failed\r\n"); 00174 } 00175 /* Disable local echo */ 00176 modem.printf("ATE0\n"); 00177 if(modem_command_check_ok("ATE0")) 00178 { 00179 pc.printf("Local echo disabled\r\n"); 00180 } 00181 /* Write to mem */ 00182 if(modem_command_check_ok("AT&W")) 00183 { 00184 pc.printf("Settings saved!\r\n"); 00185 } 00186 } 00187 00188 bool modem_command_check_ok(char * command) 00189 { 00190 /* first clear serial data buffers */ 00191 while(modem.readable()) modem.getc(); 00192 /* Timeout for response of the modem */ 00193 Timeout tmout; 00194 ser_timeout = false; 00195 /* Buffer for incoming data */ 00196 char responsebuffer[6]; 00197 /* Flag to set when we get 'OK' response */ 00198 bool ok = false; 00199 bool error = false; 00200 /* Print command to TD120x */ 00201 modem.printf(command); 00202 /* Newline to activate command */ 00203 modem.printf("\n"); 00204 /* Wait untill serial feedback, min 7 seconds before timeout */ 00205 tmout.attach(&sertmout, 7.0); 00206 while(!modem.readable()&& ser_timeout == false); 00207 while(!ok && !ser_timeout && !error) 00208 { 00209 if(modem.readable()) 00210 { 00211 for(int i = 0; i < 5; i++) 00212 { 00213 responsebuffer[i] = responsebuffer[i+1]; 00214 } 00215 responsebuffer[5] = modem.getc(); 00216 if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'O' && responsebuffer[3] == 'K' && responsebuffer[4] == '\r' && responsebuffer[5] == '\n' ) 00217 { 00218 ok = true; 00219 } 00220 else if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'E' && responsebuffer[3] == 'R' && responsebuffer[4] == 'R' && responsebuffer[5] == 'O' ) 00221 { 00222 error = true; 00223 } 00224 } 00225 } 00226 tmout.detach(); 00227 return ok; 00228 } 00229 /* Button 1 ISR */ 00230 void sw1interrupt() 00231 { 00232 if(dice1 == false){ 00233 pc.printf("\nDice 1 has been thrown\n\r"); 00234 dice1 = true; 00235 dice1val = (rand() % 6)+1; 00236 } 00237 else{ 00238 pc.printf("\nDice 1 has already been thrown\n\r"); 00239 } 00240 00241 } 00242 00243 /* Button 2 ISR */ 00244 void sw2interrupt() 00245 { 00246 if(dice2 == false){ 00247 pc.printf("\nDice 2 has been thrown\n\r"); 00248 dice2 = true; 00249 dice2val = (rand() % 6)+1; 00250 } 00251 else{ 00252 pc.printf("\nDice 2 has already been thrown\n\r"); 00253 } 00254 } 00255 00256 /* ISR for serial timeout */ 00257 void sertmout() 00258 { 00259 ser_timeout = true; 00260 } 00261 00262 00263
Generated on Tue Jul 26 2022 10:05:53 by
1.7.2
