Spanish Italian
17436 Users    

FTPmicro Tutorial - Example: ANSA news on the LCD

  Download PDF version of the Article

Introduction
In this article we'll see another example about the FTPMicro and Microchip TCP/IP stack use. In particular the TCP client will be used to visualize in a LCD display the latest news, directly from the ANSA site.

Hardware
The hardware is simply constituted from FTPMicro and from a Hitachi-compatible alphanumeric LCD display. The connexions between these two elements are configurable to file taste from the file HardwareProfile.h, in the FTPMicro section (#elif defined(FTPMICRO)):

	#define LCD_DATA_TRIS		(TRISE)
	#define LCD_DATA_IO		(LATE)
	#define LCD_RD_WR_TRIS		(TRISBbits.TRISB5)
	#define LCD_RD_WR_IO		(LATBbits.LATB5)
	#define LCD_RS_TRIS		(TRISFbits.TRISF6)
	#define LCD_RS_IO		(LATFbits.LATF6)
	#define LCD_E_TRIS		(TRISFbits.TRISF5)
	#define LCD_E_IO		(LATFbits.LATF5)

The original software predicts just a display of 16x2 type, but the presented code was modified to support also the LCD 20x4; that's why is presented the definition of LCD_MODE_20x4 in the file LCDBlocking.h.

RSS
To read the ANSA news, FTPMicro doesn't consult the web pages, but it commits at a feed RSS: it's a XML file, permanently updated, that contains only essentials dates like title, text, date etc.
Let's see a part of this file:

<item>
<title>Miss Italia 2007 is Silvia Battisti</title>
<description>She's arrived to the finals with the Miss Veneto band</description>
<link>http://www.ansa.it/site/notizie/awnplus/topnews/news/2007-09-25_125104380.html</link>
<copyright>Copyright ANSA All rights reserved</copyright>
<pubDate>2007-09-25 01:08</pubDate>
</item>

That interests us is the title of the news, that it will be visualized on the display.
So, the program heart is constituted from one parser which analyzes the file and extrapolates only the titles.

GenericTCPClient
The stack includes a file named GenericTCPClient, that contains an example ready to use by the HTTP client.
Let's see how is functioning the parser:
- A buffer is presented where are saved 4 or 8 news (depending on display), for which was reserved a RAM bank in the linker script .
- In the SM_PROCESS_RESPONSE section, the file is examined, and so the news are saved in the buffer from the number t_count, until the buffer replenishment.
- Finished the analyze, we pass to the SM_DONE section where: the process is beginning again if any news isn't read (news_i = 0), or we pass to SM_DISPLAY.
- In this other one, the news is visualized every 5 seconds on the display.
In general, are saved the news NEWS_NUMBER and visualized one by one, then the Feed is downloaded again and saved the next; reached to the end of the file, the process is beginning again.

Let's examines some details.
The variable declaration which functions as buffer:

#pragma udata news_buf
char news[256];
#pragma udata

The code that constitutes the parser:

while(TCPGet(MySocket, &i))
	switch(ParsingState){
		case PS_HOME:
			k = 0;
			if (i == '<' && (news_i < NEWS_NUMBER)) ParsingState = PS_TAG;
			break;
		case PS_TAG:
			if (i == '>') {
				if (k == 5 && !memcmppgm2ram((char*)LCDText, "title", 5) && (t_cnt++ >= t_count)){
					ParsingState = PS_TITLE;
				} else
					ParsingState = PS_HOME;
				k = 0;
				break;
			}
			if (k < 5)
				LCDText[k++] = i;
			break;
		case PS_TITLE:
			if (i == '<'){
				if (k < NEWS_LEN) news[news_i*NEWS_LEN + k] = '\0';
				ParsingState = PS_HOME;
				news_i++;
				break;
			}
			if (k < NEWS_LEN)
				news[news_i*NEWS_LEN + k++] = i;
			break;
}

It is searched the tag<title> and the content is saved on the buffer, but it are skimped the titles t_count.

Visualization:

case SM_DISPLAY:
	if (TickGet() - Timer < 5*TICK_SECOND) break;
	memcpy((void*)LCDText, (void*)&news[k*NEWS_LEN], NEWS_LEN);
	LCDUpdate();
	k++;
	Timer = TickGet();
	if (k == news_i) {
		if (news_i < NEWS_NUMBER)
			t_count = 0;
		else
			t_count += NEWS_NUMBER;
		t_cnt = 0;
		news_i = 0;
		GenericTCPExampleState = SM_HOME;
	}
	break;

In this last part the visualization on the LCD of the current news; the variable t_count, like already mentioned, matter the news blocks NEWS_NUMBER, while k the news in the buffer is currently visualized.
Once the news visualized in the memory, t_count is increased of NEWS_NUMBER and it is turned to SM_HOME, where the Feed is refilled; if you are arrived to the file end, instead t_count turns to zero.

AttachmentSize
FTPMicro_ANSA.rar1.23 MB

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
4 + 13 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Who's new

  • pulper
  • mauriss
  • jbares
  • christiank79
  • agabor
  • fabriziopd
  • irenix
  • pepershoe
  • raghun14
  • andreaspousette

Who's online

There are currently 1 user and 51 guests online.

Online users

  • pepershoe