Tiago Barros | 7428dfd | 2016-09-06 17:56:00 -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 | #include <stdint.h> |
| 11 | |
| 12 | #include "KNoTThing.h" |
| 13 | |
| 14 | KNoTThing::KNoTThing() |
| 15 | { |
Rodrigo Alves | 3740c83 | 2016-11-04 13:08:12 -0300 | [diff] [blame] | 16 | |
Tiago Barros | 7428dfd | 2016-09-06 17:56:00 -0300 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | KNoTThing::~KNoTThing() |
| 20 | { |
| 21 | knot_thing_exit(); |
| 22 | } |
| 23 | |
Rodrigo Alves | 4d633dc | 2016-11-09 11:03:04 -0300 | [diff] [blame] | 24 | int KNoTThing::init(const char *thing_name) |
Tiago Barros | 7428dfd | 2016-09-06 17:56:00 -0300 | [diff] [blame] | 25 | { |
Rodrigo Alves | 4d633dc | 2016-11-09 11:03:04 -0300 | [diff] [blame] | 26 | return knot_thing_init(thing_name); |
Tiago Barros | 7428dfd | 2016-09-06 17:56:00 -0300 | [diff] [blame] | 27 | } |
| 28 | |
Rodrigo Alves | 2d4e99d | 2016-11-23 11:19:11 -0300 | [diff] [blame] | 29 | int KNoTThing::registerIntData(const char *name, uint8_t sensor_id, |
| 30 | uint16_t type_id, uint8_t unit, |
| 31 | intDataFunction read, intDataFunction write) |
Tiago Barros | 7428dfd | 2016-09-06 17:56:00 -0300 | [diff] [blame] | 32 | { |
| 33 | knot_data_functions func; |
| 34 | func.int_f.read = read; |
| 35 | func.int_f.write = write; |
| 36 | |
Rodrigo Alves | 3740c83 | 2016-11-04 13:08:12 -0300 | [diff] [blame] | 37 | return knot_thing_register_data_item(sensor_id, name, type_id, |
Rodrigo Alves | 2d4e99d | 2016-11-23 11:19:11 -0300 | [diff] [blame] | 38 | KNOT_VALUE_TYPE_INT, unit, &func); |
Tiago Barros | 7428dfd | 2016-09-06 17:56:00 -0300 | [diff] [blame] | 39 | } |
| 40 | |
Rodrigo Alves | 2d4e99d | 2016-11-23 11:19:11 -0300 | [diff] [blame] | 41 | int KNoTThing::registerFloatData(const char *name, uint8_t sensor_id, |
| 42 | uint16_t type_id, uint8_t unit, |
| 43 | floatDataFunction read, floatDataFunction write) |
Tiago Barros | 7428dfd | 2016-09-06 17:56:00 -0300 | [diff] [blame] | 44 | { |
| 45 | knot_data_functions func; |
| 46 | func.float_f.read = read; |
| 47 | func.float_f.write = write; |
| 48 | |
Rodrigo Alves | 3740c83 | 2016-11-04 13:08:12 -0300 | [diff] [blame] | 49 | return knot_thing_register_data_item(sensor_id, name, type_id, |
Rodrigo Alves | 2d4e99d | 2016-11-23 11:19:11 -0300 | [diff] [blame] | 50 | KNOT_VALUE_TYPE_FLOAT, unit, &func); |
Tiago Barros | 7428dfd | 2016-09-06 17:56:00 -0300 | [diff] [blame] | 51 | |
| 52 | } |
| 53 | |
Rodrigo Alves | 2d4e99d | 2016-11-23 11:19:11 -0300 | [diff] [blame] | 54 | int KNoTThing::registerBoolData(const char *name, uint8_t sensor_id, |
| 55 | uint16_t type_id, uint8_t unit, |
| 56 | boolDataFunction read, boolDataFunction write) |
Tiago Barros | 7428dfd | 2016-09-06 17:56:00 -0300 | [diff] [blame] | 57 | { |
| 58 | knot_data_functions func; |
| 59 | func.bool_f.read = read; |
| 60 | func.bool_f.write = write; |
| 61 | |
Rodrigo Alves | 3740c83 | 2016-11-04 13:08:12 -0300 | [diff] [blame] | 62 | return knot_thing_register_data_item(sensor_id, name, type_id, |
Rodrigo Alves | 2d4e99d | 2016-11-23 11:19:11 -0300 | [diff] [blame] | 63 | KNOT_VALUE_TYPE_BOOL, unit, &func); |
Tiago Barros | 7428dfd | 2016-09-06 17:56:00 -0300 | [diff] [blame] | 64 | |
| 65 | } |
| 66 | |
Rodrigo Alves | 2d4e99d | 2016-11-23 11:19:11 -0300 | [diff] [blame] | 67 | int KNoTThing::registerRawData(const char *name, uint8_t *raw_buffer, |
| 68 | uint8_t raw_buffer_len, uint8_t sensor_id, uint16_t type_id, |
| 69 | uint8_t unit, rawDataFunction read, rawDataFunction write) |
Tiago Barros | 7428dfd | 2016-09-06 17:56:00 -0300 | [diff] [blame] | 70 | { |
| 71 | knot_data_functions func; |
| 72 | func.raw_f.read = read; |
| 73 | func.raw_f.write = write; |
| 74 | |
Rodrigo Alves | 2d4e99d | 2016-11-23 11:19:11 -0300 | [diff] [blame] | 75 | return knot_thing_register_raw_data_item(sensor_id, name, raw_buffer, |
| 76 | raw_buffer_len, type_id, KNOT_VALUE_TYPE_RAW, unit, &func); |
Tiago Barros | 7428dfd | 2016-09-06 17:56:00 -0300 | [diff] [blame] | 77 | } |
| 78 | |
Tiago Barros | 7428dfd | 2016-09-06 17:56:00 -0300 | [diff] [blame] | 79 | void KNoTThing::run() |
| 80 | { |
| 81 | knot_thing_run(); |
| 82 | } |
| 83 | |
Larissa Lages | 6fd84f2 | 2017-05-08 08:07:12 -0300 | [diff] [blame] | 84 | int KNoTThing::registerDefaultConfig(uint8_t sensor_id, uint8_t event_flags, |
| 85 | uint16_t time_sec, int32_t upper_int, uint32_t upper_dec, |
| 86 | int32_t lower_int, uint32_t lower_dec) |
| 87 | { |
Claudio Takahasi | a04a2d4 | 2018-07-24 14:24:28 -0300 | [diff] [blame^] | 88 | knot_value_type lower; |
| 89 | knot_value_type upper; |
Larissa Lages | 56f067b | 2017-07-19 15:08:06 -0300 | [diff] [blame] | 90 | |
| 91 | lower.val_f.value_int = lower_int; |
| 92 | lower.val_f.value_dec = lower_dec; |
| 93 | upper.val_f.value_int = upper_int; |
| 94 | upper.val_f.value_dec = upper_dec; |
| 95 | |
| 96 | return knot_thing_config_data_item(sensor_id, event_flags, time_sec, |
| 97 | &lower, &upper); |
Larissa Lages | 6fd84f2 | 2017-05-08 08:07:12 -0300 | [diff] [blame] | 98 | } |