Example #1
0
void ProcessIO( void )
{
    if ( ( USBGetDeviceState() < CONFIGURED_STATE ) || USBIsDeviceSuspended() )
        return;

    ServiceRequests();
}
Example #2
0
void ProcessIO(void)
{   
    //BlinkUSBStatus();

    // User Application USB tasks
    if((usb_device_state < CONFIGURED_STATE)||(UCONbits.SUSPND==1)) return;
    
    // UCAM
    ServiceRequests();
}
Example #3
0
int TCPServer::ServiceLoop()
{
  RequestPacket rq;
  ReplyPacket rp;
  StatusPacket sp;
  
  int sz;
  
  Adafruit_CC3000_ClientRef client = _server.available();
  if (client) {
    // Check if there is data available to read.
    if (client.available() > 0) {
      if(sz = client.read(static_cast<void*>(&rq), sizeof(RequestPacket))){
	#ifdef AAAAAA
	Serial.print("rq._command = ");
	Serial.println(rq._command);
	#endif
      }

      if(ServiceRequests(&rq, &rp, &sp) == 0){
	if(rq._command != CMD_QUERY_STATE){

	  char* packet_ptr = (char*)&rp;
	  char* packet_end = packet_ptr + sizeof(ReplyPacket);
	  int sz = sizeof(ReplyPacket);
	  int sent_sz;	  
	  do{
	    sent_sz += client.write(static_cast<void*>(packet_ptr), sz);
	    packet_ptr += sent_sz;
	    sz -= sent_sz;
	  } while(sz > 0);
	  
	}
	else {
	  char* packet_ptr = (char*)&sp;
	  char* packet_end = packet_ptr + sizeof(StatusPacket);
	  int sz = sizeof(StatusPacket);
	  int sent_sz;
	  do{
	    sent_sz += client.write(static_cast<void*>(packet_ptr), sz);
	    packet_ptr += sent_sz;
	    sz -= sent_sz;
	  } while(sz > 0);
	}
      } // ServiceRequests
      else {
	Serial.println(F("TCPServer::ServiceLoop: ServiceRequests() failed"));
	return -1;
      }
    } // client.available()
  } // client

  return 0;
}
Example #4
0
/******************************************************************************
 * Function:        void ProcessIO(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function is a place holder for other user routines.
 *                  It is a mixture of both USB and non-USB tasks.
 *
 * Note:            None
 *****************************************************************************/
void ProcessIO(void)
{   
    //Blink the LEDs according to the USB device status
    if(blinkStatusValid)
    {
        BlinkUSBStatus();
    }

    // User Application USB tasks
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;

    //respond to any USB commands that might have come over the bus
	PollTempOnHPCExplorer();			
    ServiceRequests();

    //if we are logging temp data
    if(temp_mode == TEMP_LOGGING)
    {
        #if defined(__18CXX)
        //if the 1 second timer has expired (PIC18 only)
        if(INTCONbits.TMR0IF == 1)
        #endif
        {
            #if defined(__18CXX)
            INTCONbits.TMR0IF = 0;          // Clear flag
            TMR0H = TIMER0H_VAL;
            TMR0L = TIMER0L_VAL;            // Reinit timer value;
            #endif

            //get a new temp reading
            if(AcquireTemperature())
            {
                //store the new value in the buffer
                temp_data[pTemp] = temperature.Val;
                
                // First update valid_temp
                if(valid_temp < 30)         // 30 data points max
                    valid_temp++;
                    
                // Next update pTemp
                if(pTemp == 29)
                    pTemp = 0;
                else
                    pTemp++;
            }//end if
        }//end if
    }//end if
}//end ProcessIO
Example #5
0
/******************************************************************************
 * Function:        void ProcessIO(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function is a place holder for other user routines.
 *                  It is a mixture of both USB and non-USB tasks.
 *
 * Note:            None
 *****************************************************************************/
void ProcessIO(void)
{   
    //**********Blink the LEDs according to the USB device status****************
    if(blinkStatusValid)
    {
        BlinkUSBStatus();
    }

	//****************Main background code goes here**************************
	// This code runs if there is neither a HISR nor a LISR
	// This cannot be timed data but should still run at a reasonable speed

	//*********switch ON the backlight if a voltage is applied (in all circumstances)****************
	//**********also deal with the backlight counter - count up and ramp down the light when done****
	if(backlight_flag==TRUE)	// only do this if the backlight is enabled
	{
		if(voltage_applied==TRUE)	// only do this if the backlight is there
		{
			LCD_ramp_up=TRUE;
			LCD_backlight_counter=0;
		}
		else
		{
			LCD_backlight_counter++;
		}

		if(LCD_backlight_counter>=LCD_backlight_max)
		{
			// While here also count up for the backlight delay rountine
			// If an buttons are pressed then reset the counter to wait
			// Counter will have quite big numbers in it as this will run quickly
			// Want to set this delay in approx seconds - might need more counters?
			LCD_ramp_up=FALSE;
			LCD_backlight_counter=0;
		}
	}

	//************* CHECK BUTTONS - HAVE ANY BEEN PRESSED?*******************************
	// This routine checks the switches. If one has been pressed there is a delay loop to ensure that it is
	// a positive press and to debounce. 
	switch(switch_mode)
	{
		case 1:
		{			
			//Is either button pressed?		
			//N return to 1
			//Y goto state 2
			if(sw1 || sw2 == TRUE)
			{	
				switch_mode=2;
			}
		}
		break;
		
		case 2:
		{
			// Wait 0.1ish seconds (debouce) 	
			// Goto state 3
			counter1++;
			if(counter1>= 5000)	// Just a short counter to do a debouce
			{
				counter1=0;
				switch_mode=3;
			}
		}
		break;

		case 3:
		{
			// Is a button pressed?
			// Look up SW0 and SW1 chart:
			// SW0 SW1
			//	0	0	Return to 1
			//	1	0	If adjustment_flag is TRUE goto state 7
			//			Else Goto state 4 - scroll through values
			//	0   1	If adjustment_flag is TRUE goto state 7
			//			Else Goto state 10 - start/stop the hold values
			// 	1	1	If adjustment_flag is TRUE goto state 8
			//			Else Goto state 6
			
			if(sw1==FALSE && sw2==FALSE)
			{
				switch_mode=1;		// No button pressed hence return to waiting for button press
				break;
			}

			// Is the LCD backlight on already? Does it need to be on?
			if(backlight_flag==TRUE)	// only do this if the backlight is there
			{
				if(LCD_ramp_up==FALSE)
				{
					switch_mode=11;
					break;
				}
			}

			if ((sw1==TRUE&&sw2==FALSE))  // 1 0 pressed 
			{
				buzzer_flag=TRUE;		// Beep the buzzer

				if(adjustment_flag==TRUE)
				{
					switch_mode=7;	// adjust value up/down
				} else {
					switch_mode=4;	// scroll up/down	
				}
			}
			else if ((sw1==FALSE&&sw2==TRUE))  // 0 1 pressed 
			{
				buzzer_flag=TRUE;		// Beep the buzzer
				if(adjustment_flag==TRUE)
				{
					switch_mode=7;	// adjust value up/down
				} else {
					switch_mode=10;	// enter start/stop mode
				}
			}
			else if (sw1==TRUE&&sw2==TRUE)	// Both sw1 and sw2 pressed - enter or leave adjustment mode
			{
				buzzer_flag=TRUE;		// Beep the buzzer
				if(adjustment_flag==TRUE)
				{
					switch_mode=8;		// leave adjustment mode
				} else {
					switch_mode=6;		// Enter adjustment mode
				}			
			}
	
			LCD_backlight_counter=0;	// This resets the LCD backlight counter when a button has been pressed
		}
		break;

		case 4:
		{
			//If SW0 = 1 then scroll up (increment display counter)
			//If SW1 = 1 then scroll down (decrement display counter)
			//Goto state 5
			if(sw1==TRUE)
			{
				display_mode++;
				if(display_mode>max_display_mode)
				{
					display_mode=1;
				}
				switch_pressed=1;	// Tells case 5 which switch was previously pressed
			}
			if(sw2==TRUE)
			{
				display_mode--;
				if(display_mode<=0)
				{
					display_mode=max_display_mode;
				}
				switch_pressed=2;	// Tells case 5 which switch was previously pressed
			}
			update_display=TRUE;
			switch_mode=5;	// Go to the delay to stop scrolling through too quickly
		}
		break;

		case 5:
		{
			//Count for 0.5s – this is to stop scrolling through too quickly.
			//During this:
			//If the switch changes from 1 to zero then goto state 1
			//At the end
			//Goto state 1
			counter1++;
			if(counter1>= 30000)	// Counter to stop things scrolling through too quickly
			{
				counter1=0;
				switch_mode=1;
			}
			if(switch_pressed==1&&sw1==FALSE)	// This is the case if sw1 is released, do not need the counter
			{
				counter1=0;			//	reset the counter
				switch_pressed=0;	//  reset the switch_pressed
				switch_mode=1;		//  return from this case as switch released
			}
			if(switch_pressed==2&&sw2==FALSE)	// This is the case if sw2 is released, do not need the counter
			{
				counter1=0;			//	reset the counter
				switch_pressed=0;	//  reset the switch_pressed
				switch_mode=1;		//  return from this case as switch released
			}
		}
		break;

		case 6:
		{
			//Entered this state to do adjustments
			//Want to check what display mode we are in and do things accordingly
			//Have all the display_modes here:
			//0
			//Set adjustment_flag = TRUE
			//1
			//Set adjustment_flag = FALSE
			//2
			//3… etc
			//Goto state 5

			switch (display_mode)		// There must be as many cases here as there are display modes which allow adjustment
			{	

				case 8:
				{
					// Adjustment allowed
					adjustment_flag=TRUE;  // In case of a problem then set everything to normal and return to mode 1
					switch_mode=9;	// wait for person to let go of buttons  
				}
				break;

				default:
				{
					// Display: Adjustment not allowed
					adjustment_flag=FALSE;  // In case of a problem then set everything to normal and return to mode 1
					switch_mode=9;	// wait for person to let go of buttons  
				}
				break;
			}
		}
		break;

		case 7:
		{
			//Enter this mode to increment any conversion factors
			//Have all the display_modes which can be adjusted here:
			switch (display_mode)		// There must be as many cases here as there are display modes which allow adjustment
			{	
				case 8:			// Adjust the Cost_conv
				{
					if(sw2==TRUE)
					{
						//********ramp up the value****************
						M_conv=M_conv+0.5;
						if(M_conv>=99.9)
						{
							M_conv=99.9;	// Stops the value going too high
						}			
						switch_pressed=1;	// Tells case 5 which switch was previously pressed
					}
					if(sw1==TRUE)
					{
						//********ramp down the value****************
						M_conv=M_conv-0.5;
						if(M_conv<=0)
						{
							M_conv=0;	// Stops the value going too low
						}
						switch_pressed=2;	// Tells case 5 which switch was previously pressed
					}

					/* This is where the value is written to EEPROM - stop interrupts for this time to ensure it works */

					INTCONbits.GIE = 0;				// Disable global interrupt bit

					writeEE_float(0x10,M_conv);		//Store the new value to EEPROM so it can be used next time
													// This must be done in the HISR or else there might be an issue
													// This could be done by having a flag and doing the update in the HISR

					INTCONbits.GIE = 1;				//Enable global interrupt bit

					M_conversion = M_conv/10; 		// Must recaluclate the new conversion value (done in initialisation routine)

					update_display=TRUE;			// Show the new value on the screen
					switch_mode=5;	// Go to the delay to stop scrolling through too quickly  
				}
				break;

				default:
				{
					// Display: Adjustment not allowed some error so leave this state
					adjustment_flag=FALSE;  // In case of a problem then set everything to normal and return to mode 1
					switch_mode=9;	// wait for person to let go of buttons  
				}
				break;
				update_display=TRUE;  // Want to do this here.
			}

		}
		break;

		case 8:
		{
			//Set adjustment_flag = FALSE
			//Goto state 5
			// Entered here as user has wanted to leave adjustment mode			

			// Display displaying mode on top line
			// update_display=TRUE;
			adjustment_flag=FALSE;	// Set the adjustment flag FALSE as leaving that mode

			//Wait here until both buttons are released. 
			if(sw1==FALSE&&sw2==FALSE)
			{
				switch_mode=5;
			}
		}
		break;

		case 9:
		{
			// Entered here as user has wanted to enter adjustment mode			
			// ***********TO DO********************
			// Display adjustment mode on top line, if adjustment flag is set

			//Wait here until both buttons are released. 
			if(sw1==FALSE&&sw2==FALSE)
			{
//				update_display=TRUE;
				switch_mode=5;
			}
		}
		break;

		case 10:
		{
			// Entered here as user has wanted start/stop the timer for pedalling		

			buzzer_flag=TRUE;		// Beep the buzzer
			reset_temp_flag++;		// This increments the reset flag to ensure the main loop does the correct thing
			if(reset_temp_flag==4)	// 0=nothing, 1=reset, 2=nothing, 3=hold values
			{
				reset_temp_flag=0;
			}
			switch_mode=5;
			
		}
		break;

		case 11:
		{
			// entered here as it was the first time a switch was pressed and the backlight was set true
			buzzer_flag=TRUE;		// Beep the buzzer
			LCD_ramp_up=TRUE;
			//first_sw_press=FALSE;
			LCD_backlight_counter=0;
			switch_mode=5;		// first button pressed hence return to waiting for next button press
		}
		break;

		default:
		{
                Nop();
		}
        break;
	}

    // User Application USB tasks
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;

    //respond to any USB commands that might have come over the bus			
    ServiceRequests();

}//end ProcessIO