Skip to content

Commit

Permalink
use std::function<void()> instead of global callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
baggior committed Mar 19, 2018
1 parent 3a05ff8 commit eed84d3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/ModbusMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ serial ports, etc. is permitted within callback function.
@see ModbusMaster::ModbusMasterTransaction()
*/
void ModbusMaster::idle(void (*idle)())
void ModbusMaster::idle(service_fn_t idle)
{
_idle = idle;
}
Expand All @@ -193,7 +193,7 @@ Driver Enable pin, and optionally disable its Receiver Enable pin.
@see ModbusMaster::ModbusMasterTransaction()
@see ModbusMaster::postTransmission()
*/
void ModbusMaster::preTransmission(void (*preTransmission)())
void ModbusMaster::preTransmission(service_fn_t preTransmission)
{
_preTransmission = preTransmission;
}
Expand All @@ -211,7 +211,7 @@ Receiver Enable pin, and disable its Driver Enable pin.
@see ModbusMaster::ModbusMasterTransaction()
@see ModbusMaster::preTransmission()
*/
void ModbusMaster::postTransmission(void (*postTransmission)())
void ModbusMaster::postTransmission(service_fn_t postTransmission)
{
_postTransmission = postTransmission;
}
Expand Down
20 changes: 14 additions & 6 deletions src/ModbusMaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Set to 1 to enable debugging features within class:
/* _____STANDARD INCLUDES____________________________________________________ */
// include types & constants of Wiring core API
#include "Arduino.h"
#include <functional>

/* _____UTILITY MACROS_______________________________________________________ */

Expand All @@ -69,12 +70,16 @@ RS232/485 (via RTU protocol).
class ModbusMaster
{
public:

typedef std::function<void()> service_fn_t;

ModbusMaster();

void begin(uint8_t, Stream &serial);
void idle(void (*)());
void preTransmission(void (*)());
void postTransmission(void (*)());

void idle(service_fn_t idle_fn);
void preTransmission(service_fn_t pre_fn);
void postTransmission(service_fn_t post_fn);

// Modbus exception codes
/**
Expand Down Expand Up @@ -255,11 +260,14 @@ class ModbusMaster
uint8_t ModbusMasterTransaction(uint8_t u8MBFunction);

// idle callback function; gets called during idle time between TX and RX
void (*_idle)();
service_fn_t _idle;
// void (*_idle)();
// preTransmission callback function; gets called before writing a Modbus message
void (*_preTransmission)();
service_fn_t _preTransmission;
// void (*_preTransmission)();
// postTransmission callback function; gets called after a Modbus message has been sent
void (*_postTransmission)();
service_fn_t _postTransmission;
// void (*_postTransmission)();
};
#endif

Expand Down

0 comments on commit eed84d3

Please sign in to comment.