Added simple C++ example for the events in telldus-core

This commit is contained in:
Micke Prag 2010-05-11 13:57:31 +00:00
parent f7be6f0e53
commit db07521372

53
examples/events/main.cpp Normal file
View 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;
}