|
| 1 | +/* |
| 2 | +* Author: Hiroyuki Mino <[email protected]> |
| 3 | +* Copyright (c) 2019 Omron Electronic Components - Americas |
| 4 | +* |
| 5 | +* Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 6 | +* this software and associated documentation files (the "Software"), to deal in |
| 7 | +* the Software without restriction, including without limitation the rights to |
| 8 | +* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 9 | +* the Software, and to permit persons to whom the Software is furnished to do so, |
| 10 | +* subject to the following conditions: |
| 11 | +* |
| 12 | +* The above copyright notice and this permission notice shall be included in all |
| 13 | +* copies or substantial portions of the Software. |
| 14 | +* |
| 15 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 17 | +* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 18 | +* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 19 | +* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 20 | +* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +/*=========================================================================*/ |
| 24 | + |
| 25 | +#pragma once |
| 26 | + |
| 27 | +#include <string> |
| 28 | +#include <iostream> |
| 29 | +#include <limits> |
| 30 | + |
| 31 | +#include <stdint.h> |
| 32 | +#include <stdlib.h> |
| 33 | +#include <unistd.h> |
| 34 | +#include <string.h> |
| 35 | +#include <fcntl.h> |
| 36 | +#include <errno.h> |
| 37 | +#include <sys/select.h> |
| 38 | +#include <sys/types.h> |
| 39 | +#include <sys/stat.h> |
| 40 | + |
| 41 | + |
| 42 | +#include <upm/upm_utilities.h> |
| 43 | + |
| 44 | + |
| 45 | +/*MACROS and enum */ |
| 46 | + |
| 47 | + |
| 48 | +#define OM2JCIEBU_FLASH_LONG_DATA_READ_ADD_UART 0x500E |
| 49 | +#define OM2JCIEBU_LIVE_LONG_DATA_READ_ADD_UART 0x5021 |
| 50 | + |
| 51 | +#define OM2JCIEBU_LED_CONFIGUARTION_ADD_UART 0x5111 |
| 52 | +#define OM2JCIEBU_ADV_CONFIGUARTION_ADD_UART 0x5115 |
| 53 | + |
| 54 | +#define OM2JCIEBU_LED_CONFIGUARTION_UUID_BLE "ab705111-0a3a-11e8-ba89-0ed5f89f718b" |
| 55 | +#define OM2JCIEBU_ADV_CONFIGUARTION_UUID_BLE "ab705115-0a3a-11e8-ba89-0ed5f89f718b" |
| 56 | +#define OM2JCIEBU_LIVE_LONG_DATA_READ_UUID_BLE "ab705012-0a3a-11e8-ba89-0ed5f89f718b" |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +#define OM2JCIEBU_CRC_LENGTH 2 |
| 61 | +#define OM2JCIEBU_CRC16 0xFFFF |
| 62 | +#define OM2JCIEBU_INTERVAL_UNIT 0.625 |
| 63 | + |
| 64 | + |
| 65 | +/*=========================================================================*/ |
| 66 | + |
| 67 | +namespace upm |
| 68 | +{ |
| 69 | +/** |
| 70 | + * @brief omron 2JCIEBU01 Environment sensor |
| 71 | + * @defgroup 2jciebu01 libupm-2jciebu01 |
| 72 | + * @ingroup Omron USB type |
| 73 | + */ |
| 74 | + |
| 75 | +/** |
| 76 | + * @library 2jciebu01 |
| 77 | + * @sensor 2jciebu01 |
| 78 | + * @comname Environment Sensor Module |
| 79 | + * @altname Omron Environment sensor USB type |
| 80 | + * @type USB |
| 81 | + * @man Omron |
| 82 | + * @web https://www.components.omron.com/solutions/mems-sensors/environment-sensor |
| 83 | + * @con usb |
| 84 | + * |
| 85 | + * @brief API for the Omron USB type environment Sensor Module using USB to UART interface |
| 86 | + * |
| 87 | + * It is connected via a UART at 115200 baud. |
| 88 | + * |
| 89 | + * @snippet 2jciebu01.cxx Interesting |
| 90 | + */ |
| 91 | +class OM2JCIEBU |
| 92 | +{ |
| 93 | +public : |
| 94 | + typedef enum { |
| 95 | + ALL_PARAM, |
| 96 | + TEMP, |
| 97 | + HUMIDITY, |
| 98 | + AMBIENT_LIGHT, |
| 99 | + PRESSURE, |
| 100 | + NOISE, |
| 101 | + ETVOC, |
| 102 | + ECO2, |
| 103 | + DISCOMFORT_INDEX, |
| 104 | + HEAT_STROKE, |
| 105 | + LED_CONFIGURE, |
| 106 | + ADV_CONFIGURE, |
| 107 | + } OM2JCIEBU_ATTRIBUTE_T; |
| 108 | + |
| 109 | + typedef enum { |
| 110 | + BLE, |
| 111 | + USB_TO_UART |
| 112 | + } OM2JCIEBU_INTERFACE_T; |
| 113 | + |
| 114 | + typedef enum { |
| 115 | + SENSOR_DATA = 1, |
| 116 | + ACCELERATION_DATA, |
| 117 | + ACCELERATION_SENSOR_DATA, |
| 118 | + ACCELERATION_SENSOR_FLAG, |
| 119 | + SERIAL_NUMBER |
| 120 | + } OM2JCIEBU_ADV_PARAM_T; |
| 121 | + |
| 122 | + typedef enum { |
| 123 | + ERROR_CRC_WRONG = -1, |
| 124 | + ERROR_WRONG_COMMAND, |
| 125 | + ERROR_WRONG_ADDRESS, |
| 126 | + ERROR_WRONG_LENGTH, |
| 127 | + ERROR_DATA_RANGE, |
| 128 | + ERROR_BUSY, |
| 129 | + ERROR_UNKNOWN, |
| 130 | + ERROR_CRC_MISMATCH, |
| 131 | + FAILURE = 0, |
| 132 | + SUCCESS = 1 |
| 133 | + } OM2JCIEBU_ERROR_T; |
| 134 | + |
| 135 | + typedef enum { |
| 136 | + NORMALLY_OFF = 0, |
| 137 | + NORMALLY_ON, |
| 138 | + TEMP_SACLE, |
| 139 | + HUMIDITY_SCALE, |
| 140 | + AMBIENT_LIGHT_SCALE, |
| 141 | + PRESSURE_SCALE, |
| 142 | + NOISE_SCALE, |
| 143 | + ETVOC_SCALE, |
| 144 | + SI_SCALE, |
| 145 | + PGA_SCALE |
| 146 | + } OM2JCIEBU_LED_SCALE_T; |
| 147 | + |
| 148 | + typedef struct { |
| 149 | + uint8_t sequence_number; |
| 150 | + int16_t temperature; |
| 151 | + int16_t relative_humidity; |
| 152 | + int16_t ambient_light; |
| 153 | + int32_t pressure; |
| 154 | + int16_t noise; |
| 155 | + int16_t eTVOC; |
| 156 | + int16_t eCO2; |
| 157 | + int16_t discomfort_index; |
| 158 | + int16_t heat_stroke; |
| 159 | + } __attribute__((packed))om2jciebuData_t; |
| 160 | + |
| 161 | + /** |
| 162 | + * OM2JCIEBU destructor |
| 163 | + */ |
| 164 | + virtual ~OM2JCIEBU() {} |
| 165 | + |
| 166 | + /** |
| 167 | + * get address or UUID based on attribute name |
| 168 | + * |
| 169 | + * @param attribute_name attribute name of sensor |
| 170 | + * @param interface Interface name of sensor |
| 171 | + * @param attribute_value address value and UUID based on attribute name |
| 172 | + */ |
| 173 | + void getAddress(OM2JCIEBU_ATTRIBUTE_T attribute_name, OM2JCIEBU_INTERFACE_T interface, void *attribute_value); |
| 174 | + |
| 175 | + /** |
| 176 | + * Delay for read sensor data; |
| 177 | + * |
| 178 | + * @param second second for delay |
| 179 | + */ |
| 180 | + void delay(int second); |
| 181 | + |
| 182 | + /** |
| 183 | + * Calculate crc-16 from the header |
| 184 | + * to the end of the payload. |
| 185 | + * |
| 186 | + * @param data Packet |
| 187 | + * @param length length of packet |
| 188 | + * @return 16 bit crc of payload |
| 189 | + */ |
| 190 | + uint16_t crc_16(uint8_t *data, int length); |
| 191 | + |
| 192 | + |
| 193 | + /** |
| 194 | + * Set LED configartion of sensor |
| 195 | + * |
| 196 | + * @param state state for led configuartion |
| 197 | + * @param red value of red |
| 198 | + * @param green value of green |
| 199 | + * @param blue value of blue |
| 200 | + */ |
| 201 | + virtual void configureSensorLedState(OM2JCIEBU_LED_SCALE_T state, uint8_t red, uint8_t green, uint8_t blue) = 0; |
| 202 | + |
| 203 | + /** |
| 204 | + * Set Advertise configuration of sensor |
| 205 | + * |
| 206 | + * @param miliseconds interval for Advertise data |
| 207 | + * @param adv_mode Advertise mode |
| 208 | + */ |
| 209 | + virtual void configureSensorAdvSetting(uint16_t milliseconds, OM2JCIEBU_ADV_PARAM_T adv_mode) = 0; |
| 210 | + |
| 211 | + |
| 212 | + /** |
| 213 | + * Calculate and parse sensor data and store into |
| 214 | + * Sensor data structure |
| 215 | + * |
| 216 | + * @param data Packet |
| 217 | + * |
| 218 | + */ |
| 219 | + virtual void parseSensorData(uint8_t *data) = 0; |
| 220 | + |
| 221 | + /** |
| 222 | + * Get omron sensor live data as per attribute name |
| 223 | + * |
| 224 | + * @param attribute_name Name of attribute |
| 225 | + * @param attribute_data Data of attirbute |
| 226 | + * @return One of the OM2JCIEBU_ERROR_T values |
| 227 | + */ |
| 228 | + virtual OM2JCIEBU_ERROR_T getSensorData(OM2JCIEBU_ATTRIBUTE_T attribute_name, void *attribute_data) = 0; |
| 229 | + |
| 230 | + /** |
| 231 | + * Verifies the packet header and indicates it is valid or not |
| 232 | + * |
| 233 | + * @param pkt Packet to check |
| 234 | + * @param len length of packet |
| 235 | + * @return One of the OM2JCIEBU_ERROR_T values |
| 236 | + */ |
| 237 | + |
| 238 | + virtual OM2JCIEBU_ERROR_T verifyPacket(uint8_t *pkt, int len) = 0; |
| 239 | +}; |
| 240 | +} |
0 commit comments