/** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ int main(void) { char s1 = 0; char data =0; SetupHardware(); /* Create a regular character stream for the interface so that it can be used with the stdio.h functions */ CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream); uart_init(); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); GlobalInterruptEnable(); for (;;) { CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); s1 = fread(&charIn,1,1,&USBSerialStream); if(s1>0){ bufin[bcount] = charIn; bcount++; if(charIn == '\r'){ for(uint8_t i=0; i<bcount; i++){ fputc(bufin[0],&USBSerialStream); uart_write(bufin[i]); } bcount = 0; }else if(bcount>=256){ bcount=0; } } while(uart_char_is_waiting()){ data=uart_read(); fputc(data,&USBSerialStream); } } }
char uart_read() { while (!uart_char_is_waiting()); char x = UDR0; return x; }
void poll(){ // Main polling loop // Check for a character from the USART // See if there is a command comming in if (uart_char_is_waiting()) { char c = uart_read(); // Update the command state-machine if (c == '\n' || c == '\r' ) { // End of a command, process it and reset the command string if (current_command == COMMAND_NONE) { } else if (current_command == COMMAND_INFLATE) { syringe_inflate(); current_state = STATE_INFLATE; } else if (current_command == COMMAND_DEFLATE) { syringe_deflate(); current_state = STATE_DEFLATE; } else if (current_command == COMMAND_SET_PRESSURE) { int ret = sscanf_P(value_str, PSTR("%d"), &target_pressure); // printf_P(PSTR("1 = %d\r\n"), ret ); // printf_P(PSTR("2 = %d\r\n"), target_pressure ); } value_str_i = 0; current_command = COMMAND_NONE; current_command_state = CMD_STATE_COMMAND; } else { // In a command string if (current_command_state == CMD_STATE_COMMAND) { // Command character if (c == 'i') { // Inflate current_command = COMMAND_INFLATE; } else if (c == 'd') { // Deflate current_command = COMMAND_DEFLATE; } else if (c == 'p') { // Set pressure point current_command = COMMAND_SET_PRESSURE; } else { current_command = COMMAND_NONE; } current_command_state = CMD_STATE_EQUAL; } else if (current_command_state == CMD_STATE_EQUAL) { // Assume the second char is always '=' current_command_state = CMD_STATE_VALUE; } else if (current_command_state == CMD_STATE_VALUE) { // In value part of command value_str[value_str_i++] = c; if (value_str_i >= MAX_VALUE_LENGTH) { value_str_i = MAX_VALUE_LENGTH-1; } value_str[value_str_i] = 0; // Write NULL terminator } } } // Check the current pressure // take 100 samples and average them! // holder variables for temperature data uint16_t last_sample = 0; double this_temp; double pres_avg = 0.0; uint8_t i; for(i=0; i<100; i++) { last_sample = adc_read(); //this_temp = sampleTokPi(last_sample); this_temp = last_sample; // add this contribution to the average pres_avg = pres_avg + this_temp/100.0; } // Check to see if we hit the target presure if ( current_state == STATE_INFLATE ) { if ( pres_avg >= target_pressure ) { // Stop Inflating syringe_off(); current_state = STATE_IDLE; } } else if ( current_state == STATE_DEFLATE ) { if ( pres_avg <= target_pressure ) { // Stop Deflating syringe_off(); current_state = STATE_IDLE; } } // write message to LCD lcd_home(); lcd_write_string(PSTR("ADC: ")); lcd_write_int16(last_sample); lcd_write_string(PSTR(" of 1024 ")); lcd_line_two(); if ( current_state == STATE_IDLE ) { fprintf_P(&lcd_stream, PSTR("State: Idle ")); } else if ( current_state == STATE_RECV ) { fprintf_P(&lcd_stream, PSTR("State: Recv ")); } else if ( current_state == STATE_INFLATE ) { fprintf_P(&lcd_stream, PSTR("State: Inflate ")); } else if ( current_state == STATE_DEFLATE ) { fprintf_P(&lcd_stream, PSTR("State: Deflate ")); } lcd_line_three(); fprintf_P(&lcd_stream, PSTR("Target Pres: %d"), target_pressure ); // write message to serial port //printf_P(PSTR("%.2f degrees F\r\n"), temp_avg); //printf_P(PSTR("Pres = %d\r\n"), target_pressure ); //printf_P(PSTR("Value = %s\r\n"), value_str ); }