int main(void) { uint8_t i = 1; uint8_t count = 3; setup(); blink(3); _delay_ms(100); while (1) { // Print greeting at most count times. if (i <= count) { if (USB_DeviceState == DEVICE_STATE_Configured && ok_to_send) { blink(i); CDC_Device_SendString(&VirtualSerial_CDC_Interface, "Hello World! "); CDC_Device_SendByte(&VirtualSerial_CDC_Interface, '0' + i); CDC_Device_SendString(&VirtualSerial_CDC_Interface, "\r\n"); CDC_Device_Flush(&VirtualSerial_CDC_Interface); i++; } } if (USB_DeviceState == DEVICE_STATE_Configured) { /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */ CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); } CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } }
/** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */ void CheckJoystickMovement(void) { uint8_t JoyStatus_LCL = Joystick_GetStatus(); char* ReportString = NULL; static bool ActionSent = false; if (JoyStatus_LCL & JOY_UP) ReportString = "Joystick Up\r\n"; else if (JoyStatus_LCL & JOY_DOWN) ReportString = "Joystick Down\r\n"; else if (JoyStatus_LCL & JOY_LEFT) ReportString = "Joystick Left\r\n"; else if (JoyStatus_LCL & JOY_RIGHT) ReportString = "Joystick Right\r\n"; else if (JoyStatus_LCL & JOY_PRESS) ReportString = "Joystick Pressed\r\n"; else ActionSent = false; if ((ReportString != NULL) && (ActionSent == false)) { ActionSent = true; CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString); } else{ CDC_Device_SendString(&VirtualSerial_CDC_Interface, "No report\r\n"); } }
/** 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) { 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); sei(); DDRB |= (1 << 4); PORTB |= (1 << 4); for (;;) { int16_t c = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); if (c > 0){ switch (c) { case '1': // this will eventually be to strobe the modem on pin of a telit module // set port to output mode and low state // stall for 1000 ms // set port back to high impedance CDC_Device_SendString(&VirtualSerial_CDC_Interface, "received 1\r\n"); DDRD |= (1 << 0); PORTD &= ~(1 << 0); _delay_ms(1000); DDRD &= ~(1 << 0); PORTD |= (1 << 0); break; case 'r': // this will be to strobe the reset pin of a telit module CDC_Device_SendString(&VirtualSerial_CDC_Interface, "received r\r\n"); PORTB &= ~(1 << 4); _delay_ms(200); PORTB |= (1 << 4); break; case '?': // this wil be to inquire abouth the powermon pin of a telit module CDC_Device_SendString(&VirtualSerial_CDC_Interface, "received ?\r\n"); PORTB |= ~(1 << 4); break; default: CDC_Device_SendString(&VirtualSerial_CDC_Interface, "unrecognized input\r\n"); } } CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } }
void print_usb(char* input) { vTaskSuspendAll(); CDC_Device_SendString(&USB_Interface, input); xTaskResumeAll(); }
void sendData(){ for(uint16_t i=0; i<sizeof(random_data); i++){ //CDC_Device_SendByte(&cdcif, random_data[i]); } if(CDC_Device_SendString(&cdcif, "Fnord!\n") == ENDPOINT_RWSTREAM_NoError){ PORTD |= 0x30; }else{ PORTD &= 0xCF; } CDC_Device_Flush(&cdcif); }
uint8_t classify_sensors(uint8_t nSensors) { int i = 0; /* classify sensors */ for (i = 0; i < nSensors; i++) { if (gSensorIDs[i][0] == DS18B20_FAMILY_CODE) { CDC_Device_SendString(&USB_Interface, "DS18B20 found "); } if (DS18X20_get_power_status( &gSensorIDs[i][0]) == DS18X20_POWER_PARASITE) { CDC_Device_SendString(&USB_Interface, "parasite\n\r"); } else { CDC_Device_SendString(&USB_Interface, "externally\n\r"); } } }
/* This function sends string data to the USB host. */ status_t USBVC001_SendString(const char* const DataString) { status_t Status = (uint32_t)DAVEApp_SUCCESS; do{ /* Send string to the host */ if(CDC_Device_SendString(&USBVC001_CDCInterface, DataString) != ENDPOINT_RWSTREAM_NoError) { Status = USBVC001_USBCDC001_ERROR; } else if(CDC_Device_Flush(&USBVC001_CDCInterface) != ENDPOINT_READYWAIT_NoError) { Status = USBVC001_USBCDC001_ERROR; } }while(0); return Status; }
void usb_serial_writeln(const char* const buffer) { CDC_Device_SendString(&VirtualSerial_CDC_Interface, buffer); usb_serial_write_P(PSTR("\r\n")); }
void usb_serial_write(const char* const buffer) { CDC_Device_SendString(&VirtualSerial_CDC_Interface, buffer); }
static void vUserInput(void) { TickType_t xLastWakeTime; const TickType_t xFrequency = 100; // Initialise the xLastWakeTime variable with the current time. xLastWakeTime = xTaskGetTickCount(); vTaskSuspendAll(); CDC_Device_SendString(&USB_Interface, "enter command: "); xTaskResumeAll(); for (;;) { int count = 0; buffer[0] = 0; /* read bytes from USB interface */ vTaskSuspendAll(); BytesAvailable = CDC_Device_BytesReceived(&USB_Interface); for (count = 0; count < BytesAvailable; count++) { buffer[count] = CDC_Device_ReceiveByte(&USB_Interface); } xTaskResumeAll(); /* Null terminate buffer*/ buffer[count] = 0; /* Buffer is not empty */ if (buffer[0] != 0) { /* echo user input back */ vTaskSuspendAll(); CDC_Device_SendString(&USB_Interface, buffer); xTaskResumeAll(); /* combine into buffer */ strncat(end_buffer, buffer, 1); if (buffer[0] == '\r') { strncat(end_buffer, '\0', 1); handle_input(end_buffer); /* 'reset' end_buffer */ end_buffer[0] = '\0'; vTaskSuspendAll(); CDC_Device_SendString(&USB_Interface, "enter command: "); xTaskResumeAll(); } } vTaskDelayUntil(&xLastWakeTime, xFrequency); } }
void handle_input(char* input) { if (state == START) { if (strcmp("set fan\r", input) == 0) { print_usb("enter fan speed, 0...40 \n\r"); /* advance state machine */ state = SET_FAN; } else if (strcmp("set pump\r", input) == 0) { print_usb("enter pump speed, 0...200 \n\r"); /* advance state machine */ state = SET_PUMP; } else if (strcmp("set led\r", input) == 0) { print_usb("enter led brightness, 0...255 \n\r"); /* advance state machine */ state = SET_LED; } else if (strcmp("read temp\r", input) == 0) { print_usb("reading temp... \n\r"); /* advance state machine */ state = READ_TEMP; } else { /* didnt understand string */ print_usb("did not understand command \n\r"); /* dont change state */ state = START; } } else if (state == SET_LED) { static uint16_t pwm; char *garbage = NULL; pwm = strtol(input, &garbage, 0); OCR4A = pwm; print_usb("led set. \n\r"); /* go back to start */ state = START; } else if (state == SET_FAN) { static uint16_t pwm; char *garbage = NULL; pwm = strtol(input, &garbage, 0); OCR3A = pwm; print_usb("fan set. \n\r"); /* go back to start */ state = START; } else if (state == SET_PUMP) { static uint16_t pwm; char *garbage = NULL; pwm = strtol(input, &garbage, 0); OCR4B = pwm; print_usb("pump set. \n\r"); /* go back to start */ state = START; } else if (state == READ_TEMP) { uint8_t nSensors; char maxres_buffer[10]; ow_set_bus(&PIND, &PORTD, &DDRD, PD4); ow_reset(); /* search for temperature sensors */ nSensors = search_sensors(); /* classify sensors */ classify_sensors(nSensors); /* measure temperature */ measure_temp(nSensors, temp_eminus4); DS18X20_format_from_maxres(temp_eminus4[0], maxres_buffer, 10); print_usb("\n\r "); print_usb("temp max res 1: "); print_usb(maxres_buffer); print_usb("\n\r "); DS18X20_format_from_maxres(temp_eminus4[1], maxres_buffer, 10); print_usb("temp max res 2: "); print_usb(maxres_buffer); print_usb("\n\r "); /* go back to start */ state = START; } else { CDC_Device_SendString(&USB_Interface, "went into unknown state!? \n\r"); state = START; } }