blob: 53ec9cd74716d289a28277185cca4156f23942af [file] [log] [blame]
Claudio Takahasiceb82702016-08-12 22:30:34 -03001/*
2 * Copyright (c) 2016, CESAR.
3 * All rights reserved.
4 *
5 * This software may be modified and distributed under the terms
6 * of the BSD license. See the LICENSE file for details.
7 *
8 */
9
10/*
11 * Build instructions:
Tiago Barros6978d8f2016-09-06 09:19:34 -030012 * gcc $(pkg-config --cflags --libs glib-2.0) -Isrc -I<path to protocol>/knot-protocol-source/src \
13 * -o examples/rpi src/knot_thing_main.c examples/rpi.c <path to protocol>/knot-protocol-source/src/knot_protocol.c
14 *
15 * PS: Knot Thing code depends on knot_protocol, so we need to compile it also.
Claudio Takahasiceb82702016-08-12 22:30:34 -030016 */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <stdint.h>
21#include <string.h>
22#include <glib.h>
23
Tiago Barros6978d8f2016-09-06 09:19:34 -030024#include "knot_thing_main.h"
25#include "knot_types.h"
Claudio Takahasiceb82702016-08-12 22:30:34 -030026
27static GMainLoop *main_loop;
Tiago Barros6978d8f2016-09-06 09:19:34 -030028static int32_t speed_value = 0;
Claudio Takahasiceb82702016-08-12 22:30:34 -030029
30static void sig_term(int sig)
31{
32 g_main_loop_quit(main_loop);
33}
34
35static int speed_read(int32_t *val, int32_t *multiplier)
36{
Claudio Takahasiceb82702016-08-12 22:30:34 -030037
Tiago Barros6978d8f2016-09-06 09:19:34 -030038 *val = speed_value++;
39 *multiplier = 1;
40 printf("speed_read(): %d\n", *val);
41 return 0;
Claudio Takahasiceb82702016-08-12 22:30:34 -030042}
43
Tiago Barros6978d8f2016-09-06 09:19:34 -030044static int speed_write(int32_t *val, int32_t *multiplier)
45{
46 speed_value = *val;
47 printf("speed_write(): %d\n", *val);
48 return 0;
49}
Claudio Takahasiceb82702016-08-12 22:30:34 -030050
51static gboolean loop(gpointer user_data)
52{
Tiago Barros6978d8f2016-09-06 09:19:34 -030053 knot_thing_run();
Claudio Takahasiceb82702016-08-12 22:30:34 -030054
55 return TRUE;
56}
57
Tiago Barros6978d8f2016-09-06 09:19:34 -030058#define SPEED_SENSOR_ID 3
59#define SPEED_SENSOR_NAME "Speed Sensor"
60
Claudio Takahasiceb82702016-08-12 22:30:34 -030061int main(int argc, char *argv[])
62{
Claudio Takahasiceb82702016-08-12 22:30:34 -030063 /*
Tiago Barros6978d8f2016-09-06 09:19:34 -030064 * RPi fake speed sensor. This example shows how KNOT Thing
65 * should be used to register and implement sensor
Claudio Takahasiceb82702016-08-12 22:30:34 -030066 * read/write callbacks.
67 */
68
Tiago Barros6978d8f2016-09-06 09:19:34 -030069 int err, timeout_id;
70
71 knot_data_functions functions;
72 functions.int_f.read = speed_read;
73 functions.int_f.write = NULL;
74
75 knot_data_values lower_limit, upper_limit;
76 lower_limit.value_i.value = 5;
77 lower_limit.value_i.multiplier = 1;
78 upper_limit.value_i.value = 10;
79 upper_limit.value_i.multiplier = 1;
80
Claudio Takahasiceb82702016-08-12 22:30:34 -030081 signal(SIGTERM, sig_term);
82 signal(SIGINT, sig_term);
83 signal(SIGPIPE, SIG_IGN);
84
85 main_loop = g_main_loop_new(NULL, FALSE);
Tiago Barros6978d8f2016-09-06 09:19:34 -030086 printf("Starting...\n");
87 knot_thing_init();
Claudio Takahasiceb82702016-08-12 22:30:34 -030088
89 /* Register an integer sensor: should be called from Arduino setup() */
Tiago Barros6978d8f2016-09-06 09:19:34 -030090 knot_thing_register_data_item(SPEED_SENSOR_ID, SPEED_SENSOR_NAME, KNOT_TYPE_ID_SPEED,
91 KNOT_VALUE_TYPE_INT, KNOT_UNIT_SPEED_MS, &functions);
92
93 /* Configure the sensor triggers */
94 knot_thing_config_data_item(SPEED_SENSOR_ID, (KNOT_EVT_FLAG_LOWER_THRESHOLD|KNOT_EVT_FLAG_UPPER_THRESHOLD),
95 &lower_limit, &upper_limit);
Claudio Takahasiceb82702016-08-12 22:30:34 -030096
97 /* Calls loop() each 1 seconds: simulates Arduino loop function */
98 timeout_id = g_timeout_add_seconds(1, loop, NULL);
99
100 g_main_loop_run(main_loop);
101
102 g_source_remove(timeout_id);
103
104 g_main_loop_unref(main_loop);
105
Tiago Barros6978d8f2016-09-06 09:19:34 -0300106 knot_thing_exit();
107
Claudio Takahasiceb82702016-08-12 22:30:34 -0300108 return 0;
109}