/**************************************************************************** Function: int main(void) Summary: main function Description: main function Precondition: None Parameters: None Return Values: int - exit code for main function Remarks: None ***************************************************************************/ int main(void) { DWORD size = 0; BOOL responseNeeded; BYTE mode = 0; BYTE wasMode = 0; BYTE pushButtonValues = 0xFF; BYTE potPercentage = 0xFF; BOOL buttonsNeedUpdate = FALSE; BOOL potNeedsUpdate = FALSE; BOOL motorON = FALSE; BOOL readyToRead = TRUE; BOOL writeInProgress = FALSE; BYTE tempValue = 0xFF; BYTE errorCode; ACCESSORY_APP_PACKET* command_packet = NULL; CLKDIV = 0; /* set for default clock operations Fcyc = 4MHz */ AD1PCFGL = 0xffff; AD1PCFGH = 0x0003; BOOL connected_to_app = FALSE; BOOL need_to_disconnect_from_app = FALSE; #if defined(__PIC32MX__) InitPIC32(); #endif #if defined(__dsPIC33EP512MU810__) || defined (__PIC24EP512GU810__) // Configure the device PLL to obtain 60 MIPS operation. The crystal // frequency is 8MHz. Divide 8MHz by 2, multiply by 60 and divide by // 2. This results in Fosc of 120MHz. The CPU clock frequency is // Fcy = Fosc/2 = 60MHz. Wait for the Primary PLL to lock and then // configure the auxilliary PLL to provide 48MHz needed for USB // Operation. PLLFBD = 38; /* M = 60 */ CLKDIVbits.PLLPOST = 0; /* N1 = 2 */ CLKDIVbits.PLLPRE = 0; /* N2 = 2 */ OSCTUN = 0; /* Initiate Clock Switch to Primary * Oscillator with PLL (NOSC= 0x3)*/ __builtin_write_OSCCONH(0x03); __builtin_write_OSCCONL(0x01); while (OSCCONbits.COSC != 0x3); // Configuring the auxiliary PLL, since the primary // oscillator provides the source clock to the auxiliary // PLL, the auxiliary oscillator is disabled. Note that // the AUX PLL is enabled. The input 8MHz clock is divided // by 2, multiplied by 24 and then divided by 2. Wait till // the AUX PLL locks. ACLKCON3 = 0x24C1; ACLKDIV3 = 0x7; ACLKCON3bits.ENAPLL = 1; while(ACLKCON3bits.APLLCK != 1); TRISBbits.TRISB5 = 0; LATBbits.LATB5 = 1; #endif USBInitialize(0); AndroidAppStart(&myDeviceInfo); responseNeeded = FALSE; mInitPOT(); InitializeTimer2For_PWM(); PwmInit(); //InitMOTOR(); DEBUG_Init(0); InitAllLEDs(); while(1) { //Keep the USB stack running USBTasks(); //If the device isn't attached yet, if(device_attached == FALSE || mode == 1) { buttonsNeedUpdate = TRUE; potNeedsUpdate = TRUE; need_to_disconnect_from_app = FALSE; connected_to_app = FALSE; size = 0; /**/ BYTE curPush = GetPushbuttons(); if ((curPush == 0x8) || (mode == 1)) { LED0_On(); mode = 1; if (wasMode == 0) { pot2LEDs(); PwmInit(); } tempValue = ReadPOT(); wasMode = 1; //If it is different than the last time we read the pot, then we need // to send it to the Android device if(tempValue != potPercentage) { potNeedsUpdate = TRUE; //setRPM(tempValue); setPWM(); } } if ((curPush == 0x4) || (mode == 0)) { mode = 0; //LED0_Off(); if (wasMode == 1) { SetLEDs(0b00000000); wasMode = 0; setRPM(0); } //Reset the accessory state variables InitAllLEDs(); //Continue to the top of the while loop to start the check over again. continue; } /* //Reset the accessory state variables InitAllLEDs(); //Continue to the top of the while loop to start the check over again. continue; }*/ //} } //If the accessory is ready, then this is where we run all of the demo code if(readyToRead == TRUE && mode == 0) { errorCode = AndroidAppRead(device_handle, (BYTE*)&read_buffer, (DWORD)sizeof(read_buffer)); //If the device is attached, then lets wait for a command from the application if( errorCode != USB_SUCCESS) { //Error DEBUG_PrintString("Error trying to start read"); } else { readyToRead = FALSE; } } size = 0; if(AndroidAppIsReadComplete(device_handle, &errorCode, &size) == TRUE) { //We've received a command over the USB from the Android device. if(errorCode == USB_SUCCESS) { //Maybe process the data here. Maybe process it somewhere else. command_packet = (ACCESSORY_APP_PACKET*)&read_buffer[0]; } else { //Error DEBUG_PrintString("Error trying to complete read request"); } } while(size > 0) { if(connected_to_app == FALSE) { if(command_packet->command == COMMAND_APP_CONNECT) { connected_to_app = TRUE; need_to_disconnect_from_app = FALSE; } } else { switch(command_packet->command) { case COMMAND_SET_LEDS: SetLEDs(command_packet->data); break; case COMMAND_APP_DISCONNECT: need_to_disconnect_from_app = TRUE; break; case COMMAND_SET_PWM: setRPM(command_packet->data); break; default: //Error, unknown command DEBUG_PrintString("Error: unknown command received"); break; } } //All commands in this example are two bytes, so remove that from the queue size -= 2; //And move the pointer to the next packet (this works because // all command packets are 2 bytes. If variable packet size // then need to handle moving the pointer by the size of the // command type that arrived. command_packet++; if(need_to_disconnect_from_app == TRUE) { break; } } if(size == 0) { readyToRead = TRUE; } //Get the current pushbutton settings tempValue = GetPushbuttons(); //If the current button settings are different than the last time // we read the button values, then we need to send an update to the // attached Android device if(tempValue != pushButtonValues) { buttonsNeedUpdate = TRUE; pushButtonValues = tempValue; } //Get the current potentiometer setting tempValue = ReadPOT(); //If it is different than the last time we read the pot, then we need // to send it to the Android device if(tempValue != potPercentage) { potNeedsUpdate = TRUE; potPercentage = tempValue; } //If there is a write already in progress, we need to check its status if( writeInProgress == TRUE ) { if(AndroidAppIsWriteComplete(device_handle, &errorCode, &size) == TRUE) { writeInProgress = FALSE; if(need_to_disconnect_from_app == TRUE) { connected_to_app = FALSE; need_to_disconnect_from_app = FALSE; } if(errorCode != USB_SUCCESS) { //Error DEBUG_PrintString("Error trying to complete write"); } } } if((need_to_disconnect_from_app == TRUE) && (writeInProgress == FALSE)) { outgoing_packet.command = COMMAND_APP_DISCONNECT; outgoing_packet.data = 0; writeInProgress = TRUE; errorCode = AndroidAppWrite(device_handle,(BYTE*)&outgoing_packet, 2); if( errorCode != USB_SUCCESS ) { DEBUG_PrintString("Error trying to send button update"); } } if(connected_to_app == FALSE) { //If the app hasn't told us to start sending data, let's not do anything else. continue; } //If we need up update the button status on the Android device and we aren't // already busy in a write, then we can send the new button data. if((buttonsNeedUpdate == TRUE) && (writeInProgress == FALSE)) { outgoing_packet.command = COMMAND_UPDATE_PUSHBUTTONS; outgoing_packet.data = pushButtonValues; errorCode = AndroidAppWrite(device_handle,(BYTE*)&outgoing_packet, 2); if( errorCode != USB_SUCCESS ) { DEBUG_PrintString("Error trying to send button update"); } buttonsNeedUpdate = FALSE; writeInProgress = TRUE; } //If we need up update the pot status on the Android device and we aren't // already busy in a write, then we can send the new pot data. if((potNeedsUpdate == TRUE) && (writeInProgress == FALSE)) { outgoing_packet.command = COMMAND_UPDATE_POT; outgoing_packet.data = potPercentage; errorCode = AndroidAppWrite(device_handle,(BYTE*)&outgoing_packet, 2); if( errorCode != USB_SUCCESS ) { DEBUG_PrintString("Error trying to send pot update"); } potNeedsUpdate = FALSE; writeInProgress = TRUE; } } //while(1) main loop }
int main() // MAIN PROGRAM AND First Level Menu { uint8 c,MenuSel; // local var to hold result of which menu item we selected InitPIC32(); // initialise MPU CloseWiFi(); // Wifi powered Off CloseBarcode(); // Barcode powered Off PeripheralReset(); InitLCD(); // initialise LCD CloseRFID(); // RFID off / sleep if(BrownOutReset()) // if the system crapped out previously, then just warn the user to charge the device and power off { LCDInform(" POWER! ","Please Charge!"); PWR_HOLD(0); // turn off power when above message acknowleged } while(1) // main program loop (do this until powered off or sleep) { MenuSel = GetMenuItem("READY","Scan Barcode","Scan RFID","Setup","Shutdown",""); if(MenuSel==1) // SCAN barcode { if(ReadBarcode()>1) // we got a barcode! Barcodes are encoded with the first letter designating the barcode type (or obviously the system wont have a clue!) { if(BarcodeData[0]=='J') OldJobMenu(BarcodeData); else if(BarcodeData[0]=='X') NewJobMenu(BarcodeData); else if(BarcodeData[0]=='S' && BarcodeData[1]=='M') StockMenu(BarcodeData); else if(BarcodeData[0]=='A') AssetMenu(BarcodeData); else if(BarcodeData[0]=='L') LabMenu(BarcodeData); else LCDInform("UID",BarcodeData); } } if(MenuSel==2) // scan RFID tag { CLS(); LCDWriteStrAt(1,6,"SCAN RFID TAG"); if (ReadRFIDUID()) // get UID { LCDInform("UID",uid.SerialNo); ReadRFIDBlock(1); } } if(MenuSel==3) // setup menu { SetupMenu(); } if(MenuSel==4) // power off { PWR_HOLD(0); // remove power to CPU - if on battery power this will turn off power to CPU and effectively end the program here! CLS(); if(ReadFloatAt(0,8,500,7)) LCDInform("Number was",KeypadData); } } }