football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Revision:
81:3cd7de5d01ef
Parent:
80:8d4f190bd253
Child:
82:1e552cc1a615
--- a/main.cpp	Wed May 25 07:43:48 2016 +0000
+++ b/main.cpp	Wed May 25 17:23:00 2016 +0000
@@ -11,12 +11,17 @@
 
 DigitalIn buttonTeam(BUT_TEAM);
 DigitalIn buttonSpace(BUT_SPACE);
+DigitalIn buttonVolMore(BUT_VOL_MORE);
+DigitalIn buttonVolLess(BUT_VOL_LESS);
+
 DigitalOut ledTeamA(LED_TEAM_A);
 DigitalOut ledTeamB(LED_TEAM_B);
 DigitalOut ledSpace5(LED_SPACE5);
 DigitalOut ledSpace10(LED_SPACE10);
 DigitalOut ledSpace15(LED_SPACE15);
 DigitalOut ledSpace20(LED_SPACE20);
+DigitalOut ledBuzzer(LED_BUZZER_ON);
+
 AnalogIn ain(ANALOG_IN);    // used for randomizing  
 #ifdef NORDIC
 DigitalOut buzzer(BUZZER);
@@ -34,12 +39,13 @@
 
 /////////////////////////////////// FUNCTIONS ///////////////////////////////////
 
-void beep(float period, float time) { 
+void beep(float period, float time, uint8_t vol) { 
 #ifdef ENABLE_SOUND
+    if(!vol) return;
 #ifdef NORDIC
-    if(period>=NOTE_A3) {
+    if(vol>=3) {
         buzzLow = 0; buzzMed = 1; buzzHigh = 1;     // P-MOSFET
-    } else if(period>=NOTE_A4) {
+    } else if(vol>=2) {
         buzzLow = 1; buzzMed = 0; buzzHigh = 1;
     } else {
         buzzLow = 1; buzzMed = 1; buzzHigh = 0;
@@ -49,7 +55,12 @@
     buzzer = 1;
 #else
     speaker.period(period);
-    speaker =0.5; //50% duty cycle - max volume
+    if(vol>=3) 
+        speaker = 0.5; //50% duty cycle - max volume
+    else if(vol>=2) 
+        speaker = 0.33;
+    else
+        speaker = 0.2;
     wait(time);
     speaker=0.0; // turn off audio
 #endif
@@ -70,7 +81,7 @@
         wait(1.0);
         z >>= 1;
     }
-    beep(NOTE_A4, 1.0);
+    beep(NOTE_A4, 1.0, 2);
 #endif
 #endif
 }
@@ -150,7 +161,7 @@
         }
     }
     
-    void showAll(int8_t team, uint8_t level) {
+    void showAll(int8_t team, uint8_t level, uint8_t volume) {
         // Output the current state to the user:
         //   - show the current information table and/or
         //   - beep if the user is too close to another one
@@ -173,10 +184,10 @@
         } 
         //if(!i) {
         //    out.printf("Nobody around\r\n");
-        //    beep(NOTE_A5,0.5);
+        //    beep(NOTE_A5,0.5,volume);
         //}
         if(-maxTeamSignal<SPACE[level]) {
-            beep(NOTE_A4,0.33);
+            beep(NOTE_A4, 0.33, volume);
         }
     }
 };
@@ -190,11 +201,14 @@
     char rand_name[NAME_LEN+1] = {0};
     char other_name[NAME_LEN+1] = {0};
     int8_t myTeam = 1, otherTeam;
-    uint8_t level = 1;
+    uint8_t level = 1, volume = 1;      // SPACE10, VOL_MED
     
-    beep(NOTE_A5, 0.5);
+    ledBuzzer = volume ? 1 : 0;
+    beep(NOTE_A5, 0.5, volume);
 
     buttonSpace.mode(PullDown);
+    buttonVolMore.mode(PullDown);
+    buttonVolLess.mode(PullDown);
 #ifdef NORDIC
     buttonTeam.mode(PullDown);
     int bTeamOld = 0;
@@ -202,7 +216,7 @@
     buttonTeam.mode(PullUp);
     int bTeamOld = 1;
 #endif
-    int bTeamNew, bSpaceOld=0, bSpaceNew;
+    int bTeamNew, bSpaceOld=0, bSpaceNew, bVMNew, bVLNew, bVMOld=0, bVLOld=0;
     
     char this_node = int(ain.read()*255+17)*int(ain.read()*255+11); // random node value
     out.printf("Node: %d\r\n", this_node);
@@ -266,15 +280,39 @@
             out.sleep(2);
         }
         bSpaceOld = bSpaceNew;
-        
+
+        // Read volume buttons
+        bVMNew = buttonVolMore;
+        bVLNew = buttonVolLess;
+        if(bVMNew && !bVMOld) {
+            volume++;
+            if(volume>3) volume=3;
+            ledBuzzer = 1;
+            out.clear();
+            out.printf("New volume: %d\r\n", volume);
+            out.sleep(2);
+        }
+        if(bVLNew && !bVLOld) {
+            if(volume>0) volume--;
+            if(!volume) ledBuzzer = 0;
+            out.clear();
+            out.printf("New volume: %d\r\n", volume);
+            out.sleep(2);
+        }
+        bVMOld = bVMNew;
+        bVLOld = bVLNew;
+
+
+        // Output
         unsigned long current_time = t.read_ms();
         
         if (current_time - last_shown > CYCLE_MS)
         {
-            players.showAll(myTeam, level);
+            players.showAll(myTeam, level, volume);
             last_shown = current_time;
         }
         
+        // Radios 
         if (current_time - last_send > SEND_RATE_MS)
         {
             // Send message
@@ -291,7 +329,7 @@
             //out.printf("T: %d, G: %d\r\n", tempC, gain);
             //if(freq!=868000000) out.printf("Freq: %d\r\n", freq);
             
-            //beep(NOTE_A3, 0.05);
+            //beep(NOTE_A3, 0.05, volume);
         }
 
         if (radio.receiveDone())
@@ -312,7 +350,7 @@
                 //out.printf("Other: %s\r\n", other_name);
                 //out.printf("RSSI: %d, G: %d\r\n", radio.RSSI, gain);
                 
-                //beep(NOTE_A5, 0.5);
+                //beep(NOTE_A5, 0.5, volume);
             } else {    // received unknown signal 
                 uint8_t gain = (radio.readReg(REG_LNA) & 0b111000)>>3;   // LNA Current Gain
                 out.clear();