Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: max32630fthr Adafruit_FeatherOLED USBDevice
TempComm.cpp
00001 /*************************************************************************** 00002 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved. 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a 00005 * copy of this software and associated documentation files (the "Software"), 00006 * to deal in the Software without restriction, including without limitation 00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 * and/or sell copies of the Software, and to permit persons to whom the 00009 * Software is furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included 00012 * in all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES 00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00020 * OTHER DEALINGS IN THE SOFTWARE. 00021 * 00022 * Except as contained in this notice, the name of Maxim Integrated 00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated 00024 * Products, Inc. Branding Policy. 00025 * 00026 * The mere transfer of this software does not imply any licenses 00027 * of trade secrets, proprietary technology, copyrights, patents, 00028 * trademarks, maskwork rights, or any other form of intellectual 00029 * property whatsoever. Maxim Integrated Products, Inc. retains all 00030 * ownership rights. 00031 **************************************************************************** 00032 */ 00033 00034 #include <ctype.h> 00035 #include "TempComm.h" 00036 #include "mxc_errors.h" 00037 #include "MAX30205.h" 00038 #include "CRC8.h" 00039 #include "Peripherals.h" 00040 #include "utils.h" 00041 #include "BLE_ICARUS.h" 00042 00043 #define TP_REG_COUNT 64 00044 00045 00046 00047 00048 00049 #define MINIMUM_PERIOD_SECOND 0.5 00050 00051 const char *cmd_tbl_tp[] = { 00052 "get_format temp 0", 00053 "read temp 0", /* raw */ 00054 "set_cfg temp sr", //set sample rate 00055 "get_reg temp", 00056 "set_reg temp", 00057 "dump_reg temp", 00058 }; 00059 00060 TempComm::TempComm(USBSerial* USB): 00061 SensorComm("temp", true) 00062 { 00063 m_USB = USB; 00064 TempComm_Set_ReadTempStatus(false); 00065 ticker_period_second_ = MINIMUM_PERIOD_SECOND; 00066 sampling_period_ms_ = MINIMUM_PERIOD_SECOND * 1000; 00067 00068 } 00069 00070 void TempComm::stop() 00071 { 00072 int ret; 00073 comm_mutex.lock(); 00074 data_report_mode = 0; 00075 comm_mutex.unlock(); 00076 ret = sensor->sensor_enable(0); 00077 m_tempcomm_ticker_.detach(); 00078 TempComm_Set_ReadTempStatus(false); 00079 if (ret < 0) { 00080 pr_err("sensor_enable failed. ret: %d", ret); 00081 } 00082 } 00083 00084 bool TempComm::parse_command(const char* cmd) 00085 { 00086 int i; 00087 int ret = EXIT_SUCCESS; 00088 uint8_t reg_addr; 00089 uint16_t val; 00090 bool recognizedCmd = false; 00091 int data_len = 0; 00092 char charbuf[512]; 00093 addr_val_pair reg_vals[TP_REG_COUNT]; 00094 bool comma; 00095 00096 if (sensor == NULL) { 00097 pr_err("sensor object is invalid!"); 00098 return false; 00099 } 00100 00101 for (i = 0; i < NUM_CMDS; i++) { 00102 if (starts_with(cmd, cmd_tbl_tp[i])) { 00103 cmd_state_t user_cmd = (cmd_state_t)i; 00104 recognizedCmd = true; 00105 switch (user_cmd) { 00106 case get_tp_format_mode0: 00107 if(AsciiEn) 00108 { 00109 m_USB->printf("\r\n%s format=smpleCnt,temp err=0\r\n", 00110 cmd); 00111 } 00112 else 00113 { 00114 data_len = snprintf(charbuf, sizeof(charbuf), "\r\n%s enc=bin cs=1 format={smpleCnt,8},{temp,16,2} err=0\r\n", 00115 cmd); 00116 m_USB->printf(charbuf); 00117 } 00118 break; 00119 case read_tp_mode0: 00120 comm_mutex.lock(); 00121 data_report_mode = read_tp_mode0; 00122 comm_mutex.unlock(); 00123 sample_count = 0; 00124 ret = sensor->sensor_enable(1); 00125 if (ret < 0) { 00126 pr_err("sensor_enable failed. ret: %d\r\n", ret); 00127 } 00128 00129 m_tempcomm_ticker_.detach(); 00130 m_tempcomm_ticker_.attach(callback(this,&TempComm::TempComm_Set_ReadTempStatus_Ticker), ticker_period_second_); 00131 data_len = snprintf(charbuf, sizeof(charbuf), "\r\n%s err=%d\r\n", cmd, ret); 00132 m_USB->printf(charbuf); 00133 break; 00134 case set_cfg_sr: 00135 ret = (parse_cmd_data(cmd, cmd_tbl_tp[i], &sampling_period_ms_, 1, false) != 1); 00136 if (ret) { 00137 data_len = snprintf(charbuf, sizeof(charbuf), "\r\n%s err=%d\r\n", cmd, -1); 00138 m_USB->printf(charbuf); 00139 break; 00140 } 00141 ticker_period_second_ = sampling_period_ms_/ 1000.0; 00142 if(ticker_period_second_ < MINIMUM_PERIOD_SECOND){ 00143 pr_err("minimum value is less than 0.5\r\n"); 00144 sampling_period_ms_ = MINIMUM_PERIOD_SECOND * 1000; 00145 ticker_period_second_ = MINIMUM_PERIOD_SECOND; 00146 ret = -1; 00147 } 00148 data_len = snprintf(charbuf, sizeof(charbuf), "\r\n%s err=%d\r\n", cmd, ret); 00149 m_USB->printf(charbuf); 00150 break; 00151 case get_reg: 00152 reg_addr = 0; 00153 val = 0; 00154 ret = parse_get_reg_cmd(cmd, sensor_type, ®_addr); 00155 if (!ret) { 00156 ret = ((MAX30205*)sensor)->readRegister(static_cast<MAX30205::Registers_e> (reg_addr), val); 00157 } 00158 00159 reg_vals[0].addr = reg_addr; 00160 reg_vals[0].val = val; 00161 InsertRegValuesIntoBleQeueu(reg_vals, 1); 00162 00163 data_len = snprintf(charbuf, sizeof(charbuf), "\r\n%s reg_val=%02X err=%d\r\n", cmd, val, ret); 00164 m_USB->printf(charbuf); 00165 break; 00166 case set_reg: 00167 ret = parse_set_reg_cmd(cmd, sensor_type, ®_addr, &val); 00168 if (!ret) { 00169 ret = ((MAX30205*)sensor)->writeRegister(static_cast<MAX30205::Registers_e> (reg_addr), val); 00170 } 00171 00172 data_len = snprintf(charbuf, sizeof(charbuf), "\r\n%s err=%d\r\n", cmd, ret); 00173 m_USB->printf(charbuf); 00174 break; 00175 case dump_regs: 00176 for (int i = 0; i < TP_REG_COUNT; i++) { 00177 reg_vals[i].addr = 0xFF; 00178 } 00179 00180 ret = sensor->dump_registers(reg_vals); 00181 00182 if (ret) { 00183 m_USB->printf("\r\n%s err=%d\n", cmd, ret); 00184 00185 data_len = snprintf(charbuf, sizeof(charbuf), "\r\n%s err=%d\n", cmd, ret); 00186 } 00187 else { 00188 data_len = snprintf(charbuf, sizeof(charbuf), "\r\n%s reg_val=", cmd); 00189 comma = false; 00190 for (int i = 0; i < TP_REG_COUNT; i++) { 00191 if(reg_vals[i].addr == 0xFF) 00192 break; 00193 if (comma) { 00194 data_len += snprintf(charbuf + data_len, 00195 sizeof(charbuf) - data_len - 1, ","); 00196 } 00197 data_len += snprintf(charbuf + data_len, 00198 sizeof(charbuf) - data_len - 1, 00199 "{%X,%X}", (unsigned int)reg_vals[i].addr, (unsigned int)reg_vals[i].val); 00200 comma = true; 00201 } 00202 data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len - 1, " err=0\r\n"); 00203 00204 m_USB->printf(charbuf); 00205 00206 } 00207 00208 break; 00209 00210 default: 00211 break; 00212 } 00213 00214 00215 if (BLE::Instance().gap().getState().connected) { 00216 BLE_Icarus_AddtoQueue((uint8_t *)charbuf, (int32_t)sizeof(charbuf), data_len); 00217 } 00218 00219 00220 } 00221 } 00222 00223 return recognizedCmd; 00224 } 00225 00226 int TempComm::data_report_execute(char* buf, int size) 00227 { 00228 int16_t data_len = 0; 00229 uint8_t tmp_report_mode; 00230 uint16_t tp_val; 00231 uint32_t ret; 00232 uint32_t tp_val_ext; 00233 float Celsius; 00234 00235 tp0_comm_packet *data_packet; 00236 00237 if (sensor == NULL) 00238 return 0; 00239 00240 if(!is_enabled()) 00241 return 0; 00242 00243 comm_mutex.lock(); 00244 tmp_report_mode = data_report_mode; 00245 comm_mutex.unlock(); 00246 00247 switch(tmp_report_mode) { 00248 case read_tp_mode0: 00249 if ( (m_can_read_temp_) ) { 00250 TempComm_Set_ReadTempStatus(false); 00251 ret = ((MAX30205*)sensor)->readTemperature(tp_val); 00252 00253 00254 if (ret != 0) 00255 return 0; 00256 tp_val_ext = (uint32_t)tp_val; 00257 Celsius = ((MAX30205*)sensor)->toCelsius(tp_val_ext); 00258 tp_val = Celsius*100; 00259 TempComm_instant_temp_celsius = Celsius; 00260 00261 if(AsciiEn) 00262 { 00263 data_len = snprintf(buf, size - 1, 00264 "%lu,%2.3f\r\n", 00265 sample_count++, 00266 Celsius); 00267 } 00268 else{ 00269 data_packet = (tp0_comm_packet *)buf; 00270 data_packet->start_byte = 0xAA; 00271 data_packet->smpleCnt = sample_count++; 00272 data_packet->tp = tp_val; 00273 data_packet->crc8 = crc8((uint8_t*)data_packet, sizeof(*data_packet) - sizeof(uint8_t)); 00274 data_len = sizeof(*data_packet); 00275 } 00276 } 00277 break; 00278 00279 default: 00280 return 0; 00281 } 00282 00283 if (data_len < 0) { 00284 pr_err("snprintf buf failed"); 00285 } else if (data_len > size) { 00286 pr_err("buffer is insufficient to hold data"); 00287 } 00288 00289 return data_len; 00290 } 00291 00292 void TempComm::TempComm_Set_ReadTempStatus(bool en){ 00293 m_can_read_temp_ = en; 00294 } 00295 00296 void TempComm::TempComm_Set_ReadTempStatus_Ticker(){ 00297 m_can_read_temp_ = true; 00298 }
Generated on Tue Jul 12 2022 20:09:29 by
