Move function inside custom library

Hi everybody.

To make my code more readable, I want to move a function inside a library.

My working code is:

code here
...
WebServer webserver(PREFIX, 80);
...
void parsedCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{code here}
...
void setup
{more code}
...

To move the function...

  • Create a folder in sketchbook/libraries/MyPages
  • Create MyPages.h and MyPages.cpp

MyPages.h content:

/*
  MyPages.h - Librería personal para
  el servidor web 
*/
#ifndef MyPages_h
#define MyPages_h
#include "Arduino.h"

void parsedCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete);
#endif

MyPages.cpp content:

/*
  myWebServer.cpp - Librería personal para
  el servidor web
*/

#include "Arduino.h"
#include "MyPages.h"



void parsedCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{code here}

Then I modify the sketch to:

WebServer webserver(PREFIX, 80);
...
#include "MyPages.h" 
...
void setup
{more code}
...

But when I try to compile it, I get this errors:

In file included from /home/jose/Escritorio/proyecto/sketchbook/libraries/MyPages/MyPages.cpp:7:0:
/home/jose/Escritorio/proyecto/sketchbook/libraries/MyPages/MyPages.h:11:16: error: variable or field 'parsedCmd' declared void
 void parsedCmd(WebServer &, WebServer::ConnectionType , char *, bool );
                ^
/home/jose/Escritorio/proyecto/sketchbook/libraries/MyPages/MyPages.h:11:16: error: 'WebServer' was not declared in this scope
/home/jose/Escritorio/proyecto/sketchbook/libraries/MyPages/MyPages.h:11:27: error: expected primary-expression before ',' token
 void parsedCmd(WebServer &, WebServer::ConnectionType , char *, bool );
                           ^
/home/jose/Escritorio/proyecto/sketchbook/libraries/MyPages/MyPages.h:11:29: error: 'WebServer' has not been declared
 void parsedCmd(WebServer &, WebServer::ConnectionType , char *, bool );

include "WebServer.h" is written some lines upper.

I have no idea what is happening. ¿Any help?

Thanks in advance

[quote author=Jose Francisco link=msg=2776039 date=1464469591]

include "WebServer.h" is written some lines upper.

[/quote]

You have to add that also to MyPages.h

Ok, new MyPages.h:

/*
  MyPages.h - Librería personal para
  el servidor web 
*/
#ifndef MyPages_h
#define MyPages_h

#include "Arduino.h"
#include "WebServer.h"

void parsedCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete);


#endif

but I get errors again:

In file included from /home/jose/Escritorio/proyecto/sketchbook/libraries/MyPages/MyPages.cpp:7:0:
/home/jose/Escritorio/proyecto/sketchbook/libraries/MyPages/MyPages.h:11:16: error: variable or field 'parsedCmd' declared void
 void parsedCmd(WebServer &, WebServer::ConnectionType , char *, bool );
                ^
/home/jose/Escritorio/proyecto/sketchbook/libraries/MyPages/MyPages.h:11:16: error: 'WebServer' was not declared in this scope
/home/jose/Escritorio/proyecto/sketchbook/libraries/MyPages/MyPages.h:11:27: error: expected primary-expression before ',' token
 void parsedCmd(WebServer &, WebServer::ConnectionType , char *, bool );
                           ^
/home/jose/Escritorio/proyecto/sketchbook/libraries/MyPages/MyPages.h:11:29: error: 'WebServer' has not been declared
 void parsedCmd(WebServer &, WebServer::ConnectionType , char *, bool );
                             ^

I've made some changes without success. Any help, please?

I've made some changes

And didn't post the new code.

Any help, please?

With code we can't see? No.

Do you need a library, or do you just need to break out some of your code into it's own .cpp and .h files inside the sketch you are writing? Doing that is a first step. I'd only bother writing up a full library (with examples and a library.properties) once I personally had used a bit of code in more than one project.

PaulS.
Changes I made were unsuccessful and I returned to the original code.

PaulMurrayCbr
I just need to break out my code. I don’t think this functions will be used again.

I’m using Webduino, a library to serve web pages and organize them. With this library you can link a query like http://myarduinoip/mypageN.html to a function. This function will send the web page and do the proper actions.

The way to use this library is:

  • #include SPI, Ethernet, avr/pgmspace, Webserver, and others
  • Create a WebServer object: Webserver webserver(prefix, port);
  • #define constants and store strings in pgmspace (P())
  • declare a function for each webpage (this is what I want to break out)
  • In setup(), link “query” and command: webserver.addCommand(“title.html”, &titleCmd);
  • In loop(), check for incomings query: webserver.processConnection(buff, &len);

Here is an example of function to move:

void titleCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{

  /* this line sends the standard "we're all OK" headers back to the browser */
  server.httpSuccess();


  if (type == WebServer::HEAD)
    return;

 server.printP(Page_start);

P(index)= "<body style=\"background-color: #006699;\">"
          "<h1>Sunmaster</h1>"; 


      server.printP(index);
    server.printP(Page_end);

}

Inside titleCmd function, strings declared inside or outside the function can be printed. In the previous example, P(index) is defined inside, Page_end is defined outside, before void setup()

My code starts to need too much scrolling and functions for webpages will be very large. I think that moving these functions to a separate file will make my code easier to maintain.

Thanks a lot.

Hi. I've got it.

I only need to move definitions to a .h file. cpp files isn't needed.

Thanks