Skip to content

Commit

Permalink
Refactor 'mcuicom_send' to elliminate the second argument.
Browse files Browse the repository at this point in the history
The first argument is a pointer to the data to be sent and the second
argument represents the size of that data. After refactoring, the
function no longer needs the size of the data to be provided, since it now
sends bytes until a null terminator is found.
  • Loading branch information
Jonan CM committed Dec 4, 2013
1 parent ab5012a commit fbe7fe7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
18 changes: 9 additions & 9 deletions gpcore/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "self_test.h"
#include "../mcuicom.h"
#include "mctlcom.h"
#include "../macros.h"

#include "../debug.h"
//#ifndef NDEBUG
Expand Down Expand Up @@ -1754,7 +1753,8 @@ inline void hostcmd_pa(void)
buf[0] = 'R';
buf[1] = param1.value.letter;
buf[2] = *CMDEND;
mcuicom_send(buf, 3);
buf[3] = '\0';
mcuicom_send(buf);
// Get response (motor steps) and re-send it to the host PC
size = mctlcom_get_response(buf, size);
hostcom_send(buf, size);
Expand Down Expand Up @@ -2965,7 +2965,7 @@ inline void hostcmd_mc(void)
}
else
{
mcuicom_send("MC" CMDEND, STRLEN("MC" CMDEND));
mcuicom_send("MC" CMDEND);
}
}

Expand Down Expand Up @@ -3007,7 +3007,7 @@ inline void hostcmd_mi(void)
}
else
{
mcuicom_send("MI" CMDEND, STRLEN("MI" CMDEND));
mcuicom_send("MI" CMDEND);
}
}

Expand Down Expand Up @@ -3040,7 +3040,7 @@ inline void hostcmd_mm(void)
buf[0] = 'S';
buf[1] = param1.value.letter;
buf[2] = *CMDEND;
mcuicom_send(buf, 3);
mcuicom_send(buf);
}
else
{
Expand Down Expand Up @@ -3258,7 +3258,7 @@ inline void hostcmd_pd(void)

// Send MCUICOM command AA, AB, ..., AF depending on motor letter (param 1)
snprintf(buf, size, "A%c,%d%c", param1.value.letter, intparam2, *CMDEND);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}
else
{
Expand Down Expand Up @@ -3345,7 +3345,7 @@ inline void hostcmd_pr(void)

// Send MCUICOM command BA, BB, ..., BF depending on motor letter (param 1)
snprintf(buf, size, "B%c,%d%c", param1.value.letter, intparam2, *CMDEND);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}
else
{
Expand Down Expand Up @@ -3444,7 +3444,7 @@ inline void hostcmd_px(void)
const int size = 64;
char buf[size];
snprintf(buf, size, "C%c,%.2f%c", param1.value.letter, (double) floatparam2, *CMDEND);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}
else
{
Expand Down Expand Up @@ -3545,7 +3545,7 @@ inline void hostcmd_py(void)
const int size = 64;
char buf[size];
snprintf(buf, size, "D%c,%.2f%c", param1.value.letter, (double) floatparam2, *CMDEND);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions mcuicom.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ inline void mcuicom_setup(void)
U1STAbits.UTXEN = 1;
}

int mcuicom_send(const char * const data, const int size)
int mcuicom_send(const char * const data)
{
int sent;
for (sent = 0; sent < size; ++sent)
for (sent = 0; data[sent] != '\0'; ++sent)
{
while (U1STAbits.UTXBF); // Wait while the transmit buffer is full
U1TXREG = data[sent];
Expand Down
2 changes: 1 addition & 1 deletion mcuicom.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extern buffer_t mcuicom_rcv_buf;
*/
inline void mcuicom_setup(void);

int mcuicom_send(const char * const data, const int size);
int mcuicom_send(const char * const data);

/**
* Copy the first fully received GPMCU command from the receive buffer to the
Expand Down
25 changes: 12 additions & 13 deletions motorctl/gpcorecom.c
Original file line number Diff line number Diff line change
Expand Up @@ -613,48 +613,47 @@ void interpret_cmd(void)
******************************************************************************/

#include <stdio.h>
#include <string.h>

inline void read_encoder_a(void)
{
char buf[64];
snprintf(buf, 64, "%d%c", motor_steps[MOTOR_A], *CMDEND);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}

inline void read_encoder_b(void)
{
char buf[64];
snprintf(buf, 64, "%d%c", motor_steps[MOTOR_B], *CMDEND);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}

inline void read_encoder_c(void)
{
char buf[64];
snprintf(buf, 64, "%d%c", motor_steps[MOTOR_C], *CMDEND);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}

inline void read_encoder_d(void)
{
char buf[64];
snprintf(buf, 64, "%d%c", motor_steps[MOTOR_D], *CMDEND);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}

inline void read_encoder_e(void)
{
char buf[64];
snprintf(buf, 64, "%d%c", motor_steps[MOTOR_E], *CMDEND);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}

inline void read_encoder_f(void)
{
char buf[64];
snprintf(buf, 64, "%d%c", motor_steps[MOTOR_F], *CMDEND);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}

inline void stop_motor_a(void)
Expand Down Expand Up @@ -1050,47 +1049,47 @@ inline void set_cartesian_abs_x(void)
// TODO: implement
char buf[64];
snprintf(buf, 64, "set_cartesian_abs_x: %f\n", (double) cartesian_desired_pos[COORD_X]);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}

inline void set_cartesian_abs_y(void)
{
// TODO: implement
char buf[64];
snprintf(buf, 64, "set_cartesian_abs_y: %f\n", (double) cartesian_desired_pos[COORD_Y]);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}

inline void set_cartesian_abs_z(void)
{
// TODO: implement
char buf[64];
snprintf(buf, 64, "set_cartesian_abs_z: %f\n", (double) cartesian_desired_pos[COORD_Z]);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}

inline void set_cartesian_rel_x(void)
{
// TODO: implement
char buf[64];
snprintf(buf, 64, "set_cartesian_rel_x: %f\n", (double) cartesian_desired_pos[COORD_X]);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}

inline void set_cartesian_rel_y(void)
{
// TODO: implement
char buf[64];
snprintf(buf, 64, "set_cartesian_rel_y: %f\n", (double) cartesian_desired_pos[COORD_Y]);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}

inline void set_cartesian_rel_z(void)
{
// TODO: implement
char buf[64];
snprintf(buf, 64, "set_cartesian_rel_z: %f\n", (double) cartesian_desired_pos[COORD_Z]);
mcuicom_send(buf, strlen(buf));
mcuicom_send(buf);
}

inline void move_independent(void)
Expand Down

0 comments on commit fbe7fe7

Please sign in to comment.