work on training

master
edwin 4 years ago
parent 66ce6a2812
commit ff66e63fa6

@ -8,5 +8,13 @@
#Helpfull Links
for Callbacks
https://embeddedartistry.com/blog/2017/02/01/improving-your-callback-game/
https://cognitivewaves.wordpress.com/callbacks/
for std::bind
https://www.youtube.com/watch?v=JtUZmkvroKg

@ -58,6 +58,7 @@ int main(void)
//std::cout << ret << std::endl;
//auto cb = std::bind(&Interface::read_write, &iface,_1);
auto cb = std::bind(&Interface::read_write, &iface, std::placeholders::_1);
Device device(cb);
device.do_something();

@ -0,0 +1,20 @@
cpp_src = $(wildcard *.cpp)
cpp_obj = $(cpp_src:.cpp=.o)
c_obj = $(c_src:.c=.o)
CC = g++
CFLAGS = -Wall -pedantic -li2c
LDFLAGS =
EXEC = runtest
all : $(EXEC)
$(EXEC): $(cpp_obj) $(c_obj)
$(CC) -o $@ $^ $(LDFLAGS)
clean:
rm -rf $(c_obj) $(cpp_obj) $(EXEC)
cleanall:
rm -rf $(c_obj) $(cpp_obj) $(EXEC)

@ -0,0 +1,74 @@
#include <stdint.h>
#include <unistd.h>
#include <iostream>
#include <functional>
class Interface
{
public:
Interface()
{
};
void write(uint8_t val)
{
std::cout << "called Interface::write()" << std::endl;
};
private:
};
class Driver
{
public:
using cb_t = void (*)(uint8_t);
//typedef void(*cb_t)(uint8_t);
Driver(cb_t cb) : cb(cb)
{
};
void do_something()
{
cb(50);
cb(40);
};
private:
cb_t cb;
};
template<class T>
using CallbackObjectMethod = void(T::*)(uint8_t v);
//template<class T>
//void libFunctionObjectMethod(CallbackObjectMethod cbMethod, T* pFO)
//{
// int val = 3;
// (pFO->*cbMethod)(val);
//}
int main(void)
{
//using namespace std::placeholders; // for _1, _2, ...
std::cout << "callback_example" << std::endl;
Interface iface;
iface.write(1);
//Driver driv([](uint8_t a){ std::cout << a << std::endl;});
Driver driv([iface.this](uint8_t a) -> void { this->write(a);});
driv.do_something();
return 0;
}

@ -4,7 +4,7 @@
#include <stdint.h>
#include <unistd.h>
#include <iostream>
#include <memory>
namespace hw
{
@ -26,13 +26,14 @@ namespace hw
I2C();
void writeByte(uint8_t address, uint8_t byte);
private:
};
//
// implementation CH0
//
template<>
I2C<i2c_ch0>::I2C()
{

@ -0,0 +1,13 @@
#ifndef _I2C_CH0_IMPL_HPP_
#define _I2C_CH0_IMPL_HPP_
#include "i2c.hpp"
struct I2C_CH0_impl
{
};
#endif //_I2C_CH0_IMPL_HPP_

@ -0,0 +1,14 @@
#ifndef _I2C_CH1_IMPL_HPP_
#define _I2C_CH1_IMPL_HPP_
#include "i2c.hpp"
struct I2C_CH1_impl
{
};
#endif //_I2C_CH1_IMPL_HPP_

Binary file not shown.

@ -0,0 +1,20 @@
cpp_src = $(wildcard *.cpp)
cpp_obj = $(cpp_src:.cpp=.o)
c_obj = $(c_src:.c=.o)
CC = g++
CFLAGS = -Wall -pedantic -li2c
LDFLAGS =
EXEC = runtest
all : $(EXEC)
$(EXEC): $(cpp_obj) $(c_obj)
$(CC) -o $@ $^ $(LDFLAGS)
clean:
rm -rf $(c_obj) $(cpp_obj) $(EXEC)
cleanall:
rm -rf $(c_obj) $(cpp_obj) $(EXEC)

@ -0,0 +1,44 @@
#include "i2c.hpp"
I2C::I2C():i2cPimpl(new i2cImpl()){}
I2C::~I2C(){}
uint8_t I2C::readByte(const uint8_t& address, const uint8_t& reg)
{
return i2cPimpl->readByte(address,reg);
}
#if 0
uint16_t I2C::readWord(const uint8_t& address, const uint8_t& reg)
{
return i2cPimpl->readWord(address,reg);
}
uint8_t I2C::writeByte(const uint8_t& address, const uint8_t& data)
{
return i2cPimpl->writeByte(address,data);
}
uint8_t I2C::writeWord(const uint8_t& address, const uint8_t& reg, const uint8_t& data)
{
return i2cPimpl->writeWord(address,reg,data);
}
void I2C::writeBuffer(const uint8_t& address, const uint8_t* buffer, uint8_t len)
{
return i2cPimpl->writeBuffer(address,buffer,len);
}
void I2C::readBuffer(const uint8_t& address, const uint8_t* buffer, uint8_t len)
{
return i2cPimpl->readBuffer(address,buffer,len);
}
void I2C::throwError(I2C::errors errNo)
{
i2cPimpl->throwError(errNo);
}
#endif

@ -0,0 +1,87 @@
#ifndef _I2C_HPP_
#define _I2C_HPP_
#include <stdint.h>
#include <unistd.h>
#include <iostream>
#include <memory>
#if 0
// i2c channels
enum i2c_ch
{
i2c_ch0,
i2c_ch1
};
#endif
//
// template
//
template<typename impl_T>
class I2C
{
public:
I2C();
void writeByte(const uint8_t& address, const uint8_t& byte)
{
pimpl->writeByte(address, byte);
}
private:
//struct pimpl_T;
std::unique_ptr<impl_T> pimpl;
};
template<typename impl_>
I2C::I2C():pimpl(new impl()){}
template<typename impl_>
I2C::~I2C(){}
template<typename impl_>
void I2C::readByte(const uint8_t& address, const uint8_t& reg)
{
return pimpl->writeByte(address,byte);
}
#if 0
//
// implementation CH0
//
template<>
I2C<>::I2C()
{
}
template<>
void I2C<i2c_ch0, I2C_CH0_pimpl>::writeByte(uint8_t address, uint8_t byte)
{
std::cout << "wrtiteByte() of i2c CH0 has been called!" << std::endl;
}
//
// implementation CH1
//
template<>
I2C<i2c_ch1, I2C_CH1_pimpl>::I2C()
{
std::cout << "i2c_ch1 has been created" << std::endl;
}
template<>
void I2C<i2c_ch1, I2C_CH1_pimpl>::writeByte(uint8_t address, uint8_t byte)
{
std::cout << "wrtiteByte() of i2c CH1 has been called!" << std::endl;
}
#endif
#endif // _I2C_HPP_

@ -0,0 +1,16 @@
#ifndef _I2C_CH0_IMPL_HPP_
#define _I2C_CH0_IMPL_HPP_
#include "i2c.hpp"
struct I2C_CH0_impl
{
void writeByte(const uint8_t address, const uint8_t& byte)
{
std::cout << "write byte [" << byte << "] to address [" << address << "]" << std::endl;
}
};
#endif //_I2C_CH0_IMPL_HPP_

@ -0,0 +1,24 @@
#include <stdint.h>
#include <unistd.h>
#include <iostream>
#include <functional>
#include "i2c_ch0_impl.hpp"
#include "i2c.hpp"
int main(void)
{
//using namespace std::placeholders; // for _1, _2, ...
std::cout << "structure idea test" << std::endl;
I2C<I2C_CH0_impl> i2c_ch_0;
//hw::I2C<hw::i2c_ch1> i2c_ch_1;
//i2c_ch_0.writeByte(1,1);
//i2c_ch_1.writeByte(1,2);
return 0;
}
Loading…
Cancel
Save