Added simple C++ example for the events in telldus-core
This commit is contained in:
parent
f7be6f0e53
commit
db07521372
1 changed files with 53 additions and 0 deletions
53
examples/events/main.cpp
Normal file
53
examples/events/main.cpp
Normal file
|
@ -0,0 +1,53 @@
|
|||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <telldus-core.h>
|
||||
|
||||
class Events {
|
||||
public:
|
||||
Events();
|
||||
~Events();
|
||||
void deviceEvent(int deviceId, int method, const char *data);
|
||||
|
||||
static void deviceEventCallback(int deviceId, int method, const char *data, int callbackId, void *context);
|
||||
|
||||
private:
|
||||
int callbackId;
|
||||
};
|
||||
|
||||
Events::Events() {
|
||||
callbackId = tdRegisterDeviceEvent( reinterpret_cast<TDDeviceEvent>(&Events::deviceEventCallback), this );
|
||||
}
|
||||
|
||||
Events::~Events() {
|
||||
tdUnregisterCallback(callbackId);
|
||||
}
|
||||
|
||||
void Events::deviceEvent(int deviceId, int method, const char *data) {
|
||||
printf("Event from device %i\n", deviceId);
|
||||
}
|
||||
|
||||
void Events::deviceEventCallback(int deviceId, int method, const char *data, int callbackId, void *context) {
|
||||
Events *e = reinterpret_cast<Events *>(context);
|
||||
if (e) {
|
||||
/** Please note!
|
||||
* We are here in another thread than the main. Some measures to syncronize
|
||||
* this must be taken!
|
||||
**/
|
||||
e->deviceEvent(deviceId, method, data);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
tdInit();
|
||||
|
||||
Events ev;
|
||||
|
||||
//Our own simple eventloop
|
||||
while(1) {
|
||||
sleep(100);
|
||||
}
|
||||
|
||||
tdClose();
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue