int main() { char whoamiCheck = 0; unsigned char data[14]; short output[7]; int oc1New = 0; int oc2New = 0; // PIC32 Setup __builtin_disable_interrupts(); __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583); BMXCONbits.BMXWSDRM = 0x0; // 0 data RAM access wait states INTCONbits.MVEC = 0x1; // enable multi vector interrupts DDPCONbits.JTAGEN = 0; // disable JTAG to get pins back TRISAbits.TRISA4 = 0; // RA4 output TRISBbits.TRISB4 = 1; // RB4 input LATAbits.LATA4 = 0; // LED off i2cMasterSetup(); tim2Setup(); oc1Setup(); oc2Setup(); __builtin_enable_interrupts(); whoamiCheck = i2cMasterRead(IMU_ADDR,WHO_AM_I); if (whoamiCheck == WHOAMI_VAL) { LATAbits.LATA4 = 1; } while(1) { _CP0_SET_COUNT(0); i2cMasterReadAll(IMU_ADDR,OUT_TEMP_L,14,data); char2short(data,output,14); oc1New = (PER2+1)/2+(output[4]/20); oc2New = (PER2+1)/2+(output[5]/20); if(oc1New>(PER2+1)){ oc1New=(PER2+1); } if(oc2New>(PER2+1)){ oc2New=(PER2+1); } OC1RS = oc1New; OC2RS = oc2New; LATAbits.LATA4 = !LATAbits.LATA4; while(_CP0_GET_COUNT()<UPDATER){ ; } } }
int readWaveData(unsigned char *fileName, struct wav_file *waveData) { //some local variables FILE *fp; long fileSize; size_t result; unsigned char buffer[16]; unsigned short int waveFormat; // 0x0001 - WAVE_FORMAT_PCM; 0x0003 - WAVE_FORMAT_IEEE_FLOAT; 0xFFFE - WAVE_FORMAT_EXTENSIBLE; otherwise unsupported //Open file and check for an error fp = fopen(fileName, "r"); if (fp == NULL) { fprintf(stderr, "Failed to open file: '%s'!\n", fileName); fclose(fp); return -1; } //Obtain file size fseek(fp, 0, SEEK_END); //Put file reader pointer to the very end fileSize = ftell(fp); //Check current reader pointer position rewind(fp); //Return reader pointer to the beginning of the file printf("File size: %d bytes.\n", fileSize); //------------------DEBUG------------------ // 1 - Check if it "RIFF" file // 2 - Check if it has "WAVE" format // 3 - Check if it has "fmt " chunk ID // 4 - Make decision based on wave format (PCM, nonPCM, extensible) // 1 - Check if it "RIFF" file result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -1; } printf("Chunk ID: %s \n", buffer); //------------------DEBUG------------------ if (memcmp("RIFF", buffer, 4) != 0) { fprintf(stderr, "This wave file doesn't have RIFF chunk!\n"); fclose(fp); return -2; } memcpy(waveData->chunkId, buffer, 4); // read chunk RIFF size result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("Chunk size: %d\n", char2int(buffer));//------------------DEBUG------------------ waveData->chunkSize = char2int(buffer); // 2 - Check if it has "WAVE" format result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -1; } printf("Wave ID: %s \n", buffer); //------------------DEBUG------------------ if (memcmp("WAVE", buffer, 4) != 0) { fprintf(stderr, "This wave file doesn't have WAVE format!\n"); fclose(fp); return -2; } memcpy(waveData->format, buffer, 4); // 3 - Check if it has "fmt " chunk ID result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -1; } printf("Sub Chunk 1 ID: %s \n", buffer); //------------------DEBUG------------------ if (memcmp("fmt ", buffer, 4) != 0) { fprintf(stderr, "This wave file doesn't have 'fmt ' chunk ID!\n"); fclose(fp); return -2; } memcpy(waveData->subChunk1Id, buffer, 4); // read sub chunk 1 size result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("Chunk size: %d\n", char2int(buffer)); //------------------DEBUG------------------ waveData->subChunk1Size = char2int(buffer); // read wave audio format result = fread(buffer, 1, 2, fp); if(result != 2) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("wFormatTag: %d\n", char2short(buffer)); //------------------DEBUG------------------ waveFormat = char2short(buffer); waveData->audioFormat = char2short(buffer); // read number of channels result = fread(buffer, 1, 2, fp); if(result != 2) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("Number of channels: %d \n", char2short(buffer)); //------------------DEBUG------------------ waveData->numChannels = char2short(buffer); // read sample rate result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("Samples per sec: %d \n", char2int(buffer)); //------------------DEBUG------------------ waveData->sampleRate = char2int(buffer); result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("AvgBytes per sec: %d \n", char2int(buffer)); //------------------DEBUG------------------ waveData->byteRate = char2int(buffer); result = fread(buffer, 1, 2, fp); if(result != 2) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("nBlockAlign: %d \n", char2short(buffer)); //------------------DEBUG------------------ waveData->blockAlign = char2short(buffer); result = fread(buffer, 1, 2, fp); if(result != 2) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("wBitsPerSample: %d \n", char2short(buffer)); //------------------DEBUG------------------ waveData->bitsPerSample = char2short(buffer); // ============= Make Decision based on format =================================== switch(waveFormat) { case 0x0001: // WAVE_FORMAT_PCM break; case 0x0003: // WAVE_FORMAT_IEEE_FLOAT result = fread(buffer, 1, 2, fp); if(result != 2) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("cbSize: %d \n", char2short(buffer)); //------------------DEBUG------------------ waveData->cbSize = char2short(buffer); result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("ckID: %s \n", buffer); //------------------DEBUG------------------ memcpy(waveData->subChunk2Id, buffer, 4); result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("cksize: %d \n", char2int(buffer)); //------------------DEBUG------------------ waveData->subChunk2Size = char2int(buffer); result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("dwSampleLength: %d \n", char2int(buffer)); //------------------DEBUG------------------ waveData->sampleLength = char2int(buffer); break; case 0xFFFE: // WAVE_FORMAT_EXTENSIBLE result = fread(buffer, 1, 2, fp); if(result != 2) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("cbSize: %d \n", char2short(buffer)); //------------------DEBUG------------------ waveData->cbSize = char2short(buffer); // 3 missing result = fread(buffer, 1, 2, fp); if(result != 2) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("wValidBitsPerSample: %d \n", char2short(buffer)); //------------------DEBUG------------------ waveData->wValidBitsPerSample = char2short(buffer); result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("dwChannelMask: %d \n", char2int(buffer)); //------------------DEBUG------------------ waveData->dwChannelMask = char2int(buffer); result = fread(buffer, 1, 16, fp); if(result != 16) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("SubFormat: %s \n", buffer); //------------------DEBUG------------------ memcpy(waveData->SubFormat, buffer, 16); //--- result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("ckID: %s \n", buffer); //------------------DEBUG------------------ memcpy(waveData->subChunk2Id, buffer, 4); result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("cksize: %d \n", char2int(buffer)); //------------------DEBUG------------------ waveData->subChunk2Size = char2int(buffer); result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("dwSampleLength: %d \n", char2int(buffer)); //------------------DEBUG------------------ waveData->sampleLength = char2int(buffer); break; default: // WAVE_FORMAT_ALAW or WAVE_FORMAT_MULAW fprintf(stderr, "Unsupported wave format\n"); fclose(fp); return -2; break; } result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("ckID: %s \n", buffer); //------------------DEBUG------------------ memcpy(waveData->subChunk3Id, buffer, 4); result = fread(buffer, 1, 4, fp); if(result != 4) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } printf("cksize: %d \n", char2int(buffer)); //------------------DEBUG------------------ waveData->subChunk3Size = char2int(buffer); // read actual data set waveData->data = malloc(waveData->subChunk3Size * sizeof *waveData->data); result = fread(waveData->data, 1, waveData->subChunk3Size, fp); if(result != waveData->subChunk3Size) { fprintf(stderr, "Failed reading file!\n"); fclose(fp); return -2; } waveData->playProgress = 0; //Close the file fclose(fp); }
void APP_Tasks ( void ) { static int8_t vector = 0; static uint8_t movement_length = 0; static bool sent_dont_move = false; unsigned char data[14]; short output[7]; /* Check the application's current state. */ switch ( appData.state ) { /* Application's initial state. */ case APP_STATE_INIT: { /* Open the device layer */ appData.deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(appData.deviceHandle != USB_DEVICE_HANDLE_INVALID) { /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(appData.deviceHandle, 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: /* Check if the device is configured. The * isConfigured flag is updated in the * Device Event Handler */ if(appData.isConfigured) { appData.state = APP_STATE_MOUSE_EMULATE; } break; case APP_STATE_MOUSE_EMULATE: APP_ProcessSwitchPress(); /* The following logic rotates the mouse icon when * a switch is pressed */ if(appData.isSwitchPressed) { /* Toggle the mouse emulation with each switch press */ appData.emulateMouse ^= 1; appData.isSwitchPressed = false; } if(appData.emulateMouse) { sent_dont_move = false; if(movement_length > 50) { _CP0_SET_COUNT(0); i2cMasterReadAll(IMU_ADDR,OUT_TEMP_L,14,data); char2short(data,output,14); LATAbits.LATA4 = !LATAbits.LATA4; while(_CP0_GET_COUNT()<UPDATER){;} appData.mouseButton[0] = MOUSE_BUTTON_STATE_RELEASED; appData.mouseButton[1] = MOUSE_BUTTON_STATE_RELEASED; appData.xCoordinate =(int8_t)(output[4]/500);//dir_table[vector & 0x07] ; appData.yCoordinate =(int8_t)(output[5]/500);//dir_table[(vector+2) & 0x07]; vector ++; movement_length = 0; } } else { appData.mouseButton[0] = MOUSE_BUTTON_STATE_RELEASED; appData.mouseButton[1] = MOUSE_BUTTON_STATE_RELEASED; appData.xCoordinate = 0; appData.yCoordinate = 0; } if(!appData.isMouseReportSendBusy) { if(((sent_dont_move == false) && (!appData.emulateMouse)) || (appData.emulateMouse)) { /* This means we can send the mouse report. The isMouseReportBusy flag is updated in the HID Event Handler. */ appData.isMouseReportSendBusy = true; /* Create the mouse report */ MOUSE_ReportCreate(appData.xCoordinate, appData.yCoordinate, appData.mouseButton, &mouseReport); if(memcmp((const void *)&mouseReportPrevious, (const void *)&mouseReport, (size_t)sizeof(mouseReport)) == 0) { /* Reports are same as previous report. However mouse reports * can be same as previous report as the co-ordinate positions are relative. * In that case it needs to be send */ if((appData.xCoordinate == 0) && (appData.yCoordinate == 0)) { /* If the coordinate positions are 0, that means there * is no relative change */ if(appData.idleRate == 0) { appData.isMouseReportSendBusy = false; } else { /* Check the idle rate here. If idle rate time elapsed * then the data will be sent. Idle rate resolution is * 4 msec as per HID specification; possible range is * between 4msec >= idlerate <= 1020 msec. */ if(appData.setIdleTimer * APP_USB_CONVERT_TO_MILLISECOND >= appData.idleRate * 4) { /* Send REPORT as idle time has elapsed */ appData.isMouseReportSendBusy = true; } else { /* Do not send REPORT as idle time has not elapsed */ appData.isMouseReportSendBusy = false; } } } } if(appData.isMouseReportSendBusy == true) { /* Copy the report sent to previous */ memcpy((void *)&mouseReportPrevious, (const void *)&mouseReport, (size_t)sizeof(mouseReport)); /* Send the mouse report. */ USB_DEVICE_HID_ReportSend(appData.hidInstance, &appData.reportTransferHandle, (uint8_t*)&mouseReport, sizeof(MOUSE_REPORT)); appData.setIdleTimer = 0; } movement_length ++; sent_dont_move = true; } } break; case APP_STATE_ERROR: break; /* The default state should never be executed. */ default: { /* TODO: Handle error in application's state machine. */ break; } } }