/*
The microcontroller functions as a go-between, between the computer (via USB)
and the GSM/GPRS module.
The GSM/GPRS module should first be powered on manually
using the Pwr_Key button, then it can be communicated with using AT commands
from the mikroElektronika USB (HID) Terminal
*/

//<USB>
char USBin[64];
char USBout[64];
//</USB>

char pos;

void interrupt() {
  USB_Interrupt_Proc();
}

void main() {
  OSCCON = 0b01110000; //8MHz
  PLLEN_bit = 1; //Enable PLL (now 48MHz)
  TRISD = 0b00000000;
  LATD = 0b00000000;
  UART1_Init(9600);
  HID_Enable(&USBin,&USBout); // Enable HID communication
  
  while (1) {
    if (HID_Read() != 0) {
      UART1_Write_Text(USBin);
      LATD++;
    }
    pos = 0;
    while (UART1_Data_Ready()) {
      USBout[pos] = UART1_Read();
      pos++;
    }
    if (pos > 0) {
      USBout[pos] = 0;
      HID_Write(&USBout, 64);
    }
  }
}