Writing "library" headers

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Writing "library" headers

David Cannings
I have a bunch of .c/.h files which make up libraries for different
devices.  So in my main code, I can #include "widgit.h".

To allow changing pins around easily I #define WIDGIT_OUT LATC1 in the
header file.  Right now I just include copies of the .c/.h in the
project and modify the widgit header file to move pins.

I am trying to get to a point where I can store all libraries in one
folder and change pins in my main code.  This allows easier source
control and sharing of libraries between projects.

I thought this should work:

// In main.c
#define WIDGIT_OUT LATC1
#include "widgit.h"

// In widgit.h
#ifndef WIDGIT_OUT
#warning Output pin not defined!
#endif
/* #define WIDGIT_OUT LATC1 <-- Now in main.c */

But it appears that this doesn't work.  I get errors from widgit.c about
WIDGIT_OUT being an undefined identifier, using #ifndef/#warning in
widgit.h also prints the message.

So I'd like to know if:

1) The above should work, but I'm doing something wrong.

2) What ways people use to write reusable code.

David
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: Writing "library" headers

Wouter van Ooijen
> I thought this should work:
>
> // In main.c
> #define WIDGIT_OUT LATC1
> #include "widgit.h"
>
> // In widgit.h
> #ifndef WIDGIT_OUT
> #warning Output pin not defined!
> #endif
> /* #define WIDGIT_OUT LATC1 <-- Now in main.c */
>
> But it appears that this doesn't work.  I get errors from widgit.c about
> WIDGIT_OUT being an undefined identifier, using #ifndef/#warning in
> widgit.h also prints the message.

This approach should work. Did you include widgit.h in widgit.c?

> 2) What ways people use to write reusable code.

Another trick (that is somewhat compiler-dependent) is to have widgit.h
include widgit-pins.h, which is in the project directory. This how it is
done in Jal.

This configuration problem is one of the reasons I like using C++, where
I can create a pin class, and pass objects to the widget constructor.

--

Wouter van Ooijen

-- -------------------------------------------
Van Ooijen Technische Informatica: www.voti.nl
consultancy, development, PICmicro products
docent Hogeschool van Utrecht: www.voti.nl/hvu
C++ on uC blog: http://www.voti.nl/erblog
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
V G
Reply | Threaded
Open this post in threaded view
|

Re: Writing "library" headers

V G
In reply to this post by David Cannings
On Mon, Aug 27, 2012 at 11:50 AM, David <[hidden email]> wrote:

> I have a bunch of .c/.h files which make up libraries for different
> devices.  So in my main code, I can #include "widgit.h".
>
> To allow changing pins around easily I #define WIDGIT_OUT LATC1 in the
> header file.  Right now I just include copies of the .c/.h in the
> project and modify the widgit header file to move pins.
>
> I am trying to get to a point where I can store all libraries in one
> folder and change pins in my main code.  This allows easier source
> control and sharing of libraries between projects.
>
> I thought this should work:
>
> // In main.c
> #define WIDGIT_OUT LATC1
> #include "widgit.h"
>
> // In widgit.h
> #ifndef WIDGIT_OUT
> #warning Output pin not defined!
> #endif
> /* #define WIDGIT_OUT LATC1 <-- Now in main.c */
>
> But it appears that this doesn't work.  I get errors from widgit.c about
> WIDGIT_OUT being an undefined identifier, using #ifndef/#warning in
> widgit.h also prints the message.
>
> So I'd like to know if:
>
> 1) The above should work, but I'm doing something wrong.
>
> 2) What ways people use to write reusable code.
>
>
You have the right idea, header files are the way to go. Make sure they're
being found by your compiler, and make sure that they're the FIRST header
found (as in, make sure that another header file with the same name isn't
overriding the one you want). Always use #ifndef/#define/#endif to wrap
your headers to prevent them from being defined more than once. Post your
full source so we can see if its correct or not.
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: Writing "library" headers

David Cannings
In reply to this post by Wouter van Ooijen
On 27/08/2012 17:13, Wouter van Ooijen wrote:

>> I thought this should work:
>>
>> // In main.c
>> #define WIDGIT_OUT LATC1
>> #include "widgit.h"
>>
>> // In widgit.h
>> #ifndef WIDGIT_OUT
>> #warning Output pin not defined!
>> #endif
>> /* #define WIDGIT_OUT LATC1 <-- Now in main.c */
>>
>> But it appears that this doesn't work.  I get errors from widgit.c about
>> WIDGIT_OUT being an undefined identifier, using #ifndef/#warning in
>> widgit.h also prints the message.
>
> This approach should work. Did you include widgit.h in widgit.c?

Yes, widgit.h is also included here.  The header can only be included
once with the usual trick of #ifndef _WIDGIT_H ... #endif

I have checked the compile order and main.c comes before widgit.c.

>> 2) What ways people use to write reusable code.
>
> Another trick (that is somewhat compiler-dependent) is to have widgit.h
> include widgit-pins.h, which is in the project directory. This how it is
> done in Jal.

That's a neat idea, I will try this and see if I can get it to work.
MPLABX and HiTech (I've not moved to XC8 yet) do some things I need to
understand better when you add files from multiple folders into one project.

Thanks,

David

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: Writing "library" headers

Isaac Marino Bavaresco
In reply to this post by David Cannings
If your "libraries" are indeed ".lib" files, that approach won't work,
because the library's code was compiled with a fixed value for the
constant macros.

You must have access to all source files and compile them all together
with your application.


Isaac



Em 27/8/2012 12:50, David escreveu:

> I have a bunch of .c/.h files which make up libraries for different
> devices.  So in my main code, I can #include "widgit.h".
>
> To allow changing pins around easily I #define WIDGIT_OUT LATC1 in the
> header file.  Right now I just include copies of the .c/.h in the
> project and modify the widgit header file to move pins.
>
> I am trying to get to a point where I can store all libraries in one
> folder and change pins in my main code.  This allows easier source
> control and sharing of libraries between projects.
>
> I thought this should work:
>
> // In main.c
> #define WIDGIT_OUT LATC1
> #include "widgit.h"
>
> // In widgit.h
> #ifndef WIDGIT_OUT
> #warning Output pin not defined!
> #endif
> /* #define WIDGIT_OUT LATC1 <-- Now in main.c */
>
> But it appears that this doesn't work.  I get errors from widgit.c about
> WIDGIT_OUT being an undefined identifier, using #ifndef/#warning in
> widgit.h also prints the message.
>
> So I'd like to know if:
>
> 1) The above should work, but I'm doing something wrong.
>
> 2) What ways people use to write reusable code.
>
> David

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: Writing "library" headers

Tamas Rudnai
In reply to this post by David Cannings
My bet would be that widigit.h is inlcuded somewere where you forgot to
define that WIDIGIT_OUT prior to the inclusion. This is not necessarily
happening within your code, but in another header file you included
directly or indirectly in your source files.

Tamas




On 27 August 2012 08:50, David <[hidden email]> wrote:

> I have a bunch of .c/.h files which make up libraries for different
> devices.  So in my main code, I can #include "widgit.h".
>
> To allow changing pins around easily I #define WIDGIT_OUT LATC1 in the
> header file.  Right now I just include copies of the .c/.h in the
> project and modify the widgit header file to move pins.
>
> I am trying to get to a point where I can store all libraries in one
> folder and change pins in my main code.  This allows easier source
> control and sharing of libraries between projects.
>
> I thought this should work:
>
> // In main.c
> #define WIDGIT_OUT LATC1
> #include "widgit.h"
>
> // In widgit.h
> #ifndef WIDGIT_OUT
> #warning Output pin not defined!
> #endif
> /* #define WIDGIT_OUT LATC1 <-- Now in main.c */
>
> But it appears that this doesn't work.  I get errors from widgit.c about
> WIDGIT_OUT being an undefined identifier, using #ifndef/#warning in
> widgit.h also prints the message.
>
> So I'd like to know if:
>
> 1) The above should work, but I'm doing something wrong.
>
> 2) What ways people use to write reusable code.
>
> David
> --
> http://www.piclist.com PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist
>



--
int main() { char *a,*s,*q; printf(s="int main() { char *a,*s,*q;
printf(s=%s%s%s, q=%s%s%s%s,s,q,q,a=%s%s%s%s,q,q,q,a,a,q); }",
q="\"",s,q,q,a="\\",q,q,q,a,a,q); }
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: Writing "library" headers

David Cannings
In reply to this post by Isaac Marino Bavaresco
On 27/08/2012 17:43, Isaac Marino Bavaresco wrote:
> If your "libraries" are indeed ".lib" files, that approach won't work,
> because the library's code was compiled with a fixed value for the
> constant macros.

They are not .lib files, hence my use of quotes.

David

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: Writing "library" headers

David Cannings
In reply to this post by David Cannings
On 27/08/2012 17:28, David wrote:
> On 27/08/2012 17:13, Wouter van Ooijen wrote:
>> Another trick (that is somewhat compiler-dependent) is to have widgit.h
>> include widgit-pins.h, which is in the project directory. This how it is
>> done in Jal.
>
> That's a neat idea, I will try this and see if I can get it to work.
> MPLABX and HiTech (I've not moved to XC8 yet) do some things I need to
> understand better when you add files from multiple folders into one project.

This approach works well with MPLABX and HiTech.  I have to add the
project directory to the list of include paths, so that the #include
"widgit-pins.h" can be resolved by the preprocessor.

Thanks,

David

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: Writing "library" headers

Isaac Marino Bavaresco
In reply to this post by David Cannings
Em 27/8/2012 13:48, David escreveu:
> On 27/08/2012 17:43, Isaac Marino Bavaresco wrote:
>> If your "libraries" are indeed ".lib" files, that approach won't work,
>> because the library's code was compiled with a fixed value for the
>> constant macros.
> They are not .lib files, hence my use of quotes.
>
> David


If you don't need too much speed, you could use a pin abstraction layer.
I developed one for a PLC-like application where the pins are operated
under control of a remote client sending commands.

This is part of file "IO.h":

void                     IOInit            ( void );
unsigned char    IOGetCount        ( void );
signed char        IOReadValue        ( unsigned char IONumber );
signed char        IOSetDirection    ( unsigned char IONumber, unsigned
char Direction );
signed char        IOSetValue        ( unsigned char IONumber, unsigned
char Value );
signed char        IOToggle        ( unsigned char IONumber );
signed char        IOClear            ( unsigned char IONumber );
signed char        IOSet            ( unsigned char IONumber );


And this is how the pins are associated with numbers:

const rom Pin_t    IOMap[]    =
    {
        /* Simple digital I/O pins */

        P(D,0), P(D,1), P(D,2), P(H,0), P(H,1), P(H,2), P(H,3), P(J,4),
        P(J,5), P(B,4), P(B,5), P(C,1), P(A,4), P(C,0),

        /* Analog-capable I/O pins */

        P(A,2), P(A,3), P(A,5), P(F,1), P(F,2), P(F,3), P(F,4), P(F,5),
        P(F,6), P(H,4), P(H,5), P(H,6), P(H,7),

        /* PWM capable I/O pins */

        P(C,2), P(E,6), P(E,5), P(G,4), P(G,0), P(E,4), P(E,3), P(G,3),
        P(E,7), P(E,2), P(E,1), P(E,0),

        /* Communication capable I/O pins */

        P(C,6), P(C,7), P(G,1), P(G,2), P(C,3), P(C,4), P(C,5), P(F,7),
        P(B,0), P(B,1), P(B,2), P(B,3)
    };


Each pin-manipulating function takes around 30 instruction cycles in
assembly. First I wrote a C version for reference.
This was developed for a PIC18F87J60, by the way.


Best regards,

Isaac

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: Writing "library" headers

Dave Tweed
In reply to this post by David Cannings
David wrote:

> I have a bunch of .c/.h files which make up libraries for different
> devices.  So in my main code, I can #include "widgit.h".
>
> To allow changing pins around easily I #define WIDGIT_OUT LATC1 in the
> header file.  Right now I just include copies of the .c/.h in the
> project and modify the widgit header file to move pins.
>
> I am trying to get to a point where I can store all libraries in one
> folder and change pins in my main code.  This allows easier source
> control and sharing of libraries between projects.
>
> I thought this should work:
>
> // In main.c
> #define WIDGIT_OUT LATC1
> #include "widgit.h"
>
> // In widgit.h
> #ifndef WIDGIT_OUT
> #warning Output pin not defined!
> #endif
> /* #define WIDGIT_OUT LATC1 <-- Now in main.c */
>
> But it appears that this doesn't work.  I get errors from widgit.c about
> WIDGIT_OUT being an undefined identifier, using #ifndef/#warning in
> widgit.h also prints the message.
>
> So I'd like to know if:
>
> 1) The above should work, but I'm doing something wrong.
>
> 2) What ways people use to write reusable code.

That works for compiling main.c (or at least it should), but when you compile
widgit.c, the symbol won't be defined. You need to put the symbol definition
in another header file that gets included for both compiles.

-- Dave Tweed
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: Writing "library" headers

Paul Anderson-19
In reply to this post by David Cannings
The problem is that the preprocessor does not maintain state across multiple compilations. The #define in main.c exists *only* in main.c. Instead of the #warning, make the #define in your header set a sensible default. Or find a different way to pass the information around at runtime.

--------
Paul Anderson -- VE3HOP

On 2012-08-27, at 11:50 AM, David <[hidden email]> wrote:

> I have a bunch of .c/.h files which make up libraries for different
> devices.  So in my main code, I can #include "widgit.h".
>
> To allow changing pins around easily I #define WIDGIT_OUT LATC1 in the
> header file.  Right now I just include copies of the .c/.h in the
> project and modify the widgit header file to move pins.
>
> I am trying to get to a point where I can store all libraries in one
> folder and change pins in my main code.  This allows easier source
> control and sharing of libraries between projects.
>
> I thought this should work:
>
> // In main.c
> #define WIDGIT_OUT LATC1
> #include "widgit.h"
>
> // In widgit.h
> #ifndef WIDGIT_OUT
> #warning Output pin not defined!
> #endif
> /* #define WIDGIT_OUT LATC1 <-- Now in main.c */
>
> But it appears that this doesn't work.  I get errors from widgit.c about
> WIDGIT_OUT being an undefined identifier, using #ifndef/#warning in
> widgit.h also prints the message.
>
> So I'd like to know if:
>
> 1) The above should work, but I'm doing something wrong.
>
> 2) What ways people use to write reusable code.
>
> David
> --
> http://www.piclist.com PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist