Ring on a String

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

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

Committer:
jkelley38
Date:
Thu Dec 14 19:24:46 2017 +0000
Revision:
6:a7796fddaa16
Parent:
5:bc0296a5ad8a
Ring on a string program for ECE4180.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:e2a155f50119 1 // ESP8266 Static page WEB server to control Mbed
star297 0:e2a155f50119 2
jkelley38 6:a7796fddaa16 3 /* ----------------------------------------------------------------------
jkelley38 6:a7796fddaa16 4 * Copyright (C) 2010-2012 ARM Limited. All rights reserved.
jkelley38 6:a7796fddaa16 5 *
jkelley38 6:a7796fddaa16 6 * $Date: 17. January 2013
jkelley38 6:a7796fddaa16 7 * $Revision: V1.4.0
jkelley38 6:a7796fddaa16 8 *
jkelley38 6:a7796fddaa16 9 * Project: CMSIS DSP Library
jkelley38 6:a7796fddaa16 10 * Title: arm_fft_bin_example_f32.c
jkelley38 6:a7796fddaa16 11 *
jkelley38 6:a7796fddaa16 12 * Description: Example code demonstrating calculation of Max energy bin of
jkelley38 6:a7796fddaa16 13 * frequency domain of input signal.
jkelley38 6:a7796fddaa16 14 *
jkelley38 6:a7796fddaa16 15 * Target Processor: Cortex-M4/Cortex-M3
jkelley38 6:a7796fddaa16 16 *
jkelley38 6:a7796fddaa16 17 * Redistribution and use in source and binary forms, with or without
jkelley38 6:a7796fddaa16 18 * modification, are permitted provided that the following conditions
jkelley38 6:a7796fddaa16 19 * are met:
jkelley38 6:a7796fddaa16 20 * - Redistributions of source code must retain the above copyright
jkelley38 6:a7796fddaa16 21 * notice, this list of conditions and the following disclaimer.
jkelley38 6:a7796fddaa16 22 * - Redistributions in binary form must reproduce the above copyright
jkelley38 6:a7796fddaa16 23 * notice, this list of conditions and the following disclaimer in
jkelley38 6:a7796fddaa16 24 * the documentation and/or other materials provided with the
jkelley38 6:a7796fddaa16 25 * distribution.
jkelley38 6:a7796fddaa16 26 * - Neither the name of ARM LIMITED nor the names of its contributors
jkelley38 6:a7796fddaa16 27 * may be used to endorse or promote products derived from this
jkelley38 6:a7796fddaa16 28 * software without specific prior written permission.
jkelley38 6:a7796fddaa16 29 *
jkelley38 6:a7796fddaa16 30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
jkelley38 6:a7796fddaa16 31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
jkelley38 6:a7796fddaa16 32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
jkelley38 6:a7796fddaa16 33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
jkelley38 6:a7796fddaa16 34 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
jkelley38 6:a7796fddaa16 35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
jkelley38 6:a7796fddaa16 36 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
jkelley38 6:a7796fddaa16 37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
jkelley38 6:a7796fddaa16 38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
jkelley38 6:a7796fddaa16 39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
jkelley38 6:a7796fddaa16 40 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
jkelley38 6:a7796fddaa16 41 * POSSIBILITY OF SUCH DAMAGE.
jkelley38 6:a7796fddaa16 42 * -------------------------------------------------------------------- */
jkelley38 6:a7796fddaa16 43
star297 0:e2a155f50119 44 #include "mbed.h"
jkelley38 6:a7796fddaa16 45 #include "XNucleo53L0A1.h"
jkelley38 6:a7796fddaa16 46 #include "uLCD_4DGL.h"
jkelley38 6:a7796fddaa16 47 #include <stdio.h>
star297 0:e2a155f50119 48
jkelley38 6:a7796fddaa16 49 #include "mbed-dsp/cmsis_dsp/arm_math.h"
jkelley38 6:a7796fddaa16 50 #include "mbed-dsp/cmsis_dsp/arm_const_structs.h"
jkelley38 6:a7796fddaa16 51
jkelley38 6:a7796fddaa16 52 #define TEST_LENGTH_SAMPLES 1024
jkelley38 6:a7796fddaa16 53 //Ticker to control microphone interrupt
jkelley38 6:a7796fddaa16 54 Ticker ticker;
jkelley38 6:a7796fddaa16 55 //FFT output
jkelley38 6:a7796fddaa16 56 float32_t testOutput[TEST_LENGTH_SAMPLES/2];
jkelley38 6:a7796fddaa16 57 //Microphone
jkelley38 6:a7796fddaa16 58 AnalogIn a_in(p16);
jkelley38 6:a7796fddaa16 59 //Serial Out to PC for debug
star297 0:e2a155f50119 60 Serial pc(USBTX, USBRX);
jkelley38 6:a7796fddaa16 61 //Signal for fft
jkelley38 6:a7796fddaa16 62 float32_t testInput_f32_10khz[1024];
jkelley38 6:a7796fddaa16 63 //uint32_t counter = 0;
jkelley38 6:a7796fddaa16 64 arm_status status = ARM_MATH_SUCCESS;
jkelley38 6:a7796fddaa16 65 //Max valued FFT bin
jkelley38 6:a7796fddaa16 66 float32_t maxValue;
jkelley38 6:a7796fddaa16 67 /* ------------------------------------------------------------------
jkelley38 6:a7796fddaa16 68 * Global variables for FFT Bin Example
jkelley38 6:a7796fddaa16 69 * ------------------------------------------------------------------- */
jkelley38 6:a7796fddaa16 70 uint32_t refIndex = 213, testIndex = 0;
jkelley38 6:a7796fddaa16 71 uint32_t fftSize = 512;
jkelley38 6:a7796fddaa16 72 uint32_t ifftFlag = 0;
jkelley38 6:a7796fddaa16 73 uint32_t doBitReverse = 1;
star297 0:e2a155f50119 74
star297 2:d4c6bc0f2dc4 75
jkelley38 6:a7796fddaa16 76 DigitalIn pb(p7);
jkelley38 6:a7796fddaa16 77
jkelley38 6:a7796fddaa16 78 Serial esp(p13, p14); // tx, rx
jkelley38 6:a7796fddaa16 79
jkelley38 6:a7796fddaa16 80 // Lidar
jkelley38 6:a7796fddaa16 81 DigitalOut shdn(p26);
jkelley38 6:a7796fddaa16 82 //I2C sensor pins
jkelley38 6:a7796fddaa16 83 #define VL53L0_I2C_SDA p28
jkelley38 6:a7796fddaa16 84 #define VL53L0_I2C_SCL p27
jkelley38 6:a7796fddaa16 85 static XNucleo53L0A1 *board=NULL;
jkelley38 6:a7796fddaa16 86
star297 0:e2a155f50119 87 // Standard Mbed LED definitions
ausdong 5:bc0296a5ad8a 88 DigitalOut led1(LED1);
ausdong 5:bc0296a5ad8a 89 DigitalOut led2(LED2);
ausdong 5:bc0296a5ad8a 90 DigitalOut led3(LED3);
ausdong 5:bc0296a5ad8a 91 DigitalOut led4(LED4);
star297 0:e2a155f50119 92
jkelley38 6:a7796fddaa16 93 Timer gameTimer;
jkelley38 6:a7796fddaa16 94 Timer restartGame;
jkelley38 6:a7796fddaa16 95
jkelley38 6:a7796fddaa16 96 char ssid[32] = "Test123"; // enter WiFi router ssid inside the quotes
jkelley38 6:a7796fddaa16 97 char pwd [32] = "joe12345"; // enter WiFi router password inside the quotes
star297 0:e2a155f50119 98
jkelley38 6:a7796fddaa16 99 //Declare Game variables Here ~~~~~~~~~~~~~~~~~~~~`
jkelley38 6:a7796fddaa16 100 //Used to keep track of top 5 players. if scoreCurrent is > score1[1} then score[1]
jkelley38 6:a7796fddaa16 101 //is shifted to the next and so forth
jkelley38 6:a7796fddaa16 102 float score1 = 0, score2 = 0, score3 = 0, score4 = 0, score5 = 0;
jkelley38 6:a7796fddaa16 103 float scoreBuffer = 0;
jkelley38 6:a7796fddaa16 104 float scoreCurrent = 0;
jkelley38 6:a7796fddaa16 105
jkelley38 6:a7796fddaa16 106 char score1Char[10];
jkelley38 6:a7796fddaa16 107 char score2Char[10];
jkelley38 6:a7796fddaa16 108 char score3Char[10];
jkelley38 6:a7796fddaa16 109 char score4Char[10];
jkelley38 6:a7796fddaa16 110 char score5Char[10];
jkelley38 6:a7796fddaa16 111
ausdong 5:bc0296a5ad8a 112
ausdong 5:bc0296a5ad8a 113 // things for sending/receiving data over serial
ausdong 5:bc0296a5ad8a 114 volatile int tx_in=0;
ausdong 5:bc0296a5ad8a 115 volatile int tx_out=0;
ausdong 5:bc0296a5ad8a 116 volatile int rx_in=0;
ausdong 5:bc0296a5ad8a 117 volatile int rx_out=0;
ausdong 5:bc0296a5ad8a 118 const int buffer_size = 4095;
ausdong 5:bc0296a5ad8a 119 char tx_buffer[buffer_size+1];
ausdong 5:bc0296a5ad8a 120 char rx_buffer[buffer_size+1];
ausdong 5:bc0296a5ad8a 121 void Tx_interrupt();
ausdong 5:bc0296a5ad8a 122 void Rx_interrupt();
ausdong 5:bc0296a5ad8a 123 void send_line();
ausdong 5:bc0296a5ad8a 124 void read_line();
ausdong 5:bc0296a5ad8a 125
ausdong 5:bc0296a5ad8a 126 int DataRX;
ausdong 5:bc0296a5ad8a 127 int update;
ausdong 5:bc0296a5ad8a 128 int count;
ausdong 5:bc0296a5ad8a 129 char cmdbuff[1024];
ausdong 5:bc0296a5ad8a 130 char replybuff[4096];
ausdong 5:bc0296a5ad8a 131 char webdata[4096]; // This may need to be bigger depending on WEB browser used
ausdong 5:bc0296a5ad8a 132 char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added
star297 0:e2a155f50119 133 char timebuf[30];
ausdong 5:bc0296a5ad8a 134 void SendCMD(),getreply(),ReadWebData(),startserver();
jkelley38 6:a7796fddaa16 135 void gettime(),setRTC(), getScore1(), getScore2(), getScore3(), getScore4(), getScore5();
ausdong 5:bc0296a5ad8a 136 char rx_line[1024];
ausdong 5:bc0296a5ad8a 137 int port =80; // set server port
ausdong 5:bc0296a5ad8a 138 int SERVtimeout =5; // set server timeout in seconds in case link breaks.
ausdong 5:bc0296a5ad8a 139 struct tm t;
star297 1:71ed1afbf344 140 // manual set RTC values
4180_1 4:40dd020463ea 141 int minute =00; // 0-59
4180_1 4:40dd020463ea 142 int hour =12; // 2-23
4180_1 4:40dd020463ea 143 int dayofmonth =26; // 1-31
4180_1 4:40dd020463ea 144 int month =8; // 1-12
star297 1:71ed1afbf344 145 int year =15; // last 2 digits
star297 2:d4c6bc0f2dc4 146
jkelley38 6:a7796fddaa16 147 bool gameIsRunning = false;
jkelley38 6:a7796fddaa16 148
jkelley38 6:a7796fddaa16 149 DigitalOut led_1(LED1);
jkelley38 6:a7796fddaa16 150 DigitalOut led_2(LED2);
jkelley38 6:a7796fddaa16 151
jkelley38 6:a7796fddaa16 152 void init_uLCD() {
jkelley38 6:a7796fddaa16 153 // uLCD.baudrate(3000000);
jkelley38 6:a7796fddaa16 154 }
jkelley38 6:a7796fddaa16 155
jkelley38 6:a7796fddaa16 156 void get_fft() {
jkelley38 6:a7796fddaa16 157 /* Process the data through the CFFT/CIFFT module */
jkelley38 6:a7796fddaa16 158 arm_cfft_f32(&arm_cfft_sR_f32_len512, testInput_f32_10khz, ifftFlag, doBitReverse);
jkelley38 6:a7796fddaa16 159 /* Process the data through the Complex Magnitude Module for
jkelley38 6:a7796fddaa16 160 calculating the magnitude at each bin */
jkelley38 6:a7796fddaa16 161 arm_cmplx_mag_f32(testInput_f32_10khz, testOutput, fftSize);
jkelley38 6:a7796fddaa16 162 /* Calculates maxValue and returns corresponding BIN value */
jkelley38 6:a7796fddaa16 163 testOutput[0] = 0;
jkelley38 6:a7796fddaa16 164 arm_max_f32(testOutput, fftSize, &maxValue, &testIndex);
jkelley38 6:a7796fddaa16 165 //pc.printf("\n\rMax Value: %f\n\rAt bin: %i\n\r",maxValue,testIndex);
jkelley38 6:a7796fddaa16 166 if(gameIsRunning) {
jkelley38 6:a7796fddaa16 167 if(testIndex >= 180 && testIndex <= 200) {
jkelley38 6:a7796fddaa16 168 scoreCurrent += 1;
jkelley38 6:a7796fddaa16 169 led2 = 1;
jkelley38 6:a7796fddaa16 170 pc.printf("Current score is %f !!! \r\n", scoreCurrent);
jkelley38 6:a7796fddaa16 171 pc.printf("The current game time is %f seconds!\r\n", gameTimer.read());
jkelley38 6:a7796fddaa16 172 }
jkelley38 6:a7796fddaa16 173 }
jkelley38 6:a7796fddaa16 174 //Arrays have to be cleared for multiple FFT calculations
jkelley38 6:a7796fddaa16 175 for (int i = 0; i < 1024; i++) {
jkelley38 6:a7796fddaa16 176 testInput_f32_10khz[i] = 0;
jkelley38 6:a7796fddaa16 177 }
jkelley38 6:a7796fddaa16 178 for (int i = 0; i < 512; i++) {
jkelley38 6:a7796fddaa16 179 testOutput[i] = 0;
jkelley38 6:a7796fddaa16 180 }
jkelley38 6:a7796fddaa16 181 }
jkelley38 6:a7796fddaa16 182
jkelley38 6:a7796fddaa16 183 void get_signal()
jkelley38 6:a7796fddaa16 184 {
jkelley38 6:a7796fddaa16 185 //Begin sampling when microphone picks up loud sound
jkelley38 6:a7796fddaa16 186 led2 = 0;
jkelley38 6:a7796fddaa16 187 if (a_in > 0.6 || a_in < 0.4){
jkelley38 6:a7796fddaa16 188 for(int i = 0; i < 1024; i += 2) {
jkelley38 6:a7796fddaa16 189 testInput_f32_10khz[i] = a_in.read();
jkelley38 6:a7796fddaa16 190 }
jkelley38 6:a7796fddaa16 191 get_fft();
jkelley38 6:a7796fddaa16 192 wait(1.0/100.0);
jkelley38 6:a7796fddaa16 193 }
jkelley38 6:a7796fddaa16 194 }
jkelley38 6:a7796fddaa16 195
jkelley38 6:a7796fddaa16 196
4180_1 4:40dd020463ea 197 int main()
4180_1 4:40dd020463ea 198 {
jkelley38 6:a7796fddaa16 199
jkelley38 6:a7796fddaa16 200 int status;
jkelley38 6:a7796fddaa16 201 uint32_t distance;
jkelley38 6:a7796fddaa16 202 DevI2C *device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
jkelley38 6:a7796fddaa16 203 /* creates the 53L0A1 expansion board singleton obj */
jkelley38 6:a7796fddaa16 204 board = XNucleo53L0A1::instance(device_i2c, A2, D8, D2);
jkelley38 6:a7796fddaa16 205 shdn = 0; //must reset sensor for an mbed reset to work
jkelley38 6:a7796fddaa16 206 wait(0.1);
jkelley38 6:a7796fddaa16 207 shdn = 1;
jkelley38 6:a7796fddaa16 208 wait(0.1);
jkelley38 6:a7796fddaa16 209 /* init the 53L0A1 board with default values */
jkelley38 6:a7796fddaa16 210 status = board->init_board();
jkelley38 6:a7796fddaa16 211 while (status) {
jkelley38 6:a7796fddaa16 212 pc.printf("Failed to init board! \r\n");
jkelley38 6:a7796fddaa16 213 status = board->init_board();
jkelley38 6:a7796fddaa16 214 } // Failed to init Lidar Sensor
jkelley38 6:a7796fddaa16 215
ausdong 5:bc0296a5ad8a 216 // Setup a serial interrupt function to receive data
ausdong 5:bc0296a5ad8a 217 esp.attach(&Rx_interrupt, Serial::RxIrq);
ausdong 5:bc0296a5ad8a 218 // Setup a serial interrupt function to transmit data
ausdong 5:bc0296a5ad8a 219 esp.attach(&Tx_interrupt, Serial::TxIrq);
4180_1 4:40dd020463ea 220 if (time(NULL) < 1420070400) {
4180_1 4:40dd020463ea 221 setRTC();
4180_1 4:40dd020463ea 222 }
jkelley38 6:a7796fddaa16 223
jkelley38 6:a7796fddaa16 224 // Initialize microphone code
jkelley38 6:a7796fddaa16 225 for (int i = 0; i < 1024; i++) {
jkelley38 6:a7796fddaa16 226 testInput_f32_10khz[i] = 0;
jkelley38 6:a7796fddaa16 227 }
jkelley38 6:a7796fddaa16 228 ticker.attach(&get_signal, 2.0/100.0);
jkelley38 6:a7796fddaa16 229
jkelley38 6:a7796fddaa16 230 //Check that the LCD Works Statement
jkelley38 6:a7796fddaa16 231 // uLCD.locate(1,1);
jkelley38 6:a7796fddaa16 232 //uLCD.printf("Hello from LCD screen");
jkelley38 6:a7796fddaa16 233
jkelley38 6:a7796fddaa16 234 //Start the server
star297 0:e2a155f50119 235 startserver();
ausdong 5:bc0296a5ad8a 236 DataRX=0;
ausdong 5:bc0296a5ad8a 237 count=0;
jkelley38 6:a7796fddaa16 238
jkelley38 6:a7796fddaa16 239 while(1){
jkelley38 6:a7796fddaa16 240
jkelley38 6:a7796fddaa16 241 pc.baud(9600);
jkelley38 6:a7796fddaa16 242 esp.baud(9600);
jkelley38 6:a7796fddaa16 243
jkelley38 6:a7796fddaa16 244 // Handle the pushbutton
jkelley38 6:a7796fddaa16 245 pb.mode(PullUp);
jkelley38 6:a7796fddaa16 246 int old_pb = 0;
jkelley38 6:a7796fddaa16 247 int new_pb;
jkelley38 6:a7796fddaa16 248
jkelley38 6:a7796fddaa16 249 pc.printf("Welcome to the Loop on a Rope Game!\r\n");
jkelley38 6:a7796fddaa16 250 wait(1);
jkelley38 6:a7796fddaa16 251 pc.printf("Please press the pushbutton to start the game!\r\n");
jkelley38 6:a7796fddaa16 252
jkelley38 6:a7796fddaa16 253 gameTimer.reset();
jkelley38 6:a7796fddaa16 254 gameTimer.stop();
jkelley38 6:a7796fddaa16 255
jkelley38 6:a7796fddaa16 256 scoreCurrent = 0;
jkelley38 6:a7796fddaa16 257 while(gameTimer.read() < 70){
jkelley38 6:a7796fddaa16 258 new_pb = pb;
jkelley38 6:a7796fddaa16 259 if ((new_pb==0) && (old_pb==1)){
jkelley38 6:a7796fddaa16 260
jkelley38 6:a7796fddaa16 261 //while(1){
jkelley38 6:a7796fddaa16 262 gameIsRunning = true;
jkelley38 6:a7796fddaa16 263 pc.printf("\nGame Begins NOW! You have 120 seconds\r\n");
jkelley38 6:a7796fddaa16 264
jkelley38 6:a7796fddaa16 265 //loop taking and printing distance
jkelley38 6:a7796fddaa16 266 //gameTimer.start(); // old game timer start
jkelley38 6:a7796fddaa16 267 //float extraTime = gameTimer.read();
jkelley38 6:a7796fddaa16 268 //pc.printf("Did we make it here? \r\n");
jkelley38 6:a7796fddaa16 269 gameTimer.start();
jkelley38 6:a7796fddaa16 270 while (gameTimer.read() < 60) {
jkelley38 6:a7796fddaa16 271 //pc.printf("Did we make it here? x 2 \r\n");
jkelley38 6:a7796fddaa16 272 status = board->sensor_centre->get_distance(&distance);
jkelley38 6:a7796fddaa16 273 if (status == VL53L0X_ERROR_NONE) {
jkelley38 6:a7796fddaa16 274 if (distance < 200){
jkelley38 6:a7796fddaa16 275 led_1 = 1;
jkelley38 6:a7796fddaa16 276 pc.printf("D=%ld mm\r\n", distance);
jkelley38 6:a7796fddaa16 277 wait(1.5);
jkelley38 6:a7796fddaa16 278 status = board->sensor_centre->get_distance(&distance);
jkelley38 6:a7796fddaa16 279 if (status == VL53L0X_ERROR_NONE) {
jkelley38 6:a7796fddaa16 280 if (distance < 200) {
jkelley38 6:a7796fddaa16 281 pc.printf("Congrats, You hooked it! Please Remove The hook ASAP to ensure a fair score!\r\n");
jkelley38 6:a7796fddaa16 282 scoreCurrent = scoreCurrent + 10;
jkelley38 6:a7796fddaa16 283 pc.printf("Current score is %f !!! \r\n", scoreCurrent);
jkelley38 6:a7796fddaa16 284 pc.printf("The current game time is %f seconds! You only have 120 seconds!\r\n", gameTimer.read());
jkelley38 6:a7796fddaa16 285 //wait(4);
jkelley38 6:a7796fddaa16 286 gameTimer.stop();
jkelley38 6:a7796fddaa16 287 gameIsRunning = false;
jkelley38 6:a7796fddaa16 288 while(distance < 200){
jkelley38 6:a7796fddaa16 289 status = board->sensor_centre->get_distance(&distance);
jkelley38 6:a7796fddaa16 290 }
jkelley38 6:a7796fddaa16 291 gameTimer.start();
jkelley38 6:a7796fddaa16 292 gameIsRunning = true;
jkelley38 6:a7796fddaa16 293 } //If distance is still greater than 100
jkelley38 6:a7796fddaa16 294 led_1 = 0;
jkelley38 6:a7796fddaa16 295 }
jkelley38 6:a7796fddaa16 296 } // If distance > 100 end
jkelley38 6:a7796fddaa16 297 //else {
jkelley38 6:a7796fddaa16 298 // pc.printf("Please retry to hook it ASAP!\r\n");
jkelley38 6:a7796fddaa16 299 //pc.printf("The current game time is %f seconds! Remember, you only have 120 seconds!\r\n", gameTimer.read());
jkelley38 6:a7796fddaa16 300 //wait(4);
jkelley38 6:a7796fddaa16 301 //} //else statement for
jkelley38 6:a7796fddaa16 302 } //if statement for using the LIDAR with no error
jkelley38 6:a7796fddaa16 303 } //While gameTimer < 120 end
jkelley38 6:a7796fddaa16 304 //} //Second while(1) loop end
jkelley38 6:a7796fddaa16 305 } //if statement end with pushbutton
jkelley38 6:a7796fddaa16 306 old_pb = new_pb;
jkelley38 6:a7796fddaa16 307 //gameTimer.reset();
jkelley38 6:a7796fddaa16 308 } //final while(1) loop
jkelley38 6:a7796fddaa16 309
jkelley38 6:a7796fddaa16 310 old_pb = 0;
jkelley38 6:a7796fddaa16 311 gameTimer.stop();
jkelley38 6:a7796fddaa16 312 gameIsRunning = false;
jkelley38 6:a7796fddaa16 313
jkelley38 6:a7796fddaa16 314 pc.printf("Congrats, your final score was %f ! \r\n", scoreCurrent);
jkelley38 6:a7796fddaa16 315
jkelley38 6:a7796fddaa16 316 wait(1);
jkelley38 6:a7796fddaa16 317
jkelley38 6:a7796fddaa16 318 if (scoreCurrent > score1){
jkelley38 6:a7796fddaa16 319 scoreBuffer = score1;
jkelley38 6:a7796fddaa16 320 score1 = scoreCurrent;
jkelley38 6:a7796fddaa16 321 score5 = score4;
jkelley38 6:a7796fddaa16 322 score4 = score3;
jkelley38 6:a7796fddaa16 323 score3 = score2;
jkelley38 6:a7796fddaa16 324 score2 = scoreBuffer;
jkelley38 6:a7796fddaa16 325 pc.printf("Congrats on placing 1/5 on the High Scores! \r\n");
jkelley38 6:a7796fddaa16 326 } else if (scoreCurrent > score2){
jkelley38 6:a7796fddaa16 327 scoreBuffer = score2;
jkelley38 6:a7796fddaa16 328 score5 = score4;
jkelley38 6:a7796fddaa16 329 score4 = score3;
jkelley38 6:a7796fddaa16 330 score3 = scoreBuffer;
jkelley38 6:a7796fddaa16 331 score2 = scoreCurrent;
jkelley38 6:a7796fddaa16 332 pc.printf("Congrats on placing 2/5 on the High Scores! \r\n");
jkelley38 6:a7796fddaa16 333 } else if (scoreCurrent > score3){
jkelley38 6:a7796fddaa16 334 scoreBuffer = score3;
jkelley38 6:a7796fddaa16 335 score5 = score4;
jkelley38 6:a7796fddaa16 336 score4 = scoreBuffer;;
jkelley38 6:a7796fddaa16 337 score3 = scoreCurrent;
jkelley38 6:a7796fddaa16 338 pc.printf("Congrats on placing 3/5 on the High Scores! \r\n");
jkelley38 6:a7796fddaa16 339 } else if (scoreCurrent > score4){
jkelley38 6:a7796fddaa16 340 scoreBuffer = score4;
jkelley38 6:a7796fddaa16 341 score5 = scoreBuffer;
jkelley38 6:a7796fddaa16 342 score4 = scoreCurrent;
jkelley38 6:a7796fddaa16 343 pc.printf("Congrats on placing 4/5 on the High Scores! \r\n");
jkelley38 6:a7796fddaa16 344 } else if (scoreCurrent > score5){
jkelley38 6:a7796fddaa16 345 scoreBuffer = score5;
jkelley38 6:a7796fddaa16 346 score5 = scoreCurrent;
jkelley38 6:a7796fddaa16 347 pc.printf("Congrats on placing 5/5 on the High Scores! \r\n");
jkelley38 6:a7796fddaa16 348 } else {
jkelley38 6:a7796fddaa16 349 pc.printf("Sorry, you didn't make it on the HighScores! Please check 172.20.10.3 to view the table! \r\n");
jkelley38 6:a7796fddaa16 350 }
jkelley38 6:a7796fddaa16 351
jkelley38 6:a7796fddaa16 352 wait(5);
jkelley38 6:a7796fddaa16 353
jkelley38 6:a7796fddaa16 354 // while(1) {
4180_1 4:40dd020463ea 355 if(DataRX==1) {
star297 0:e2a155f50119 356 ReadWebData();
ausdong 5:bc0296a5ad8a 357 esp.attach(&Rx_interrupt, Serial::RxIrq);
ausdong 5:bc0296a5ad8a 358 }
ausdong 5:bc0296a5ad8a 359 if(update==1) // update time, hit count, and analog levels in the HUZZAH chip
ausdong 5:bc0296a5ad8a 360 {
ausdong 5:bc0296a5ad8a 361 // get new values
ausdong 5:bc0296a5ad8a 362 gettime();
jkelley38 6:a7796fddaa16 363 getScore1();
jkelley38 6:a7796fddaa16 364 getScore2();
jkelley38 6:a7796fddaa16 365 getScore3();
jkelley38 6:a7796fddaa16 366 getScore4();
jkelley38 6:a7796fddaa16 367 getScore5();
ausdong 5:bc0296a5ad8a 368 count++;
ausdong 5:bc0296a5ad8a 369 // send new values
jkelley38 6:a7796fddaa16 370 sprintf(cmdbuff, "score1read,score2read,score3read,score4read,score5read = \"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\r\n", score1Char,score2Char,score3Char,score4Char,score5Char);
ausdong 5:bc0296a5ad8a 371 SendCMD();
ausdong 5:bc0296a5ad8a 372 getreply();
ausdong 5:bc0296a5ad8a 373 update=0;
4180_1 4:40dd020463ea 374 }
jkelley38 6:a7796fddaa16 375 // }
star297 0:e2a155f50119 376 }
4180_1 4:40dd020463ea 377 }
jkelley38 6:a7796fddaa16 378 // END OF MAIN HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
star297 0:e2a155f50119 379
4180_1 4:40dd020463ea 380 // Reads and processes GET and POST web data
star297 0:e2a155f50119 381 void ReadWebData()
4180_1 4:40dd020463ea 382 {
4180_1 4:40dd020463ea 383 wait_ms(200);
ausdong 5:bc0296a5ad8a 384 esp.attach(NULL,Serial::RxIrq);
4180_1 4:40dd020463ea 385 DataRX=0;
4180_1 4:40dd020463ea 386 memset(webdata, '\0', sizeof(webdata));
ausdong 5:bc0296a5ad8a 387 strcpy(webdata, rx_buffer);
ausdong 5:bc0296a5ad8a 388 memset(rx_buffer, '\0', sizeof(rx_buffer));
ausdong 5:bc0296a5ad8a 389 rx_in = 0;
ausdong 5:bc0296a5ad8a 390 rx_out = 0;
ausdong 5:bc0296a5ad8a 391 // check web data for form information
ausdong 5:bc0296a5ad8a 392 if( strstr(webdata, "check=led1v") != NULL ) {
ausdong 5:bc0296a5ad8a 393 led1=!led1;
ausdong 5:bc0296a5ad8a 394 }
ausdong 5:bc0296a5ad8a 395 if( strstr(webdata, "check=led2v") != NULL ) {
ausdong 5:bc0296a5ad8a 396 led2=!led2;
ausdong 5:bc0296a5ad8a 397 }
ausdong 5:bc0296a5ad8a 398 if( strstr(webdata, "check=led3v") != NULL ) {
ausdong 5:bc0296a5ad8a 399 led3=!led3;
ausdong 5:bc0296a5ad8a 400 }
ausdong 5:bc0296a5ad8a 401 if( strstr(webdata, "check=led4v") != NULL ) {
ausdong 5:bc0296a5ad8a 402 led4=!led4;
ausdong 5:bc0296a5ad8a 403 }
ausdong 5:bc0296a5ad8a 404 if( strstr(webdata, "POST") != NULL ) { // set update flag if POST request
ausdong 5:bc0296a5ad8a 405 update=1;
ausdong 5:bc0296a5ad8a 406 }
ausdong 5:bc0296a5ad8a 407 if( strstr(webdata, "GET") != NULL && strstr(webdata, "favicon") == NULL ) { // set update flag for GET request but do not want to update for favicon requests
ausdong 5:bc0296a5ad8a 408 update=1;
4180_1 4:40dd020463ea 409 }
star297 0:e2a155f50119 410 }
ausdong 5:bc0296a5ad8a 411 // Starts webserver
star297 0:e2a155f50119 412 void startserver()
star297 0:e2a155f50119 413 {
ausdong 5:bc0296a5ad8a 414 gettime();
jkelley38 6:a7796fddaa16 415 getScore1();
jkelley38 6:a7796fddaa16 416 getScore2();
jkelley38 6:a7796fddaa16 417 getScore3();
jkelley38 6:a7796fddaa16 418 getScore4();
jkelley38 6:a7796fddaa16 419 getScore5();
4180_1 4:40dd020463ea 420 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
ausdong 5:bc0296a5ad8a 421 strcpy(cmdbuff,"node.restart()\r\n");
ausdong 5:bc0296a5ad8a 422 SendCMD();
ausdong 5:bc0296a5ad8a 423 wait(2);
ausdong 5:bc0296a5ad8a 424 getreply();
ausdong 5:bc0296a5ad8a 425
ausdong 5:bc0296a5ad8a 426 pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> ");
ausdong 5:bc0296a5ad8a 427
ausdong 5:bc0296a5ad8a 428 // initial values
jkelley38 6:a7796fddaa16 429 sprintf(cmdbuff, "score1read,score2read,score3read,score4read,score5read = \"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\r\n", score1Char,score2Char,score3Char,score4Char,score5Char);
ausdong 5:bc0296a5ad8a 430 SendCMD();
ausdong 5:bc0296a5ad8a 431 getreply();
ausdong 5:bc0296a5ad8a 432 wait(0.5);
ausdong 5:bc0296a5ad8a 433
ausdong 5:bc0296a5ad8a 434 //create server
ausdong 5:bc0296a5ad8a 435 sprintf(cmdbuff, "srv=net.createServer(net.TCP,%d)\r\n",SERVtimeout);
ausdong 5:bc0296a5ad8a 436 SendCMD();
ausdong 5:bc0296a5ad8a 437 getreply();
ausdong 5:bc0296a5ad8a 438 wait(0.5);
ausdong 5:bc0296a5ad8a 439 strcpy(cmdbuff,"srv:listen(80,function(conn)\r\n");
star297 0:e2a155f50119 440 SendCMD();
star297 1:71ed1afbf344 441 getreply();
ausdong 5:bc0296a5ad8a 442 wait(0.3);
ausdong 5:bc0296a5ad8a 443 strcpy(cmdbuff,"conn:on(\"receive\",function(conn,payload) \r\n");
ausdong 5:bc0296a5ad8a 444 SendCMD();
ausdong 5:bc0296a5ad8a 445 getreply();
ausdong 5:bc0296a5ad8a 446 wait(0.3);
ausdong 5:bc0296a5ad8a 447
ausdong 5:bc0296a5ad8a 448 //print data to mbed
ausdong 5:bc0296a5ad8a 449 strcpy(cmdbuff,"print(payload)\r\n");
ausdong 5:bc0296a5ad8a 450 SendCMD();
ausdong 5:bc0296a5ad8a 451 getreply();
ausdong 5:bc0296a5ad8a 452 wait(0.2);
ausdong 5:bc0296a5ad8a 453
jkelley38 6:a7796fddaa16 454 //web page data CODE FOR TABLE FOUND HERE
jkelley38 6:a7796fddaa16 455 strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><head><style>body { background-color:#D6DBDF; color: blue; font: 12px arial, verdana; }')\r\n");
jkelley38 6:a7796fddaa16 456 SendCMD();
jkelley38 6:a7796fddaa16 457 getreply();
jkelley38 6:a7796fddaa16 458 wait(0.4);
jkelley38 6:a7796fddaa16 459 strcpy(cmdbuff,"conn:send('table {border-collapse: collapse;}')\r\n");
ausdong 5:bc0296a5ad8a 460 SendCMD();
ausdong 5:bc0296a5ad8a 461 getreply();
ausdong 5:bc0296a5ad8a 462 wait(0.4);
jkelley38 6:a7796fddaa16 463 strcpy(cmdbuff,"conn:send('th {text-align: center; border: 4px solid; background-color: #e2dee5; font-size: 18px;}')\r\n");
jkelley38 6:a7796fddaa16 464 SendCMD();
jkelley38 6:a7796fddaa16 465 getreply();
jkelley38 6:a7796fddaa16 466 wait(0.4);
jkelley38 6:a7796fddaa16 467 strcpy(cmdbuff,"conn:send('td {text-align: center; border: 2px dashed; padding: 5px; height: 15px; font-size: 14px}')\r\n");
star297 1:71ed1afbf344 468 SendCMD();
star297 2:d4c6bc0f2dc4 469 getreply();
jkelley38 6:a7796fddaa16 470 wait(0.4);
jkelley38 6:a7796fddaa16 471 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");
jkelley38 6:a7796fddaa16 472 SendCMD();
jkelley38 6:a7796fddaa16 473 getreply();
jkelley38 6:a7796fddaa16 474 wait(0.4);
jkelley38 6:a7796fddaa16 475 strcpy(cmdbuff,"conn:send('<body><h2 align=\"center\"><font size=\"5\">Loop on a Rope Highscores</font></h2>')\r\n");
ausdong 5:bc0296a5ad8a 476 SendCMD();
ausdong 5:bc0296a5ad8a 477 getreply();
ausdong 5:bc0296a5ad8a 478 wait(0.4);
jkelley38 6:a7796fddaa16 479 strcpy(cmdbuff,"conn:send('<br><table border=\"1\" style=\"width:100%\"><tr><th>Rank</th><th>High Score</th></tr><br><hr>')\r\n");
star297 1:71ed1afbf344 480 SendCMD();
star297 3:f7febfa77784 481 getreply();
jkelley38 6:a7796fddaa16 482 wait(0.4);
jkelley38 6:a7796fddaa16 483 strcpy(cmdbuff,"conn:send('<tr><td>1</td><td> '..score1read..' </td></tr>')\r\n");
ausdong 5:bc0296a5ad8a 484 SendCMD();
ausdong 5:bc0296a5ad8a 485 getreply();
jkelley38 6:a7796fddaa16 486 wait(0.4);
jkelley38 6:a7796fddaa16 487 strcpy(cmdbuff,"conn:send('<tr><td>2</td><td> '..score2read..' </td></tr>')\r\n");
ausdong 5:bc0296a5ad8a 488 SendCMD();
ausdong 5:bc0296a5ad8a 489 getreply();
jkelley38 6:a7796fddaa16 490 wait(0.4);
jkelley38 6:a7796fddaa16 491 strcpy(cmdbuff,"conn:send('<tr><td>3</td><td> '..score3read..' </td></tr>')\r\n");
ausdong 5:bc0296a5ad8a 492 SendCMD();
ausdong 5:bc0296a5ad8a 493 getreply();
jkelley38 6:a7796fddaa16 494 wait(0.4);
jkelley38 6:a7796fddaa16 495 strcpy(cmdbuff,"conn:send('<tr><td>4</td><td> '..score4read..' </td></tr>')\r\n");
star297 3:f7febfa77784 496 SendCMD();
star297 2:d4c6bc0f2dc4 497 getreply();
jkelley38 6:a7796fddaa16 498 wait(0.4);
jkelley38 6:a7796fddaa16 499 strcpy(cmdbuff,"conn:send('<tr><td>5</td><td> '..score5read..' </td></tr></table>')\r\n");
jkelley38 6:a7796fddaa16 500 SendCMD();
jkelley38 6:a7796fddaa16 501 getreply();
jkelley38 6:a7796fddaa16 502 wait(0.4);
jkelley38 6:a7796fddaa16 503 strcpy(cmdbuff,"conn:send('<form method=\"POST\"')\r\n");
ausdong 5:bc0296a5ad8a 504 SendCMD();
ausdong 5:bc0296a5ad8a 505 getreply();
ausdong 5:bc0296a5ad8a 506 wait(0.3);
ausdong 5:bc0296a5ad8a 507 strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"send-refresh\"></form>')\r\n");
ausdong 5:bc0296a5ad8a 508 SendCMD();
ausdong 5:bc0296a5ad8a 509 getreply();
ausdong 5:bc0296a5ad8a 510 wait(0.3);
jkelley38 6:a7796fddaa16 511
ausdong 5:bc0296a5ad8a 512 // end web page data
jkelley38 6:a7796fddaa16 513
ausdong 5:bc0296a5ad8a 514 strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection
ausdong 5:bc0296a5ad8a 515 SendCMD();
ausdong 5:bc0296a5ad8a 516 getreply();
ausdong 5:bc0296a5ad8a 517 wait(0.3);
ausdong 5:bc0296a5ad8a 518 strcpy(cmdbuff, "end)\r\n");
ausdong 5:bc0296a5ad8a 519 SendCMD();
ausdong 5:bc0296a5ad8a 520 getreply();
ausdong 5:bc0296a5ad8a 521 wait(0.2);
ausdong 5:bc0296a5ad8a 522 strcpy(cmdbuff, "end)\r\n");
ausdong 5:bc0296a5ad8a 523 SendCMD();
ausdong 5:bc0296a5ad8a 524 getreply();
ausdong 5:bc0296a5ad8a 525 wait(0.2);
ausdong 5:bc0296a5ad8a 526
ausdong 5:bc0296a5ad8a 527 strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\r\n");
ausdong 5:bc0296a5ad8a 528 SendCMD();
ausdong 5:bc0296a5ad8a 529 getreply();
ausdong 5:bc0296a5ad8a 530 wait(0.2);
ausdong 5:bc0296a5ad8a 531 strcpy(cmdbuff, "if wifi.sta.getip() == nil then\r\n");
ausdong 5:bc0296a5ad8a 532 SendCMD();
ausdong 5:bc0296a5ad8a 533 getreply();
ausdong 5:bc0296a5ad8a 534 wait(0.2);
ausdong 5:bc0296a5ad8a 535 strcpy(cmdbuff, "print(\"Connecting to AP...\\n\")\r\n");
ausdong 5:bc0296a5ad8a 536 SendCMD();
ausdong 5:bc0296a5ad8a 537 getreply();
ausdong 5:bc0296a5ad8a 538 wait(0.2);
ausdong 5:bc0296a5ad8a 539 strcpy(cmdbuff, "else\r\n");
ausdong 5:bc0296a5ad8a 540 SendCMD();
ausdong 5:bc0296a5ad8a 541 getreply();
ausdong 5:bc0296a5ad8a 542 wait(0.2);
ausdong 5:bc0296a5ad8a 543 strcpy(cmdbuff, "ip, nm, gw=wifi.sta.getip()\r\n");
ausdong 5:bc0296a5ad8a 544 SendCMD();
ausdong 5:bc0296a5ad8a 545 getreply();
ausdong 5:bc0296a5ad8a 546 wait(0.2);
ausdong 5:bc0296a5ad8a 547 strcpy(cmdbuff,"print(\"IP Address: \",ip)\r\n");
ausdong 5:bc0296a5ad8a 548 SendCMD();
ausdong 5:bc0296a5ad8a 549 getreply();
ausdong 5:bc0296a5ad8a 550 wait(0.2);
ausdong 5:bc0296a5ad8a 551 strcpy(cmdbuff,"tmr.stop(0)\r\n");
ausdong 5:bc0296a5ad8a 552 SendCMD();
ausdong 5:bc0296a5ad8a 553 getreply();
ausdong 5:bc0296a5ad8a 554 wait(0.2);
ausdong 5:bc0296a5ad8a 555 strcpy(cmdbuff,"end\r\n");
ausdong 5:bc0296a5ad8a 556 SendCMD();
ausdong 5:bc0296a5ad8a 557 getreply();
ausdong 5:bc0296a5ad8a 558 wait(0.2);
ausdong 5:bc0296a5ad8a 559 strcpy(cmdbuff,"end)\r\n");
ausdong 5:bc0296a5ad8a 560 SendCMD();
ausdong 5:bc0296a5ad8a 561 getreply();
ausdong 5:bc0296a5ad8a 562 wait(0.2);
ausdong 5:bc0296a5ad8a 563
ausdong 5:bc0296a5ad8a 564 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
4180_1 4:40dd020463ea 565 }
ausdong 5:bc0296a5ad8a 566
ausdong 5:bc0296a5ad8a 567
star297 0:e2a155f50119 568 // ESP Command data send
star297 0:e2a155f50119 569 void SendCMD()
star297 0:e2a155f50119 570 {
ausdong 5:bc0296a5ad8a 571 int i;
ausdong 5:bc0296a5ad8a 572 char temp_char;
ausdong 5:bc0296a5ad8a 573 bool empty;
ausdong 5:bc0296a5ad8a 574 i = 0;
ausdong 5:bc0296a5ad8a 575 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 576 NVIC_DisableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 577 empty = (tx_in == tx_out);
ausdong 5:bc0296a5ad8a 578 while ((i==0) || (cmdbuff[i-1] != '\n')) {
ausdong 5:bc0296a5ad8a 579 // Wait if buffer full
ausdong 5:bc0296a5ad8a 580 if (((tx_in + 1) % buffer_size) == tx_out) {
ausdong 5:bc0296a5ad8a 581 // End Critical Section - need to let interrupt routine empty buffer by sending
ausdong 5:bc0296a5ad8a 582 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 583 while (((tx_in + 1) % buffer_size) == tx_out) {
ausdong 5:bc0296a5ad8a 584 }
ausdong 5:bc0296a5ad8a 585 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 586 NVIC_DisableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 587 }
ausdong 5:bc0296a5ad8a 588 tx_buffer[tx_in] = cmdbuff[i];
ausdong 5:bc0296a5ad8a 589 i++;
ausdong 5:bc0296a5ad8a 590 tx_in = (tx_in + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 591 }
ausdong 5:bc0296a5ad8a 592 if (esp.writeable() && (empty)) {
ausdong 5:bc0296a5ad8a 593 temp_char = tx_buffer[tx_out];
ausdong 5:bc0296a5ad8a 594 tx_out = (tx_out + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 595 // Send first character to start tx interrupts, if stopped
ausdong 5:bc0296a5ad8a 596 esp.putc(temp_char);
ausdong 5:bc0296a5ad8a 597 }
ausdong 5:bc0296a5ad8a 598 // End Critical Section
ausdong 5:bc0296a5ad8a 599 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 600 return;
4180_1 4:40dd020463ea 601 }
ausdong 5:bc0296a5ad8a 602
4180_1 4:40dd020463ea 603 // Get Command and ESP status replies
star297 0:e2a155f50119 604 void getreply()
4180_1 4:40dd020463ea 605 {
ausdong 5:bc0296a5ad8a 606 read_line();
ausdong 5:bc0296a5ad8a 607 sscanf(rx_line,replybuff);
ausdong 5:bc0296a5ad8a 608 }
ausdong 5:bc0296a5ad8a 609
ausdong 5:bc0296a5ad8a 610 // Read a line from the large rx buffer from rx interrupt routine
ausdong 5:bc0296a5ad8a 611 void read_line() {
ausdong 5:bc0296a5ad8a 612 int i;
ausdong 5:bc0296a5ad8a 613 i = 0;
ausdong 5:bc0296a5ad8a 614 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 615 NVIC_DisableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 616 // Loop reading rx buffer characters until end of line character
ausdong 5:bc0296a5ad8a 617 while ((i==0) || (rx_line[i-1] != '\r')) {
ausdong 5:bc0296a5ad8a 618 // Wait if buffer empty
ausdong 5:bc0296a5ad8a 619 if (rx_in == rx_out) {
ausdong 5:bc0296a5ad8a 620 // End Critical Section - need to allow rx interrupt to get new characters for buffer
ausdong 5:bc0296a5ad8a 621 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 622 while (rx_in == rx_out) {
ausdong 5:bc0296a5ad8a 623 }
ausdong 5:bc0296a5ad8a 624 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 625 NVIC_DisableIRQ(UART1_IRQn);
star297 2:d4c6bc0f2dc4 626 }
ausdong 5:bc0296a5ad8a 627 rx_line[i] = rx_buffer[rx_out];
ausdong 5:bc0296a5ad8a 628 i++;
ausdong 5:bc0296a5ad8a 629 rx_out = (rx_out + 1) % buffer_size;
4180_1 4:40dd020463ea 630 }
ausdong 5:bc0296a5ad8a 631 // End Critical Section
ausdong 5:bc0296a5ad8a 632 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 633 rx_line[i-1] = 0;
ausdong 5:bc0296a5ad8a 634 return;
ausdong 5:bc0296a5ad8a 635 }
ausdong 5:bc0296a5ad8a 636
ausdong 5:bc0296a5ad8a 637
ausdong 5:bc0296a5ad8a 638 // Interupt Routine to read in data from serial port
ausdong 5:bc0296a5ad8a 639 void Rx_interrupt() {
ausdong 5:bc0296a5ad8a 640 DataRX=1;
ausdong 5:bc0296a5ad8a 641 //led3=1;
ausdong 5:bc0296a5ad8a 642 // Loop just in case more than one character is in UART's receive FIFO buffer
ausdong 5:bc0296a5ad8a 643 // Stop if buffer full
ausdong 5:bc0296a5ad8a 644 while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
ausdong 5:bc0296a5ad8a 645 rx_buffer[rx_in] = esp.getc();
ausdong 5:bc0296a5ad8a 646 // Uncomment to Echo to USB serial to watch data flow
ausdong 5:bc0296a5ad8a 647 pc.putc(rx_buffer[rx_in]);
ausdong 5:bc0296a5ad8a 648 rx_in = (rx_in + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 649 }
ausdong 5:bc0296a5ad8a 650 //led3=0;
ausdong 5:bc0296a5ad8a 651 return;
ausdong 5:bc0296a5ad8a 652 }
ausdong 5:bc0296a5ad8a 653
ausdong 5:bc0296a5ad8a 654
ausdong 5:bc0296a5ad8a 655 // Interupt Routine to write out data to serial port
ausdong 5:bc0296a5ad8a 656 void Tx_interrupt() {
ausdong 5:bc0296a5ad8a 657 //led2=1;
ausdong 5:bc0296a5ad8a 658 // Loop to fill more than one character in UART's transmit FIFO buffer
ausdong 5:bc0296a5ad8a 659 // Stop if buffer empty
ausdong 5:bc0296a5ad8a 660 while ((esp.writeable()) && (tx_in != tx_out)) {
ausdong 5:bc0296a5ad8a 661 esp.putc(tx_buffer[tx_out]);
ausdong 5:bc0296a5ad8a 662 tx_out = (tx_out + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 663 }
ausdong 5:bc0296a5ad8a 664 //led2=0;
ausdong 5:bc0296a5ad8a 665 return;
ausdong 5:bc0296a5ad8a 666 }
ausdong 5:bc0296a5ad8a 667
ausdong 5:bc0296a5ad8a 668 void gettime()
ausdong 5:bc0296a5ad8a 669 {
ausdong 5:bc0296a5ad8a 670 time_t seconds = time(NULL);
ausdong 5:bc0296a5ad8a 671 strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
ausdong 5:bc0296a5ad8a 672 }
ausdong 5:bc0296a5ad8a 673
ausdong 5:bc0296a5ad8a 674 void setRTC()
ausdong 5:bc0296a5ad8a 675 {
ausdong 5:bc0296a5ad8a 676 t.tm_sec = (0); // 0-59
ausdong 5:bc0296a5ad8a 677 t.tm_min = (minute); // 0-59
ausdong 5:bc0296a5ad8a 678 t.tm_hour = (hour); // 0-23
ausdong 5:bc0296a5ad8a 679 t.tm_mday = (dayofmonth); // 1-31
ausdong 5:bc0296a5ad8a 680 t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format
ausdong 5:bc0296a5ad8a 681 t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year
ausdong 5:bc0296a5ad8a 682 set_time(mktime(&t)); // set RTC clock
star297 0:e2a155f50119 683 }
jkelley38 6:a7796fddaa16 684
jkelley38 6:a7796fddaa16 685 void getScore1()
star297 0:e2a155f50119 686 {
jkelley38 6:a7796fddaa16 687 sprintf(score1Char,"%2.3f",score1);
jkelley38 6:a7796fddaa16 688 }
jkelley38 6:a7796fddaa16 689 void getScore2()
jkelley38 6:a7796fddaa16 690 {
jkelley38 6:a7796fddaa16 691 sprintf(score2Char,"%2.3f",score2);
star297 0:e2a155f50119 692 }
jkelley38 6:a7796fddaa16 693 void getScore3()
jkelley38 6:a7796fddaa16 694 {
jkelley38 6:a7796fddaa16 695 sprintf(score3Char,"%2.3f",score3);
jkelley38 6:a7796fddaa16 696 }
jkelley38 6:a7796fddaa16 697 void getScore4()
4180_1 4:40dd020463ea 698 {
jkelley38 6:a7796fddaa16 699 sprintf(score4Char,"%2.3f",score4);
jkelley38 6:a7796fddaa16 700 }
jkelley38 6:a7796fddaa16 701 void getScore5()
jkelley38 6:a7796fddaa16 702 {
jkelley38 6:a7796fddaa16 703 sprintf(score5Char,"%2.3f",score5);
ausdong 5:bc0296a5ad8a 704 }