#include <iostream>
#include <functional>
//#include "analogmuxdemux.h"
using byte = unsigned char ;
typedef unsigned char byte ; // or a typedef
struct AnalogMux
{
int AnalogRead( byte pin )
{
std::cout << "AnalogMux::AnalogRead: pin == " << int(pin) << '\n' ;
// read
return 0 ;
}
};
class potentiometer
{
// http://e...content-available-to-author-only...e.com/w/cpp/utility/functional/function
using read_function_t = std::function< int(byte) > ;
//typedef std::function< int(byte) > read_function_t ; // alternate syntax
byte pinNum;
read_function_t readFunction ;
public:
potentiometer( byte, read_function_t fun );
int analogRead(); // analogreads the pot
};
potentiometer::potentiometer( byte pin , read_function_t fun )
{
pinNum = pin ;
readFunction = fun ;
}
int potentiometer::analogRead()
{
return readFunction(pinNum);
}
int non_member_AnalogRead( byte pin )
{
std::cout << "non_member_AnalogRead: pin == " << int(pin) << '\n' ;
// read
return 0 ;
}
int main()
{
AnalogMux aMux ; // instantiate a mux
potentiometer p1( 1, non_member_AnalogRead ); // attached to pin 1
p1.analogRead() ;
// attached to input 4 on aMux
// bind the function to AnalogMux::AnalogRead to be invoked on aMux
// http://e...content-available-to-author-only...e.com/w/cpp/utility/functional/bind
potentiometer p2( 4, std::bind( &AnalogMux::AnalogRead, aMux, std::placeholders::_1 ) ) ;
p2.analogRead() ;
}