// Initialise serial cable and run one time setup
bool application_init(void)
{

    char *portname = "/dev/ttyUSB0";

    fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0)
    {
        NABTO_LOG_INFO(("error %d opening %s: %s\n", errno, portname, strerror (errno)));
        return 0;
    }
    else {
        NABTO_LOG_INFO(("Opening %s: %s\n", portname, strerror (errno)));
    }

    set_interface_attribs (fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
    set_blocking (fd, 0);                // set no blocking

    // Wake Roomba
    setRTS(fd, 0);
    usleep(100000); //0.1 sec
    setRTS(fd, 1);
    sleep(2);

    // Songs can be defined while in passive mode (which we are in right now)
    char DSB[] = {140, 0, 3, 74, 40, 75, 20, 70, 32};
    write(fd, &DSB, sizeof(DSB));
    usleep ((sizeof(DSB)+25) * 100);

    char reverse[] = {140, 1, 6, 84, 32, 30, 32, 84, 32, 30, 32, 84, 32, 30, 32};
    write(fd, &reverse, sizeof(reverse));
    usleep ((sizeof(reverse)+25) * 100);

    char car_low[] = {140, 2, 12, 74,10, 71,10, 67,10, 67,10, 67,10, 69,10, 71,10, 72,10, 74,10, 74,10, 74,10, 71,10};
    write(fd, &car_low, sizeof(car_low));
    usleep ((sizeof(car_low)+25) * 100);

    char car_high[] = {140, 3, 12, 86,10, 83,10, 79,10, 79,10, 79,10, 81,10, 83,10, 84,10, 86,10, 86,10, 86,10, 83,10};
    write(fd, &car_high, sizeof(car_high));
    usleep ((sizeof(car_high)+25) * 100);

    return 1;
}
/* This is our thread function.  It is like main(), but for a thread */
void* sendOrder(void *arg)
{

    // Set thread to be detachable such that it will release resources when finished.
    pthread_detach(pthread_self());

    printf("Now running thread\n");

    // Get the passed item/integer
    fd = *((int *) arg);

    // This is the clean cycle task

    // Wake Roomba
    setRTS(fd, 0);
    usleep(100000); //0.1 sec
    setRTS(fd, 1);
    sleep(2);

    // The trick is to send char arrays

    // Put into safe mode
    char safe[] = {128, 131};
    write(fd, &safe, sizeof(safe));
    usleep(20*1000); // When changing mode, allow 20 milliseconds

    // Set display to NAb T/O
    char display[] = {163, 55, 119, 124, 93};
    write(fd, &display, sizeof(display));
    usleep ((sizeof(display)+25) * 100);

    // Start clean cycle
    char clean[] = {135};
    write(fd, &clean, sizeof(clean));
    usleep ((sizeof(clean)+25) * 100);


    return NULL;
}
Example #3
0
int openserial(char *devicename) 
{
     struct termios attr;

     if ((fd = open(devicename, O_RDWR)) == -1) return 0; /* Error */ 
     atexit(closeserial);

     if (tcgetattr(fd, &oldterminfo) == -1) return 0; /* Error */
     attr = oldterminfo;
     attr.c_cflag |= CRTSCTS | CLOCAL;
     attr.c_oflag = 0;
     if (tcflush(fd, TCIOFLUSH) == -1) return 0; /* Error */
     if (tcsetattr(fd, TCSANOW, &attr) == -1) return 0; /* Error */ 

     /* Set the lines to a known state, and */
     /* finally return non-zero is successful. */
     return setRTS(0) && setDTR(0);
}
Example #4
0
void CSerialDummy::setRTSDTR(bool rts, bool dtr) {
	setRTS(rts);
	setDTR(dtr);
}
Example #5
0
uint16_t transmitSerialData(void)
{
	uint16_t i, res=0;
	int fd_flags=0;
	struct sigaction save_buffer[2];

	fd_flags=fcntl(serialportFd, F_GETFL);
	fcntl(serialportFd, F_SETFL, O_RDWR | O_SYNC);

//	tcflush(serialportFd, TCIFLUSH);
//	tcflush(serialportFd, TCOFLUSH);
//
//	if(tcsetattr(serialportFd, TCSANOW, &tp)<0)
//	{
//		perror("Couldn't set term attributes");
//		return -1;
//	}

	sigaction(SIGALRM, NULL, save_buffer);

	setRTS(1);
	switch(mode)
	{
		case RADIOTFTP_MODE_UHF:
			usleep(RADIOTFTP_BIM2A_TX_PREDELAY);
			break;
		case RADIOTFTP_MODE_VHF:
			usleep(RADIOTFTP_UHX1_TX_PREDELAY);
			break;
		case RADIOTFTP_MODE_SERIAL:
			usleep(RADIOTFTP_SERIAL_TX_PREDELAY);
			break;
		default:
			perror("unknown baud rate");
			safe_exit(-1);
			break;
	}
	{
#if 0
		res = write(serialportFd, transmit_buffer, transmit_length);
#else
		printf("transmit_length=%d\n", transmit_length);
		srand(time(NULL));
		for(i=0; i < transmit_length; i++)
		{
			double myFloatRandom=(((float) rand()) / (float) RAND_MAX);
			if(myFloatRandom < BYTE_ERROR_PROB)
			{
				printf("%f < %f [0x%02X]=", myFloatRandom, BYTE_ERROR_PROB, transmit_buffer[i]);
				transmit_buffer[i]^=0xFF;
				printf("[0x%02X]\n", transmit_buffer[i]);
			}
			res=write(serialportFd, transmit_buffer + i, 1);
			//fprintf(stdout, "%c", io[i]);
			// Potential preamble problem??
			//usleep(10000);
			if(res < 0)
			{
				printf("serial write failed\n");
				return -3;
			}
		}
#endif

		if(res < 0)
		{
			return -2;
		}
		//wait for the buffer to be flushed
		tcdrain(serialportFd);
		switch(mode)
		{
			case RADIOTFTP_MODE_UHF:
				usleep(RADIOTFTP_BIM2A_TX_POSTDELAY);
				break;
			case RADIOTFTP_MODE_VHF:
				usleep(RADIOTFTP_UHX1_TX_POSTDELAY);
				break;
			case RADIOTFTP_MODE_SERIAL:
				usleep(RADIOTFTP_SERIAL_TX_POSTDELAY);
				break;
			default:
				perror("unknown baud rate");
				safe_exit(-1);
				break;
		}
	}

	sigaction(SIGALRM, save_buffer, NULL);

	setRTS(0);

	fcntl(serialportFd, F_GETFL);
	fcntl(serialportFd, F_SETFL, fd_flags | O_RDWR | O_ASYNC);

//	print_time("data sent");

	return 0;
}
Example #6
0
void APP_Tasks(void) {

    /* Check if device is configured.  See if it is configured with correct
     * configuration value  */

    switch (appData.state) {
        case APP_STATE_INIT:



            /* Open the device layer */
            appData.usbDevHandle = USB_DEVICE_Open(USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE);

            if (appData.usbDevHandle != USB_DEVICE_HANDLE_INVALID) {
                /* Register a callback with device layer to get event notification (for end point 0) */
                USB_DEVICE_EventHandlerSet(appData.usbDevHandle, APP_USBDeviceEventHandler, 0);

                appData.state = APP_STATE_WAIT_FOR_CONFIGURATION;
            } else {
                /* The Device Layer is not ready to be opened. We should try
                 * again later. */
            }

            break;

        case APP_STATE_WAIT_FOR_CONFIGURATION:

            if (appData.deviceConfigured == true) {
                /* Device is ready to run the main task */
                appData.hidDataReceived = false;
                appData.hidDataTransmitted = true;
                appData.state = APP_STATE_MAIN_TASK;

                /* Place a new read request. */
                USB_DEVICE_HID_ReportReceive(USB_DEVICE_HID_INDEX_0,
                        &appData.rxTransferHandle, appData.receiveDataBuffer, 64);
            }
            break;

        case APP_STATE_MAIN_TASK:

            if (!appData.deviceConfigured) {
                /* Device is not configured */
                appData.state = APP_STATE_WAIT_FOR_CONFIGURATION;
            } else if (appData.hidDataReceived) {
                /* Look at the data the host sent, to see what
                 * kind of application specific command it sent. */

                switch (appData.receiveDataBuffer[0]) {
                    case 0x80:

                        /* Toggle on board LED1 to LED2. */
                        BSP_LEDToggle(APP_USB_LED_1);
                        BSP_LEDToggle(APP_USB_LED_2);



                        setRTR();
                        break;

                    case 0x81:
                        if (appData.hidDataTransmitted) {
                            /* Echo back to the host PC the command we are fulfilling in
                             * the first byte.  In this case, the Get Push-button State
                             * command. */

                            appData.transmitDataBuffer[0] = 0x81;

                            appData.transmitDataBuffer[1] = 0b1 & BSP_SwitchStateGet(APP_USB_SWITCH_1);

                            appData.transmitDataBuffer[2] = 111;


                            setRTS();
                            setRTR();
                        }
                        break;


                    case 0x82:
                        if (!appData.numTX || _CP0_GET_COUNT() > 200000) {

                            //prepare new data to send
                            acc_read_register(OUT_X_L_A, (unsigned char *) appData.accels, 6);
                            appData.transmitDataBuffer[0] = 1; //we have data to send
                            appData.transmitDataBuffer[1] = appData.accels[0] >> 8; //x high byte
                            appData.transmitDataBuffer[2] = appData.accels[0] & 0xFF; //x low byte
                            appData.transmitDataBuffer[3] = appData.accels[1] >> 8; //y high byte
                            appData.transmitDataBuffer[4] = appData.accels[1] & 0xFF; //y low byte
                            appData.transmitDataBuffer[5] = appData.accels[2] >> 8; //z high byte
                            appData.transmitDataBuffer[6] = appData.accels[2] & 0xFF; //z low byte

                            // reset core timer for 100 hz
                            _CP0_SET_COUNT(0);
                            appData.numTX++;
                        }
                        else {
                            appData.transmitDataBuffer[0] = 0;  // we don't have new data
                        }

                        setRTS();
                        setRTR();
                        break;

                    case 0x83:
                        // prepare for a bout of sending accel data
                        //parse incoming data to screen
                        oled_clear_buffer();
                        int row = appData.receiveDataBuffer[1];
                        char * msg;
                        msg = &appData.receiveDataBuffer[2];

                        oled_draw_string(0, row, msg, 1);
                        oled_update();


                        // clear buffered accel data so we read new data to send
                        acc_read_register(OUT_X_L_A, (unsigned char *) appData.accels, 6);

                        appData.numTX = 0; //we're starting over

                        setRTR();
                        break;

                    case 0x84:
                        // done asking for data
                        oled_draw_string(0, 55, "Done!", 1);
                        oled_update();

                        setRTR();
                        break;

                    default:
                        setRTR();
                        break;
                }
            }