Ejemplo n.º 1
0
//"gets" function through USBUART instead of stdio
//echos the input
//terminates on CR or LF
//returns size of string
uint16 UARTgets(uint8 * str, int bufferSize){
	uint16 i = 0;
	uint16 size;
	do{
		while(USBUART_DataIsReady() == 0);
		size = USBUART_GetCount();
		if(size+i > bufferSize){
			str[i]=0;
			UARTprintf("gets: Buffer not big enough. Size: %d Input: %s\r\n", size, str);
			return i;
		}
		USBUART_GetData(&(str[i]),size);
		i+=size;
		if(size == 1)UARTputc(str[i-1]); //echo if single char
	}while((str[i-1] != '\n') && (str[i-1] != '\r'));
	str[--i] = 0; //null terminate, remove the CR/LF
	//UARTprintf("gets: LF or CR found at end of message. Size: %d Input: %s\r\n", size, str);
	//while(USBUART_CDCIsReady()==0);
	//USBUART_PutCRLF(); //print CRLF
	return i;
}
Ejemplo n.º 2
0
int main()
{
    CyGlobalIntEnable; 
   
    /* Start SPI bus. */
    SDSPI_Start();

    
    // set up the USB connection
    USBUART_Start(0, USBUART_3V_OPERATION);
    while(USBUART_GetConfiguration() == 0){};
    USBUART_CDC_Init();
    
     
    
    while(true) {
        
        _USBBufDataCnt = USBUART_GetCount();
        
        // when we get usb data, grab it and parse it
        if (_USBBufDataCnt != 0) {
            if (_USBBufDataCnt > 128) {
                // too much data, do something (we throw away all the data)
                while(_USBBufDataCnt > 128) {
                    USBUART_GetData(_USBRxBuffer, 128);
                    _USBBufDataCnt -= 128;
                }
                USBUART_GetData(_USBRxBuffer, _USBBufDataCnt);
            }
            else {
                USBUART_GetData(_USBRxBuffer, _USBBufDataCnt);
                bool parseRes = Parse_USBBuffer();
                
                // if we got a vailid command, figure out what it was and run it
                if (parseRes) {
                    if (!strcmp(_CmdBuf, "list")) {
                        List_Dir();
                    }
                    else if (!strcmp(_CmdBuf, "?")) {
                        Display_Help();
                    }
                    else if (!strcmp(_CmdBuf, "mount")) {
                        Mount_Disk(&_FatFs);
                    }
                    else if (!strcmp(_CmdBuf, "free")) {
                        Get_FreeSpace(&_FatFs);        
                    }
                    else if (!strcmp(_CmdBuf, "print")) {
                        Print_File(_FnameBuf);
                    }
                    else if (!strcmp(_CmdBuf, "erase")) {
                        Erase_File(_FnameBuf);   
                    }
                    else if (!strcmp(_CmdBuf, "create")) {
                        Create_File(_FnameBuf);   
                    }
                    else if (!strcmp(_CmdBuf, "append")) {
                        Append_File(_FnameBuf, _DataBuf);
                    }
                }
            }
        }
    }
}