Skip to content

Commit 7666d71

Browse files
committed
API: add C++ callback class for convenience
1 parent 31f0af0 commit 7666d71

3 files changed

Lines changed: 56 additions & 17 deletions

File tree

examples/cpp/cp_app.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,22 @@ static struct osdp_channel cp_channel = {
5050
.close = nullptr,
5151
};
5252

53-
int event_handler(void *data, const osdp_t *ctx, int pd, struct osdp_event *event) {
54-
(void)(data);
55-
(void)(ctx);
53+
class EventHandler : public OSDP::ControlPanelEventHandler
54+
{
55+
private:
56+
const char* _message;
57+
public:
58+
explicit EventHandler(const char* message) : _message{message} {}
5659

57-
std::cout << "PD" << pd << " EVENT: " << event->type << std::endl;
58-
return 0;
59-
}
60+
int event_handler(const OSDP::ControlPanel& cp,
61+
int pd, struct osdp_event *event) override
62+
{
63+
(void)(cp);
64+
std::cout << "PD " << pd << " EVENT: " << event->type << std::endl;
65+
std::cout << _message << std::endl;
66+
return 0;
67+
}
68+
};
6069

6170
int main()
6271
{
@@ -66,11 +75,12 @@ int main()
6675

6776
cp.setup(&cp_channel, 1, pd_info);
6877

69-
cp.set_event_callback(event_handler, nullptr);
78+
EventHandler handler { "Hello world!" };
79+
80+
cp.set_event_callback(&handler);
7081

7182
while (1) {
7283
// your application code.
73-
7484
cp.refresh();
7585
std::this_thread::sleep_for(std::chrono::microseconds(10 * 1000));
7686
}

examples/platformio/cp.ino

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,23 @@ void init_cp_info()
5858
cp_channel.send = serial1_send_func;
5959
}
6060

61-
int event_handler(void *data, const osdp_t *ctx, int pd, struct osdp_event *event)
61+
class EventHandler : public OSDP::ControlPanelEventHandler
6262
{
63-
(void)(data);
64-
(void)(ctx);
63+
private:
64+
const char* _message;
65+
public:
66+
explicit EventHandler(const char* message) : _message{message} {}
6567

66-
Serial.println("Received an event!");
67-
return 0;
68-
}
68+
int event_handler(const OSDP::ControlPanel& cp,
69+
int pd, struct osdp_event *event) override
70+
{
71+
(void)(cp);
72+
(void)(pd);
73+
(void)(event);
74+
Serial.println("Received an event!");
75+
return 0;
76+
}
77+
};
6978

7079
void setup()
7180
{
@@ -76,7 +85,9 @@ void setup()
7685

7786
init_cp_info();
7887
cp.setup(&cp_channel, 1, pd_info);
79-
cp.set_event_callback(event_handler, nullptr);
88+
89+
EventHandler handler { "Hello world!" };
90+
cp.set_event_callback(&handler);
8091
}
8192

8293
void loop()

include/osdp.hpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ class OSDP_EXPORT Common {
7171
osdp_t *_ctx;
7272
};
7373

74+
class ControlPanel;
75+
76+
class OSDP_EXPORT ControlPanelEventHandler {
77+
public:
78+
virtual int event_handler(const ControlPanel& cp, int pd, struct osdp_event *ev) = 0;
79+
};
80+
7481
class OSDP_EXPORT ControlPanel : public Common {
7582
public:
7683
ControlPanel() {}
@@ -127,9 +134,9 @@ class OSDP_EXPORT ControlPanel : public Common {
127134
return osdp_cp_flush_commands(_ctx, pd);
128135
}
129136

130-
void set_event_callback(cp_event_callback_t cb, void *arg)
137+
void set_event_callback(ControlPanelEventHandler *cb)
131138
{
132-
osdp_cp_set_event_callback(_ctx, cb, arg);
139+
osdp_cp_set_event_callback(_ctx, ControlPanel::event_callback, cb);
133140
}
134141

135142
void set_command_completion_callback(cp_command_completion_callback_t cb,
@@ -168,6 +175,17 @@ class OSDP_EXPORT ControlPanel : public Common {
168175
return osdp_cp_is_pd_enabled(_ctx, pd);
169176
}
170177

178+
private:
179+
static int event_callback(void *arg, const osdp_t *ctx, int pd,
180+
struct osdp_event *ev)
181+
{
182+
ControlPanel cp;
183+
cp._ctx = const_cast<osdp_t *>(ctx);
184+
int result = static_cast<ControlPanelEventHandler*>(arg)
185+
->event_handler(const_cast<const ControlPanel&>(cp), pd, ev);
186+
cp._ctx = nullptr;
187+
return result;
188+
}
171189
};
172190

173191
class OSDP_EXPORT PeripheralDevice : public Common {

0 commit comments

Comments
 (0)