I am using a driver that retrieves data from HW (Driver) and a display (Viewer) that will output data to the user.
The user calls a trigger function to initiate the getting of data from the Driver. The Driver's constructor accepts a "publish" callback function to be called when data is ready.
The Viewer receives a "get data" callback on construction, to be called when it wants data from my code.
My code must integrate the "pull" for data from the viewer with the data "pushed" back from the Driver once I call its trigger function. Another block of code (that I do not have access to) initiates requests from the Viewer and acts upon the data it received back.
This is represented by the main method in my example, below.
Thanks in advance!
#include <cstdint>
#include <iostream>
#include <functional>
class Viewer
{
public:
Viewer(std::function<void(int&)> get_data_cb) : m_get_data_cb(get_data_cb)
{
}
void populate_view(int &data)
{
m_get_data_cb(data);
}
private:
std::function<void(int&)> m_get_data_cb;
};
class Driver
{
public:
Driver(std::function<void(int&)> publish_data_cb) : m_publish_data_cb(publish_data_cb)
{
}
void trigger_event()
{
// tells RTL to start collecting data
++rtl_int;
// trigger_event does not call the IRQ - this is just for demo purposes
event_IRQ_handler();
}
void event_IRQ_handler()
{
// fires when RTL has finished collecting data
m_publish_data_cb(rtl_int);
}
private:
std::function<void(int&)> m_publish_data_cb;
int rtl_int{ 0 };
};
/**********************************************************************/
// Code I have control of starts here
volatile uint32_t publish_count{0};
int my_int{0};
// callback to send to Driver
void my_publish_data_cb(int &i)
{
my_int = i;
++publish_count;
}
Driver my_driver = Driver{ my_publish_data_cb };
// callback to send to Viewer
void my_get_data_cb(int &i)
{
uint32_t before = publish_count;
my_driver.trigger_event();
// I need to give the driver's event_IRQ_handler() time
// to get the data but is this the best way???
while (before == publish_count){};
i = my_int;
//cout << "Viewer: i =" << i << endl;
}
Viewer my_viewer = Viewer{ my_get_data_cb };
// Code I have control of ends here
/**********************************************************************/
int main()
{
my_viewer.populate_view(my_int);
std::cout << "my_int = " << my_int << std::endl;
my_viewer.populate_view(my_int);
std::cout << "my_int = " << my_int << std::endl;
my_viewer.populate_view(my_int);
std::cout << "my_int = " << my_int << std::endl;
return 0;
}
uint32_t
is undefined. Did you intendstd::uint32_t
from<cstdint>
? \$\endgroup\$