i_mouse.cpp 1.86 KB
#include "blc_core.h"
#include "blc_channel.h"
#include "blc_program.h"
#include <unistd.h>
#include <termios.h>
#include <libgen.h> //basename

int main(int argc, char** argv){
    char const  *period_str, *text, *gain_str;
    char const *channel_name, *filename, *time_str;
    char const *number_str;
    char  *default_output;
    int number, ret;
    uint64_t executing_time, previous_executing_time;
    size_t linecap=0;
    ssize_t line_size;
    blc_channel channel;
    float gain;
    int  fd;
    int  period, read_chars_nb;
    signed char data[3];

    
    asprintf(&default_output, "/%s%d", basename(argv[0]), getpid());
    blc_program_set_description("Read the movements of the mouse");
    blc_program_add_option(&gain_str, 'g', "gain", "real", "Gain to multiply the values between [-128, 127]", "1");
    blc_program_add_option(&channel_name, 'o', "output_channel", "string", "Name of the channel to output the data", default_output);
    blc_program_add_option(&period_str, 'p', "period", "integer", "Period in µs to read the mouse", "0");
    blc_program_init(&argc, &argv, blc_quit);
    blc_command_forward_blc_channels();

    SSCANF(1, gain_str, "%f", &gain);

    SSCANF(1, period_str, "%d", &period);
    
    SYSTEM_ERROR_CHECK(fd=open("/dev/input/mice", O_RDONLY | O_NONBLOCK), -1, "Opening mouse file");
    channel.create_or_open(channel_name, BLC_CHANNEL_WRITE, 'FL32', 'NDEF', 1, 2);
    blc_loop_try_add_posting_semaphore(channel.sem_new_data);
    blc_loop_try_add_waiting_semaphore(channel.sem_ack_data);
    channel.publish();
    BLC_COMMAND_LOOP(period){
        read_chars_nb=read(fd, data, sizeof(data));
        if (read_chars_nb==-1){
            CLEAR(data);
        }
        channel.floats[0]=data[1]*gain;
        channel.floats[1]=data[2]*gain;
    }
    SYSTEM_ERROR_CHECK(close(fd), -1, "Closing ");
    FREE(default_output);
    return EXIT_SUCCESS;
}