// Waits for the next byte and returns it. Runs play_check to keep // the music playing and calls pid_check() to keep following the line. char read_next_byte() { while(serial_get_received_bytes() == read_index) { play_check(); // pid_check takes some time; only run it if we don't have more bytes to process if(serial_get_received_bytes() == read_index) pid_check(); } char ret = buffer[read_index]; read_index ++; if(read_index >= 100) read_index = 0; return ret; }
void check_for_new_bytes_received( ) { while(serial_get_received_bytes(USB_COMM) != receive_buffer_position) { // Remember the byte that was received, ignore if user pushed too many bytes if ( bytes_received > sizeof(my_received_buffer) ) { bytes_received = 0; memset( my_received_buffer, 0, sizeof(my_received_buffer) ); serial_print( "Buffer Exceeded, try again!" ); } else { if ( receive_buffer[receive_buffer_position] == 0x8 || receive_buffer[receive_buffer_position] == 0x7F ) { // treat this as a backspace bytes_received = (bytes_received > 0) ? bytes_received - 1 : bytes_received; my_received_buffer[bytes_received] = '\0'; } else { my_received_buffer[bytes_received] = receive_buffer[receive_buffer_position]; bytes_received++; } // echo what was typed on the serial console back to the serial console serial_print_char( receive_buffer[receive_buffer_position] ); } // Increment receive_buffer_position, but wrap around when it gets to // the end of the buffer. if (receive_buffer_position == sizeof(receive_buffer)-1) { receive_buffer_position = 0; } else { receive_buffer_position++; } } if ( bytes_received && (my_received_buffer[bytes_received-1] == '\r' || my_received_buffer[bytes_received-1] == '\n') ) { // echo string to serial console serial_print( "%s", my_received_buffer ); // check makes sure we have atleast 3 bytes of data from user interface if ( bytes_received > 1) { strip( my_received_buffer, sizeof(my_received_buffer) ); process_received_bytes( my_received_buffer ); } memset( my_received_buffer, 0, sizeof(my_received_buffer) ); bytes_received = 0; } }
unsigned char rob_serial_get_received_bytes_usb_comm (void) { unsigned char numBytes; vTaskSuspendAll(); { numBytes = serial_get_received_bytes (USB_COMM); } xTaskResumeAll(); return numBytes; }
int s_read(char* buf, int want, int msecTimeout) { static int ringNext = -1; // -1 means uninitialized static char ringBuffer[kBufSize]; // size must fit in unsigned char if (want <= 0 || buf == 0) return 0; int maxRead = kBufSize - 1; if (want > maxRead) want = maxRead; if (ringNext < 0) { serial_receive_ring(USB_COMM, ringBuffer, kBufSize); ringNext = 0; } int avail = 0; int done = 0; while (!done) { serial_check(); int nbytes = serial_get_received_bytes(USB_COMM); avail = (nbytes - ringNext) & kSizeMask; if (avail > 0 || msecTimeout <= 0) done = 1; else { delay_ms(1); --msecTimeout; } } if (want > avail) want = avail; int got = 0; while (got < want) { buf[got++] = ringBuffer[ringNext++]; ringNext &= kSizeMask; } return got; }
void check_for_new_bytes_received() { while(serial_get_received_bytes(USB_COMM) != receive_buffer_position) { // Process the new byte that has just been received. process_received_byte(receive_buffer[receive_buffer_position]); // Increment receive_buffer_position, but wrap around when it gets to // the end of the buffer. if (receive_buffer_position == sizeof(receive_buffer)-1) { receive_buffer_position = 0; } else { receive_buffer_position++; } } }
//--------------------------------------------------------------------------------------- // If there are received bytes to process, this function loops through the receive_buffer // accumulating new bytes (keystrokes) in another buffer for processing. void check_for_new_bytes_received() { /* The receive_buffer is a ring buffer. The call to serial_check() (you should call prior to this function) fills the buffer. serial_get_received_bytes is an array index that marks where in the buffer the most current received character resides. receive_buffer_position is an array index that marks where in the buffer the most current PROCESSED character resides. Both of these are incremented % (size-of-buffer) to move through the buffer, and once the end is reached, to start back at the beginning. This process and data structures are from the Pololu library. See examples/serial2/test.c and src/OrangutanSerial/ * A carriage return from your comm window initiates the transfer of your keystrokes. All key strokes prior to the carriage return will be processed with a single call to this function (with multiple passes through this loop). On the next function call, the carriage return is processes with a single pass through the loop. The menuBuffer is used to hold all keystrokes prior to the carriage return. The "received" variable, which indexes menuBuffer, is reset to 0 after each carriage return. */ char menuBuffer[32]; static int received = 0; int evaluate = 0; // while there are unprocessed keystrokes in the receive_buffer, grab them and buffer // them into the menuBuffer while(serial_get_received_bytes(USB_COMM) != receive_buffer_position) { // place in a buffer for processing menuBuffer[received] = receive_buffer[receive_buffer_position]; print_usb_char( menuBuffer[received] ); #ifdef ECHO2LCD lcd_goto_xy(0,0); print("RX: ("); print_long(menuBuffer[received]); print_character(')'); for (int i=0; i<received; i++) { print_character(menuBuffer[i]); } #endif if ( menuBuffer[received] == '\r' ) { print_usb( "\n" ); evaluate = 1; } ++received; // Increment receive_buffer_position, but wrap around when it gets to // the end of the buffer. if ( receive_buffer_position == sizeof(receive_buffer) - 1 ) { receive_buffer_position = 0; } else { receive_buffer_position++; } } #ifdef ECHO2LCD lcd_goto_xy(0,1); print("RX: ("); print_long(received); print_character(')'); for (int i=0; i<received; i++) { print_character(menuBuffer[i]); } #endif // If there were keystrokes processed, check if a menu command if ( evaluate ) {/* // if only 1 received, it was MOST LIKELY a carriage return. // Even if it was a single keystroke, it is not a menu command, so ignore it. if ( 1 == received ) { received = 0; return; }*/ // Process buffer: terminate string, process, reset index to beginning of array to receive another command menuBuffer[received] = '\0'; #ifdef ECHO2LCD lcd_goto_xy(0,1); print("RX: ("); print_long(received); print_character(')'); for (int i=0; i<received; i++) { print_character(menuBuffer[i]); } #endif process_received_string(menuBuffer); received = 0; } }
// Parse user-input and implement commands int cli_service(void) { int received_bytes = 0; char c; bool buffers_updated = false; // process received bytes from the ring buffer and move them into the // command buffer while ((received_bytes = serial_get_received_bytes(USB_COMM)) != g_receive_buffer.ring_buffer_position) { // add the current byte to the command buffer buffers_updated = true; c = g_receive_buffer.ring_buffer[g_receive_buffer.ring_buffer_position]; LOG("%c", c); if (c == '\b') { if (g_receive_buffer.command_buffer_length) { LOG(" \b"); /* erase previous character from screen */ g_receive_buffer.command_buffer_length--; /* backspace */ } else { LOG(" "); /* fill back space */ } } else { g_receive_buffer.command_buffer[g_receive_buffer.command_buffer_length++] = c; } if (g_receive_buffer.ring_buffer_position == sizeof(g_receive_buffer.ring_buffer)) { g_receive_buffer.ring_buffer_position = 0; } else { g_receive_buffer.ring_buffer_position++; } } // Scan for a "line" (that is words followed by \r\n or \r\r\n. If found, // record that we have a match and null terminate the string if (buffers_updated) { char *loc; int idx; int end_of_command = false; bool command_waiting = false; if ((loc = strstr(g_receive_buffer.command_buffer, "\r\n")) != NULL) { LOG("\r\n"); idx = loc - g_receive_buffer.command_buffer; command_waiting = true; end_of_command = idx + 1; g_receive_buffer.command_buffer[idx] = '\0'; g_receive_buffer.command_buffer[idx + 1] = '\0'; } else if ((loc = strchr(g_receive_buffer.command_buffer, '\r')) != NULL) { LOG("\r\n"); idx = loc - g_receive_buffer.command_buffer; command_waiting = true; end_of_command = idx; g_receive_buffer.command_buffer[idx] = '\0'; } if (command_waiting) { // now, process the command and move any modify the contents of the // command_buffer to be in a valid state again cli_process_command(g_receive_buffer.command_buffer); LOG("#> "); memcpy(g_receive_buffer.command_buffer, &g_receive_buffer.command_buffer[end_of_command + 1], sizeof(g_receive_buffer.command_buffer) - end_of_command); g_receive_buffer.command_buffer_length -= (end_of_command + 1); } } return 0; } // end menu()