|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2015 Adafruit Industries |
| 4 | +// Author: Tony DiCola |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all |
| 14 | +// copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +// SOFTWARE. |
| 23 | +#include "Adafruit_IO_Arduino.h" |
| 24 | + |
| 25 | +FeedData::FeedData() { |
| 26 | + // Feed data is invalid without a value so write all zeros. |
| 27 | + memset(_value, 0, sizeof(_value)); |
| 28 | +} |
| 29 | + |
| 30 | +FeedData::FeedData(const FeedData& copy) { |
| 31 | + // Copy the value from the provided FeedData object. |
| 32 | + memset(_value, 0, sizeof(_value)); |
| 33 | + strncpy(_value, copy._value, sizeof(_value)-1); |
| 34 | +} |
| 35 | + |
| 36 | +FeedData::FeedData(const char* value) { |
| 37 | + // Copy the provided string value as the feed data value. If the string |
| 38 | + // can't fit into the feed data's memory then keep the value in an invalid |
| 39 | + // state (all zeros). |
| 40 | + memset(_value, 0, sizeof(_value)); |
| 41 | + if (strlen(value) <= FEEDDATA_LENGTH-1) { |
| 42 | + strncpy(_value, value, sizeof(_value)-1); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +FeedData::FeedData(Stream& stream, uint16_t length, uint32_t timeoutMS) { |
| 47 | + // Load the data value from the provided stream. Will read a value up to |
| 48 | + // length characters in size. If the data can't be read for some reason, |
| 49 | + // like the timeout exceeding, then the FeedData will be left in an invalid |
| 50 | + // state. |
| 51 | + memset(_value, 0, sizeof(_value)); |
| 52 | + if (length > FEEDDATA_LENGTH-1) { |
| 53 | + // Not enough space to store the value. |
| 54 | + return; |
| 55 | + } |
| 56 | + stream.setTimeout(timeoutMS); |
| 57 | + if (stream.readBytes(_value, length) != length) { |
| 58 | + // Didn't find enough data, set the value as invalid. |
| 59 | + memset(_value, 0, sizeof(_value)); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +bool FeedData::intValue(int* value) { |
| 64 | + // Attempt to convert the value to an integer. Returns true if it succeeds, |
| 65 | + // and false if it fails for some reason. |
| 66 | + char* endptr; |
| 67 | + *value = (int)strtol(_value, &endptr, 10); |
| 68 | + return (*_value != 0 && *endptr == 0); |
| 69 | +} |
| 70 | + |
| 71 | +bool FeedData::uintValue(unsigned int* value) { |
| 72 | + // Attempt to convert the value to an unsigned integer. Returns true if it |
| 73 | + // succeeds, and false if it fails for some reason. |
| 74 | + char* endptr; |
| 75 | + *value = (unsigned int)strtoul(_value, &endptr, 10); |
| 76 | + return (*_value != 0 && *endptr == 0); |
| 77 | +} |
| 78 | + |
| 79 | +bool FeedData::longValue(long* value) { |
| 80 | + // Attempt to convert the value to a long. Returns true if it |
| 81 | + // succeeds, and false if it fails for some reason. |
| 82 | + char* endptr; |
| 83 | + *value = strtol(_value, &endptr, 10); |
| 84 | + return (*_value != 0 && *endptr == 0); |
| 85 | +} |
| 86 | + |
| 87 | +bool FeedData::ulongValue(unsigned long* value) { |
| 88 | + // Attempt to convert the value to an unsigned long. Returns true if it |
| 89 | + // succeeds, and false if it fails for some reason. |
| 90 | + char* endptr; |
| 91 | + *value = strtoul(_value, &endptr, 10); |
| 92 | + return (*_value != 0 && *endptr == 0); |
| 93 | +} |
| 94 | + |
| 95 | +bool FeedData::floatValue(float* value) { |
| 96 | + // Attempt to convert the value to a float. Returns true if it succeeds, |
| 97 | + // and false if it fails for some reason. |
| 98 | + char* endptr; |
| 99 | + *value = (float)strtod(_value, &endptr); |
| 100 | + return (*_value != 0 && *endptr == 0); |
| 101 | +} |
| 102 | + |
| 103 | +bool FeedData::doubleValue(double* value) { |
| 104 | + // Attempt to convert the value to a double. Returns true if it succeeds, |
| 105 | + // and false if it fails for some reason. |
| 106 | + char* endptr; |
| 107 | + *value = strtod(_value, &endptr); |
| 108 | + return (*_value != 0 && *endptr == 0); |
| 109 | +} |
| 110 | + |
| 111 | +// Buffer to store values converted from numbers to strings before sending to IO. |
| 112 | +static char _converted[FEEDDATA_LENGTH]; |
| 113 | + |
| 114 | +bool Adafruit_IO_Feed::send(int value) { |
| 115 | + // Convert int to string, then send the value (being careful not to quote it). |
| 116 | + memset(_converted, 0, sizeof(_converted)); |
| 117 | + itoa(value, _converted, 10); |
| 118 | + return _adapter->send(_name, _converted, _key, false); |
| 119 | +} |
| 120 | + |
| 121 | +bool Adafruit_IO_Feed::send(unsigned int value) { |
| 122 | + // Convert uint to string, then send the value (being careful not to quote it). |
| 123 | + memset(_converted, 0, sizeof(_converted)); |
| 124 | + utoa(value, _converted, 10); |
| 125 | + return _adapter->send(_name, _converted, _key, false); |
| 126 | +} |
| 127 | + |
| 128 | +bool Adafruit_IO_Feed::send(long value) { |
| 129 | + // Convert long to string, then send the value (being careful not to quote it). |
| 130 | + memset(_converted, 0, sizeof(_converted)); |
| 131 | + ltoa(value, _converted, 10); |
| 132 | + return _adapter->send(_name, _converted, _key, false); |
| 133 | +} |
| 134 | + |
| 135 | +bool Adafruit_IO_Feed::send(unsigned long value) { |
| 136 | + // Convert ulong to string, then send the value (being careful not to quote it). |
| 137 | + memset(_converted, 0, sizeof(_converted)); |
| 138 | + ultoa(value, _converted, 10); |
| 139 | + return _adapter->send(_name, _converted, _key, false); |
| 140 | +} |
| 141 | + |
| 142 | +bool Adafruit_IO_Feed::send(float value) { |
| 143 | + // Convert float to string using scientific notation, then send the value |
| 144 | + // (being careful not to quote it). |
| 145 | + memset(_converted, 0, sizeof(_converted)); |
| 146 | + dtostre(value, _converted, 10, 0); |
| 147 | + return _adapter->send(_name, _converted, _key, false); |
| 148 | +} |
| 149 | + |
| 150 | +bool Adafruit_IO_Feed::send(double value) { |
| 151 | + // Convert double to string using scientific notation, then send the value |
| 152 | + // (being careful not to quote it). |
| 153 | + memset(_converted, 0, sizeof(_converted)); |
| 154 | + dtostre(value, _converted, 10, 0); |
| 155 | + return _adapter->send(_name, _converted, _key, false); |
| 156 | +} |
0 commit comments