Skip to main content
added 359 characters in body
Source Link
Mikael Patel
  • 8k
  • 2
  • 16
  • 21

First you need a header file - let's say lib.h:

#ifndef LIB_H
#define LIB_H

typedef void (*num_func) ();
extern num_func functions[];

#endif

And then the implementation, lib.cpp:

#include "lib.h"

void zero();
void one();
void two();
void three();
void four();
void five();
void six();
void seven();
void eight();
void nine();

num_func functions[] = { zero, one, two, three, four, five, six, seven, eight, nine};

The functions could be declared as static (if you want to hide them) and they do need implementation. They are only forward declarations above. Below is a snippet of a sketch using the library (assuming that it is within the sketch folder).

#include "lib.h"

void setup()
{
  ...
}
void loop()
{
  ...
  // Call to the one() function
  functions[1]();
  ...
}

Cheers!

First you need a header file - let's say lib.h:

#ifndef LIB_H
#define LIB_H

typedef void (*num_func) ();
extern num_func functions[];

#endif

And then the implementation, lib.cpp:

#include "lib.h"

void zero();
void one();
void two();
void three();
void four();
void five();
void six();
void seven();
void eight();
void nine();

num_func functions[] = { zero, one, two, three, four, five, six, seven, eight, nine};

The functions could be declared as static (if you want to hide them).

Cheers!

First you need a header file - let's say lib.h:

#ifndef LIB_H
#define LIB_H

typedef void (*num_func) ();
extern num_func functions[];

#endif

And then the implementation, lib.cpp:

#include "lib.h"

void zero();
void one();
void two();
void three();
void four();
void five();
void six();
void seven();
void eight();
void nine();

num_func functions[] = { zero, one, two, three, four, five, six, seven, eight, nine};

The functions could be declared as static (if you want to hide them) and they do need implementation. They are only forward declarations above. Below is a snippet of a sketch using the library (assuming that it is within the sketch folder).

#include "lib.h"

void setup()
{
  ...
}
void loop()
{
  ...
  // Call to the one() function
  functions[1]();
  ...
}

Cheers!

Source Link
Mikael Patel
  • 8k
  • 2
  • 16
  • 21

First you need a header file - let's say lib.h:

#ifndef LIB_H
#define LIB_H

typedef void (*num_func) ();
extern num_func functions[];

#endif

And then the implementation, lib.cpp:

#include "lib.h"

void zero();
void one();
void two();
void three();
void four();
void five();
void six();
void seven();
void eight();
void nine();

num_func functions[] = { zero, one, two, three, four, five, six, seven, eight, nine};

The functions could be declared as static (if you want to hide them).

Cheers!