Added C example of our sensor-api using callbacks
This commit is contained in:
parent
1b98044f3d
commit
97e88a175f
2 changed files with 56 additions and 0 deletions
13
examples/c/sensors/callback/Makefile
Normal file
13
examples/c/sensors/callback/Makefile
Normal file
|
@ -0,0 +1,13 @@
|
|||
CC=gcc
|
||||
CFLAGS=-Wall -ltelldus-core
|
||||
|
||||
OBJS = main.o
|
||||
|
||||
all: ${OBJS}
|
||||
${CC} -o sensor ${CFLAGS} ${OBJS}
|
||||
|
||||
main.c:
|
||||
${CC} ${CFLAGS} -c main.c
|
||||
|
||||
clean:
|
||||
rm -f sensor main.o
|
43
examples/c/sensors/callback/main.c
Normal file
43
examples/c/sensors/callback/main.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
#include <telldus-core.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void WINAPI sensorEvent(const char *protocol, const char *model, int sensorId, int dataType, const char *value, int ts, int callbackId, void *context) {
|
||||
char timeBuf[80];
|
||||
time_t timestamp = ts;
|
||||
|
||||
//Print the sensor
|
||||
printf("%s,\t%s,\t%i\n", protocol, model, sensorId);
|
||||
|
||||
//Retrieve the values the sensor supports
|
||||
if (dataType == TELLSTICK_TEMPERATURE) {
|
||||
strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", localtime(×tamp));
|
||||
printf("Temperature:\t%sº\t(%s)\n", value, timeBuf);
|
||||
|
||||
} else if (dataType == TELLSTICK_HUMIDITY) {
|
||||
strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", localtime(×tamp));
|
||||
printf("Humidity:\t%s%%\t(%s)\n", value, timeBuf);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int callbackId = 0;
|
||||
|
||||
tdInit();
|
||||
|
||||
//Register for callback
|
||||
callbackId = tdRegisterSensorEvent( (TDSensorEvent)&sensorEvent, 0 );
|
||||
|
||||
//Our own simple eventloop
|
||||
while(1) {
|
||||
sleep(100);
|
||||
}
|
||||
|
||||
//Cleanup
|
||||
tdUnregisterCallback( callbackId );
|
||||
tdClose();
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue