need help with library

this is my timer.h
#ifndef timer4_h
#define timer4_h
#include "WProgram.h"

enum TIMERSTATE {
MARCHE,
ARRET,
FINI
};

class TIMER {

public:
TIMER(int ledPin);
void TIM_Init(void);
void TIM_Stop(void);
void TIM_Start (void);
void TIM_Set(unsigned long ulTempo);
struct Timer {
enum TIMERSTATE eEtat ;
unsigned long ulValue ;
unsigned long ulTempo ;
} ;
enum TIMERSTATE TIM_GetState(void) ;
private:
int _ledPin;
struct Timer gstTimer;
};
#endif

this is my timer.cpp
#include "timer4.h"
#include "WProgram.h"

TIMER::TIMER (int ledPin){
_ledPin = ledPin;
pinMode(ledPin, OUTPUT);
}

void TIMER::TIM_Init(void){
ASSR = 0 ; // Internal clock
TCCR2A = 0 ; // Normal mode pour le timer2
TCCR2B = 4 ; // Prescaler = 64 + Normal Mode
TIMSK2 = 1 ; // Valid interrupt on TOIE2
}

void TIMER::TIM_Stop (void){
gstTimer.eEtat = ARRET ;
}

void TIMER::TIM_Start(void){
gstTimer.eEtat = MARCHE ;
}

void TIMER::TIM_Set(unsigned long ulTimer){
gstTimer.ulValue = 0 ;
gstTimer.ulTempo = ulTimer ;
}

enum TIMERSTATE TIMER::TIM_GetState(void) {
return gstTimer.eEtat ;
}

ISR ( TIMER2_OVF_vect ){
if (gstTimer.eEtat == MARCHE) {
gstTimer.ulValue ++ ;
if (gstTimer.ulValue >= gstTimer.ulTempo) {
gstTimer.eEtat = FINI ;
gstTimer.ulValue = 0 ;
}
}
}

and this is my main programme
#include <timer4.h>

const int ledPin = 13;

int iEtat ;
int i ;

TIMER monTIMER = TIMER(ledPin);
void setup(){
pinMode (13,OUTPUT); //declarer le pin 13 comme output(LED)
iEtat = 0 ;
digitalWrite (13,iEtat);

monTIMER.TIM_Init() ;
monTIMER.TIM_Stop() ;
monTIMER.TIM_Set(5000) ;
monTIMER.TIM_Start() ;
}

void loop(){
enum TIMERSTATE EtatDuTimer ;

while(1) {

digitalWrite (13,iEtat);
EtatDuTimer = monTIMER.TIM_GetState() ;
if(EtatDuTimer == FINI) {
iEtat = ! iEtat ;
digitalWrite (13,iEtat);
monTIMER.TIM_Start() ;
}
}
}

and the problem is when i compiled this program, there r an error..can someone help me with this please..

Knowing the error would help a lot more than most of the code you posted...

Apart from that, I seem to see some discrepancy between the way you include your timer4.h file. Are you sure you want to use <> instead of "" in the main function?

when im compiled the programme, the error that occured is :
error: 'gstTimer' was not declared in this scope..
but i think that im already declared it in my timer.h...

class TIMER {

   public:
         TIMER(int ledPin);      
         void TIM_Init(void);         
         void TIM_Stop(void);         
         void TIM_Start (void);         
         void TIM_Set(unsigned long ulTempo);         
         struct Timer {
            enum TIMERSTATE eEtat ;
            unsigned long ulValue ;
            unsigned long ulTempo ;
         } ;

Why is the struct Timer declaration/definition INSIDE the class declaration/definition? It should not be,

i d already removed it and put it outside the class..my problem is in my timer.cpp inside the fonction ISR..
there is an error that said my gstTimer.eEtat is not declared..

largos:
when im compiled the programme, the error that occured is :
error: 'gstTimer' was not declared in this scope..
but i think that im already declared it in my timer.h...

The ISR is not a member of the class. It needs an object of the class to work on.

One way to do things:

Create an object in timer4.cpp It will have a fixed name. All references in your sketch and the library cpp file will use this name. (That's the way the core HardwareSerial library works with an object named Serial that is instantiated in HardwareSerial.cpp)

You don't to implement a constructor, but you need a way to specify the LED pin, so maybe you can implement a Timer4::begin() function that assigns the LED.

Maybe something along these lines:

//Timer4.h
#ifndef Timer4_h
#define Timer4_h
#include "WProgram.h"

enum TIMERSTATE {
           MARCHE,
           ARRET,
           FINI
         };
         
class TIMER
{

   public:
         void begin(int ledPin, int iEtat);      
         void TIM_Init(void);         
         void TIM_Stop(void);         
         void TIM_Start (void);         
         void TIM_Set(unsigned long ulTempo);         
         struct Timer {
            enum TIMERSTATE eEtat ;
            unsigned long ulValue ;
            unsigned long ulTempo ;
         } Timer;            
         enum TIMERSTATE TIM_GetState(void) ;
         
   private:         
         int _ledPin;
         struct Timer gstTimer;         
};

extern TIMER Timer4;
#endif
//Timer4.cpp
#include "Timer4.h"
#include "WProgram.h"

// Instantiate a global object of this class
TIMER Timer4;


 void TIMER::begin(int ledPin, int iEtat)
{
   _ledPin = ledPin;
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, iEtat);
}
   
void TIMER::TIM_Init(void)
{
   ASSR = 0 ;              // Internal clock
    TCCR2A = 0 ;          // Normal mode pour le timer2
    TCCR2B = 4 ;          // Prescaler = 64 + Normal Mode
    TIMSK2 = 1 ;          // Valid interrupt on TOIE2
}

void TIMER::TIM_Stop (void)
{
   gstTimer.eEtat = ARRET ;
}

void TIMER::TIM_Start(void)
{
   gstTimer.eEtat = MARCHE ;
}

void TIMER::TIM_Set(unsigned long ulTimer
){
   gstTimer.ulValue = 0 ;
   gstTimer.ulTempo = ulTimer ;
}

enum TIMERSTATE TIMER::TIM_GetState(void)
{
   return gstTimer.eEtat ;
}

ISR ( TIMER2_OVF_vect )
{
  if (Timer4.Timer.eEtat == MARCHE)  {
    Timer4.Timer.ulValue ++ ;
    if (Timer4.Timer.ulValue >= Timer4.Timer.ulTempo)  {
      Timer4.Timer.eEtat = FINI ;
      Timer4.Timer.ulValue = 0 ;
    }
  }
}
// TestTimer4.pde
//
// If Timer4.h and Timer4.cpp are in the sketchbook/libraries directory use this:
//
#include <Timer4.h>

// If Timer4.h and Timer4.cpp are in the same directory as this sketch, use this:
//#include "Timer4.h"

const int ledPin =  13;

int iEtat = 0 ;

void setup(
){
  Timer4.begin(ledPin, iEtat);
  Timer4.TIM_Init() ;
  Timer4.TIM_Stop() ;
  Timer4.TIM_Set(5000) ;
  Timer4.TIM_Start() ;
}

void loop()
{
  enum TIMERSTATE EtatDuTimer ;

  while(1) {
   
   digitalWrite (ledPin,iEtat);
    EtatDuTimer = Timer4.TIM_GetState() ;
    if(EtatDuTimer == FINI) {
      iEtat = ! iEtat ;
      digitalWrite (ledPin,iEtat);
      Timer4.TIM_Start() ;
    }     
  } 
}

Regards,

Dave

Footnote:
I compiled the code with my changes, but I haven't tested it, so I don't know if any part of it is correct. I did not try to impose any of my personal stylistic preferences or try to implement a "better" data-hiding strategy. I just took your code and made minimal changes to show one way of getting started with what I think you want to do. There are other ways...

I copied all of your code to the Arduino IDE. The error message it is showing is in this code:

ISR ( TIMER2_OVF_vect ){
  if (gstTimer.eEtat == MARCHE)  {
    gstTimer.ulValue ++ ;
    if (gstTimer.ulValue >= gstTimer.ulTempo)  {
      gstTimer.eEtat = FINI ;
      gstTimer.ulValue = 0 ;
    }
  }
}

In that non-class function, gstTimer is NOT in scope. The compiler is correct.

thanks to all that helped me...i d tried the way u suggested, theres no more error..
but the fonction did not work..thanks for all ur helped..im really appriciated it..

I would say that in the ISR function, which can be not a class function.

May be it would be better to use a pointer of an object, instead of an object.

something like (I don't code it, its just for explaining).

put this line in your hader file after the class definition

TIMER *vect_timer=OL;

add in your class TIMER the folowing function :

void Assign_Vect_Timer();

in your body, try to add this code :

void TIMER::Assign_Vect_Timer()
{
vectTimer = this;
}

ISR ( TIMER2_OVF_vect )
{
if (vect_timer != OL) // to prevent a crash if vect_timer not yet assigned
{
if (vect_timer->gstTimer.eEtat == MARCHE)
{
vect_timer->gstTimer.ulValue ++ ;
if (vect_timer->gstTimer.ulValue >= vect_timer->gstTimer.ulTempo) {
vect_timer->gstTimer.eEtat = FINI ;
vect_timer->gstTimer.ulValue = 0 ;
}
}
}
}

May be it will work.

Or another way to do it, is from Quadrature library :

in the header file :

#define NBTIMERS 10

Add to your class : 

static void Isr(void);
static TIMER* _registry[NBTIMERS];

in your body : 

inline void Timer::Isr(void)
{
  int i;

  for(i=0; i<NBTIMERS; i++)
  {
     TIMER *t;

    if (_registry[i])
    {
       t = _registry[i];

       if (t->gstTimer.eEtat == MARCHE)  {
            t->gstTimer.ulValue ++ ;
            if (t->gstTimer.ulValue >= t->gstTimer.ulTempo)  {
                t->gstTimer.eEtat = FINI ;
                t->gstTimer.ulValue = 0 ;
        }
     }
    }
  }
}

ISR(TIMER2_OVF_vect)
{
    TIMER::Isr();
}

May be the gsTimer variable of class must declared as volatile because the isr is modfying some values !!!!