int main() { log_init(); log_printf("***** Hello from app-layer! *******"); ConnectionInit(); SoftReset(); while (1) { BOOL connected = ConnectionTasks(); if (!connected && state > STATE_OPEN_CHANNEL) { // just got disconnected log_printf("Disconnected"); SoftReset(); state = STATE_INIT; } switch (state) { case STATE_INIT: handle = INVALID_CHANNEL_HANDLE; state = STATE_OPEN_CHANNEL; break; case STATE_OPEN_CHANNEL: if ((handle = OpenAvailableChannel()) != INVALID_CHANNEL_HANDLE) { log_printf("Connected"); state = STATE_WAIT_CHANNEL_OPEN; } break; case STATE_WAIT_CHANNEL_OPEN: if (ConnectionCanSend(handle)) { log_printf("Channel open"); AppProtocolInit(handle); state = STATE_CONNECTED; } break; case STATE_CONNECTED: AppProtocolTasks(handle); break; case STATE_ERROR: ConnectionCloseChannel(handle); SoftReset(); state = STATE_INIT; break; } } return 0; }
int main() { CFGCONbits.JTAGEN = 0; // turn off JTAG, get back those pins for IO use // set PIC32to max computing power DEBUGLED = 0; // enable multi-vector interrupts INTEnableSystemMultiVectoredInt(); INTEnableInterrupts(); PIC32MX250_setup_pins(); SYSTEMConfigPerformance(SYS_FREQ); setTimer2(OUTPUT_FREQ); // setupTimer3(BUFFER_FREQ); initPWM(); //Initializing PWM on OC3. // Initialize texture buffer and index to zero int i; tBuff.Index = 0; //init to zero tBuff.On = 0; //turn off tBuff.Length0 = 0; //start with zero length tBuff.Length1 = 0; //start with zero length tBuff.Front = 0; for (i = 0; i < MAX_BUFFER_LENGTH; i++) { tBuff.Buff0[i] = 0; //init entire buffer to zero tBuff.Buff1[i] = 0; //init entire buffer to zero } // Initialize the USB host ConnectionInit(); DEBUGLED = 1; //Main USB State Machine while (1) { // Keep the USB connection running; // Handle incoming data and manage outgoing data. ConnectionTasks(); // Main state machine switch (state) { case STATE_INIT: state = STATE_WAITING; h = INVALID_CHANNEL_HANDLE; break; case STATE_WAITING: DEBUGLED = 0; if (ADBAttached()) { state = STATE_CONNECTING; } break; case STATE_CONNECTING: if (ADBConnected()) { // Open a channel to the Android device // See "adb.h" in libadb for more details // (I don't think the name tcp:4545 matters) h = ADBOpen("tcp:4545", &ADBCallback); if (h != INVALID_CHANNEL_HANDLE) { state = STATE_CONNECTED; WriteCoreTimer(0); // Send plaintext and let the recipient do formatting ADBWrite(h & 0xFF, "Hello from TPAD!", 17); } } break; case STATE_CONNECTED: DEBUGLED = 1; if (!ADBAttached()) { state = STATE_INIT; } if (ADBChannelReady(h)) { // Execute tasks that rely on the Android-PIC32 connection // Here we will just wait for messages to come in and be handled below } // Timeout timer. If the coretimer is not reset by a keepalive command from the Android, the PIC will reset the USB communications if (ReadCoreTimer() > 200000000) { state = STATE_INIT; } break; } // end state machine } // end while loop return 0; }