void loop() { printf("-------------------- GO ! --------------------\n"); int i = 0; while ( i >= 0 && !done ) { for ( int i=0; i<num_sources; i++ ) { mdev_poll( source_device_list[i], 0 ); } for ( int i=0; i<num_dests; i++ ) { mdev_poll( dest_device_list[i], 0 ); } usleep(50 * 1000); i++; } }
void wait_local_devices(int *cancel) { int keep_waiting = 1; while ( keep_waiting && !*cancel ) { keep_waiting = 0; for ( int i=0; i<num_sources; i++ ) { mdev_poll( source_device_list[i], 0 ); if ( !mdev_ready(source_device_list[i]) ) { keep_waiting = 1; } } for ( int i=0; i<num_dests; i++ ) { mdev_poll( dest_device_list[i], 0 ); if ( !mdev_ready(dest_device_list[i]) ) { keep_waiting = 1; } } } //mapper_db_dump(); }
void connect_signals() { char source_name[1024], destination_name[1024]; printf("%s\n", mdev_name(source)); printf("%s\n", mdev_name(destination)); lo_address a = lo_address_new_from_url("osc.udp://224.0.1.3:7570"); lo_address_set_ttl(a, 1); lo_send(a, "/link", "ss", mdev_name(source), mdev_name(destination)); int j = 50; while (j >= 0) { mdev_poll(source, 10); mdev_poll(destination, 10); j--; } msig_full_name(sendsig, source_name, 1024); msig_full_name(recvsig, destination_name, 1024); lo_send(a, "/connect", "ss", source_name, destination_name); lo_address_free(a); }
void connect_signals() { char source_name[1024], destination_name[1024]; printf("%s\n", mdev_name(source)); printf("%s\n", mdev_name(destination)); lo_address a = lo_address_new_from_url("osc.udp://224.0.1.3:7570"); lo_address_set_ttl(a, 1); lo_send(a, "/link", "ss", mdev_name(source), mdev_name(destination)); // wait for link to be processed int j = 50; while (j >= 0) { mdev_poll(source, 10); mdev_poll(destination, 10); j--; } msig_full_name(sendsig, source_name, 1024); msig_full_name(recvsig, destination_name, 1024); lo_send(a, "/connect", "ssssss", source_name, destination_name, "@mode", "expression", "@expression", "y=y{-1}+1"); lo_address_free(a); // wait for connection to be processed j = 50; while (j >= 0) { mdev_poll(source, 10); mdev_poll(destination, 10); j--; } }
void wait_local_devices() { while (!(mdev_ready(source) && mdev_ready(destination))) { mdev_poll(source, 25); mdev_poll(destination, 25); } }
void loop() { printf("-------------------- GO ! --------------------\n"); int i = 0; if (automate) { char source_name[1024], destination_name[1024]; printf("%s\n", mdev_name(source)); printf("%s\n", mdev_name(destination)); lo_address a = lo_address_new_from_url("osc.udp://224.0.1.3:7570"); lo_address_set_ttl(a, 1); lo_send(a, "/link", "ss", mdev_name(source), mdev_name(destination)); msig_full_name(sendsig, source_name, 1024); msig_full_name(recvsig, destination_name, 1024); lo_send(a, "/connect", "ssss", source_name, destination_name, "@mode", "reverse"); lo_address_free(a); } while (i >= 0 && !done) { msig_update_float(recvsig, ((i % 10) * 1.0f)); printf("\ndestination value updated to %f -->\n", (i % 10) * 1.0f); mdev_poll(destination, 1); mdev_poll(source, 100); i++; } }
void loop(int iterations) { printf("-------------------- GO ! --------------------\n"); int i = 0, j = 0; float value = 0; while (i >= 0 && iterations-- >= 0 && !done) { // here we should create, update and destroy some instances switch (rand() % 5) { case 0: // try to create a new instance for (j = 0; j < 10; j++) { if (!sendinst[j]) { sendinst[j] = nextid++; printf("--> Created new sender instance: %d\n", sendinst[j]); break; } } break; case 1: // try to destroy an instance j = rand() % 10; if (sendinst[j]) { printf("--> Retiring sender instance %ld\n", (long)sendinst[j]); msig_release_instance(sendsig, sendinst[j], MAPPER_TIMETAG_NOW); sendinst[j] = 0; break; } break; default: j = rand() % 10; if (sendinst[j]) { // try to update an instance value = (rand() % 10) * 1.0f; msig_update_instance(sendsig, sendinst[j], &value, 0, MAPPER_TIMETAG_NOW); printf("--> sender instance %d updated to %f\n", sendinst[j], value); sent++; } break; } print_instance_ids(sendsig); print_instance_ids(recvsig); printf("\n"); mdev_poll(destination, 100); mdev_poll(source, 0); i++; //usleep(1 * 1000); } }
void wait_ready() { while (!(mdev_ready(source) && mdev_ready(destination))) { mdev_poll(source, 0); mdev_poll(destination, 0); usleep(500 * 1000); } }
void wait_local_devices() { while (!done && !(mdev_ready(source) && mdev_ready(destination))) { mdev_poll(source, 0); mdev_poll(destination, 0); usleep(50 * 1000); } }
void loop() { printf("Polling device..\n"); int i; for (i = 0; i < 10; i++) { mdev_poll(source, 0); printf("Updating signal %s to %f\n", sendsig->props.name, (i * 1.0f)); msig_update_float(sendsig, (i * 1.0f)); sent++; usleep(250 * 1000); mdev_poll(destination, 0); } }
int main() { signal(SIGINT, ctrlc); mapper_device dev = mdev_new("pwm", 9000, 0); float min0 = 0; float max1 = 1; float max1000 = 1000; mdev_add_input(dev, "/freq", 1, 'f', "Hz", &min0, &max1000, (mapper_signal_handler*)handler_freq, 0); mdev_add_input(dev, "/gain", 1, 'f', "Hz", &min0, &max1, (mapper_signal_handler*)handler_gain, 0); mdev_add_input(dev, "/duty", 1, 'f', "Hz", &min0, &max1, (mapper_signal_handler*)handler_duty, 0); run_synth(); set_duty(0.1); set_freq(110.0); set_gain(0.1); printf("Press Ctrl-C to quit.\n"); while (!done) mdev_poll(dev, 10); mdev_free(dev); set_freq(0); sleep(1); stop_synth(); }
int setup_connection() { mval src_min, src_max, dest_min, dest_max; src_min.f = 0.; src_max.f = 1.; dest_min.f = -10.; dest_max.f = 10.; mapper_monitor mon = mapper_monitor_new(source->admin, 0); char src_name[1024], dest_name[1024]; mapper_monitor_link(mon, mdev_name(source), mdev_name(destination), 0, 0); msig_full_name(sendsig, src_name, 1024); msig_full_name(recvsig, dest_name, 1024); mapper_connection_range_t range; range.src_min = &src_min; range.src_max = &src_max; range.dest_min = &dest_min; range.dest_max = &dest_max; range.known = CONNECTION_RANGE_KNOWN; mapper_db_connection_t props; props.range = range; props.src_length = 1; props.dest_length = 1; props.src_type = 'f'; props.dest_type = 'f'; props.mode = MO_LINEAR; mapper_monitor_connect(mon, src_name, dest_name, &props, CONNECTION_RANGE_KNOWN | CONNECTION_MODE); // poll devices for a bit to allow time for connection int i = 0; while (i++ < 10) { mdev_poll(destination, 10); mdev_poll(source, 10); } mapper_monitor_free(mon); return 0; }
void loop() { printf("Polling device..\n"); int i; for (i = 0; i < 10; i++) { mdev_poll(source, 0); float v[3]; v[0] = (float)i; v[1] = (float)i+1; v[2] = (float)i+2; printf("Updating signal %s to [%f, %f, %f]\n", sendsig->props.name, v[0], v[1], v[2]); msig_update(sendsig, v); sent++; usleep(250 * 1000); mdev_poll(destination, 0); } }
int main() { int result = 0; value = (float)rand(); signal(SIGINT, ctrlc); if (setup_destination()) { printf("Error initializing destination.\n"); result = 1; goto done; } if (setup_source()) { printf("Done initializing source.\n"); result = 1; goto done; } wait_local_devices(); if (automate) connect_signals(); // start things off printf("STARTING TEST...\n"); times[0] = get_current_time(); msig_update_instance(sendsig, counter++, &value, 0, MAPPER_TIMETAG_NOW); while (!done) { mdev_poll(destination, 0); mdev_poll(source, 0); } goto done; done: cleanup_destination(); cleanup_source(); print_results(); return result; }
void loop() { printf("-------------------- GO ! --------------------\n"); int i = 0, j = 0, count; if (automate) { char source_name[1024], destination_name[1024]; printf("%s\n", mdev_name(source)); printf("%s\n", mdev_name(destination)); lo_address a = lo_address_new_from_url("osc.udp://224.0.1.3:7570"); lo_address_set_ttl(a, 1); lo_send(a, "/link", "ss", mdev_name(source), mdev_name(destination)); for (int i = 0; i < 4; i++) { msig_full_name(sendsig[i], source_name, 1024); msig_full_name(recvsig[i], destination_name, 1024); lo_send(a, "/connect", "ss", source_name, destination_name); } lo_address_free(a); } while (i >= 0 && !done) { for (j = 0; j < 4; j++) { msig_update_float(recvsig[j], ((i % 10) * 1.0f)); } printf("\ndestination values updated to %d -->\n", i % 10); for (j = 0; j < 4; j++) { count = msig_query_remote(sendsig[j], dummysig[j]); printf("Sent %i queries for sendsig[%i]\n", count, j); } mdev_poll(destination, 100); mdev_poll(source, 0); i++; usleep(500 * 1000); } }
void loop() { printf("-------------------- GO ! --------------------\n"); int i = 0; if (automate) { char source_name_1[1024], destination_name_1[1024]; char source_name_2[1024], destination_name_2[1024]; printf("%s\n", mdev_name(source)); printf("%s\n", mdev_name(destination)); lo_address a = lo_address_new_from_url("osc.udp://224.0.1.3:7570"); lo_address_set_ttl(a, 1); lo_send(a, "/link", "ss", mdev_name(source), mdev_name(destination)); msig_full_name(sendsig_1, source_name_1, 1024); msig_full_name(recvsig_1, destination_name_1, 1024); lo_send(a, "/connect", "ss", source_name_1, destination_name_1); msig_full_name(sendsig_2, source_name_2, 1024); msig_full_name(recvsig_2, destination_name_2, 1024); lo_send(a, "/connect", "ss", source_name_2, destination_name_2); lo_address_free(a); } while (i >= 0 && !done) { mdev_poll(source, 0); msig_update_float(source->outputs[0], ((i % 10) * 1.0f)); msig_update_float(source->outputs[1], ((i % 10) * 1.0f)); printf("source value updated to %d -->\n", i % 10); printf("Received %i messages.\n\n", mdev_poll(destination, 100)); i++; } }
void loop() { int i = 0; printf("Loading devices...\n"); while (i >= 0 && !done) { for (i=0; i<5; i++) mdev_poll(devices[i], 0); lo_timetag_now(&system_time); if (system_time.sec != last_update) { last_update = system_time.sec; if (ready) { for (i=0; i<5; i++) { mdev_timetag_now(devices[i], &device_times[i]); } // calculate standard deviation double mean = 0; for (i=0; i<5; i++) { mean += mapper_timetag_get_double(device_times[i]); } mean /= 5; double difference_aggregate = 0; for (i=0; i<5; i++) { difference_aggregate += powf(mapper_timetag_get_double(device_times[i]) - mean, 2); } // print current system time and device diffs printf("%f", (double)system_time.sec + (double)system_time.frac * 0.00000000023283064365); for (i=0; i<5; i++) { printf(" | %f", mapper_timetag_difference(system_time, device_times[i])); } printf(" | %f", sqrtf(difference_aggregate / 5)); printf("\n"); } else { int count = 0; for (i=0; i<5; i++) { count += mdev_ready(devices[i]); } if (count >= 5) { printf("\nSYSTEM TIME ***** | OFFSETS *****\n"); for (i=0; i<5; i++) { // Give each device clock a random starting offset devices[i]->admin->clock.offset = (rand() % 100) - 50; } ready = 1; } } } usleep(10 * 1000); } }
int setup_connection() { int count = 0; mapper_monitor mon = mapper_monitor_new(source->admin, 0); if (!mon) goto error; char src_name[1024], dest_name[1024]; mapper_monitor_link(mon, mdev_name(source), mdev_name(destination), 0, 0); while (!done && !source->routers) { if (count++ > 50) goto error; mdev_poll(source, 10); mdev_poll(destination, 10); } msig_full_name(sendsig, src_name, 1024); msig_full_name(recvsig, dest_name, 1024); mapper_monitor_connect(mon, src_name, dest_name, 0, 0); // wait until connection has been established while (!done && !source->routers->num_connections) { if (count++ > 50) goto error; mdev_poll(source, 10); mdev_poll(destination, 10); } eprintf("Connection established.\n"); mapper_monitor_free(mon); return 0; error: return 1; }
struct _agentInfo *agentInit() { obs[0] = obs[1] = 0; id = 0; struct _agentInfo *info = &agentInfo; memset(info, 0, sizeof(struct _agentInfo)); info->influence_device_name = strdup("/influence.1"); info->xagora_device_name = strdup("/XAgora_receiver.1"); info->admin = mapper_admin_new(0, 0, 0); // add device info->dev = mdev_new("agent", 9000 + id, info->admin); while (!mdev_ready(info->dev)) { mdev_poll(info->dev, 100); } printf("ordinal: %d\n", mdev_ordinal(info->dev)); fflush(stdout); // add monitor and monitor callbacks info->mon = mapper_monitor_new(info->admin, 0); info->db = mapper_monitor_get_db(info->mon); mapper_db_add_device_callback(info->db, dev_db_callback, info); mapper_db_add_link_callback(info->db, link_db_callback, info); // add signals float mn=-1, mx=1; mdev_add_input(info->dev, "observation", 2, 'f', "norm", &mn, &mx, signal_handler, 0); int imn=0, imx=WIDTH; info->sig_pos = mdev_add_output(info->dev, "position", 2, 'i', 0, &imn, &imx); info->sig_gain = mdev_add_output(info->dev, "gain", 1, 'f', "normalized", &mn, &mx); mx = 0.9; info->sig_fade = mdev_add_output(info->dev, "fade", 1, 'f', "normalized", &mn, &mx); mn = -1.5; mx = 1.5; info->sig_spin = mdev_add_output(info->dev, "spin", 1, 'f', "radians", &mn, &mx); mn = -3.1415926; mx = 3.1315926; info->sig_dir = mdev_add_output(info->dev, "direction", 1, 'f', "radians", &mn, &mx); mn = -1; info->sig_flow = mdev_add_output(info->dev, "flow", 1, 'f', "noramlized", &mn, &mx); return info; }
int test_recv() { mapper_device md = mdev_new("synth", 0, 0); if (!md) goto error; printf("Mapper device created.\n"); float mn=0, mx=1; mapper_signal sig = mdev_add_input(md, "/mapped1", 1, 'f', 0, &mn, &mx, handler, 0); printf("Input signal /mapped1 registered.\n"); printf("Number of inputs: %d\n", mdev_num_inputs(md)); printf("Waiting for port/ordinal allocation..\n"); int i; for (i = 0; i < 10; i++) { mdev_poll(md, 500); if (mdev_ready(md)) break; usleep(500 * 1000); } if (i >= 10) { printf("Timed out waiting for signal name.\n"); goto error; } char port[10]; sprintf(port, "%i", md->admin->port); printf("using port = %s\n", port); lo_address a = lo_address_new("localhost", port); if (!a) { printf("Error creating lo_address for test.\n"); return 1; } printf("Polling device..\n"); for (i = 0; i < 10; i++) { lo_send(a, sig->props.name, "f", (float) i); printf("Updating signal %s to %f\n", sig->props.name, (float) i); sent++; mdev_poll(md, 500); //usleep(500 * 1000); } if (sent != received) { printf("Not all sent values were received.\n"); printf("Sent %d values, but %d received.\n", sent, received); goto error; } if (sent == 0) { printf("Unable to send any values.\n"); goto error; } printf("Sent and received %d values.\n", sent); mdev_free(md); lo_address_free(a); return 0; error: if (md) mdev_free(md); lo_address_free(a); return 1; }
/* This is where we test the use of select() to wait on multiple * devices at once. */ void select_on_both_devices(int block_ms) { int i, updated = 0; fd_set fdr; int nfds1 = mdev_num_fds(source); int nfds2 = mdev_num_fds(destination); int *fds1 = alloca(sizeof(int)*nfds1); int *fds2 = alloca(sizeof(int)*nfds2); int mfd = -1; nfds1 = mdev_get_fds(source, fds1, nfds1); nfds2 = mdev_get_fds(destination, fds2, nfds2); FD_ZERO(&fdr); for (i = 0; i < nfds1; i++) { FD_SET(fds1[i], &fdr); if (fds1[i] > mfd) mfd = fds1[i]; } for (i = 0; i < nfds2; i++) { FD_SET(fds2[i], &fdr); if (fds2[i] > mfd) mfd = fds2[i]; } struct timeval timeout = { block_ms * 0.001, (block_ms * 1000) % 1000000 }; struct timeval now, then; gettimeofday(&now, NULL); then.tv_sec = now.tv_sec + block_ms * 0.001; then.tv_usec = now.tv_usec + block_ms * 1000; if (then.tv_usec > 1000000) { then.tv_sec++; then.tv_usec %= 1000000; } while (timercmp(&now, &then, <)) { if (select(mfd+1, &fdr, 0, 0, &timeout) > 0) { for (i = 0; i < nfds1; i++) { if (FD_ISSET(fds1[i], &fdr)) mdev_service_fd(source, fds1[i]); } for (i = 0; i < nfds2; i++) { if (FD_ISSET(fds2[i], &fdr)) mdev_service_fd(destination, fds2[i]); } updated ++; } gettimeofday(&now, NULL); // not necessary in Linux since timeout is updated by select() timersub(&then, &now, &timeout); } if (!updated) { /* If nothing happened in 100 ms, we should poll the devices * anyways in case action needs to be taken. */ mdev_poll(source, 0); mdev_poll(destination, 0); } }
int main(int argc, char *argv[]) { if (argc > 1) id = atoi(argv[1]); signal(SIGINT, ctrlc); struct _agentInfo *info = agentInit(); if (!info->dev) goto done; float pos[2]; pos[0] = rand()%WIDTH/2+WIDTH/4; pos[1] = rand()%HEIGHT/2+HEIGHT/4; float gain = 0.2; float damping = 0.9; float limit = 1; float vel[2] = {0,0}; int counter = 0; while (!done) { mapper_monitor_poll(info->mon, 0); if (mdev_poll(info->dev, 10)) { vel[0] += obs[0] * gain; vel[1] += obs[1] * gain; pos[0] += vel[0]; pos[1] += vel[1]; vel[0] *= damping; vel[1] *= damping; if (vel[0] > limit) vel[0] = limit; if (vel[0] < -limit) vel[0] = -limit; if (vel[1] > limit) vel[1] = limit; if (vel[1] < -limit) vel[1] = -limit; if (pos[0] < 0) { pos[0] = 0; vel[0] *= -0.95; } if (pos[0] >= WIDTH) { pos[0] = WIDTH-1; vel[0] *= -0.95; } if (pos[1] < 0) { pos[1] = 0; vel[1] *= -0.95; } if (pos[1] >= HEIGHT) { pos[1] = HEIGHT-1; vel[1] *= -0.95; } int p[2]; p[0] = (int)pos[0]; p[1] = (int)pos[1]; msig_update(info->sig_pos, p); counter = 0; } else { if (counter++ > 100) { int p[2]; p[0] = (int)pos[0]; p[1] = (int)pos[1]; msig_update(info->sig_pos, p); counter = 0; } } } done: agentLogout(); return 0; }