From 812922a639c8540d5ef78d9fcdbabd23d09d9025 Mon Sep 17 00:00:00 2001 From: Martin Peach Date: Wed, 13 Mar 2013 19:02:25 +0000 Subject: Two objects to convert between floats and their binary representation as bytes, so you can send floats through [comport] for example. svn path=/trunk/externals/mrpeach/; revision=17064 --- serializer/b2f-help.pd | 14 ++++++++ serializer/b2f.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ serializer/f2b-help.pd | 28 ++++++++++++++++ serializer/f2b.c | 63 ++++++++++++++++++++++++++++++++++++ 4 files changed, 193 insertions(+) create mode 100644 serializer/b2f-help.pd create mode 100644 serializer/b2f.c create mode 100644 serializer/f2b-help.pd create mode 100644 serializer/f2b.c diff --git a/serializer/b2f-help.pd b/serializer/b2f-help.pd new file mode 100644 index 0000000..fc5a295 --- /dev/null +++ b/serializer/b2f-help.pd @@ -0,0 +1,14 @@ +#N canvas 666 174 450 300 10; +#X obj 103 178 b2f; +#X msg 103 83 0 1 2 3; +#X text 77 53 [b2f] converts lists of 4 bytes to floats.; +#X text 43 17 Assuming you received 4 bytes as a binary representation +of a floating-point number \,; +#X text 234 258 Martin Peach 2013_03_13; +#X msg 181 83 208 15 73 64; +#X floatatom 103 209 12 0 0 0 - - -; +#X msg 269 83 219 15 73 64; +#X connect 0 0 6 0; +#X connect 1 0 0 0; +#X connect 5 0 0 0; +#X connect 7 0 0 0; diff --git a/serializer/b2f.c b/serializer/b2f.c new file mode 100644 index 0000000..91d1096 --- /dev/null +++ b/serializer/b2f.c @@ -0,0 +1,88 @@ +/* b2f.c MP 20130313 */ +/* Convert a list of 4 bytes to a Pd float */ +#include "m_pd.h" +#include + +typedef struct _b2f +{ + t_object x_obj; + t_outlet *x_out; +} t_b2f; + +static t_class *b2f_class; + +void b2f_setup(void); +static void *b2f_new(t_symbol *s, int argc, t_atom *argv); +static void b2f_free(t_b2f *x); +static void b2f_list(t_b2f *x, t_symbol *s, int argc, t_atom *argv); + +union fbuf +{ + t_float f; + unsigned char b[4]; +}; + +static void b2f_list(t_b2f *x, t_symbol *s, int argc, t_atom *argv) +{ + int i, d; + union fbuf buf; + + post("b2f_list: s is %s, argc is %d", s->s_name, argc); + if (0 != strncmp("list", s->s_name, 4)) + { + post("b2f_list: not a list of floats"); + return; + } + if (argc != 4) + { + post("b2f_list: need 4 floats"); + return; + } + for (i = 0; i < 4; ++i) + { + if (argv[i].a_type != A_FLOAT) + { + post("b2f_list: list element %d is not a float", i); + return; + } + d = argv[i].a_w.w_float; + if (d != argv[i].a_w.w_float) + { + post("b2f_list: list element %d is not an integer", i); + return; + } + if (d < 0 || d > 255) + { + post("b2f_list: list element %d is not an integer on [0..255]", i); + return; + } + buf.b[i] = d; + } + outlet_float(x->x_out, buf.f); +} + +static void b2f_free(t_b2f *x) +{ + return; +} + +static void *b2f_new(t_symbol *s, int argc, t_atom *argv) +{ + t_b2f *x; + + x = (t_b2f *)pd_new(b2f_class); + if (x == NULL) return (x); + x->x_out = outlet_new((t_object *)x, &s_float); + return (x); +} + +void b2f_setup(void) +{ + b2f_class = class_new(gensym("b2f"), + (t_newmethod)b2f_new, + (t_method)b2f_free, + sizeof(t_b2f), 0, 0); /* no arguments */ + class_addlist(b2f_class, b2f_list); +} +/* end b2f.c */ + diff --git a/serializer/f2b-help.pd b/serializer/f2b-help.pd new file mode 100644 index 0000000..3e7c7dd --- /dev/null +++ b/serializer/f2b-help.pd @@ -0,0 +1,28 @@ +#N canvas 626 544 450 300 10; +#X obj 57 151 f2b; +#X msg 57 118 3.14159; +#X obj 57 187 unpack 0 0 0 0; +#X floatatom 57 228 5 0 0 0 - - -; +#X floatatom 92 228 5 0 0 0 - - -; +#X floatatom 127 228 5 0 0 0 - - -; +#X floatatom 162 228 5 0 0 0 - - -; +#X text 244 268 Martin Peach 2013_03_13; +#X msg 135 118 100000; +#X text 53 8 [f2b] converts floats to lists of four bytes suitable +for sending through a serial connection. This is the binary representation +\, so the receiver must use the same floating-point format.; +#X obj 213 114 * 4; +#X floatatom 213 141 12 0 0 0 - - -; +#X obj 213 91 atan; +#X msg 213 68 1; +#X connect 0 0 2 0; +#X connect 1 0 0 0; +#X connect 2 0 3 0; +#X connect 2 1 4 0; +#X connect 2 2 5 0; +#X connect 2 3 6 0; +#X connect 8 0 0 0; +#X connect 10 0 11 0; +#X connect 10 0 0 0; +#X connect 12 0 10 0; +#X connect 13 0 12 0; diff --git a/serializer/f2b.c b/serializer/f2b.c new file mode 100644 index 0000000..a9ae19e --- /dev/null +++ b/serializer/f2b.c @@ -0,0 +1,63 @@ +/* f2b.c MP 20130313 */ +/* Convert a Pd float to a list of 4 bytes */ +#include "m_pd.h" +#include + +typedef struct _f2b +{ + t_object x_obj; + t_outlet *x_out; +} t_f2b; + +static t_class *f2b_class; + +void f2b_setup(void); +static void *f2b_new(t_symbol *s, int argc, t_atom *argv); +static void f2b_free(t_f2b *x); +static void f2b_float(t_f2b *x, t_float f); + +union fbuf +{ + t_float f; + unsigned char b[4]; +}; + +static void f2b_float(t_f2b *x, t_float f) +{ + int i; + union fbuf buf; + t_atom outs[4]; + + + post("f2b_float: f is %f", f); + buf.f = f; + for (i = 0; i < 4; ++i) SETFLOAT(&outs[i], buf.b[i]); + + outlet_list(x->x_out, gensym("list"), 4, outs); +} + +static void f2b_free(t_f2b *x) +{ + return; +} + +static void *f2b_new(t_symbol *s, int argc, t_atom *argv) +{ + t_f2b *x; + + x = (t_f2b *)pd_new(f2b_class); + if (x == NULL) return (x); + x->x_out = outlet_new((t_object *)x, &s_float); + return (x); +} + +void f2b_setup(void) +{ + f2b_class = class_new(gensym("f2b"), + (t_newmethod)f2b_new, + (t_method)f2b_free, + sizeof(t_f2b), 0, 0); /* no arguments */ + class_addfloat(f2b_class, f2b_float); +} +/* end f2b.c */ + -- cgit v1.2.1