football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Revision:
79:9db29f41dc9d
Parent:
78:43a6b54f0372
Child:
80:8d4f190bd253
--- a/main.cpp	Mon May 09 01:08:01 2016 +0000
+++ b/main.cpp	Wed May 25 05:02:53 2016 +0000
@@ -8,32 +8,27 @@
 Output out;
 
 ///////////////////////////////////  PINS   ///////////////////////////////////
+
+DigitalIn buttonTeam(BUT_TEAM);
+DigitalIn buttonSpace(BUT_SPACE);
+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);
+AnalogIn ain(ANALOG_IN);    // used for randomizing  
 #ifdef NORDIC
-//PwmOut speaker(PB_8);     // TODO: add sound for Nordic
-DigitalIn din(BTN1);
-AnalogIn ain(P0_1);       // used for randomizing
-#else // not NORDIC
-#ifdef NUCLEO
-PwmOut speaker(PB_8);
-DigitalIn din(USER_BUTTON);
-AnalogIn ain(A5);       // used for randomizing
-#else // LPC assumed
-PwmOut speaker(p21);
-DigitalIn din(p5);
-AnalogIn ain(p15);      // used for randomizing
-#endif // NUCLEO
-#endif // NORDIC
+DigitalOut buzzer(BUZZER);
+DigitalOut buzzLow(BUZZ_LOW);
+DigitalOut buzzMed(BUZZ_MED);
+DigitalOut buzzHigh(BUZZ_HIGH);
+#else
+PwmOut speaker(BUZZER);     // passive buzzer
+#endif
 
 ///////////////////////////////////  RADIO  ///////////////////////////////////
-#ifdef NORDIC
-static RFM69 radio(P0_20,P0_22,P0_25,P0_24,P0_23);      // Update according to your setup
-#else // not NORDIC
-#ifdef NUCLEO
-static RFM69 radio(D11,D12,D13,D10,D9);
-#else // LPC assumed
-static RFM69 radio(p11,p12,p13,p10,p9);
-#endif // NUCLEO
-#endif // NORDIC
+static RFM69 radio(RFM_MOSI, RFM_MISO, RFM_SCK, RFM_SS, RFM_IRQ);
 
 static bool promiscuousMode = true; // set 'true' to sniff all packets on the same network
 
@@ -41,17 +36,31 @@
 
 void beep(float period, float time) { 
 #ifdef ENABLE_SOUND
+#ifdef NORDIC
+    if(period>=NOTE_A3) {
+        buzzLow = 1; buzzMed = 0; buzzHigh = 0;
+    } else if(period>=NOTE_A4) {
+        buzzLow = 0; buzzMed = 1; buzzHigh = 0;
+    } else {
+        buzzLow = 0; buzzMed = 0; buzzHigh = 1;
+    }
+    buzzer = 1;
+    wait(time);
+    buzzer = 0;
+#else
     speaker.period(period);
     speaker =0.5; //50% duty cycle - max volume
     wait(time);
     speaker=0.0; // turn off audio
 #endif
+#endif
 }
 
 void binary_sound(int z) {
     // Beeps numbers according to their binary form
     // (used for debugging in display-less and serial-less environments)
 #ifdef ENABLE_SOUND
+#ifndef NORDIC
     speaker.period(0.004);
     while(z) {
         speaker = 0.5;
@@ -61,7 +70,8 @@
         wait(1.0);
         z >>= 1;
     }
-    beep(0.002, 1.0);
+    beep(NOTE_A4, 1.0);
+#endif
 #endif
 }
 
@@ -140,23 +150,33 @@
         }
     }
     
-    void show_all(int8_t team) {
-        // Show the current information table
-        int i=0, contacts, dist;
+    void showAll(int8_t team, uint8_t level) {
+        // Output the current state to the user:
+        //   - show the current information table and/or
+        //   - beep if the user is too close to another one
+        int i=0, nContacts, signal, maxTeamSignal = -128;
         list<Player>::iterator it;
         out.clear();
         for (it=players.begin(); it != players.end(); ++it) {
-            dist = it->get_distance(contacts);
-            if(dist>-128) {
+            signal = it->get_distance(nContacts);
+            if(signal>-128) {
                 out.printf("%d ", ++i);
                 out.printf((team==it->team) ? "+" : "-");       // teammate or opponent?
                 out.printf("%s ", it->name);
-                out.printf("%d/%d\r\n", -dist, contacts);
+                out.printf("%d/%d", -signal, nContacts);
+                out.printf((-signal<SPACE[level]) ? "!" : " ");
+                out.printf("\r\n");
+            }
+            if(team==it->team && signal>maxTeamSignal) {
+                maxTeamSignal = signal;
             }
         } 
-        if(!i) {
-            out.printf("Nobody around\r\n");
-            beep(0.001,0.5);
+        //if(!i) {
+        //    out.printf("Nobody around\r\n");
+        //    beep(NOTE_A5,0.5);
+        //}
+        if(-maxTeamSignal<SPACE[level]) {
+            beep(NOTE_A4,0.33);
         }
     }
 };
@@ -169,12 +189,20 @@
     char rx_buff[100] = {0};
     char rand_name[NAME_LEN+1] = {0};
     char other_name[NAME_LEN+1] = {0};
-    int8_t team = 1, other_team;
+    int8_t myTeam = 1, otherTeam;
+    uint8_t level = 1;
     
-    beep(0.002, 0.5);
+    beep(NOTE_A5, 0.5);
 
-    din.mode(PullUp);    
-    int button_old = 1, button_new;
+    buttonSpace.mode(PullDown);
+#ifdef NORDIC
+    buttonTeam.mode(PullDown);
+    int bTeamOld = 0;
+#else
+    buttonTeam.mode(PullUp);
+    int bTeamOld = 1;
+#endif
+    int bTeamNew, bSpaceOld=0, bSpaceNew;
     
     char this_node = int(ain.read()*255+17)*int(ain.read()*255+11); // random node value
     out.printf("Node: %d\r\n", this_node);
@@ -193,6 +221,11 @@
     out.sleep(2.0);
         
     t.start();
+    ledTeamA = myTeam & 1;
+    ledTeamB = ~myTeam & 1;
+#ifdef NORDIC
+    buzzMed = 1;
+#endif
     
     Players players;
     
@@ -203,27 +236,49 @@
 
     while (true)
     {
-        button_new = din;
-        if(!button_new && button_old) {
-            team = (team==1) ? 2 : 1;
+        // Read team button
+        bTeamNew = buttonTeam;
+        if(bTeamNew==PRESSED && bTeamOld==RELEASED) {
+            myTeam = (myTeam==1) ? 2 : 1;
+            ledTeamA = myTeam & 1;
+            ledTeamB = ~myTeam & 1;
             out.clear();
-            out.printf("New team: %d\r\n", team);
+            out.printf("New team: %d\r\n", myTeam);
             out.sleep(2);
         }
-        button_old = button_new;
+        bTeamOld = bTeamNew;
+        
+        // Read space button
+        bSpaceNew = buttonSpace;
+        if(bSpaceNew && !bSpaceOld) {
+            level = (level+1) & 0b11;   // four states
+            if(level<=0) {
+                ledSpace5 = 1; ledSpace10 = 0; ledSpace15 = 0; ledSpace20 = 0;
+            } else if(level<=1) {
+                ledSpace5 = 0; ledSpace10 = 1; ledSpace15 = 0; ledSpace20 = 0;
+            } else if(level<=2) {
+                ledSpace5 = 0; ledSpace10 = 0; ledSpace15 = 1; ledSpace20 = 0;
+            } else {
+                ledSpace5 = 0; ledSpace10 = 0; ledSpace15 = 0; ledSpace20 = 1;
+            }
+            out.clear();
+            out.printf("New level: %d\r\n", level);
+            out.sleep(2);
+        }
+        bSpaceOld = bSpaceNew;
         
         unsigned long current_time = t.read_ms();
         
-        if (current_time - last_shown > 2000L)
+        if (current_time - last_shown > CYCLE_MS)
         {
-            players.show_all(team);
+            players.showAll(myTeam, level);
             last_shown = current_time;
         }
         
         if (current_time - last_send > SEND_RATE_MS)
         {
             // Send message
-            snprintf(tx_buff, sizeof(tx_buff), "N=%s,%d", rand_name, team);
+            snprintf(tx_buff, sizeof(tx_buff), "N=%s,%d", rand_name, myTeam);
             radio.send(EVERY_NODE, tx_buff, strlen(tx_buff));
             last_send = current_time;
             //send_wait = min_wait + (rand() % SEND_RATE_MS)*SEND_DESYNC;
@@ -236,7 +291,7 @@
             //out.printf("T: %d, G: %d\r\n", tempC, gain);
             //if(freq!=868000000) out.printf("Freq: %d\r\n", freq);
             
-            //beep(0.003, 0.05);
+            //beep(NOTE_A3, 0.05);
         }
 
         if (radio.receiveDone())
@@ -248,16 +303,16 @@
                 memcpy(other_name, rx_buff+2, NAME_LEN);
                 other_name[5] = 0x00;
                 if(sizeof(rx_buff)>8 && rx_buff[7]==',') {
-                    other_team = rx_buff[8]-'0';
-                } else other_team = 1;
-                players.update(other_name, other_team, radio.RSSI, t.read_ms());
+                    otherTeam = rx_buff[8]-'0';
+                } else otherTeam = 1;
+                players.update(other_name, otherTeam, radio.RSSI, t.read_ms());
 
                 //uint8_t gain = (radio.readReg(REG_LNA) & 0b111000)>>3;   // LNA Current Gain
                 //out.clear();
                 //out.printf("Other: %s\r\n", other_name);
                 //out.printf("RSSI: %d, G: %d\r\n", radio.RSSI, gain);
                 
-                //beep(0.001, 0.5);
+                //beep(NOTE_A5, 0.5);
             } else {    // received unknown signal 
                 uint8_t gain = (radio.readReg(REG_LNA) & 0b111000)>>3;   // LNA Current Gain
                 out.clear();