Skip to content

Commit

Permalink
rand: add new random blocks
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Klotzbuecher <mk@mkio.de>
  • Loading branch information
kmarkus committed May 19, 2020
1 parent 21ff5d6 commit 38f2b84
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ std_blocks/trig/Makefile
std_blocks/random/Makefile
std_blocks/const/Makefile
std_blocks/ramp/Makefile
std_blocks/rand/Makefile
std_blocks/simple_fifo/Makefile
std_blocks/webif/Makefile
std_types/Makefile
Expand Down
1 change: 1 addition & 0 deletions std_blocks/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ SUBDIRS = cppdemo \
random \
const \
ramp \
rand \
simple_fifo \
webif
25 changes: 25 additions & 0 deletions std_blocks/rand/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
ubxmoddir = $(UBX_MODDIR)

AM_CFLAGS = -I$(top_srcdir)/libubx $(UBX_CFLAGS)

ubxmod_LTLIBRARIES = rand_float.la rand_double.la rand_uint32.la rand_int32.la

rand_float_la_SOURCES = rand.c
rand_float_la_LDFLAGS = -module -avoid-version -shared -export-dynamic
rand_float_la_LIBADD = $(top_builddir)/libubx/libubx.la
rand_float_la_CPPFLAGS = -DRAND_FLOAT_T=1

rand_double_la_SOURCES = rand.c
rand_double_la_LDFLAGS = -module -avoid-version -shared -export-dynamic
rand_double_la_LIBADD = $(top_builddir)/libubx/libubx.la
rand_double_la_CPPFLAGS = -DRAND_DOUBLE_T=1

rand_uint32_la_SOURCES = rand.c
rand_uint32_la_LDFLAGS = -module -avoid-version -shared -export-dynamic
rand_uint32_la_LIBADD = $(top_builddir)/libubx/libubx.la
rand_uint32_la_CPPFLAGS = -DRAND_UINT32_T=1

rand_int32_la_SOURCES = rand.c
rand_int32_la_LDFLAGS = -module -avoid-version -shared -export-dynamic
rand_int32_la_LIBADD = $(top_builddir)/libubx/libubx.la
rand_int32_la_CPPFLAGS = -DRAND_INT32_T=1
144 changes: 144 additions & 0 deletions std_blocks/rand/rand.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* rand microblx function block
*/

#if RAND_FLOAT_T == 1
# define RAND_T float
#elif RAND_DOUBLE_T == 1
# define RAND_T double
#elif RAND_UINT32_T == 1
# define RAND_T uint32_t
#elif RAND_INT32_T == 1
# define RAND_T int32_t
#else
# error "no type defined"
#endif

#ifndef BLOCK_NAME
#define BLOCK_NAME RAND_T
#endif

#include <stdlib.h>
#include <string.h>
#include <ubx.h>

/* block meta information */
char rand_meta[] =
" { doc='" QUOTE(RAND_T) " random number generator block',"
" realtime=true,"
"}";

/* declaration of block configuration */
#define SEED "seed"

ubx_config_t rand_config[] = {
{ .name = SEED, .type_name = "long", .doc = "seed to initialize with" },
{ 0 },
};

/* declaration port block ports */
#define OUT "out"
ubx_port_t rand_ports[] = {
{ .name = OUT, .out_type_name = QUOTE(RAND_T), .out_data_len = 1, .doc = "rand generator output" },
{ 0 },
};

/* define a structure for holding the block local state. By assigning an
* instance of this struct to the block private_data pointer (see init), this
* information becomes accessible within the hook functions.
*/
struct rand_info {
ubx_port_t *p_out;
};


/* init */
int rand_init(ubx_block_t *b)
{
long len;
const long *seed;
struct rand_info *inf;

/* allocate memory for the block local state */
b->private_data = calloc(1, sizeof(struct rand_info));
inf = (struct rand_info *)b->private_data;

if (b->private_data == NULL) {
ubx_err(b, "rand: failed to alloc memory");
return EOUTOFMEM;
}

/* seed */
len = cfg_getptr_long(b, SEED, &seed);
assert(len >= 0);
srand48((len > 0) ? *seed : 0);

inf->p_out = ubx_port_get(b, OUT);
assert(inf->p_out);

return 0;
}


/* cleanup */
void rand_cleanup(ubx_block_t *b)
{
free(b->private_data);
}

/* step */
void rand_step(ubx_block_t *b)
{
struct rand_info *inf = (struct rand_info *)b->private_data;
#if RAND_FLOAT_T == 1
RAND_T val = drand48();
write_float(inf->p_out, &val);
#elif RAND_DOUBLE_T == 1
double val = drand48();
write_double(inf->p_out, &val);
#elif RAND_UINT32_T == 1
uint32_t val = lrand48();
write_uint32(inf->p_out, &val);
#elif RAND_INT32_T == 1
int32_t val = mrand48();
write_int32(inf->p_out, &val);
#else
# error "unsupported type " QUOTE(RAND_T)
#endif


}

/* put everything together */
ubx_block_t rand_block = {
.name = "rand_" QUOTE(BLOCK_NAME),
.type = BLOCK_TYPE_COMPUTATION,
.meta_data = rand_meta,
.configs = rand_config,
.ports = rand_ports,

/* ops */
.init = rand_init,
.cleanup = rand_cleanup,
.step = rand_step,
};


/* rand module init and cleanup functions */
int rand_mod_init(ubx_node_info_t *ni)
{
return ubx_block_register(ni, &rand_block);
}

void rand_mod_cleanup(ubx_node_info_t *ni)
{
ubx_block_unregister(ni, "rand_" QUOTE(BLOCK_NAME));
}

/*
* declare module init and cleanup functions, so that the ubx core can
* find these when the module is loaded/unloaded
*/
UBX_MODULE_INIT(rand_mod_init)
UBX_MODULE_CLEANUP(rand_mod_cleanup)
UBX_MODULE_LICENSE_SPDX(BSD-3-Clause)

0 comments on commit 38f2b84

Please sign in to comment.