Ring on a String

Dependencies:   4DGL-uLCD-SE X_NUCLEO_53L0A1 mbed-dsp mbed

Fork of HUZZAHESP8266-web-control-LPC1768 by Austin Dong

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // ESP8266 Static page WEB server to control Mbed
00002 
00003 /* ----------------------------------------------------------------------
00004 * Copyright (C) 2010-2012 ARM Limited. All rights reserved.
00005 *
00006 * $Date:         17. January 2013
00007 * $Revision:     V1.4.0
00008 *
00009 * Project:       CMSIS DSP Library
00010 * Title:         arm_fft_bin_example_f32.c
00011 *
00012 * Description:   Example code demonstrating calculation of Max energy bin of
00013 *                frequency domain of input signal.
00014 *
00015 * Target Processor: Cortex-M4/Cortex-M3
00016 *
00017 * Redistribution and use in source and binary forms, with or without
00018 * modification, are permitted provided that the following conditions
00019 * are met:
00020 *   - Redistributions of source code must retain the above copyright
00021 *     notice, this list of conditions and the following disclaimer.
00022 *   - Redistributions in binary form must reproduce the above copyright
00023 *     notice, this list of conditions and the following disclaimer in
00024 *     the documentation and/or other materials provided with the
00025 *     distribution.
00026 *   - Neither the name of ARM LIMITED nor the names of its contributors
00027 *     may be used to endorse or promote products derived from this
00028 *     software without specific prior written permission.
00029 *
00030 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00031 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00032 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00033 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00034 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00035 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00036 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00037 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00038 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00039 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00040 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00041 * POSSIBILITY OF SUCH DAMAGE.
00042  * -------------------------------------------------------------------- */
00043 
00044 #include "mbed.h"
00045 #include "XNucleo53L0A1.h"
00046 #include "uLCD_4DGL.h"
00047 #include <stdio.h>
00048 
00049 #include "mbed-dsp/cmsis_dsp/arm_math.h"
00050 #include "mbed-dsp/cmsis_dsp/arm_const_structs.h"
00051 
00052 #define TEST_LENGTH_SAMPLES 1024
00053 //Ticker to control microphone interrupt
00054 Ticker ticker;
00055 //FFT output
00056 float32_t testOutput[TEST_LENGTH_SAMPLES/2];
00057 //Microphone
00058 AnalogIn   a_in(p16);
00059 //Serial Out to PC for debug
00060 Serial pc(USBTX, USBRX);
00061 //Signal for fft
00062 float32_t testInput_f32_10khz[1024];
00063 //uint32_t counter = 0;
00064 arm_status status = ARM_MATH_SUCCESS;
00065 //Max valued FFT bin
00066 float32_t maxValue;
00067 /* ------------------------------------------------------------------
00068 * Global variables for FFT Bin Example
00069 * ------------------------------------------------------------------- */
00070 uint32_t refIndex = 213, testIndex = 0;
00071 uint32_t fftSize = 512;
00072 uint32_t ifftFlag = 0;
00073 uint32_t doBitReverse = 1;
00074 
00075 
00076 DigitalIn pb(p7);
00077 
00078 Serial esp(p13, p14); // tx, rx
00079 
00080 // Lidar
00081 DigitalOut shdn(p26);
00082 //I2C sensor pins
00083 #define VL53L0_I2C_SDA   p28
00084 #define VL53L0_I2C_SCL   p27
00085 static XNucleo53L0A1 *board=NULL;
00086 
00087 // Standard Mbed LED definitions
00088 DigitalOut  led1(LED1);
00089 DigitalOut  led2(LED2);
00090 DigitalOut  led3(LED3);
00091 DigitalOut  led4(LED4);
00092 
00093 Timer gameTimer;
00094 Timer restartGame;
00095 
00096 char ssid[32] = "Test123";     // enter WiFi router ssid inside the quotes
00097 char pwd [32] = "joe12345"; // enter WiFi router password inside the quotes
00098 
00099 //Declare Game variables Here ~~~~~~~~~~~~~~~~~~~~`
00100 //Used to keep track of top 5 players. if scoreCurrent is > score1[1} then score[1]
00101 //is shifted to the next and so forth 
00102 float score1 = 0, score2 = 0, score3 = 0, score4 = 0, score5 = 0;
00103 float scoreBuffer = 0;
00104 float scoreCurrent = 0;
00105 
00106 char score1Char[10];
00107 char score2Char[10];
00108 char score3Char[10];
00109 char score4Char[10];
00110 char score5Char[10];
00111 
00112 
00113 // things for sending/receiving data over serial
00114 volatile int tx_in=0;
00115 volatile int tx_out=0;
00116 volatile int rx_in=0;
00117 volatile int rx_out=0;
00118 const int buffer_size = 4095;
00119 char tx_buffer[buffer_size+1];
00120 char rx_buffer[buffer_size+1];
00121 void Tx_interrupt();
00122 void Rx_interrupt();
00123 void send_line();
00124 void read_line();
00125 
00126 int DataRX;
00127 int update;
00128 int count;
00129 char cmdbuff[1024];
00130 char replybuff[4096];
00131 char webdata[4096]; // This may need to be bigger depending on WEB browser used
00132 char webbuff[4096];     // Currently using 1986 characters, Increase this if more web page data added
00133 char timebuf[30];
00134 void SendCMD(),getreply(),ReadWebData(),startserver();
00135 void gettime(),setRTC(), getScore1(), getScore2(), getScore3(), getScore4(), getScore5();
00136 char rx_line[1024];
00137 int port        =80;  // set server port
00138 int SERVtimeout =5;    // set server timeout in seconds in case link breaks.
00139 struct tm t;
00140 // manual set RTC values
00141 int minute      =00;    // 0-59
00142 int hour        =12;    // 2-23
00143 int dayofmonth  =26;    // 1-31
00144 int month       =8;     // 1-12
00145 int year        =15;    // last 2 digits
00146 
00147 bool gameIsRunning = false;
00148 
00149 DigitalOut led_1(LED1);
00150 DigitalOut led_2(LED2);
00151 
00152 void init_uLCD() {
00153 //    uLCD.baudrate(3000000);
00154 }
00155 
00156 void get_fft() {
00157     /* Process the data through the CFFT/CIFFT module */
00158     arm_cfft_f32(&arm_cfft_sR_f32_len512, testInput_f32_10khz, ifftFlag, doBitReverse);
00159     /* Process the data through the Complex Magnitude Module for
00160     calculating the magnitude at each bin */
00161     arm_cmplx_mag_f32(testInput_f32_10khz, testOutput, fftSize);
00162     /* Calculates maxValue and returns corresponding BIN value */
00163     testOutput[0] = 0;
00164     arm_max_f32(testOutput, fftSize, &maxValue, &testIndex);
00165     //pc.printf("\n\rMax Value: %f\n\rAt bin: %i\n\r",maxValue,testIndex);
00166     if(gameIsRunning) {
00167         if(testIndex >= 180 && testIndex <= 200) {
00168             scoreCurrent += 1;
00169             led2 = 1;
00170             pc.printf("Current score is %f !!! \r\n", scoreCurrent);
00171             pc.printf("The current game time is %f seconds!\r\n", gameTimer.read());
00172         }
00173     }
00174     //Arrays have to be cleared for multiple FFT calculations
00175     for (int i = 0; i < 1024; i++) {
00176         testInput_f32_10khz[i] = 0;
00177     }
00178     for (int i = 0; i < 512; i++) {
00179         testOutput[i] = 0;
00180     }    
00181 }
00182 
00183 void get_signal()
00184 {
00185     //Begin sampling when microphone picks up loud sound
00186     led2 = 0;
00187     if (a_in > 0.6 || a_in < 0.4){
00188         for(int i = 0; i < 1024; i += 2) {
00189             testInput_f32_10khz[i] = a_in.read();
00190         } 
00191         get_fft();
00192         wait(1.0/100.0); 
00193     }     
00194 }
00195 
00196 
00197 int main()
00198 {
00199     
00200     int status;
00201     uint32_t distance;
00202     DevI2C *device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
00203     /* creates the 53L0A1 expansion board singleton obj */
00204     board = XNucleo53L0A1::instance(device_i2c, A2, D8, D2);
00205     shdn = 0; //must reset sensor for an mbed reset to work
00206     wait(0.1);
00207     shdn = 1;
00208     wait(0.1);
00209     /* init the 53L0A1 board with default values */
00210     status = board->init_board();
00211     while (status) {
00212             pc.printf("Failed to init board! \r\n");
00213             status = board->init_board();
00214             } // Failed to init Lidar Sensor
00215     
00216     // Setup a serial interrupt function to receive data
00217     esp.attach(&Rx_interrupt, Serial::RxIrq);
00218     // Setup a serial interrupt function to transmit data
00219     esp.attach(&Tx_interrupt, Serial::TxIrq);
00220     if (time(NULL) < 1420070400) {
00221         setRTC();
00222     }
00223     
00224     // Initialize microphone code
00225     for (int i = 0; i < 1024; i++) {
00226         testInput_f32_10khz[i] = 0;
00227     }
00228     ticker.attach(&get_signal, 2.0/100.0); 
00229     
00230     //Check that the LCD Works Statement
00231     // uLCD.locate(1,1);
00232     //uLCD.printf("Hello from LCD screen");
00233     
00234     //Start the server
00235     startserver();
00236     DataRX=0;
00237     count=0;
00238     
00239     while(1){
00240     
00241     pc.baud(9600);
00242     esp.baud(9600);
00243     
00244     // Handle the pushbutton
00245     pb.mode(PullUp);
00246     int old_pb = 0;
00247     int new_pb;
00248     
00249     pc.printf("Welcome to the Loop on a Rope Game!\r\n");
00250     wait(1);
00251     pc.printf("Please press the pushbutton to start the game!\r\n");
00252     
00253     gameTimer.reset();
00254     gameTimer.stop();
00255     
00256     scoreCurrent = 0;
00257     while(gameTimer.read() < 70){
00258         new_pb = pb;
00259         if ((new_pb==0) && (old_pb==1)){
00260             
00261             //while(1){
00262                 gameIsRunning = true;
00263                 pc.printf("\nGame Begins NOW! You have 120 seconds\r\n");
00264                                 
00265                 //loop taking and printing distance
00266                 //gameTimer.start(); // old game timer start
00267                 //float extraTime = gameTimer.read();
00268                 //pc.printf("Did we make it here? \r\n");
00269                 gameTimer.start();
00270                 while (gameTimer.read() < 60) {
00271                     //pc.printf("Did we make it here? x 2 \r\n");
00272                     status = board->sensor_centre->get_distance(&distance);
00273                     if (status == VL53L0X_ERROR_NONE) {
00274                         if (distance < 200){
00275                             led_1 = 1;
00276                             pc.printf("D=%ld mm\r\n", distance);
00277                             wait(1.5);
00278                             status = board->sensor_centre->get_distance(&distance);
00279                             if (status == VL53L0X_ERROR_NONE) {
00280                                 if (distance < 200) {
00281                                     pc.printf("Congrats, You hooked it! Please Remove The hook ASAP to ensure a fair score!\r\n");
00282                                     scoreCurrent = scoreCurrent + 10;
00283                                     pc.printf("Current score is %f !!! \r\n", scoreCurrent);
00284                                     pc.printf("The current game time is %f seconds! You only have 120 seconds!\r\n", gameTimer.read());
00285                                     //wait(4);
00286                                     gameTimer.stop();
00287                                     gameIsRunning = false;
00288                                     while(distance < 200){
00289                                         status = board->sensor_centre->get_distance(&distance);
00290                                     }
00291                                     gameTimer.start();
00292                                     gameIsRunning = true;
00293                                 } //If distance is still greater than 100
00294                                 led_1 = 0;
00295                             }
00296                         } // If distance > 100 end 
00297                         //else {
00298                           //  pc.printf("Please retry to hook it ASAP!\r\n");
00299                             //pc.printf("The current game time is %f seconds! Remember, you only have 120 seconds!\r\n", gameTimer.read());
00300                             //wait(4);
00301                         //} //else statement for 
00302                     } //if statement for using the LIDAR with no error
00303                 } //While gameTimer < 120 end
00304             //} //Second while(1) loop end
00305         } //if statement end with pushbutton
00306         old_pb = new_pb;
00307         //gameTimer.reset();
00308     }  //final while(1) loop    
00309     
00310     old_pb = 0;
00311     gameTimer.stop();
00312     gameIsRunning = false;
00313     
00314     pc.printf("Congrats, your final score was %f ! \r\n", scoreCurrent);
00315     
00316     wait(1);
00317     
00318     if (scoreCurrent > score1){
00319         scoreBuffer = score1;
00320         score1 = scoreCurrent;
00321         score5 = score4;
00322         score4 = score3;
00323         score3 = score2;
00324         score2 = scoreBuffer;
00325         pc.printf("Congrats on placing 1/5 on the High Scores! \r\n");
00326         }   else if (scoreCurrent > score2){
00327         scoreBuffer = score2;
00328         score5 = score4;
00329         score4 = score3;
00330         score3 = scoreBuffer;
00331         score2 = scoreCurrent;
00332         pc.printf("Congrats on placing 2/5 on the High Scores! \r\n");
00333         }   else if (scoreCurrent > score3){
00334         scoreBuffer = score3;
00335         score5 = score4;
00336         score4 = scoreBuffer;;
00337         score3 = scoreCurrent;
00338         pc.printf("Congrats on placing 3/5 on the High Scores! \r\n");
00339         }   else if (scoreCurrent > score4){
00340         scoreBuffer = score4;
00341         score5 = scoreBuffer;
00342         score4 = scoreCurrent;
00343         pc.printf("Congrats on placing 4/5 on the High Scores! \r\n");
00344         }   else if (scoreCurrent > score5){
00345             scoreBuffer = score5;
00346             score5 = scoreCurrent;
00347             pc.printf("Congrats on placing 5/5 on the High Scores! \r\n");
00348         }   else {
00349             pc.printf("Sorry, you didn't make it on the HighScores! Please check 172.20.10.3 to view the table! \r\n");
00350             }
00351     
00352     wait(5);
00353     
00354 //    while(1) {
00355         if(DataRX==1) {
00356             ReadWebData();
00357             esp.attach(&Rx_interrupt, Serial::RxIrq);
00358         }
00359         if(update==1) // update time, hit count, and analog levels in the HUZZAH chip
00360         {
00361             // get new values
00362             gettime();
00363             getScore1();
00364             getScore2();
00365             getScore3();
00366             getScore4();
00367             getScore5();
00368             count++;
00369             // send new values
00370             sprintf(cmdbuff, "score1read,score2read,score3read,score4read,score5read = \"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\r\n", score1Char,score2Char,score3Char,score4Char,score5Char);
00371             SendCMD();
00372             getreply();
00373             update=0;   
00374         }
00375 //    }
00376     }
00377 }
00378 // END OF MAIN HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00379 
00380 // Reads and processes GET and POST web data
00381 void ReadWebData()
00382 {
00383     wait_ms(200);
00384     esp.attach(NULL,Serial::RxIrq);
00385     DataRX=0;
00386     memset(webdata, '\0', sizeof(webdata));
00387     strcpy(webdata, rx_buffer);
00388     memset(rx_buffer, '\0', sizeof(rx_buffer));
00389     rx_in = 0;
00390     rx_out = 0;
00391     // check web data for form information
00392     if( strstr(webdata, "check=led1v") != NULL ) {
00393         led1=!led1;
00394     }
00395     if( strstr(webdata, "check=led2v") != NULL ) {
00396         led2=!led2;
00397     }
00398     if( strstr(webdata, "check=led3v") != NULL ) {
00399         led3=!led3;
00400     }
00401     if( strstr(webdata, "check=led4v") != NULL ) {
00402         led4=!led4;
00403     }
00404     if( strstr(webdata, "POST") != NULL ) { // set update flag if POST request
00405         update=1;
00406     }
00407     if( strstr(webdata, "GET") != NULL && strstr(webdata, "favicon") == NULL ) { // set update flag for GET request but do not want to update for favicon requests
00408         update=1;
00409     }
00410 }
00411 // Starts webserver
00412 void startserver()
00413 {
00414     gettime();
00415     getScore1();
00416     getScore2();
00417     getScore3();
00418     getScore4();
00419     getScore5();
00420     pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
00421     strcpy(cmdbuff,"node.restart()\r\n");
00422     SendCMD();
00423     wait(2);
00424     getreply();
00425     
00426     pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> ");
00427 
00428     // initial values
00429     sprintf(cmdbuff, "score1read,score2read,score3read,score4read,score5read = \"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\r\n", score1Char,score2Char,score3Char,score4Char,score5Char);
00430     SendCMD();
00431     getreply();
00432     wait(0.5);
00433 
00434     //create server
00435     sprintf(cmdbuff, "srv=net.createServer(net.TCP,%d)\r\n",SERVtimeout);
00436     SendCMD();
00437     getreply();
00438     wait(0.5);
00439     strcpy(cmdbuff,"srv:listen(80,function(conn)\r\n");
00440     SendCMD();
00441     getreply();
00442     wait(0.3);
00443         strcpy(cmdbuff,"conn:on(\"receive\",function(conn,payload) \r\n");
00444         SendCMD();
00445         getreply();
00446         wait(0.3);
00447         
00448         //print data to mbed
00449         strcpy(cmdbuff,"print(payload)\r\n");
00450         SendCMD();
00451         getreply();
00452         wait(0.2);
00453        
00454         //web page data CODE FOR TABLE FOUND HERE
00455         strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><head><style>body { background-color:#D6DBDF; color: blue; font: 12px arial, verdana; }')\r\n");
00456         SendCMD();
00457         getreply();
00458         wait(0.4);
00459         strcpy(cmdbuff,"conn:send('table {border-collapse: collapse;}')\r\n");
00460         SendCMD();
00461         getreply();
00462         wait(0.4);
00463         strcpy(cmdbuff,"conn:send('th {text-align: center; border: 4px solid; background-color: #e2dee5; font-size: 18px;}')\r\n");
00464         SendCMD();
00465         getreply();
00466         wait(0.4);
00467         strcpy(cmdbuff,"conn:send('td {text-align: center; border: 2px dashed; padding: 5px; height: 15px; font-size: 14px}')\r\n");
00468         SendCMD();
00469         getreply();
00470         wait(0.4);
00471         strcpy(cmdbuff,"conn:send('tr:nth-child(even) {background-color: #D5F5E3;}tr:nth-child(odd) {background-color: ##ABEBC6;} tr:hover {background-color: #b0bff4;}</style></head>')\r\n");
00472         SendCMD();
00473         getreply();
00474         wait(0.4);
00475         strcpy(cmdbuff,"conn:send('<body><h2 align=\"center\"><font size=\"5\">Loop on a Rope Highscores</font></h2>')\r\n");
00476         SendCMD();
00477         getreply();
00478         wait(0.4);
00479         strcpy(cmdbuff,"conn:send('<br><table border=\"1\" style=\"width:100%\"><tr><th>Rank</th><th>High Score</th></tr><br><hr>')\r\n");
00480         SendCMD();
00481         getreply();
00482         wait(0.4);
00483         strcpy(cmdbuff,"conn:send('<tr><td>1</td><td> '..score1read..' </td></tr>')\r\n");
00484         SendCMD();
00485         getreply();
00486         wait(0.4);
00487         strcpy(cmdbuff,"conn:send('<tr><td>2</td><td> '..score2read..' </td></tr>')\r\n");
00488         SendCMD();
00489         getreply();
00490         wait(0.4);
00491         strcpy(cmdbuff,"conn:send('<tr><td>3</td><td> '..score3read..' </td></tr>')\r\n");
00492         SendCMD();
00493         getreply();
00494         wait(0.4);
00495         strcpy(cmdbuff,"conn:send('<tr><td>4</td><td> '..score4read..' </td></tr>')\r\n");
00496         SendCMD();
00497         getreply();
00498         wait(0.4);
00499         strcpy(cmdbuff,"conn:send('<tr><td>5</td><td> '..score5read..' </td></tr></table>')\r\n");
00500         SendCMD();
00501         getreply();
00502         wait(0.4);
00503         strcpy(cmdbuff,"conn:send('<form method=\"POST\"')\r\n");
00504         SendCMD();
00505         getreply();
00506         wait(0.3);
00507         strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"send-refresh\"></form>')\r\n");
00508         SendCMD();
00509         getreply();
00510         wait(0.3);
00511         
00512         // end web page data
00513         
00514         strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection
00515         SendCMD();
00516         getreply();
00517         wait(0.3);
00518         strcpy(cmdbuff, "end)\r\n");
00519         SendCMD();
00520         getreply();
00521         wait(0.2);
00522     strcpy(cmdbuff, "end)\r\n");
00523     SendCMD();
00524     getreply();
00525     wait(0.2);
00526 
00527     strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\r\n");
00528     SendCMD();
00529     getreply();
00530     wait(0.2);
00531     strcpy(cmdbuff, "if wifi.sta.getip() == nil then\r\n");
00532     SendCMD();
00533     getreply();
00534     wait(0.2);
00535     strcpy(cmdbuff, "print(\"Connecting to AP...\\n\")\r\n");
00536     SendCMD();
00537     getreply();
00538     wait(0.2);
00539     strcpy(cmdbuff, "else\r\n");
00540     SendCMD();
00541     getreply();
00542     wait(0.2);
00543     strcpy(cmdbuff, "ip, nm, gw=wifi.sta.getip()\r\n");
00544     SendCMD();
00545     getreply();
00546     wait(0.2);
00547     strcpy(cmdbuff,"print(\"IP Address: \",ip)\r\n");
00548     SendCMD();
00549     getreply();
00550     wait(0.2);
00551     strcpy(cmdbuff,"tmr.stop(0)\r\n");
00552     SendCMD();
00553     getreply();
00554     wait(0.2);
00555     strcpy(cmdbuff,"end\r\n");
00556     SendCMD();
00557     getreply();
00558     wait(0.2);
00559     strcpy(cmdbuff,"end)\r\n");
00560     SendCMD();
00561     getreply();
00562     wait(0.2);
00563     
00564     pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
00565 }
00566 
00567 
00568 // ESP Command data send
00569 void SendCMD()
00570 {
00571     int i;
00572     char temp_char;
00573     bool empty;
00574     i = 0;
00575 // Start Critical Section - don't interrupt while changing global buffer variables
00576     NVIC_DisableIRQ(UART1_IRQn);
00577     empty = (tx_in == tx_out);
00578     while ((i==0) || (cmdbuff[i-1] != '\n')) {
00579 // Wait if buffer full
00580         if (((tx_in + 1) % buffer_size) == tx_out) {
00581 // End Critical Section - need to let interrupt routine empty buffer by sending
00582             NVIC_EnableIRQ(UART1_IRQn);
00583             while (((tx_in + 1) % buffer_size) == tx_out) {
00584             }
00585 // Start Critical Section - don't interrupt while changing global buffer variables
00586             NVIC_DisableIRQ(UART1_IRQn);
00587         }
00588         tx_buffer[tx_in] = cmdbuff[i];
00589         i++;
00590         tx_in = (tx_in + 1) % buffer_size;
00591     }
00592     if (esp.writeable() && (empty)) {
00593         temp_char = tx_buffer[tx_out];
00594         tx_out = (tx_out + 1) % buffer_size;
00595 // Send first character to start tx interrupts, if stopped
00596         esp.putc(temp_char);
00597     }
00598 // End Critical Section
00599     NVIC_EnableIRQ(UART1_IRQn);
00600     return;
00601 }
00602 
00603 // Get Command and ESP status replies
00604 void getreply()
00605 {
00606     read_line();
00607     sscanf(rx_line,replybuff);
00608 }
00609  
00610 // Read a line from the large rx buffer from rx interrupt routine
00611 void read_line() {
00612     int i;
00613     i = 0;
00614 // Start Critical Section - don't interrupt while changing global buffer variables
00615     NVIC_DisableIRQ(UART1_IRQn);
00616 // Loop reading rx buffer characters until end of line character
00617     while ((i==0) || (rx_line[i-1] != '\r')) {
00618 // Wait if buffer empty
00619         if (rx_in == rx_out) {
00620 // End Critical Section - need to allow rx interrupt to get new characters for buffer
00621             NVIC_EnableIRQ(UART1_IRQn);
00622             while (rx_in == rx_out) {
00623             }
00624 // Start Critical Section - don't interrupt while changing global buffer variables
00625             NVIC_DisableIRQ(UART1_IRQn);
00626         }
00627         rx_line[i] = rx_buffer[rx_out];
00628         i++;
00629         rx_out = (rx_out + 1) % buffer_size;
00630     }
00631 // End Critical Section
00632     NVIC_EnableIRQ(UART1_IRQn);
00633     rx_line[i-1] = 0;
00634     return;
00635 }
00636  
00637  
00638 // Interupt Routine to read in data from serial port
00639 void Rx_interrupt() {
00640     DataRX=1;
00641     //led3=1;
00642 // Loop just in case more than one character is in UART's receive FIFO buffer
00643 // Stop if buffer full
00644     while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
00645         rx_buffer[rx_in] = esp.getc();
00646 // Uncomment to Echo to USB serial to watch data flow
00647         pc.putc(rx_buffer[rx_in]);
00648         rx_in = (rx_in + 1) % buffer_size;
00649     }
00650     //led3=0;
00651     return;
00652 }
00653  
00654  
00655 // Interupt Routine to write out data to serial port
00656 void Tx_interrupt() {
00657     //led2=1;
00658 // Loop to fill more than one character in UART's transmit FIFO buffer
00659 // Stop if buffer empty
00660     while ((esp.writeable()) && (tx_in != tx_out)) {
00661         esp.putc(tx_buffer[tx_out]);
00662         tx_out = (tx_out + 1) % buffer_size;
00663     }
00664     //led2=0;
00665     return;
00666 }
00667 
00668 void gettime()
00669 {
00670     time_t seconds = time(NULL);
00671     strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
00672 }
00673 
00674 void setRTC()
00675 {
00676     t.tm_sec = (0);             // 0-59
00677     t.tm_min = (minute);        // 0-59
00678     t.tm_hour = (hour);         // 0-23
00679     t.tm_mday = (dayofmonth);   // 1-31
00680     t.tm_mon = (month-1);       // 0-11  "0" = Jan, -1 added for Mbed RCT clock format
00681     t.tm_year = ((year)+100);   // year since 1900,  current DCF year + 100 + 1900 = correct year
00682     set_time(mktime(&t));       // set RTC clock
00683 }
00684 
00685 void getScore1()
00686 {
00687     sprintf(score1Char,"%2.3f",score1);   
00688 }
00689 void getScore2()
00690 {
00691     sprintf(score2Char,"%2.3f",score2);   
00692 }
00693 void getScore3()
00694 {
00695     sprintf(score3Char,"%2.3f",score3);   
00696 }
00697 void getScore4()
00698 {
00699     sprintf(score4Char,"%2.3f",score4);   
00700 }
00701 void getScore5()
00702 {
00703     sprintf(score5Char,"%2.3f",score5);   
00704 }