Pages: [1]   Go Down
Author Topic: What is the Minimum Requirements for a Library Folder  (Read 502 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 85
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I understand the principles of how to use " # include <filename.h> " to link to an external .h file where coded functions or subroutines can be referenced.

By example to use the RTClib library use #include <RTClib.h>  & then ensure the RTClib library folder is accessable under the arduino library folder. The RTClib folder then contains text readable files of type .h and .CPP and also a file keywords.txt and a sub folder of example codes.

My question is: could I have a library folder with just a text file that will have the .h suffix and dump my common subroutines into that file, I intend that these lines of code will just be extracted from my top level .ino file OR must I use subroutines in the .h file that calls up further code in the .CPP file ?

I've not found a specific tutorial on librarys under the arduino.cc page.

Thanks for any assistance       
 
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My question is: could I have a library folder with just a text file that will have the .h suffix and dump my common subroutines into that file
Yes, and you can try this without a risk smiley-wink

update - one of my .h only libs
Code:
//
//    FILE: temperature.h
// VERSION: 0.1.00
// PURPOSE: temperature functions for Arduino
//
// HISTORY:
// 2011-04-17 first version
// 2011-05-01 optimized dewPointFast()
// 2012-12-25 added C2F variations
//

#ifndef temperature_h
#define temperature_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#define TEMPERATURE_LIB_VERSION "0.1.00"

// TEMPERATURE
#define Celsius2Fahrenheit(C) ((C)*9/5+32)
#define Fahrenheit2Celsius(F) (((F)-32)*5/9)
#define Celsius2Kelvin(C) ((C)+273.15)
#define Kelvin2Celsius(K) ((K)-273.15)


// DEWPOINT
// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078);   // temp var
return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
// optimized by
double dewPointFast(double celsius, double humidity)
{
#define A 17.271
#define B 237.7
double temp = (A * celsius) / (B + celsius) + log(humidity*0.01);
return (B * temp) / (A - temp);
}

/*
optimized speed -273 - 3000
long CelciusToFahrenheit(long celcius)
{
  if (celcius >=0 )
    return (celcius * 471861 >> 18) + 32;
  else
    return -((-celcius) * 471861 >> 18) + 32;
}

// range not tested yet
long FahrenheitToCelsius(int f)
{
  // http://www.codeproject.com/KB/cs/FindMulShift.aspx
  long x = f - 32;
  if (x >=0 )
    return (x * 3641 >> 15) * 5;
  else
    return -((-x) * 3641 >> 15) * 5;

 long x = (f - 32) * 5 ;
  if (x >=0 )
    return (x * 3641 >> 15);
  else
    return -((-x) * 3641 >> 15);
}

// integer version that does rounding
int Celcius2Fahrenheit(int celcius)
{
  return (celsius * 18 + 5)/10 + 32;
}

int Celcius2Fahrenheit(int prevCelsius, int celcius)
{
  return ((prevCelsius + celcius) * 9 + 325)/10;
}
 
*/

#endif
//
// END OF FILE
//
« Last Edit: December 30, 2012, 06:46:05 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18722
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes you can do that but there is a danger if you use the .h file in multiple places (multiple files) then you will get duplicate symbols.

To avoid that put the function prototypes in the .h file, and the implementation in the .cpp file.
Logged


Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've not found a specific tutorial on librarys under the arduino.cc page.

http://arduino.cc/en/Hacking/LibraryTutorial
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Yes you can do that but there is a danger if you use the .h file in multiple places (multiple files) then you will get duplicate symbols.
@Nick,
don't the scope rules apply?
and where they don't the #ifdef construction takes care of redeclaration?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Yes you can do that but there is a danger if you use the .h file in multiple places (multiple files) then you will get duplicate symbols.
@Nick,
don't the scope rules apply?
and where they don't the #ifdef construction takes care of redeclaration?

redeclaration is indeed fixed by the include guards. The problem is that, since the header file is pasted right into multiple compiled files, two files both declare the same functions, so the linker doesn't know which one to use.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18722
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The #ifdef only guards against nested includes.

If a.cpp and b.cpp both include foo.h and foo.h has an implementation (not just a function prototype) for function bar, then the linker will complain about duplicate implementations of bar.

But if you move the implementation into a .cpp file, the .cpp file is only compiled once, and this won't be a problem.
Logged


Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for explaining (never encountered this problem but still good to know)
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: