Skip to content

Commit baa19f5

Browse files
author
jenkie
committed
Serial communication rewritten
Two commands which can be sent over serial interface are implemented: - send ps? to read poti_stat and psN to write poti_stat, where N is a number - send od? to read total kilometers and send odN to write total kilometers where N is a number
1 parent 02692b2 commit baa19f5

File tree

2 files changed

+26
-62
lines changed

2 files changed

+26
-62
lines changed

Arduino_Pedelec_Controller/Arduino_Pedelec_Controller.ino

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,7 @@ byte mmc_value=0;
195195
boolean mmc_nextisvalue=false;
196196
#endif
197197

198-
#if (SERIAL_MODE & SERIAL_MODE_ANDROID)
199198
char inchar = 0; //curent read char
200-
#endif
201199

202200
//declarations for profile switching
203201
int curr_startingaid_speed=startingaid_speed;
@@ -353,13 +351,11 @@ void loop()
353351
display_update();
354352
#endif
355353

356-
#if (SERIAL_MODE & SERIAL_MODE_ANDROID)
357354
if (Serial.available() > 0)
358355
{
359356
inchar = Serial.read();
360357
next(inchar);
361358
}
362-
#endif
363359

364360
#ifdef SUPPORT_THROTTLE
365361
throttle_stat = constrain(map(analogRead(throttle_in),throttle_offset,throttle_max,0,1023),0,1023); // 0...1023

Arduino_Pedelec_Controller/bluetooth.cpp

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
2725
char firstchar = 0;
28-
const byte numberlength = 12;
26+
const byte numberlength = 5;
2927
char numberstring[numberlength];
3028
byte num_i=0;
3129
byte validcommand = 0;
3230
byte 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

5542
void 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

Comments
 (0)