Simple Library Problems

Hi,

I have plenty of experience with C programming but none with C++.
I’ve been trying to implement a simple library before using others and cant even get it working!
I have come at is just as i would have done in C, not sure whats up.
Always get an undefined reference to “getKeyNumber”.

Thanks for any advice.

Code:

#include <WProgram.h>
#include "lib_keypad.h"

void keypadInit(void)
{
	//config hardware.
}

uint_t expectedInput(uint_t btn)
{
	int input = 0;
	if(btn == 0)
	{
		//If no btn is pressed, value is pulled low, so return straight away.
		input = 0;
		return NONE_PRESSED;
	}
	else
	{
	//Calculate Denomanator
		input = 6 - btn;
		input = input * (btn + 1);
		input = input + btn;
	//Calculate overall value:
		input = 1024*input;
		input = btn*input;
	}
	
	return (uint_t)input;
	
}

uint_t getKeyNumber(uint_t input)
{
	if(input == 0)
	{//No key pressed
		return 0;
	}
	
	int i=0, expected=0;
	double margin = 0;
	for(i=1; i<(NUM_BUTTONS+1); i++)
	{	
		//Get the expected value for the button i
		expected = expectedInput(i);
		margin = expected * MARGIN_OF_ERROR;
		
		if( input < (expected + margin))
		{
			if( input > (expected - margin))
			{
				//If the input is within the margin of error, return the button number
				return i;
			}
		}
	}
	
	//if nothing returned previously, error?
	return 0;
}

Header File:

#ifndef KEYPAD_LIB_H
#define KEYPAD_LIB_H

#include <WProgram.h>
//#include <stdlib.h>

#define uint_t unsigned int

#define RESISTANCE 1000
#define NONE_PRESSED 0
//MARGIN_OF_ERROR = the percentage either side of value to allow.
#define MARGIN_OF_ERROR 5
#define NUM_BUTTONS 5

void keypadInit(void);
uint_t expectedInput(uint_t btn);
uint_t getKeyNumber(uint_t input);

#endif // #ifndef lib_keypad

Please tell us more about the hardware, not clear to me (I have some ideas..)

Do you have a working sketch (without library) that is the basis?

Please read - http://www.arduino.cc/en/Hacking/LibraryTutorial - carefully !

Check some simple libs from the playground e.g. - http://www.arduino.cc/playground/Code/StopWatchClass - try to understand how these work.

Hopes this helps.

Is your source code in a .c file or a .cpp file?

Where is the code that calls that function?

Hi,

Source for the library is in a .c file.

Is it necessary to compile the library to a .o file? I expected the compiler to do this at compile/linking time.

Cheers.

#include <lib_keypad.h>
.
.
.
.
.
void loop(void)
{ 
  //int btn = sampleButtons();
  unsigned int btn = analogRead(BUTTON_PIN);    
  btn = getKeyNumber(btn);
  
  Serial.println(analogRead(BUTTON_PIN), DEC);
  Serial.println(btn, DEC);
  
  
  delay(100);
  
}//END - void loop(void)

Is it necessary to compile the library to a .o file? I expected the compiler to do this at compile/linking time.

That is what the compiler does.

The "problem" is that C++ uses name mangling, to allow for overloaded functions. The actual name of the function is defined by the function name and the types of all the arguments. This makes for conflicts when calling a function defined in a .c file. The c compiler does not use name mangling.

You can either use

extern "C" {
}

to surround the C code, or rename the .c file to .cpp.

Hi,

Thank you very much, a simple rename to .cpp fixed the problem.

Cheers.