Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of football_project by
Diff: proto_code.cpp
- Revision:
- 70:bd4b1e19a0c6
- Parent:
- 67:5650f461722a
- Child:
- 72:84365ba93d6b
--- a/proto_code.cpp Fri Feb 12 21:13:14 2016 +0000
+++ b/proto_code.cpp Mon Feb 22 09:48:42 2016 +0000
@@ -1,6 +1,7 @@
#include <TA.h>
#include <types.h>
#include "DataStore.hh"
+#include "Radio.hh"
#define RED p3
#define GREEN p5
@@ -44,7 +45,8 @@
TA ta;
-static bool active_cones[NUM_CONES + 1]; // + 1 so we can be 1 based like the cone numbers are
+static bool active_cones[NUM_CONES + 1]; // + 1 so we can be 1 based like the cone numbers are
+static uint8_t batlev_cones[NUM_CONES + 1]; // + 1 so we can be 1 based like the cone numbers are
const static int CONTROL_CONE = 1;
@@ -112,6 +114,7 @@
void resetSensors(void);
void successCones(void);
void find_cones(void);
+void get_cones_batt_levels(void); ////
void printMsAsSeconds(unsigned long number);
void spinButtons(void);
uint8_t checkButtons(void);
@@ -147,11 +150,13 @@
int i;
+ batlev_cones[1] = getLastBattLevel();
for(i = 1; i < NUM_CONES + 1; ++i)
{
active_cones[i] = true; /// false;
+ batlev_cones[i] = 0x80; // Uninited value;
}
-
+
unsigned long time = 0;
cone_table = datastore_get_cones();
@@ -295,7 +300,8 @@
if(message == 'z')
{
- m->value = m_in->value;
+ ////m->value = m_in->value;
+ m->value = getLastBattLevel();
m->command = 'z';
m->cone = 1;
//// ta.send(m);
@@ -746,7 +752,12 @@
SL_DEBUG("MS: %d\r\n", datastore_is_master());
SL_DEBUG("RP: %d\r\n", read_params());
break;
- }
+#ifdef D_RA
+ case 'R':
+ read_all_regs();
+ break;
+#endif
+ }
}
static void interpret_master(char parameter, int value)
@@ -982,6 +993,14 @@
case 'z':
find_cones();
break;
+ case 'b':
+ get_cones_batt_levels();
+ break;
+#ifdef D_RA
+ case 'R':
+ read_all_regs();
+ break;
+#endif
}
}
@@ -1141,6 +1160,8 @@
static void find_cones(void)
{
+ const int TRIES = 6;
+
while(ta.get_buffer_size())
{
ta.spin(); // wait for all messages to leave queue
@@ -1157,80 +1178,39 @@
writeToPhone("FC\r\n");
}
- int retry_count = 0;
-
- for(i = 2; i < NUM_CONES + 1; ++i)
+ for( i=2; i <= NUM_CONES; ++i )
{
- retry_count = 0;
-go_again:
active_cones[i] = false;
m->cone = i;
- ta.send_immediate(m);
- unsigned long st = millis();
- unsigned long delta = 0L;
- /* ///
- unsigned long current = 0L;
-
- if(Dbg)
- {
- writeToPhone("S: %lu F: %d\r\n", st, i);
- }
-
- while(1)
+ for( int retry_count=0; retry_count < TRIES; retry_count++ )
{
- current = millis();
- delta = current - st;
-
- if(delta > 300L)
+ unsigned long st = millis();
+ unsigned long delta = 0L;
+
+ ta.send_immediate(m);
+
+ while( 1 )
{
- if(Dbg)
- {
- writeToPhone("FT: %d %lu\r\n", i, current);
- }
-
- if (++retry_count < 3)
+ delta = millis() - st;
+ if( delta > 21 ) break;
+ ta.spin();
+ if( ta.recieve( m_in ) )
{
- goto go_again;
- }
- else
- {
- break;
+ lonely = false;
+ active_cones[m_in->cone] = true;
+ DEBUG( "Reply from cone: %d, %dms try %d\r\n", m_in->cone, delta, retry_count+1 );
+ retry_count = TRIES;
+ break;
}
}
- if(ta.recieve(m_in))
- {
- lonely = false;
- active_cones[m_in->cone] = true;
-
- if(Dbg)
- {
- writeToPhone("FS: %d\r\n", m_in->cone);
- }
- break;
- }
- }
-
- wait_us(50);
- */ ///
- while( 1 ) ////
- {
- delta = millis() - st;
- if( delta > 50 ) break;
- ta.spin();
- if( ta.recieve( m_in ) )
- {
- lonely = false;
- active_cones[m_in->cone] = true;
- DEBUG( "Reply from cone: %d, %dms\r\n", m_in->cone, delta );
- break;
- }
+ do{ ta.spin(); } while( ta.recieve( m_in ) ); // Clear receive buffer.
}
}
writeToPhone("Available cones are: (1");
- for(i = 2; i < NUM_CONES + 1; ++i)
+ for( i=2; i < NUM_CONES +1; ++i )
{
if(active_cones[i])
{
@@ -1241,6 +1221,72 @@
writeToPhone(")\r\n");
}
+// Get cones' battery levels. Also does what find_cones() does.
+static void get_cones_batt_levels(void)
+{
+ const int TRIES = 6;
+
+ while(ta.get_buffer_size())
+ {
+ ta.spin(); // wait for all messages to leave queue
+ }
+
+ ta.beep_off();
+ uint8_t i;
+ m->command = 'z';
+
+ lonely = true;
+
+ batlev_cones[1] = getLastBattLevel();
+ for( i = 2; i <= NUM_CONES; ++i )
+ {
+ active_cones[i] = false;
+ batlev_cones[i] = 0x80; // Uninited/unavailable value
+ m->cone = i;
+
+ for( int retry_count=0; retry_count < TRIES; retry_count++ )
+ {
+ unsigned long st = millis();
+ unsigned long delta = 0L;
+
+ ta.send_immediate(m);
+
+ while( 1 )
+ {
+ delta = millis() - st;
+ if( delta > 21 ) break;
+ ta.spin();
+ if( ta.recieve( m_in ) )
+ {
+ if( m_in->command == 'z' )
+ {
+ lonely = false;
+ active_cones[m_in->cone] = true;
+
+ batlev_cones[m_in->cone] = (uint8_t)m_in->value; // Get battery level.
+ RA_DEBUG( "Batt from cone: %d, %dms try %d\r\n", m_in->cone, delta, retry_count+1 );
+ retry_count = TRIES;
+ break;
+ }
+ }
+ }
+ do{ ta.spin(); } while( ta.recieve( m_in ) ); // Clear receive buffer.
+ }
+ }
+
+ writeToPhone( "Cone battery levels: (1:%u", batlev_cones[1] );
+
+ for( i=2; i < NUM_CONES +1; ++i )
+ {
+ if( active_cones[i] )
+ {
+ writeToPhone( ", %u:%u", i, batlev_cones[i] );
+ }
+ }
+
+ writeToPhone(")\r\n");
+}
+
static void printMsAsSeconds(unsigned long number)
{
uint16_t remainder;
