aboutsummaryrefslogtreecommitdiff
path: root/xbee/packxbee.c
diff options
context:
space:
mode:
Diffstat (limited to 'xbee/packxbee.c')
-rw-r--r--xbee/packxbee.c50
1 files changed, 41 insertions, 9 deletions
diff --git a/xbee/packxbee.c b/xbee/packxbee.c
index 33a0201..23c0343 100644
--- a/xbee/packxbee.c
+++ b/xbee/packxbee.c
@@ -445,9 +445,26 @@ static void packxbee_pack_remote_frame(t_packxbee *x, t_symbol *s, int argc, t_a
if (argv[4].a_type == A_SYMBOL)
{
if (x->x_verbosity > 0) post("packxbee_pack_remote_frame symbol parameter %s", argv[4].a_w.w_symbol->s_name);
+
if (('0' == argv[4].a_w.w_symbol->s_name[0])&&(('x' == argv[4].a_w.w_symbol->s_name[1])))
- { /* this is a hexadecimal number: strip the "0x" and copy the rest to the buffer as ascii digits */
- i += sprintf((char *)&floatstring[i], "%s", &argv[4].a_w.w_symbol->s_name[2]);
+ { /* this is a hexadecimal number: copy to the buffer as raw binary */
+ result = sscanf(argv[4].a_w.w_symbol->s_name, "0x%X", &d);
+ if (result == 0)
+ {
+ post("packxbee_pack_remote_frame: argument 4 is not a hex string");
+ }
+ else
+ {
+ // put the significant part of the raw value into floatstring in big endian order
+ if (0 != ((d>>24) & 0x0FF)) digits = 4;
+ else if (0 != ((d>>16) & 0x0FF)) digits = 3;
+ else if (0 != ((d>>8) & 0x0FF)) digits = 2;
+ else digits = 1;
+ if (4 == digits) floatstring[i++] = (d>>24) & 0x0FF;
+ if (3 <= digits) floatstring[i++] = (d>>16) & 0x0FF;
+ if (2 <= digits) floatstring[i++] = (d>>8) & 0x0FF;
+ floatstring[i++] = d & 0x0FF;
+ }
}
else // if ((0 == strncmp("NI", argv[0].a_w.w_symbol->s_name, 2))||(0 == strncmp("DN", argv[0].a_w.w_symbol->s_name, 2)))
{ /* we hope it's just an ascii string for the NI command */
@@ -455,7 +472,6 @@ static void packxbee_pack_remote_frame(t_packxbee *x, t_symbol *s, int argc, t_a
{
c = argv[4].a_w.w_symbol->s_name[k];
if (0 == c) break;
- //checksum -= c;
floatstring[i++] = c;
}
}
@@ -464,10 +480,10 @@ static void packxbee_pack_remote_frame(t_packxbee *x, t_symbol *s, int argc, t_a
{
f = argv[4].a_w.w_float;
if (x->x_verbosity > 0) post("packxbee_pack_remote_frame float parameter %f", f);
- d = ((unsigned int)f)&0x0FF;
+ d = (unsigned int)f;
if (f != d)
{
- post ("packxbee_pack_remote_frame parameter not a positive integer from 0 to 255");
+ post ("packxbee_pack_remote_frame parameter not an integer");
}
else
{
@@ -528,6 +544,7 @@ static void packxbee_pack_frame(t_packxbee *x, t_symbol *s, int argc, t_atom *ar
unsigned char floatstring[256]; /* longer than the longest hex number with each character escaped plus the header and checksum overhead */
int length = 0;
unsigned char c, digits;
+ int result;
t_float f;
if (x->x_verbosity > 0) post("packxbee_AT s is %s, argc is %d", s->s_name, argc);
@@ -574,7 +591,23 @@ static void packxbee_pack_frame(t_packxbee *x, t_symbol *s, int argc, t_atom *ar
if (x->x_verbosity > 0) post("packxbee_AT symbol parameter %s", argv[1].a_w.w_symbol->s_name);
if (('0' == argv[1].a_w.w_symbol->s_name[0])&&(('x' == argv[1].a_w.w_symbol->s_name[1])))
{ /* this is a hexadecimal number: strip the "0x" and copy the rest to the buffer as ascii digits */
- i += sprintf((char *)&floatstring[i], "%s", &argv[1].a_w.w_symbol->s_name[2]);
+ result = sscanf(argv[1].a_w.w_symbol->s_name, "0x%X", &d);
+ if (result == 0)
+ {
+ post("packxbee_pack_remote_frame: argument 1 is not a hex string");
+ }
+ else
+ {
+ // put the significant part of the raw value into floatstring in big endian order
+ if (0 != ((d>>24) & 0x0FF)) digits = 4;
+ else if (0 != ((d>>16) & 0x0FF)) digits = 3;
+ else if (0 != ((d>>8) & 0x0FF)) digits = 2;
+ else digits = 1;
+ if (4 == digits) floatstring[i++] = (d>>24) & 0x0FF;
+ if (3 <= digits) floatstring[i++] = (d>>16) & 0x0FF;
+ if (2 <= digits) floatstring[i++] = (d>>8) & 0x0FF;
+ floatstring[i++] = d & 0x0FF;
+ }
}
else // if ((0 == strncmp("NI", argv[0].a_w.w_symbol->s_name, 2))||(0 == strncmp("DN", argv[0].a_w.w_symbol->s_name, 2)))
{ /* we hope it's just an ascii string for the NI command */
@@ -582,7 +615,6 @@ static void packxbee_pack_frame(t_packxbee *x, t_symbol *s, int argc, t_atom *ar
{
c = argv[1].a_w.w_symbol->s_name[k];
if (0 == c) break;
-// checksum -= c;
floatstring[i++] = c;
}
}
@@ -591,10 +623,10 @@ static void packxbee_pack_frame(t_packxbee *x, t_symbol *s, int argc, t_atom *ar
{
f = argv[1].a_w.w_float;
if (x->x_verbosity > 0) post("packxbee_AT float parameter %f", f);
- d = ((unsigned int)f)&0x0FF;
+ d = (unsigned int)f;
if (f != d)
{
- post ("packxbee_AT parameter not a positive integer from 0 to 255");
+ post ("packxbee_AT parameter not an integer");
}
else
{