Claudio Takahasi | ceb8270 | 2016-08-12 22:30:34 -0300 | [diff] [blame] | 1 | /* |
| 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 Barros | 6978d8f | 2016-09-06 09:19:34 -0300 | [diff] [blame] | 12 | * 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 Takahasi | ceb8270 | 2016-08-12 22:30:34 -0300 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <stdint.h> |
| 21 | #include <string.h> |
| 22 | #include <glib.h> |
| 23 | |
Tiago Barros | 6978d8f | 2016-09-06 09:19:34 -0300 | [diff] [blame] | 24 | #include "knot_thing_main.h" |
| 25 | #include "knot_types.h" |
Claudio Takahasi | ceb8270 | 2016-08-12 22:30:34 -0300 | [diff] [blame] | 26 | |
| 27 | static GMainLoop *main_loop; |
Tiago Barros | 6978d8f | 2016-09-06 09:19:34 -0300 | [diff] [blame] | 28 | static int32_t speed_value = 0; |
Claudio Takahasi | ceb8270 | 2016-08-12 22:30:34 -0300 | [diff] [blame] | 29 | |
| 30 | static void sig_term(int sig) |
| 31 | { |
| 32 | g_main_loop_quit(main_loop); |
| 33 | } |
| 34 | |
| 35 | static int speed_read(int32_t *val, int32_t *multiplier) |
| 36 | { |
Claudio Takahasi | ceb8270 | 2016-08-12 22:30:34 -0300 | [diff] [blame] | 37 | |
Tiago Barros | 6978d8f | 2016-09-06 09:19:34 -0300 | [diff] [blame] | 38 | *val = speed_value++; |
| 39 | *multiplier = 1; |
| 40 | printf("speed_read(): %d\n", *val); |
| 41 | return 0; |
Claudio Takahasi | ceb8270 | 2016-08-12 22:30:34 -0300 | [diff] [blame] | 42 | } |
| 43 | |
Tiago Barros | 6978d8f | 2016-09-06 09:19:34 -0300 | [diff] [blame] | 44 | static 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 Takahasi | ceb8270 | 2016-08-12 22:30:34 -0300 | [diff] [blame] | 50 | |
| 51 | static gboolean loop(gpointer user_data) |
| 52 | { |
Tiago Barros | 6978d8f | 2016-09-06 09:19:34 -0300 | [diff] [blame] | 53 | knot_thing_run(); |
Claudio Takahasi | ceb8270 | 2016-08-12 22:30:34 -0300 | [diff] [blame] | 54 | |
| 55 | return TRUE; |
| 56 | } |
| 57 | |
Tiago Barros | 6978d8f | 2016-09-06 09:19:34 -0300 | [diff] [blame] | 58 | #define SPEED_SENSOR_ID 3 |
| 59 | #define SPEED_SENSOR_NAME "Speed Sensor" |
| 60 | |
Claudio Takahasi | ceb8270 | 2016-08-12 22:30:34 -0300 | [diff] [blame] | 61 | int main(int argc, char *argv[]) |
| 62 | { |
Claudio Takahasi | ceb8270 | 2016-08-12 22:30:34 -0300 | [diff] [blame] | 63 | /* |
Tiago Barros | 6978d8f | 2016-09-06 09:19:34 -0300 | [diff] [blame] | 64 | * RPi fake speed sensor. This example shows how KNOT Thing |
| 65 | * should be used to register and implement sensor |
Claudio Takahasi | ceb8270 | 2016-08-12 22:30:34 -0300 | [diff] [blame] | 66 | * read/write callbacks. |
| 67 | */ |
| 68 | |
Tiago Barros | 6978d8f | 2016-09-06 09:19:34 -0300 | [diff] [blame] | 69 | 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 Takahasi | ceb8270 | 2016-08-12 22:30:34 -0300 | [diff] [blame] | 81 | 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 Barros | 6978d8f | 2016-09-06 09:19:34 -0300 | [diff] [blame] | 86 | printf("Starting...\n"); |
| 87 | knot_thing_init(); |
Claudio Takahasi | ceb8270 | 2016-08-12 22:30:34 -0300 | [diff] [blame] | 88 | |
| 89 | /* Register an integer sensor: should be called from Arduino setup() */ |
Tiago Barros | 6978d8f | 2016-09-06 09:19:34 -0300 | [diff] [blame] | 90 | 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 Takahasi | ceb8270 | 2016-08-12 22:30:34 -0300 | [diff] [blame] | 96 | |
| 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 Barros | 6978d8f | 2016-09-06 09:19:34 -0300 | [diff] [blame] | 106 | knot_thing_exit(); |
| 107 | |
Claudio Takahasi | ceb8270 | 2016-08-12 22:30:34 -0300 | [diff] [blame] | 108 | return 0; |
| 109 | } |