Commit e79f09fdb7ab640aaf23b638c64b7522d787449d

Authored by Arnaud Blanchard
0 parents

Initial basic applications

cmake_minimum_required(VERSION 2.6)
project(bapps)
#subdirs(i_read)
subdirs(i_oscillator)
#subdirs(i_keyboard)
subdirs(f_max)
... ...
# Set the minimum version of cmake required to build this project
cmake_minimum_required(VERSION 2.6)
# Set the name of the project as the directory basename
project(f_max)
find_package(blc_channel)
find_package(blc_program)
add_definitions(${BL_DEFINITIONS})
include_directories(${BL_INCLUDE_DIRS})
add_executable(f_max f_max.cpp)
target_link_libraries(f_max ${BL_LIBRARIES})
... ...
//
// Created by Arnaud Blanchard on 22/12/15.
// Copyright ETIS 2015. All rights reserved.
//
#include "blc_channel.h"
#include "blc_program.h"
#include <unistd.h>
#include <math.h>
#define DEFAULT_OUTPUT_NAME ":argmax<pid>"
static float find_float_max(float *floats, int floats_nb, float const *find_arg=NULL){
float max;
int i, imax;
max=floats[0];
imax=0;
for(i=1; i!=floats_nb; i++){
if(floats[i] > max){
max=floats[i];
imax=i;
}
}
if (find_arg) return (imax+0.5f)*(*find_arg);
else return max;
}
static void start_channel_float_loop(blc_channel const *input, blc_channel *output, float const *find_arg=NULL)
{
BLC_COMMAND_LOOP(0){
output->floats[0]=find_float_max(input->floats, input->total_length, find_arg);
}
}
static void start_print_float_loop(blc_channel const *input, float const* find_arg=NULL){
float imax;
BLC_COMMAND_LOOP(0){
imax=find_float_max(input->floats, input->total_length, find_arg);
printf("%f\n", imax);
if( blc_output_terminal) blc_eprint_cursor_up(1);
}
if( blc_output_terminal) blc_eprint_cursor_down(1);
}
int main(int argc, char **argv){
blc_channel input, output;
char const *output_name, *input_name, *find_arg_str;
float *find_arg_gain=NULL;
float arg_gain;
blc_program_set_description("Find the value of argument of the maximal value");
blc_program_add_option(&find_arg_str, 'a', "find_arg", "real", "Return argmax mutiplied by the real instead of max", NULL);
blc_program_add_option(&output_name, 'o', "output", "blc_channel-out", "channel name", DEFAULT_OUTPUT_NAME);
blc_program_add_parameter(&input_name, "blc_channel-in", 1, "channel you want to find max", NULL);
blc_program_init(&argc, &argv, NULL);
input.open(input_name, BLC_CHANNEL_READ);
blc_loop_try_add_waiting_semaphore(input.sem_new_data);
blc_loop_try_add_posting_semaphore(input.sem_ack_data);
if (find_arg_str){
SSCANF(1, find_arg_str, "%f", &arg_gain);
find_arg_gain=&arg_gain;
if(strcmp(output_name, DEFAULT_OUTPUT_NAME)==0) asprintf((char**)&output_name, ":imax%d", getpid());
}
else if(strcmp(output_name, DEFAULT_OUTPUT_NAME)==0) asprintf((char**)&output_name, ":max%d", getpid());
//The output is on terminal
if (strcmp(output_name, "-")==0){
switch (input.type){
case 'FL32':start_print_float_loop(&input, find_arg_gain);
break;
default:EXIT_ON_CHANNEL_ERROR(&input, "Type is not managed. Only 'FL32' is.");
break;
}
}
else
{
output.create_or_open(output_name, BLC_CHANNEL_WRITE, input.type, 'NDEF', 1, 1);
output.publish();
blc_loop_try_add_waiting_semaphore(output.sem_ack_data);
blc_loop_try_add_posting_semaphore(output.sem_new_data);
switch (input.type){
case 'FL32':start_channel_float_loop(&input, &output, find_arg_gain);
break;
default:EXIT_ON_CHANNEL_ERROR(&input, "Type is not managed. Only 'FL32' is.");
break;
}
}
return EXIT_SUCCESS;
}
... ...
# Set the minimum version of cmake required to build this project
cmake_minimum_required(VERSION 2.6)
# Set the name of the project as the directory basename
project(i_oscillator)
find_package(blc_channel)
find_package(blc_program)
add_definitions(${BL_DEFINITIONS})
include_directories(${BL_INCLUDE_DIRS})
add_executable(i_oscillator i_oscillator.cpp)
target_link_libraries(i_oscillator ${BL_LIBRARIES})
... ...
//
// Created by Arnaud Blanchard on 22/12/15.
// Copyright ETIS 2015. All rights reserved.
//
#include "blc_core.h"
#include "blc_channel.h"
#include "blc_program.h"
#include <unistd.h>
#include <math.h>
#define DEFAULT_OUTPUT_NAME ":oscillator<pid>"
int period;
float refresh_rate; //µs;
blc_channel output;
double step;
char const *display;
int display_height;
blc_mem graph_mem;
size_t i=0;
float gain=0;
int columns_nb=64;
int rows_nb=16;
static void info_cb(char const*, void*){
fprintf(stderr, "%d ", output.chars[0]);
}
static void period_cb(char const*argument, void*){
period=strtol(argument, NULL, 10);
step=(double)refresh_rate/(double)period;
}
static void refresh_cb(char const*argument, void*){
refresh_rate=strtol(argument, NULL, 10);
step=(double)refresh_rate/(double)period;
blc_command_loop_period=refresh_rate*1000;
}
static void start_float_loop(float frequency, float min, float max){
int i;
float time_gain, offset, gain;
time_gain=frequency*2*M_PI/(float)output.total_length*refresh_rate/1000;
if (output.sem_ack_data) refresh_rate=0;
gain=(max-min)/2.f; //amplitude sinus = 2
offset=min+gain; //min sinus*gain = -gain
BLC_COMMAND_LOOP(refresh_rate*1000){
FOR(i, output.total_length){
output.floats[i]=sin((blc_loop_iteration*output.total_length+i)*time_gain)*gain+offset;
}
if(display){
blc_fprint_float_graph(stderr, output.floats, output.total_length, "oscillation", columns_nb, rows_nb, 1, 0, "Time", "Intensity");
blc_eprint_cursor_up(rows_nb);
}
}
}
int main(int argc, char **argv){
char const *period_str, *refresh_rate_str, *display_str, *display_height_str, *channel_name, *buffer_length_str, *type_str, *frequency_str;
char const *min_str, *max_str;
float frequency, min, max;
int buffer_length;
uint32_t type;
blc_program_add_option(&channel_name, 'o', "channel", "blc_channel", "channel name", DEFAULT_OUTPUT_NAME);
blc_program_add_option(&display, 'd', "display", NULL, "display value at each iteration", NULL);
blc_program_add_option(&buffer_length_str, 's', "size", "integer", "buffer size (items nb)", "1");
blc_program_add_option(&frequency_str, 'f', "frequency", "float", "frequency in Hz", "1");
blc_program_add_option(&min_str, 'm', "min", "float", "minimum value of oscillation", "0");
blc_program_add_option(&max_str, 'M', "max", "float", "maximum value of oscillation", "1");
blc_program_add_option(&refresh_rate_str, 'r', "refresh", "integer", "refresh rate in ms", "10");
blc_program_add_option(&type_str, 't', "type", "FL32", "type of data", "FL32");
blc_program_init(&argc, &argv, NULL);
// period=strtol(period_str, NULL, 10);
SSCANF(1, refresh_rate_str, "%f", &refresh_rate);
step=(double)refresh_rate/(double)period;
type=STRING_TO_UINT32(type_str);
if (strcmp(DEFAULT_OUTPUT_NAME, channel_name)==0) asprintf((char**)&channel_name, ":oscillator%d",getpid());
SSCANF(1, buffer_length_str, "%d", &buffer_length);
SSCANF(1, frequency_str, "%f", &frequency);
SSCANF(1, min_str, "%f", &min);
SSCANF(1, max_str, "%f", &max);
output.create_or_open(channel_name, BLC_CHANNEL_WRITE, type, 'NDEF', 1, buffer_length);
output.publish();
/*
switch (output.type){
case 'INT8': gain=INT8_MAX;break;
case 'UIN8': gain=UINT8_MAX/2;break;
default:EXIT_ON_CHANNEL_ERROR(&output, "Type not managed");
}
gain *= amp;*/
blc_command_add("i", info_cb, NULL, "display value", NULL);
blc_command_add("p", period_cb, "period(ms)", "signal period", NULL);
blc_command_add("r", refresh_cb, "step(ms)", "time step", NULL);
blc_loop_try_add_waiting_semaphore(output.sem_ack_data);
blc_loop_try_add_posting_semaphore(output.sem_new_data);
start_float_loop(frequency, min, max);
if (display) blc_eprint_cursor_down(rows_nb);
return EXIT_SUCCESS;
}
... ...