////////////////////////////////////////////////////////////////////////
// net_poll()
// This function is an entry point from the main() program loop, and
// gives the NET framework an opportunity to poll for data.
//
// It polls the interrupt-handler async buffer and move characters into
// net_buf, depending on net_buf_mode. This internally handles normal
// line-by-line (CRLF) data, as well as SMS (+CMT) and MSG (+IPD) modes.
//
// This function is also the dispatcher for net_state_activity(),
// to pass the incoming data to the current state for handling.
//
void net_poll(void)
  {
  unsigned char x;

  while(UARTIntGetChar(&x))
    {
    if ((net_buf_mode==NET_BUF_SMS)||(net_buf_mode==NET_BUF_CRLF))
      { // CRLF (either normal or SMS second line) mode
      if (x == 0x0d) continue; // Skip 0x0d (CR)
      net_buf[net_buf_pos++] = x;
      if (net_buf_pos == NET_BUF_MAX) net_buf_pos--;
      if ((x == ':')&&(net_buf_pos>=6)&&
          (net_buf[0]=='+')&&(net_buf[1]=='I')&&
          (net_buf[2]=='P')&&(net_buf[3]=='D'))
        {
        net_buf[net_buf_pos-1] = 0; // Change the ':' to an end
        net_buf_mode = atoi(net_buf+5);
        net_buf_pos = 0;
        continue; // We have switched to IPD mode
        }
      if (x == 0x0A) // Newline?
        {
        net_buf_pos--;
        net_buf[net_buf_pos] = 0; // mark end of string for string search functions.
        if ((net_buf_pos>=4)&&
            (net_buf[0]=='+')&&(net_buf[1]=='C')&&
            (net_buf[2]=='M')&&(net_buf[3]=='T'))
          {
          x = 7;
          while ((net_buf[x++] != '\"') && (x < net_buf_pos)); // Search for start of Phone number
          net_buf[x - 1] = '\0'; // mark end of string
          net_caller[0] = '\0';
          strncpy(net_caller,net_buf+7,NET_TEL_MAX);
          net_caller[NET_TEL_MAX-1] = '\0';
          net_buf_pos = 0;
          net_buf_mode = NET_BUF_SMS;
          continue;
          }
        net_state_activity();
        net_buf_pos = 0;
        net_buf_mode = NET_BUF_CRLF;
        }
      }
    else
      { // IP data mode
      if (x != 0x0d)
        {
        net_buf[net_buf_pos++] = x; // Swallow CR
        if (net_buf_pos == NET_BUF_MAX) net_buf_pos--;
        }
      net_buf_mode--;
      if (x == 0x0A) // Newline?
        {
        net_buf_pos--;
        net_buf[net_buf_pos] = 0; // mark end of string for string search functions.
        net_state_activity();
        net_buf_pos = 0;
        }
      if (net_buf_mode==0)
        {
        net_buf_pos = 0;
        net_buf_mode = NET_BUF_CRLF;
        }
      }
    }
  }
Example #2
0
File: main.c Project: jschisler/pic
void main(void) {
    char packet[] = {'T', 'e', 's', 't'};

    unsigned char i = 0;
    unsigned char packet_size = sizeof (packet) / sizeof (packet[0]);
    unsigned char cArray[10]
            = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};

    // temporary local array
    unsigned char writtenArray[10];

    // local variables for all test cases and their initialisation
    unsigned char chData;
    unsigned char *pReadArray;
    unsigned char *pWriteArray;
    unsigned char j = 0;
    unsigned char i = 0;
    pReadArray = pWriteArray = writtenArray;

    /* Configure the oscillator for the device */
    ConfigureOscillator();

    /* Initialize I/O and Peripherals for application */
    InitApp();

    //  LCD test
    TRISAbits.RA2 = 0; // our chip select pin needs to be an output so that we can toggle it
    CS = 1; // set CS pin to high, meaning we are sending any information to the MCP23S17 chip

    // configure SPI: the MCP23S17 chip's max frequency is 10MHz, let's use 10MHz/64 (Note FOSC=10Mhz, our external oscillator)
    OpenSPI1(SPI_FOSC_64, MODE_10, SMPEND); // frequency, master-slave mode, sampling type
    // set LCD pins DB0-DB7 as outputs
    setIODIR(IODIRB_ADDRESS, 0x00);
    // set RS and E LCD pins as outputs
    setIODIR(IODIRA_ADDRESS, 0x00);
    // RS=0, E=0
    setGPIO(IODIRA_ADDRESS, 0x00);
    // Function set: 8 bit, 2 lines, 5x8
    lcdCommand(0b00111111);
    // Cursor or Display Shift
    lcdCommand(0b00001111);
    // clear display
    lcdCommand(0b00000001);
    // entry mode
    lcdCommand(0b00000110);

    // send characters
    lcdWriteString((unsigned char *) "Waiting..."); // using the string function
    lcdGoTo(0x40); // go to line two
    /*lcdChar('S'); // using the single character function
    lcdChar('P');
    lcdChar('I');
    lcdChar(' ');
    lcdChar('L');
    lcdChar('i');
    lcdChar('b');
    lcdChar('r');
    lcdChar('a');
    lcdChar('r');
    lcdChar('y');
*/
    ///////////////////////////////////////////////////
    /* TODO <INSERT USER APPLICATION CODE HERE> */
    /*
    while(1)
    {
        i = 0;
        
        do {
            UARTIntPutChar(packet[i++]);
        } while (i < packet_size);

        while (!vUARTIntStatus.UARTIntTxBufferEmpty);

        Delay10KTCYx(1000);
    }
     */
    TRISD = 0;
    PORTD = 0;

    while (1) {
/*
        for (j = 0; j < 100; j++) {
            i = 0;
            do {
                if (vUARTIntStatus.UARTIntTxBufferEmpty)
                    UARTIntPutChar(cArray[i++]);
            } while (i < 10);
        }
*/
        if (!(vUARTIntStatus.UARTIntRxError) &&
                !(vUARTIntStatus.UARTIntRxOverFlow) &&
                !(vUARTIntStatus.UARTIntRxBufferEmpty)) {
            if (UARTIntGetChar(&chData)) {
                PORTD = chData;
                lcdChar(chData);
            }
        }
    }
}