@@ -21,56 +21,32 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2121#include " bluetooth.h"
2222#include " globals.h"
2323
24- #if (SERIAL_MODE & SERIAL_MODE_ANDROID)
25- // Serial comm variables
26-
24+ extern boolean variables_saved;
2725char firstchar = 0 ;
28- const byte numberlength = 12 ;
26+ const byte numberlength = 5 ;
2927char numberstring[numberlength];
3028byte num_i=0 ;
3129byte validcommand = 0 ;
3230byte i = 0 ;
3331
34- byte com_i;
35- byte com_type; // 0=float, 1=int
36- float samplefloat;
37-
38- struct serial_float { char mnemonic[3 ]; float *pointer;};
39- serial_float sc_float[] =
40- {
41- {{" sf" },&samplefloat},
42- };
43- int n_float = sizeof (sc_float)/sizeof (serial_float);
32+ byte com_i; // index of current command
4433
45- struct serial_int { char mnemonic[3 ]; int *pointer ;};
46- serial_int sc_int [] =
34+ struct serial_command { char mnemonic[3 ];};
35+ serial_command serial_commands [] =
4736{
48- {{" ps" },&poti_stat}, // poti stat //at the moment the only valid command :) gets and sets poti stat
37+ {{" ps" }}, // 0: poti stat, gets and sets poti stat
38+ {{" od" }}, // 1: total kilometers (odo), gets and sets poti stat
4939};
50- int n_int = sizeof (sc_int)/sizeof (serial_int);
51-
52-
53-
40+ int n_commands = sizeof (serial_commands)/sizeof (serial_command); // number of commands that we have
5441
5542void find_commands () // a command is always AA# or AA? where AA# sets # to variable AA and AA? returns value of AA
5643{
5744 validcommand = 0 ;
58- for (i=0 ; i < n_float ; i++) // try float mnemonics
59- {
60- if (sc_float[i].mnemonic [0 ] == firstchar && sc_float[i].mnemonic [1 ] == inchar)
61- {
62- com_i = i;
63- com_type = 0 ;
64- validcommand = 1 ;
65- return ;
66- }
67- }
68- for (i=0 ; i < n_int ; i++) // try int mnemonics
45+ for (i=0 ; i < n_commands ; i++) // try int mnemonics
6946 {
70- if (sc_int [i].mnemonic [0 ] == firstchar && sc_int [i].mnemonic [1 ] == inchar)
47+ if (serial_commands [i].mnemonic [0 ] == firstchar && serial_commands [i].mnemonic [1 ] == inchar)
7148 {
7249 com_i = i;
73- com_type = 1 ;
7450 validcommand = 1 ;
7551 return ;
7652 }
@@ -89,22 +65,19 @@ void next(char inchar)
8965 if (validcommand && numberstring[0 ] != ' \0 ' )
9066 {
9167 // if command and number, write now!
92- switch (com_type )
68+ switch (com_i )
9369 {
94- case 0 : // float
95- *sc_float[com_i].pointer = atof (numberstring);
96- Serial.print (sc_float[com_i].mnemonic );
97- Serial.println (*sc_float[com_i].pointer ,6 );
70+ case 0 : // poti_stat
71+ poti_stat = min (atoi (numberstring)*1023.0 /power_poti_max,1023 );
9872 break ;
99- case 1 : // int
100- if (com_i==0 ) // poti-stat!
101- *sc_int[com_i].pointer = min (atoi (numberstring)*1023.0 /power_poti_max,1023 );
102- else
103- *sc_int[com_i].pointer = atoi (numberstring);
104- Serial.print (sc_int[com_i].mnemonic );
105- Serial.println (*sc_int[com_i].pointer );
73+ case 1 : // total kilometers
74+ odo = atoi (numberstring)*1000.0 /wheel_circumference;
75+ variables_saved=false ;
76+ save_eeprom ();
10677 break ;
10778 }
79+ Serial.print (serial_commands[com_i].mnemonic );
80+ Serial.println (MY_F (" OK" ));
10881 }
10982 validcommand = 0 ;
11083 firstchar = 0 ;
@@ -119,17 +92,15 @@ void next(char inchar)
11992 {
12093 if (inchar == 63 ) // "?"
12194 {
122- switch (com_type)
95+ validcommand = 0 ;
96+ Serial.print (serial_commands[com_i].mnemonic );
97+ switch (com_i)
12398 {
124- case 0 : // float
125- Serial.print (sc_float[com_i].mnemonic );
126- Serial.println (*sc_float[com_i].pointer ,6 );
127- validcommand = 0 ;
99+ case 0 : // poti_stat
100+ Serial.println (poti_stat);
128101 break ;
129- case 1 : // int
130- Serial.print (sc_int[com_i].mnemonic );
131- Serial.println (*sc_int[com_i].pointer );
132- validcommand = 0 ;
102+ case 1 : // total kilometers
103+ Serial.println (odo/1000.0 *wheel_circumference,0 );
133104 break ;
134105 }
135106 }
@@ -154,6 +125,3 @@ void next(char inchar)
154125}
155126
156127
157- #endif
158-
159-
0 commit comments