Use enum / Header-Files

Hi there

I have 2 problems.

First i tried to use an enum in a function as parameter:

ex:

// *** Defines/Enums
enum enumToolsTimer {
  TIMER_1MS   = 0,
  TIMER_10MS  = 1,
  TIMER_100MS = 2,
  TIMER_1S    = 3  
};

uint32_t TOOLS_IsTimerOver ( enum enumToolsTimer tim );

this didnt work this error was displayed:

error: use of enum 'enumToolsTimer' without previous declaration

Second i tried this. I read in a forum you have to put the enum in a header and add the header, than it should work but that didnt work, beacuse it doenst work like that either.

Could help me with this ??

Best Regards
AllineV

What is the TOOLS_IsTimerOver() function supposed to do with the enum assuming that passing an enum to a function can be made to work ?

Please post a complete program that illustrates the problem and what the function does

It has nothing to do with whether it's in a header file or not. Just get rid of the "enum" in the function declaration:

uint32_t TOOLS_IsTimerOver(enumToolsTimer tim);

Pieter

Hi

this is the function:

uint32_t TOOLS_IsTimerOver ( enum enumToolsTimer tim ) {
  uint32_t retval = false;

  switch( tim ) {
    case TIMER_1MS:   if ( !Tools.Timer[TIMER_1MS] ) retval = true;
                      break;
    case TIMER_10MS:  if ( !Tools.Timer[TIMER_10MS] ) retval = true;
                      break;
    case TIMER_100MS: if ( !Tools.Timer[TIMER_100MS] ) retval = true;
                      break;
    case TIMER_1S:    if ( !Tools.Timer[TIMER_1S] ) retval = true;
                      break;
    default:          break;
  }

  return ( retval );
}

The variable Tools.Timer[TIMER_1S] ( for 1sec ) can be set and gets counted down to 0 in a 1ms-main loop and than i can check it with the function if the time is over.

i wanted to make it with the enum to make it more understandable.

Mainloop call:

void TOOLS_Handler1ms ( void ) {
  // *** 1ms
  if ( Tools.Timer[TIMER_1MS] ) Tools.Timer[TIMER_1MS]--;
  // **10ms
  if ( ++Tools.Count10ms == 10 ) {
    Count.Timer10ms = 0;
    if ( Tools.Timer[TIMER_10MS] ) Tools.Timer[TIMER_10MS]--;
  }
  // *** 100ms
  if ( ++Tools.Count100ms == 100 ) {
    Count.Timer100ms = 0;
    if ( Tools.Timer[TIMER_100MS] ) Tools.Timer[TIMER_100MS]--;
  }
  // *** 1s
  if ( ++Tools.Count1s == 1000 ) {
    Count.Timer1s = 0;
    if ( Tools.Timer[TIMER_1S] ) Tools.Timer[TIMER_1S]--;
  }
}

to set the timer:

void TOOLS_SetTimer ( enum enumToolsTimer tim, uint32_t timerval ) {

  switch( tim ) {
    case TIMER_1MS:   Tools.Timer[TIMER_1MS] = timerval;
                      break;
    case TIMER_10MS:  Tools.Timer[TIMER_10MS] = timerval;
                      break;
    case TIMER_100MS: Tools.Timer[TIMER_100MS] = timerval;
                      break;
    case TIMER_1S:    Tools.Timer[TIMER_1S] = timerval;
                      break;
    default:          break;
  }
}

Hi

i can only write every 5 min..

PieterP:
It has nothing to do with whether it's in a header file or not. Just get rid of the "enum" in the function declaration:

uint32_t TOOLS_IsTimerOver(enumToolsTimer tim);

Pieter

That doenst work new error gets displayed:

error: 'enumToolsTimer' was not declared in this scope

this is my code so you ahve it all.

// *** Defines/Enums
enum enumToolsTimer {
  TIMER_1MS   = 0,
  TIMER_10MS  = 1,
  TIMER_100MS = 2,
  TIMER_1S    = 3,  
  TIMER_MAX   = 4
};

// *** Structs
struct TOOLS_VAR {  // Timing Handling
  uint32_t Timer[TIMER_MAX];
  
  uint32_t Count10ms;
  uint32_t Count100ms;
  uint32_t Count1s;

};
struct TOOLS_VAR Tools;

void TOOLS_Init ( void ) {
  memset((uint8_t *)&Tools, 0, sizeof(Tools));
}

void TOOLS_Handler1ms ( void ) {
  // *** 1ms
  if ( Tools.Timer[TIMER_1MS] ) Tools.Timer[TIMER_1MS]--;
  // **10ms
  if ( ++Tools.Count10ms == 10 ) {
    Tools.Count10ms = 0;
    if ( Tools.Timer[TIMER_10MS] ) Tools.Timer[TIMER_10MS]--;
  }
  // *** 100ms
  if ( ++Tools.Count100ms == 100 ) {
    Tools.Count100ms = 0;
    if ( Tools.Timer[TIMER_100MS] ) Tools.Timer[TIMER_100MS]--;
  }
  // *** 1s
  if ( ++Tools.Count1s == 1000 ) {
    Tools.Count1s = 0;
    if ( Tools.Timer[TIMER_1S] ) Tools.Timer[TIMER_1S]--;
  }
}

void TOOLS_SetTimer ( enum enumToolsTimer tim, uint32_t timerval ) {

  switch( tim ) {
    case TIMER_1MS:   Tools.Timer[TIMER_1MS] = timerval;
                      break;
    case TIMER_10MS:  Tools.Timer[TIMER_10MS] = timerval;
                      break;
    case TIMER_100MS: Tools.Timer[TIMER_100MS] = timerval;
                      break;
    case TIMER_1S:    Tools.Timer[TIMER_1S] = timerval;
                      break;
    default:          break;
  }
}


uint32_t TOOLS_IsTimerOver ( enum enumToolsTimer tim ) {
  uint32_t retval = false;

  switch( tim ) {
    case TIMER_1MS:   if ( !Tools.Timer[TIMER_1MS] ) retval = true;
                      break;
    case TIMER_10MS:  if ( !Tools.Timer[TIMER_10MS] ) retval = true;
                      break;
    case TIMER_100MS: if ( !Tools.Timer[TIMER_100MS] ) retval = true;
                      break;
    case TIMER_1S:    if ( !Tools.Timer[TIMER_1S] ) retval = true;
                      break;
    default:          break;
  }

  return ( retval );
}

and the error:

Tools:54:27: error: use of enum 'enumToolsTimer' without previous declaration

void TOOLS_SetTimer ( enum enumToolsTimer tim, uint32_t timerval ) {

^

Tools:70:34: error: use of enum 'enumToolsTimer' without previous declaration

uint32_t TOOLS_IsTimerOver ( enum enumToolsTimer tim ) {

^

Hi

It works if you deklare the eum in the Main...

If it is not in the main file it can't process it correctly, it seems. Doesnt really make sense for me.

Does someone have an explanation for this ?

Best regards
Allinev

allinev:
Does someone have an explanation for this ?

Yes, you're doing something wrong. But, from what you've posted, we can't tell what. Don't post Snippets! Post a complete program that we can copy and paste into the Arduino IDE and try.

Hi

Here the whole code:

Main

// MAIN
// *** Includes

// *** ENUM IN MAIN -->> IT WORKS
// *** Defines/Enums
enum enumToolsTimer{
  TIMER_1MS   = 0,      // Kühler ist ausgeschaltet -> System wärmt wieder auf
  TIMER_10MS  ,
  TIMER_100MS ,
  TIMER_1S    ,
  TIMER_MAX   
};


/*
 * @brief   Initialization of the Project
 */
void setup() {
  // *** Setup the Tools 
  TOOLS_Init();
}

/*
 * @brief   Main-Loop
 */
void loop() {

  delayMicroseconds(1000);

  TOOLS_Handler1ms();  
  
}

File-Tools

// FILE_Tools

// *** Structs
struct TOOLS_VAR {  // Timing Handling
  uint32_t Timer[TIMER_MAX];
  
  uint32_t Count10ms;
  uint32_t Count100ms;
  uint32_t Count1s;

};
struct TOOLS_VAR Tools;

// ENUM in other file --> doesnt WORK
// *** Defines/Enums
//enum enumToolsTimer{
//  TIMER_1MS   = 0,      // Kühler ist ausgeschaltet -> System wärmt wieder auf
//  TIMER_10MS  ,
//  TIMER_100MS ,
//  TIMER_1S    ,
//  TIMER_MAX   
//};



void TOOLS_Init ( void ) {
  memset((uint8_t *)&Tools, 0, sizeof(Tools));
}

void TOOLS_Handler1ms ( void ) {
  // *** 1ms
  if ( Tools.Timer[TIMER_1MS] ) Tools.Timer[TIMER_1MS]--;
  // **10ms
  if ( ++Tools.Count10ms == 10 ) {
    Tools.Count10ms = 0;
    if ( Tools.Timer[TIMER_10MS] ) Tools.Timer[TIMER_10MS]--;
  }
  // *** 100ms
  if ( ++Tools.Count100ms == 100 ) {
    Tools.Count100ms = 0;
    if ( Tools.Timer[TIMER_100MS] ) Tools.Timer[TIMER_100MS]--;
  }
  // *** 1s
  if ( ++Tools.Count1s == 1000 ) {
    Tools.Count1s = 0;
    if ( Tools.Timer[TIMER_1S] ) Tools.Timer[TIMER_1S]--;
  }
}

void TOOLS_SetTimer ( enum enumToolsTimer timer, uint32_t timerval ) {

  switch( timer ) {
    case TIMER_1MS:   Tools.Timer[TIMER_1MS] = timerval;
                      break;
    case TIMER_10MS:  Tools.Timer[TIMER_10MS] = timerval;
                      break;
    case TIMER_100MS: Tools.Timer[TIMER_100MS] = timerval;
                      break;
    case TIMER_1S:    Tools.Timer[TIMER_1S] = timerval;
                      break;
    default:          break;
  }
}


uint32_t TOOLS_IsTimerOver ( enum enumToolsTimer timer ) {
  uint32_t retval = false;

  switch( timer ) {
    case TIMER_1MS:   if ( !Tools.Timer[TIMER_1MS] ) retval = true;
                      break;
    case TIMER_10MS:  if ( !Tools.Timer[TIMER_10MS] ) retval = true;
                      break;
    case TIMER_100MS: if ( !Tools.Timer[TIMER_100MS] ) retval = true;
                      break;
    case TIMER_1S:    if ( !Tools.Timer[TIMER_1S] ) retval = true;
                      break;
    default:          break;
  }
  return ( retval );
}

I just change the place of declaration of the enum and than it works

Here's the correct way to do it:

Main .ino file:

#include "File-Tools.h"

void setup() {

  TOOLS_Init();
}

void loop() {

  delayMicroseconds(1000);

  TOOLS_Handler1ms();
}

File-Tools.h

#ifndef FILE_TOOLS_H_
#define FILE_TOOLS_H_

#include "Arduino.h"

enum enumToolsTimer {
  TIMER_1MS = 0, TIMER_10MS, TIMER_100MS, TIMER_1S, TIMER_MAX
};

void TOOLS_Init();
void TOOLS_Handler1ms();
void TOOLS_SetTimer(enumToolsTimer timer, uint32_t timerval);
uint32_t TOOLS_IsTimerOver(enumToolsTimer timer);

#endif /* FILE_TOOLS_H_ */

File-Tools.cpp

#include "File-Tools.h"

struct TOOLS_VAR {  // Timing Handling
  uint32_t Timer[TIMER_MAX];

  uint32_t Count10ms;
  uint32_t Count100ms;
  uint32_t Count1s;

};
TOOLS_VAR Tools;

void TOOLS_Init(void) {
  memset((uint8_t *) &Tools, 0, sizeof(Tools));
}

void TOOLS_Handler1ms(void) {
  // *** 1ms
  if (Tools.Timer[TIMER_1MS])
    Tools.Timer[TIMER_1MS]--;
  // **10ms
  if (++Tools.Count10ms == 10) {
    Tools.Count10ms = 0;
    if (Tools.Timer[TIMER_10MS])
      Tools.Timer[TIMER_10MS]--;
  }
  // *** 100ms
  if (++Tools.Count100ms == 100) {
    Tools.Count100ms = 0;
    if (Tools.Timer[TIMER_100MS])
      Tools.Timer[TIMER_100MS]--;
  }
  // *** 1s
  if (++Tools.Count1s == 1000) {
    Tools.Count1s = 0;
    if (Tools.Timer[TIMER_1S])
      Tools.Timer[TIMER_1S]--;
  }
}

void TOOLS_SetTimer(enumToolsTimer timer, uint32_t timerval) {

  switch (timer) {
    case TIMER_1MS:
      Tools.Timer[TIMER_1MS] = timerval;
      break;
    case TIMER_10MS:
      Tools.Timer[TIMER_10MS] = timerval;
      break;
    case TIMER_100MS:
      Tools.Timer[TIMER_100MS] = timerval;
      break;
    case TIMER_1S:
      Tools.Timer[TIMER_1S] = timerval;
      break;
    default:
      break;
  }
}


uint32_t TOOLS_IsTimerOver(enumToolsTimer timer) {
  uint32_t retval = false;

  switch (timer) {
    case TIMER_1MS:
      if (!Tools.Timer[TIMER_1MS])
        retval = true;
      break;
    case TIMER_10MS:
      if (!Tools.Timer[TIMER_10MS])
        retval = true;
      break;
    case TIMER_100MS:
      if (!Tools.Timer[TIMER_100MS])
        retval = true;
      break;
    case TIMER_1S:
      if (!Tools.Timer[TIMER_1S])
        retval = true;
      break;
    default:
      break;
  }
  return (retval);
}

All of the values in the enum are ints so why not just pass the value that you want the function to use ?