/* Copyright (C) 2015 José Ángel Moneo Fernández
 COMPILADOR CCS
 Test USB PIC18F2550

 created 23 de febero de 2015
 by José Angel Moneo
//   This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.

//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.

//   You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
#include "Main.h"


#include "LCD420.C"

#include <usb_cdc.h>
#define LED PIN_A0
#define pin_on   output_high

#define pin_off  output_low
void usb_debug_task(void)
{
   static int8 last_connected;
   static int8 last_enumerated;
   int8 new_connected;
   int8 new_enumerated;


   new_connected=usb_attached();
   new_enumerated=usb_enumerated();


   if (new_connected && !last_connected)
      printf(lcd_putc,"\fUSB connectado\nesperando enumaration");
   if (!new_connected && last_connected)
      printf(lcd_putc,"\fUSB desconectado\nesperando conexion");
   if (new_enumerated && !last_enumerated)
      printf(lcd_putc,"\fUSB enumerado \ncomo PC/HOST");
   if (!new_enumerated && last_enumerated)
      printf(lcd_putc,"\fUSB sin-enumerar \ncomo PC/HOST \nesperando enumeracion");

   last_connected=new_connected;
   last_enumerated=new_enumerated;
}

void main(void)
{
   BYTE i, j, address, value;
   lcd_init();
   set_tris_a(0x00);
   delay_us(10);
   pin_off(LED);
   printf(lcd_putc,"\fUSB Ejemplo");

   usb_init_cs();

   do
   {
      usb_task();
      usb_debug_task();
      if (usb_enumerated())
      {
         if (usb_cdc_kbhit())
         {
            i = toupper(usb_cdc_getc());
            
            if (i == 'R')  output_toggle(LED);
            
         }   
         
      }
   } while (TRUE);
}