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