0

I have created a bluetooth input device (stylus) and would like to connect it to both a Mac and Windows (and preferably Linux in the future).

Is there an ideal software / language to use to create a cross-platform application? I have considered writing native applications for each, but I don't feel the application will be so complex that this is absolutely necessary.

The application will take the input data of the BT device and use it to move the cursor around the screen and provide click and pressure functionality.

Thank you in advance.

user2075625
  • 93
  • 2
  • 11

1 Answers1

0

I don't know how you device is set up.

However, if you managed to put on it a PIC (Such as the Arduino ATMega328) with at least one serial interface, you could be able to connect it to your PC via Universal Serial Bus (USB).

After that you will be able to open a pipe to your device in many languages.

C is always a good choice both for Linux and OS X, using POSIX libraries will make it even easier.

This snippet I wrote taking some tips online may help to get started

int init_port (const char * port_name, int baud) {
    /* Main vars */
    struct termios toptions;
    int stream;
    /* Port data */
    speed_t brate = baud;

    if ((stream = apri_porta(port_name)) < 1)
        return 0;

    if (tcgetattr(stream, &toptions) < 0) {
        printf("Error");
        return 0;
    }
    /* INITIALIZING BAUD RATE */
    cfsetispeed(&toptions, brate);
    cfsetospeed(&toptions, brate);

    // IMPORTANT BLOCK OF OPTIONS TO MAKE TX AND RX WORKING
    toptions.c_cflag &= ~PARENB;
    toptions.c_cflag &= ~CSTOPB;
    toptions.c_cflag &= ~CSIZE;
    toptions.c_cflag |= CS8;

    toptions.c_cflag &= ~CRTSCTS;

    toptions.c_cflag |= CREAD | CLOCAL;
    toptions.c_iflag &= ~(IXON | IXOFF | IXANY);

    toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    toptions.c_oflag &= ~OPOST;

    toptions.c_cc[VMIN]  = 0;
    toptions.c_cc[VTIME] = 0;

    tcsetattr(stream, TCSANOW, &toptions);
    if (tcsetattr(stream, TCSAFLUSH, &toptions) < 0) {
        printf("Error");
        return 0;
    }

    return stream;
}

int open_port (const char * port_name) {

    int stream;

    stream = open(port_name, O_RDWR | O_NONBLOCK );

    if (stream == -1)  {
        printf("apri_porta: Impossibile aprire stream verso '%s'\n", port_name);
        return -1;
    }

    return stream;
}

int close_port (int stream) {
    return (close(stream));
}

int write_to_port(int stream, char * str) {
    int len = (int)strlen(str);
    int n = (int)write(stream, str, len);
    if (n != len)
        return 0;
    return 1;
}

int read_from_port(int fd, char * buf, int buf_max, char until) {

    int timeout = 5000;
    char b[1];
    int i=0;

    do {
        int n = (int)read(fd, b, 1);
        if( n==-1) return -1;
        if( n==0 ) {
            usleep( 1 * 1000 );
            timeout--;
            continue;
        }
        buf[i] = b[0];
        i++;
    } while( b[0] != until && i < buf_max && timeout > 0 );

    buf[i] = 0;  // null terminate the string
    return 0;
}

Objective-C (OS X) has got a good library which works like a charm (ORSSerialPort)

However, if you would like to have a cross-platform solution, Java is the best choice either for Windows, OS X and Linux.

I hope this helped you and others to get started.

Feel free to PM me if you need further help.

Best regards.

Alberto
  • 4,212
  • 5
  • 22
  • 36
  • Thank you for your reply! Bluetooth is still a viable option though if setup correctly with these languages though right? For a desktop application such as this that (in comparison to a program such as word) is very simple, would you say it's worth writing native applications or to just stick with Java? I wanted to weigh the pros and cons first. – user2075625 Sep 15 '15 at 13:54
  • C compiles everywhere: on Windows with IDEs and on Unix-based systems directly from Terminal via GCC or CLANG. Now is important to understand exactly what you want and, from what I understand, you need some kind of background utility which reads data from the Bluetooth serial interface and moves the cursor, providing click support. For this purpose, if working on windows or Liunx, go ahead with Java. Serial interfacing is easy, and mouse events are too. On OS X go with Obj-C. It was thought to do whatever you need on Macs. – Alberto Sep 15 '15 at 14:06
  • Sorry if I am a bit confusing, but what I want to say is that both reading from serial interface and moving a cursor on the screen are easy tasks. Then the choice is up to you. – Alberto Sep 15 '15 at 14:08
  • No that's brilliant - thank you so much for your help! – user2075625 Sep 15 '15 at 14:45
  • Excuse the code provided, function names were in my language and while porting it to english i forgot to change some calls() – Alberto Sep 15 '15 at 18:37