Live RSS feeds from ethernet on a 16X2 LCD

Dependencies:   EthernetNetIf mbed

main.cpp

Committer:
amli1016
Date:
2010-10-12
Revision:
0:92d9235b910a

File content as of revision 0:92d9235b910a:

#include "mbed.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"
#include "TextLCD.h"
//#include "TextLCD_SlideMessage.cpp"
TextLCD lcd(p10, p12, p15, p16, p29, p30);
EthernetNetIf eth; 
HTTPClient http;
int p=0;
int c=0;
int gl=0;
HTTPResult result;
bool completed = false;

void request_callback(HTTPResult r)
{
  result = r;
  completed = true;
}

int checks(char *a)    // parsing function returns the startin poshition of the RSS feed in the current buffer 
{
 
  int i=gl;
 // printf("%d\n\r",i);
  if(p==1)
  {
        p=0;
        if(a[0]=='t')
        {
          //c=1;
          gl=i+8;
          return i+7;
        }      
    }  
  for(;i<512;i++)
  {
  // printf("%d",i);
    if(i==511)
    {
        if(a[i]=='<')
        {
            p=1;
            gl=0;
            return -1;
        } 
    else
    {
        gl=0;
        return -1;
    }  
   }
    if (a[i]=='<')              //  check if "<t" appears 
    {
       if(a[i+1]=='t')
        {
        //if(a[i+2]=='i')
        {
          //c=1;
          
         // printf("%c",a[i+2]);
          if(i+7>511)
          {
             c=(i+7-511);
             gl=0;
             return -1;
            } 
            else
            {
            gl=i+8;
             return i+7;         // return the starting position of the feed
             }
          }
       }
    }
  }
  }
int main() {

  int i=0;
  printf("Start\n");

  printf("Setting up...\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  printf("Setup OK\n");
  
  HTTPStream stream;
 while(1)
 {
  completed=false;
  char BigBuf[512 + 1] = {0};
  stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read
  
  HTTPResult r = http.get("HTTP://rss.cnn.com/rss/cnn_tech.rss", &stream, request_callback); //Load a very large page, such as the hackaday RSS feed
  
  while(!completed)
  {
    Net::poll(); //Polls the Networking stack
    //printf("a");
    if(stream.readable())
    {
      
    
     int cnt=0;
     while(cnt!=-1)   // cnt = -1 if the current buffer is fully read
   {
     if (c!=0)
     {
        i=c-1;
        c=0;
        goto abcd;
      }   
       
       cnt=checks(BigBuf);
       if(cnt==-1)
       {
         break;
         }
         printf("\n\r");
     // printf("%d",cnt ); 
     i =cnt;
abcd:  
     {
     
                                                 // Print on LCD in rolling fashion    
      {        
         for (j = 15  ; j>=0 ; j-- ){
      
             for (int k = j ; k <= 15 ; k++ ){
               if((i+k-j)==512)
                {
                i=-10; 
                goto efgh;
                
                }
                 lcd.locate(k,0);
                 if ( BigBuf[i+k-j]!='<' )             // Checks for the end of RSS feed
                   lcd.printf("%c",BigBuf[i+k-j]) ;
                 else
                 {
                     lcd.cls();
                     goto efgh;
                  }   
             }          
             wait(0.18) ;
         }
         
         for ( int j = 1 ; BigBuf[i+j] != '<'  ; j++ ){
             for ( int k = 0; k < 16 ; k++ ){
                 if((i+k+j)==512)
                { 
                i=-10;
                goto efgh;
                
                }
                  lcd.locate(k,0) ;
                  
                 if( BigBuf[i+k + j]!='<' )
                 {                     
                     lcd.printf("%c",BigBuf[i+k+j]) ;
                     }
                 else
                 {
                     lcd.printf(" ") ;
                     lcd.cls();
                     goto efgh;
             }
             }
             wait(0.18) ;
         }    
   }
      
      
      
efgh:  if(i==-10)   //  If the buffer is full and the entire feed is not gisplayed
     {
       
       c=1;
      goto xyz;
      }                                   
      }
     
    }
    
           
                                      
xyz:    stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
    }
  }
  
  
  printf("\n--------------\n");
  if(result == HTTP_OK)
  {
    printf("Read completely\n"); 
  }
  else
  {
    printf("Error %d\n", result);
  }
  wait(5);           // Continuously receive feeds
  }
  while(1)
  {
  
  }
  
  return 0;
  
}