aboutsummaryrefslogtreecommitdiff
path: root/serializer/f2b.c
diff options
context:
space:
mode:
authorMartin Peach <mrpeach@users.sourceforge.net>2013-03-13 19:02:25 +0000
committerMartin Peach <mrpeach@users.sourceforge.net>2013-03-13 19:02:25 +0000
commit812922a639c8540d5ef78d9fcdbabd23d09d9025 (patch)
tree5fd34615595bb3dc9df959193727098d243741af /serializer/f2b.c
parent29f3ad712f9c029120da2aabd123c7a513756f12 (diff)
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
Diffstat (limited to 'serializer/f2b.c')
-rw-r--r--serializer/f2b.c63
1 files changed, 63 insertions, 0 deletions
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 <string.h>
+
+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 */
+