From 97e88a175fd64eb5deef6ec8830c81fb11eab045 Mon Sep 17 00:00:00 2001 From: Micke Prag Date: Wed, 18 May 2011 11:48:12 +0000 Subject: [PATCH] Added C example of our sensor-api using callbacks --- examples/c/sensors/callback/Makefile | 13 +++++++++ examples/c/sensors/callback/main.c | 43 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 examples/c/sensors/callback/Makefile create mode 100644 examples/c/sensors/callback/main.c diff --git a/examples/c/sensors/callback/Makefile b/examples/c/sensors/callback/Makefile new file mode 100644 index 00000000..20509a12 --- /dev/null +++ b/examples/c/sensors/callback/Makefile @@ -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 diff --git a/examples/c/sensors/callback/main.c b/examples/c/sensors/callback/main.c new file mode 100644 index 00000000..907c8d08 --- /dev/null +++ b/examples/c/sensors/callback/main.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include + +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; +}