Go Down

Topic: Problem including Wire library within my library (Read 3903 times) previous topic - next topic

Beta

I am creating a library that talks to a DS1307 RTC that uses functions from the Wire library.  I am having troubles getting my .cpp file to include Wire.h correctly.

my .h file
Code: [Select]

#ifndef DS1307_h
#define DS1307_h

#include "WProgram.h"

#define DS1307_ADDR 0x68

//masks
#define CH_BIT 0x7F
#define TIME_STYLE_24H 0x3F
#define IF_SQ_OFF_MASK 0x80
#define FREQ_CLEAR 0xFC
#define SQWE_MASK 0x10

//square wave values
#define SQ_1HZ 0x10
#define SQ_4KHZ 0x11
#define SQ_8KHZ 0x12
#define SQ_32KHZ 0x13
#define SQ_OFF_LOW 0x00
#define SQ_OFF_HIGH 0x80

class DS1307 {
 public:
   DS1307 ();
   void begin ();
   void begin (boolean);
   void begin (boolean, int);
   
   void setDate (byte, byte, byte, byte, byte, byte, byte);
   void getDate (byte*, byte*, byte*, byte*, byte*, byte*, byte*);
   
   void setSecond (byte);
   void setMinute (byte);
   void setHour (byte);
   void setDayOfWeek (byte);
   void setDayOfMonth (byte);
   void setMonth (byte);
   void setYear (byte);
   
   byte getSecond ();
   byte getMinute ();
   byte getHour ();
   byte getDayOfWeek ();
   byte getDayOfMonth ();
   byte getMonth ();
   byte getYear ();
   boolean getAMPM ();
   
   void outputIfSquareOff (boolean);
   void squareChangeFreq (int);
   void squareOnOff (boolean);
   void setSquare (boolean, boolean, int);
 private:
   void _init (boolean, boolean, int);
   byte _decToBcd (byte);
   byte _bcdToDec (byte);
   
   byte _originalHour;
   byte _squareVal;
   boolean _hourStyle12;
};

extern DS1307 RTC;

#endif


my .cpp file (since its long, I'm only showing functions that include Wire functions, and the functions included in those functions)
Code: [Select]

#include "WProgram.h"
#include "DS1307.h"
#include "../Wire/Wire.h"

DS1307::DS1307 () {}

void DS1307::_init (boolean hourStyle12, boolean SQWE, int freq) {
 Wire.begin ();
 
 _squareVal = SQ_OFF_LOW;
 _hourStyle12 = hourStyle12;
 
 if (SQWE) {
   switch (freq) {
     case 1:
       _squareVal = SQ_1HZ;
       break;
     case 4:
       _squareVal = SQ_4KHZ;
       break;
     case 8:
       _squareVal = SQ_8KHZ;
       break;
     case 32:
       _squareVal = SQ_32KHZ;
       break;
     default:
       _squareVal = SQ_1HZ;
       break;
   }
 }
}

byte DS1307::_decToBcd (byte val) {
 return ((val / 10 * 16) + (val % 10));
}

byte DS1307::_bcdToDec (byte val) {
 return ((val / 16 * 10) + (val % 16));
}

void DS1307::setDate (byte second, byte minute, byte hour,
                   byte dayOfWeek, byte dayOfMonth, byte month, byte year) {
 
 Wire.beginTransmission (DS1307_ADDR);
 
 Wire.send (0);
 Wire.send (_decToBcd(second) & CH_BIT); //0 to bit 7 starts the clock
 Wire.send (_decToBcd(minute));
 Wire.send (_decToBcd(hour));
 Wire.send (_decToBcd(dayOfWeek));
 Wire.send (_decToBcd(dayOfMonth));
 Wire.send (_decToBcd(month));
 Wire.send (_decToBcd(year));
 Wire.send (_squareVal);
 
 Wire.endTransmission();
}

void DS1307::getDate (byte *second, byte *minute, byte *hour,
                   byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year) {
 
 Wire.beginTransmission (DS1307_ADDR);
 Wire.send (0);
 Wire.endTransmission ();
 
 Wire.requestFrom (DS1307_ADDR, 7);
 *second = _bcdToDec (Wire.receive() & CH_BIT);
 *minute = _bcdToDec (Wire.receive());
 *hour = _bcdToDec (Wire.receive() & TIME_STYLE_24H);
 _originalHour = *hour;
 if (_hourStyle12) {
   if (*hour == 0) {
     *hour = 12;
   }
   else if (*hour >= 13) {
     *hour -= 12;
   }
 }
 *dayOfWeek = _bcdToDec (Wire.receive());
 *dayOfMonth = _bcdToDec (Wire.receive());
 *month = _bcdToDec (Wire.receive());
 *year = _bcdToDec (Wire.receive());
}

DS1307 RTC = DS1307();


my error (I'm "------"ing out file paths that include personal info in the path name)
Code: [Select]
C:\Users\------\AppData\Local\Temp\build2087115825781654835.tmp\DS1307\DS1307.cpp.o: In function `DS1307::getDate(unsigned char*, unsigned char*, unsigned char*, unsigned char*, unsigned char*, unsigned char*, unsigned char*)':

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:136: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:136: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:136: undefined reference to `TwoWire::beginTransmission(int)'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:137: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:137: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:137: undefined reference to `TwoWire::send(int)'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:138: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:138: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:138: undefined reference to `TwoWire::endTransmission()'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:140: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:140: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:140: undefined reference to `TwoWire::requestFrom(int, int)'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:141: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:141: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:141: undefined reference to `TwoWire::receive()'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:142: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:142: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:142: undefined reference to `TwoWire::receive()'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:143: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:143: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:143: undefined reference to `TwoWire::receive()'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:153: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:153: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:153: undefined reference to `TwoWire::receive()'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:154: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:154: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:154: undefined reference to `TwoWire::receive()'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:155: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:155: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:155: undefined reference to `TwoWire::receive()'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:156: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:156: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:156: undefined reference to `TwoWire::receive()'

C:\Users\------\AppData\Local\Temp\build2087115825781654835.tmp\DS1307\DS1307.cpp.o: In function `DS1307::_init(unsigned char, unsigned char, int)':

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:27: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:27: undefined reference to `Wire'

C:\Program Files\arduino-0018\libraries\DS1307/DS1307.cpp:27: undefined reference to `TwoWire::begin()'


Any and all help would be appreciated.

By the way, my sketch that impliments this works right if I have a #include <Wire.h> at the top of the sketch.  It's weird, because it should work exactly the same whether you include Wire.h once or twice.

Coding Badly


Does this help...

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1260640627
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1260640627/5#5

leppie

#2
Jul 30, 2010, 09:27 am Last Edit: Jul 30, 2010, 09:29 am by leppie Reason: 1
This is just the way it works AFAIK.

Edit: Why are you re-inventing the wheel, There is a library for that  chip already. It is included with the DateTime library IIRC.

Beta

@Coding Badly: I already did what was suggested in that post.

top of my .cpp file
Code: [Select]
#include "WProgram.h"
#include "DS1307.h"
#include "../Wire/Wire.h"


@leppie: I cant find anything in the DateTime library that mentions the DS1307.  In the DateTime playground page, it says that the library uses no exteral circuitry.

One sure way to get my library to work would be to include the actual Wire code in my .cpp file, and make them private functions.

In a link that was on one of the pages that Coding Badly linked to, I read that this used to work in Arduino 0015, but no longer works in Arduino 0018.  If this is true and any of the people that write the IDE updates read this, please consider fixing this.

Again, any and all help is appreciated.

GordonEndersby



You arnt the first to have this problem.
Theres an earlier ds1307 library mentioned here:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1191209057

They have had to include the wire library before there ds1307 library.
Ive been using this library for a while as it seems quite good.

Gordon

Beta

@GordonEndersby:  The thing is, I don't want to have to #include Wire.h in my sketch.

leppie

#6
Jul 30, 2010, 06:33 pm Last Edit: Jul 30, 2010, 06:35 pm by leppie Reason: 1
Quote
@leppie: I cant find anything in the DateTime library that mentions the DS1307.


Maybe it is called the Time library.  

Sorry I cant recall where I got the code.

Edit: The readme file does not mention an author... or a url...   :-/

Coding Badly

Quote
The thing is, I don't want to have to #include Wire.h in my sketch

Why not?

Beta

@Coding Badly:  For simplicity of use.  If you have to include a library in your sketch, you shoudn't have to include another library in order for the first one to work.  It should already be done for you.

GordonEndersby



Im not sure if its wise or neccesary to include wire.h in your library.
If you have several devices needing the wire library.
Mostly ive seen that you have to include the wire library then the extra libraries that add the extra functionality for the other devices.

eg.
#include <Wire.h>
#include <DS1307.h.>  
#include <widget1.h>  //requires wire.h
#include <widget2.h>  //requires wire.h

If you include the wire library in your library you have to assume that the library creators for the other devices have taken this into account. or that your library is the first to be included. So that the wire classes are available to all the libraries.

I cant remember seeing any design guidelines on this matter.
The developer mail list might be useful to you.
http://mail.arduino.cc/mailman/listinfo/developers_arduino.cc


Gordon

Beta

#10
Jul 30, 2010, 11:10 pm Last Edit: Jul 31, 2010, 06:51 am by Beta Reason: 1
@GordonEndersby:  Now that you mention it, that does sound reasonable.  I also read somewhere that when you include one of the included libraries inside of a personal library, it only includes the .h file and not the .cpp file.  I'm not sure if this is entirely true, but it would explain the errors.

EDIT: I would still like to know how to succesfully do what I was trying to do, just for the sake of learning.

Go Up