Complete Build

Dependencies:   4DGL-uLCD-SE1 Motor SDFileSystem X_NUCLEO_53L0A1 mbed-rtos mbed BotwithWavePlayerLevel

Fork of BotWithBluetoothLIDARV2 by Brandon Weiner and Carlos Tallard

Revision:
16:61e88b0e3689
Parent:
15:c8360ceaad61
Child:
17:2c6549be1ecb
--- a/main.cpp	Mon Oct 16 02:23:52 2017 +0000
+++ b/main.cpp	Thu Nov 16 16:45:51 2017 +0000
@@ -1,254 +1,200 @@
+
 #include "mbed.h"
+#include "Motor.h"
 #include "rtos.h"
-#include "uLCD_4DGL.h"
-#include "SDFileSystem.h"
-#include "wave_player.h"
-//nclude "picojpeg.h"
 
-Serial blues(p13,p14);
-Serial pc(USBTX, USBRX);
-SDFileSystem sd(p5, p6, p7, p9, "sd"); //SD card
+#include "SDFileSystem.h"
+SDFileSystem sd(p5, p6, p7, p29, "sd");
 
+
+#include "wave_player.h"
 AnalogOut DACout(p18);
-
 wave_player waver(&DACout);
- 
-PwmOut red(p23);
-PwmOut blue(p21);
-PwmOut green(p22);
-    Thread *(test);
-PwmOut myled(LED1);
-uLCD_4DGL uLCD(p28,p27,p30);
-Semaphore three_slots(1);
-Thread thread2;
-Thread thread3;
-Thread thread4;
-Thread thread5;
-Thread thread6;
+
 
 
- float y=0.0;
- int c=80;
- int d=1;
-char bnum=0;
-char current='1';
-int color=YELLOW;
-
-char previousButton;
+//BusOut myled(LED1,LED2,LED3,LED4);
+#include "XNucleo53L0A1.h"
+#include <stdio.h>
+Serial pc(USBTX,USBRX);
+DigitalOut shdn(p26);
+//I2C sensor pins
+#define VL53L0_I2C_SDA   p28
+#define VL53L0_I2C_SCL   p27
+static XNucleo53L0A1 *board=NULL;
+DigitalOut myled (LED2);
+Serial blue(p9,p10);
+Motor RW(p22, p13, p12); // pwm, fwd, rev
+Motor LW(p21, p8, p11); // pwm, fwd, rev
+Thread lidar;
+Thread siren;
+uint32_t distance;
+FILE *wave_file;
 
-double w; //Angular frequency of sinusoidal wave controlling RGB LED
-double k; 
- 
-FILE *wave_file; 
- 
-void t2() {
-    while (true) {
-       int a=20;
-       int r=8;
-       
-      three_slots.wait();
-       uLCD.background_color(BLACK);
-       uLCD.cls();
-       uLCD.filled_circle(a, 105-(y*80), r, BLUE);
-       uLCD.line(3,25,13,25,RED);
-       uLCD.line(3,105,13,105,RED);
-     
-      
-       //while(b<100){
-         //  uLCD.filled_circle(a, b, r, BLUE);
-         //  b=b+1;
-         //  uLCD.filled_circle(a, b-1, r, BLACK);
-         //  uLCD.filled_circle(a, b, r, BLUE);
-         //  Thread::wait(1000.0*0.002);
-         //  }
-        //while(b>50){
-         //  uLCD.filled_circle(a, b, r, BLUE);
-         //  b=b-1;
-          // uLCD.filled_circle(a, b+2, r, BLACK);
-          // uLCD.filled_circle(a, b, r, BLUE);
-           //Thread::wait(1000.0*0.002);
-          // }
-       
-        three_slots.release();  
-        Thread::wait(50);
-          
-    }
+
+
+void t3(){
+
+    while(1){
+   
+    wave_file=fopen("/sd/Bee Gees - Stayin' Alive.wav","r");
+    waver.play(wave_file);
+    fclose(wave_file);
+    Thread::wait(50);
 }
- 
- void t3() {
-    while (true) {
-         
-     
-         wave_file=fopen("/sd/Bee Gees - Stayin' Alive.wav","r");
-         waver.play(wave_file);
-         fclose(wave_file);
-         Thread::wait(1000);
-    }
-}
-
- void t6() {
-    while (true) {
-     
-         wave_file=fopen("/sd/Joey Scarbury - Believe it or Not.wav","r");
-         waver.play(wave_file);
-         fclose(wave_file);
-         Thread::wait(1000);
-    }
 }
 
-void t4() {
-    while (true) {
-          
-        three_slots.wait();
-        
-        if(c==70){
-            d=20;
-            }
-         if(c==110){
-            d=-20;
-            }
-       
-        uLCD.filled_circle(c, 64 , 10, WHITE);
-        uLCD.filled_circle(c, 64 , 8, color);
-        Thread::wait(50);
-         uLCD.filled_circle(c, 64 , 10, BLACK);
-        c=c+d;
-        uLCD.filled_circle(c, 64 , 10, color);
-        uLCD.filled_circle(c, 64 , 8, WHITE);
-        Thread::wait(50);
-        uLCD.filled_circle(c, 64 , 10, BLACK);
-        
-            three_slots.release(); 
-        Thread::wait(50);
-      
+void t2() {
+    
+    int status;
+    DevI2C *device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
+    /* creates the 53L0A1 expansion board singleton obj */
+    board = XNucleo53L0A1::instance(device_i2c, A2, D8, D2);
+    shdn = 0; //must reset sensor for an mbed reset to work
+    Thread::wait(100);
+    shdn = 1;
+    Thread::wait(100);
+    /* init the 53L0A1 board with default values */
+    status = board->init_board();
+    while (status) {
+        pc.printf("Failed to init board! \r\n");
+        status = board->init_board();
     }
-}
- 
- 
-void t5(){
-    
-    char bhit = 0;
-    //double d = 10;
-    
-    while(true){
+    //loop taking and printing distance
     
-         
-         if(blues.readable()==1){
-             three_slots.wait();
-         if (blues.getc()=='!') {
-            if (blues.getc()=='B') { //button data
-                bnum = blues.getc(); //button number
-                bhit = blues.getc(); //button hit = 1; miss = 0;
-                
-                if ((bnum>='1')&&(bnum<='8')){ //is a number button 1..4
-                   current=bnum;
-                   //pc.printf("Button Pressed %c bhit = %c PreviousButton = %c CurrentButton = %c\n\r", bnum, bhit, previousButton, current);
-                   previousButton = current;
-                   if(current=='1'){
-                    color=YELLOW;
-                    }
-                    if(current=='2'){
-                    color=BLUE;
-                     }
-                    if(current=='3'){
-                     color=0x551A8B; //Purple
-                        }
-                    if(current=='4'){
-                      color=GREEN;
-                    }
-                    if(current =='5' && bhit == '0'){
-                        color=BLACK;
-                        thread3.terminate();
-                         //thread3.join();
-                        fclose(wave_file);
-                        if(wave_file != NULL){
-                         pc.printf("after close t3, file is not NULL\n\r");
-                        }
-                        Thread::wait(100);
-                        thread6.start(t6);
-                         }
-                         
-                     if(current =='6' && bhit == '0'){
-                     color=RED;
-                     thread6.terminate();
-                     //thread6.join();
-                    fclose(wave_file);
-                    if(wave_file != NULL){
-                     pc.printf("after close t6, file is not NULL\n\r");
-                    }
-                    Thread::wait(100);
-                    thread3.start(t3);
-                    }    
-                    if(current =='8' && bhit == '0'){
-                      color=ORANGE;
-                      k = k*2;
-                      if(k > 8.0){
-                        k = 8.0;
-                        }
-                      
-                    
-                    } 
-                    if(current =='7' && bhit == '0'){
-                      color=0x00FFFF;
-                      k = k*0.5;
-                      
-                      if(k < 0.125){
-                        k = 0.125;
-                      }
-                    
-                    }      
-                    
-                    
-                }//end of if bnum 1 -8
-             }//end of if getc B      
-        }//end of if getc !
-         three_slots.release(); 
-        }// end of if readable
-        Thread::yield();
-        
-        
+    bool speakerOn=false;
+    
+    while (1) {
+        status = board->sensor_centre->get_distance(&distance);
+        if (status == VL53L0X_ERROR_NONE) {
+            //pc.printf("D=%ld mm\r\n", distance);
+        }
+    
+    //LIDAR checks
     
-    
-    
-    Thread::wait(50);
-   
-    }// end of while loop
+   /* if(speakerOn==false && distance<=100) //thread start sound, speakerOn=true;
+    if(speakerOn==true && distance<=100) //certain volume 
+    if(speakerOn==true && distance<50) //higher volume;
+    if(distance>100) //thread stop, speakerOn==false;
+   */ 
+    //siren.start(t3);
+    myled=!myled;
+    Thread::wait(500);
+    }
     
     }
 
 
 
- 
-int main() {
-    thread2.start((t2));
-    thread3.start(t3);
-    thread4.start((t4));
-    thread5.start((t5));
+
 
-    test = &thread4;
-    test->set_priority(osPriorityHigh);
-    
-    
-    
-    previousButton = '0';
-    
-    k = 1.0;
+int main()
+{
+    myled=0;
+    //lidar.start(t2);
+    //siren.start(t3);
+    char bnum=0;
+    char bhit=0;
     while(1) {
-        for(double x=0.0; x <= 100/k; x = x + 1) {
-            w = 3.14159*k/100.0;
-            y = sin(w*x); 
-            red= y*y*y;
-            blue= y*y*y;
-            green= y*y*y;
-            Thread::wait(1000.0*.025);
+         
+    if(blue.readable()==1){
+        if (blue.getc()=='!') {
+            if (blue.getc()=='B') { //button data packet
+                bnum = blue.getc(); //button number
+                bhit = blue.getc(); //1=hit, 0=release
+                if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
+                    //myled = bnum - '0'; //current button number will appear on LEDs
+                    switch (bnum) {
+                        case '1': //number button 1
+                            if (bhit=='1') {
+                                RW.speed(1);
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '2': //number button 2
+                            if (bhit=='1') {
+                                RW.speed(0);
+                                LW.speed(0);
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '3': //number button 3
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '4': //number button 4
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '5': //button 5 up arrow
+                            if (bhit=='1') {
+                                //add hit code here
+                                
+                                 RW.speed(1.0);
+                                 LW.speed(1.0);  
+                                
+                            } else {
+                                
+                                   LW.speed(0); 
+                                  RW.speed(0); 
+                                //add release code here
+                            }
+                            break;
+                        case '6': //button 6 down arrow
+                            if (bhit=='1') {
+                                 RW.speed(-1.0);
+                                 LW.speed(-1.0);  
+                                //add hit code here
+                            } else {
+                                RW.speed(0);
+                                 LW.speed(0);
+                                //add release code here
+                            }
+                            break;
+                        case '7': //button 7 left arrow
+                            if (bhit=='1') {
+                                RW.speed(1);
+                                 LW.speed(-1);
+                                //add hit code here
+                            } else {
+                                
+                                RW.speed(0);
+                                 LW.speed(0);
+                                //add release code here
+                            }
+                            break;
+                        case '8': //button 8 right arrow
+                            if (bhit=='1') {
+                                RW.speed(-1);
+                                 LW.speed(1);
+                                //add hit code here
+                            } else {
+                                RW.speed(0);
+                                 LW.speed(0);
+                                //add release code here
+                            }
+                            break;
+                        default:
+                            break;
+                    }
+                    }
+                    }
+                }
+          
+            }
+           
+            Thread::yield();
+            
         }
-        //red= 0.0;
-        //blue= 0.0;
-        //green= 0.0;
-        //Thread::wait(1000.0*2.5/w);
-    }
     
-    
-    
-    
-}
+   } 
\ No newline at end of file