I figured this was a small change so I commited it. Some of my samples will 
implement something like this to check if USB is connected or not. This way 
there is no need to poll USB quickly while USB is not connected.

-- define procedures and variables
procedure usb_disconnected_tasks()
procedure usb_connected_tasks()
var word char_count = 0
var word usb_check = 0
var byte usb_connected = TRUE

-- main loop to serve USB
forever loop
   -- poll the usb ISR function on a regular base, in order to
   -- serve the USB MSD requests
   usb_msd_tasks()

   -- Check if usb is still connected
   -- Another way would be to check the voltage on a USB+ pin.
   if USB_EVENT_OCCURRED == 1 then
      usb_check = 0
   elsif usb_check == 0xFFFF then
      -- USB is not connected
      usb_disconnected_tasks() -- perform your tasks
   else
      -- USB is connected
      usb_connected_tasks()    -- tasks while connected
      usb_check = usb_check + 1
   end if
end loop

-- Do anything you want here while usb is disconnected.
-- Quit the procedure when USB_EVENT_OCCURRED = 1
procedure usb_disconnected_tasks() is
   if usb_connected == TRUE then
      print_crlf(serial_data)
      print_string(serial_data, "USB is disconnected." )
      usb_connected = FALSE
   end if

   forever loop
      serial_data = "!"
      delay_1s(1)
      
      if USB_EVENT_OCCURRED == 1 then
         exit loop
      end if
   end loop
end procedure

-- Do anything you want here while usb is connected, but don't take long!
procedure usb_connected_tasks() is
   if usb_connected == FALSE then
      ;-- wait till MSD is configured
      usb_msd_wait_configured()
      print_crlf(serial_data)
      print_string(serial_data, "USB is connected." )
      usb_connected = TRUE
   end if

   -- poll the usb ISR function on a regular base, in order to
   -- serve the USB MSD requests
   usb_msd_tasks()
      
   -- let us know the PIC is actually doing something
   -- by sending char "." to serial port
   if char_count == 40000 then
      serial_data = "."
      _usec_delay(100)
      char_count = 0
   end if
   char_count = char_count + 1
end procedure

-- 
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/jallib.
For more options, visit https://groups.google.com/d/optout.

Reply via email to