mbed code for MAXREFDES131 Qt GUI demo

Dependencies:   OWGridEye OneWire mbed

Committer:
j3
Date:
Fri Dec 02 19:34:37 2016 +0000
Revision:
4:7c61c45e12ee
Parent:
3:f2194ecf91f5
Update ow lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:bd523ea97465 1 /**********************************************************************
j3 0:bd523ea97465 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:bd523ea97465 3 *
j3 0:bd523ea97465 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:bd523ea97465 5 * copy of this software and associated documentation files (the "Software"),
j3 0:bd523ea97465 6 * to deal in the Software without restriction, including without limitation
j3 0:bd523ea97465 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:bd523ea97465 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:bd523ea97465 9 * Software is furnished to do so, subject to the following conditions:
j3 0:bd523ea97465 10 *
j3 0:bd523ea97465 11 * The above copyright notice and this permission notice shall be included
j3 0:bd523ea97465 12 * in all copies or substantial portions of the Software.
j3 0:bd523ea97465 13 *
j3 0:bd523ea97465 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:bd523ea97465 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:bd523ea97465 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:bd523ea97465 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:bd523ea97465 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:bd523ea97465 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:bd523ea97465 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:bd523ea97465 21 *
j3 0:bd523ea97465 22 * Except as contained in this notice, the name of Maxim Integrated
j3 0:bd523ea97465 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:bd523ea97465 24 * Products, Inc. Branding Policy.
j3 0:bd523ea97465 25 *
j3 0:bd523ea97465 26 * The mere transfer of this software does not imply any licenses
j3 0:bd523ea97465 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:bd523ea97465 28 * trademarks, maskwork rights, or any other form of intellectual
j3 0:bd523ea97465 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:bd523ea97465 30 * ownership rights.
j3 0:bd523ea97465 31 **********************************************************************/
j3 0:bd523ea97465 32
j3 0:bd523ea97465 33
j3 0:bd523ea97465 34 #include <string>
j3 0:bd523ea97465 35 #include "maxrefdes131_demo.h"
j3 0:bd523ea97465 36
j3 0:bd523ea97465 37 enum DemoState
j3 0:bd523ea97465 38 {
j3 0:bd523ea97465 39 STATE_POR,
j3 0:bd523ea97465 40 STATE_ESTABLISH_COMMS,
j3 0:bd523ea97465 41 STATE_ENUMERATE_SENSORS,
j3 0:bd523ea97465 42 STATE_SELECT_SENSOR,
j3 0:bd523ea97465 43 STATE_SEND_DATA,
j3 0:bd523ea97465 44 STATE_STOP_DATA,
j3 0:bd523ea97465 45 STATE_IDLE,
j3 0:bd523ea97465 46 STATE_INVALID
j3 0:bd523ea97465 47 };
j3 0:bd523ea97465 48
j3 0:bd523ea97465 49 volatile DemoState demoState = STATE_POR;
j3 0:bd523ea97465 50 volatile uint8_t selectedSensor = 0;
j3 0:bd523ea97465 51
j3 0:bd523ea97465 52 Serial comms(USBTX, USBRX);
j3 1:2ad0ae1416d2 53 DigitalOut frameRecvdPulse(D4, 1);
j3 0:bd523ea97465 54
j3 0:bd523ea97465 55 void rx_callback(void)
j3 0:bd523ea97465 56 {
j3 0:bd523ea97465 57 std::string rx_str;
j3 0:bd523ea97465 58 //size to 32 chars
j3 0:bd523ea97465 59 rx_str.resize(32);
j3 0:bd523ea97465 60
j3 0:bd523ea97465 61 size_t str_idx = 0;
j3 0:bd523ea97465 62 do
j3 0:bd523ea97465 63 {
j3 0:bd523ea97465 64 rx_str[str_idx++] = comms.getc();
j3 0:bd523ea97465 65 }
j3 0:bd523ea97465 66 while(rx_str[str_idx - 1] != '\n');
j3 0:bd523ea97465 67
j3 0:bd523ea97465 68 //Just get first 5 chars that represent command
j3 0:bd523ea97465 69 std::string rx_cmd = rx_str.substr(0,5);
j3 0:bd523ea97465 70
j3 0:bd523ea97465 71
j3 0:bd523ea97465 72 if(rx_cmd.compare(0, 5, "CMD_1") == 0)
j3 0:bd523ea97465 73 {
j3 0:bd523ea97465 74 demoState = STATE_ESTABLISH_COMMS;
j3 0:bd523ea97465 75 }
j3 0:bd523ea97465 76 else if(rx_cmd.compare(0, 5, "CMD_2") == 0)
j3 0:bd523ea97465 77 {
j3 0:bd523ea97465 78 demoState = STATE_ENUMERATE_SENSORS;
j3 0:bd523ea97465 79 }
j3 0:bd523ea97465 80 else if(rx_cmd.compare(0, 5, "CMD_3") == 0)
j3 0:bd523ea97465 81 {
j3 0:bd523ea97465 82 selectedSensor = (rx_str[6] - 48);
j3 0:bd523ea97465 83 demoState = STATE_SELECT_SENSOR;
j3 0:bd523ea97465 84 }
j3 0:bd523ea97465 85 else if(rx_cmd.compare(0, 5, "CMD_4") == 0)
j3 0:bd523ea97465 86 {
j3 0:bd523ea97465 87 demoState = STATE_SEND_DATA;
j3 0:bd523ea97465 88 }
j3 0:bd523ea97465 89 else if(rx_cmd.compare(0, 5, "CMD_5") == 0)
j3 0:bd523ea97465 90 {
j3 0:bd523ea97465 91 demoState = STATE_STOP_DATA;
j3 0:bd523ea97465 92 }
j3 0:bd523ea97465 93 else if(rx_cmd.compare(0, 5, "CMD_6") == 0)
j3 0:bd523ea97465 94 {
j3 1:2ad0ae1416d2 95 frameRecvdPulse = 0;
j3 0:bd523ea97465 96 frameRecvdPulse = 1;
j3 0:bd523ea97465 97 demoState = STATE_SEND_DATA;
j3 0:bd523ea97465 98 }
j3 0:bd523ea97465 99 else
j3 0:bd523ea97465 100 {
j3 0:bd523ea97465 101 demoState = STATE_INVALID;
j3 0:bd523ea97465 102 }
j3 0:bd523ea97465 103 }
j3 0:bd523ea97465 104
j3 0:bd523ea97465 105 int main(void)
j3 0:bd523ea97465 106 {
j3 0:bd523ea97465 107 comms.baud(115200);
j3 0:bd523ea97465 108 comms.attach(&rx_callback, SerialBase::RxIrq);
j3 0:bd523ea97465 109
j3 3:f2194ecf91f5 110 I2C i2cBus(D14, D15);
j3 3:f2194ecf91f5 111 i2cBus.frequency(400000);
j3 3:f2194ecf91f5 112 DS2484 owm(i2cBus);
j3 0:bd523ea97465 113
j3 0:bd523ea97465 114 OneWireMaster::CmdResult ow_result;
j3 0:bd523ea97465 115 MultidropRomIterator selector(owm);
j3 0:bd523ea97465 116
j3 0:bd523ea97465 117 //look up table for sensors, element 0 is location 1
j3 0:bd523ea97465 118 OWGridEye * ow_grid_eye_array[10];
j3 0:bd523ea97465 119 uint8_t num_sensors = 0;
j3 0:bd523ea97465 120 OWGridEye * p_sensor;
j3 0:bd523ea97465 121 OWGridEye::CmdResult sensor_result;
j3 0:bd523ea97465 122
j3 0:bd523ea97465 123 uint8_t idx;
j3 0:bd523ea97465 124 int16_t recvBuff[64];
j3 0:bd523ea97465 125 int16_t imageBuff[64];
j3 0:bd523ea97465 126 float temperatureBuff[64];
j3 0:bd523ea97465 127 float thermistor;
j3 0:bd523ea97465 128
j3 0:bd523ea97465 129 bool sensorsEnumerated = false;
j3 0:bd523ea97465 130 bool sensorSelected = false;
j3 0:bd523ea97465 131
j3 0:bd523ea97465 132 while(demoState == STATE_POR);
j3 0:bd523ea97465 133
j3 0:bd523ea97465 134 while(1)
j3 1:2ad0ae1416d2 135 {
j3 0:bd523ea97465 136 switch(demoState)
j3 0:bd523ea97465 137 {
j3 0:bd523ea97465 138 case STATE_ESTABLISH_COMMS:
j3 0:bd523ea97465 139
j3 0:bd523ea97465 140 ow_result = owm.OWInitMaster();
j3 0:bd523ea97465 141 if(ow_result == OneWireMaster::Success)
j3 0:bd523ea97465 142 {
j3 0:bd523ea97465 143 comms.printf("MBED: 1-wire Master Initialized\n");
j3 0:bd523ea97465 144 demoState = STATE_IDLE;
j3 0:bd523ea97465 145 }
j3 0:bd523ea97465 146 else
j3 0:bd523ea97465 147 {
j3 0:bd523ea97465 148 comms.printf("MBED: Failed to initialize the 1-wire Master\n");
j3 0:bd523ea97465 149 wait_ms(500);
j3 0:bd523ea97465 150 }
j3 0:bd523ea97465 151
j3 0:bd523ea97465 152 break;
j3 0:bd523ea97465 153
j3 0:bd523ea97465 154 case STATE_ENUMERATE_SENSORS:
j3 0:bd523ea97465 155
j3 0:bd523ea97465 156 if(sensorsEnumerated)
j3 0:bd523ea97465 157 {
j3 0:bd523ea97465 158 for(uint8_t reset_idx = 0; reset_idx < num_sensors; reset_idx++)
j3 0:bd523ea97465 159 {
j3 0:bd523ea97465 160 delete ow_grid_eye_array[reset_idx];
j3 0:bd523ea97465 161 ow_grid_eye_array[reset_idx] = 0;
j3 0:bd523ea97465 162 }
j3 0:bd523ea97465 163 sensorsEnumerated = false;
j3 0:bd523ea97465 164 }
j3 0:bd523ea97465 165
j3 0:bd523ea97465 166 if(!sensorsEnumerated)
j3 0:bd523ea97465 167 {
j3 0:bd523ea97465 168 //ensure default state of DS2413 PIO
j3 0:bd523ea97465 169 num_sensors = init_sensor_state(owm, comms);
j3 0:bd523ea97465 170 comms.printf("MBED: %d sensors initialized\n", num_sensors);
j3 0:bd523ea97465 171
j3 0:bd523ea97465 172 num_sensors = enumerate_sensors(comms, owm, selector, ow_grid_eye_array);
j3 0:bd523ea97465 173 comms.printf("MBED: %d sensors enumerated\n", num_sensors);
j3 0:bd523ea97465 174
j3 0:bd523ea97465 175 comms.printf("MBED: NUM_Sensors %d\n", num_sensors);
j3 0:bd523ea97465 176 }
j3 0:bd523ea97465 177
j3 0:bd523ea97465 178 demoState = STATE_IDLE;
j3 0:bd523ea97465 179
j3 0:bd523ea97465 180 break;
j3 0:bd523ea97465 181
j3 0:bd523ea97465 182 case STATE_SELECT_SENSOR:
j3 0:bd523ea97465 183
j3 0:bd523ea97465 184 if(selectedSensor > 0)
j3 0:bd523ea97465 185 {
j3 0:bd523ea97465 186 if(sensorSelected)
j3 0:bd523ea97465 187 {
j3 0:bd523ea97465 188 sensor_result = p_sensor->disconnectGridEye();
j3 0:bd523ea97465 189 if(sensor_result != OWGridEye::Success)
j3 0:bd523ea97465 190 {
j3 0:bd523ea97465 191 comms.printf("MBED: Failed to disconnect sensor\n");
j3 0:bd523ea97465 192 }
j3 0:bd523ea97465 193 else
j3 0:bd523ea97465 194 {
j3 0:bd523ea97465 195 sensorSelected = false;
j3 0:bd523ea97465 196 }
j3 0:bd523ea97465 197 }
j3 0:bd523ea97465 198
j3 0:bd523ea97465 199 if(!sensorSelected)
j3 0:bd523ea97465 200 {
j3 0:bd523ea97465 201 p_sensor = ow_grid_eye_array[selectedSensor - 1];
j3 0:bd523ea97465 202 sensor_result = p_sensor->connectGridEye();
j3 0:bd523ea97465 203 if(sensor_result == OWGridEye::Success)
j3 0:bd523ea97465 204 {
j3 0:bd523ea97465 205 //wait for sensor to init
j3 0:bd523ea97465 206 wait_ms(50);
j3 0:bd523ea97465 207 sensorSelected = true;
j3 0:bd523ea97465 208 }
j3 0:bd523ea97465 209 else
j3 0:bd523ea97465 210 {
j3 0:bd523ea97465 211 comms.printf("MBED: Failed to connect sensor\n");
j3 0:bd523ea97465 212 sensorSelected = false;
j3 0:bd523ea97465 213 }
j3 0:bd523ea97465 214 }
j3 0:bd523ea97465 215 }
j3 0:bd523ea97465 216 else
j3 0:bd523ea97465 217 {
j3 0:bd523ea97465 218 comms.printf("MBED: Invalid Sensor Index\n");
j3 0:bd523ea97465 219 }
j3 0:bd523ea97465 220
j3 0:bd523ea97465 221 demoState = STATE_IDLE;
j3 0:bd523ea97465 222
j3 0:bd523ea97465 223 break;
j3 0:bd523ea97465 224
j3 0:bd523ea97465 225 case STATE_SEND_DATA:
j3 0:bd523ea97465 226
j3 0:bd523ea97465 227 if(sensorSelected)
j3 0:bd523ea97465 228 {
j3 0:bd523ea97465 229 //get raw data from GridEye
j3 0:bd523ea97465 230 sensor_result = p_sensor->gridEyeGetFrameTemperature(recvBuff);
j3 0:bd523ea97465 231 if(sensor_result == OWGridEye::Success)
j3 0:bd523ea97465 232 {
j3 0:bd523ea97465 233 //rotate it 180 degress
j3 0:bd523ea97465 234 vAMG_PUB_IMG_ConvertRotate180(8, 8, recvBuff, imageBuff);
j3 0:bd523ea97465 235
j3 0:bd523ea97465 236 //move rotated image to recvBuff so we can flip it and put it back
j3 0:bd523ea97465 237 //in imageBuff
j3 0:bd523ea97465 238 std::memcpy(recvBuff, imageBuff, 128);
j3 0:bd523ea97465 239
j3 0:bd523ea97465 240 //flip on X axis
j3 0:bd523ea97465 241 vAMG_PUB_IMG_ConvertFlipX(8, 8, recvBuff, imageBuff);
j3 0:bd523ea97465 242
j3 0:bd523ea97465 243 //convert to float
j3 0:bd523ea97465 244 for(idx = 0; idx < 64; idx++)
j3 0:bd523ea97465 245 {
j3 0:bd523ea97465 246 temperatureBuff[idx] = fAMG_PUB_CMN_ConvStoF(imageBuff[idx]);
j3 0:bd523ea97465 247 }
j3 0:bd523ea97465 248
j3 0:bd523ea97465 249 //get thermistor data
j3 0:bd523ea97465 250 p_sensor->gridEyeGetThermistor(recvBuff[0]);
j3 0:bd523ea97465 251
j3 0:bd523ea97465 252 //convert to float
j3 0:bd523ea97465 253 thermistor = fAMG_PUB_CMN_ConvStoF(recvBuff[0]);
j3 0:bd523ea97465 254
j3 0:bd523ea97465 255 //send to GUI
j3 0:bd523ea97465 256 send_data(comms, temperatureBuff, thermistor);
j3 0:bd523ea97465 257
j3 0:bd523ea97465 258 demoState = STATE_IDLE;
j3 0:bd523ea97465 259 }
j3 0:bd523ea97465 260 else
j3 0:bd523ea97465 261 {
j3 0:bd523ea97465 262 comms.printf("MBED: Failed to Get Frame\n");
j3 0:bd523ea97465 263 }
j3 0:bd523ea97465 264 }
j3 0:bd523ea97465 265 else
j3 0:bd523ea97465 266 {
j3 0:bd523ea97465 267 comms.printf("MBED: Please Select Sensor\n");
j3 0:bd523ea97465 268 }
j3 0:bd523ea97465 269
j3 0:bd523ea97465 270 break;
j3 0:bd523ea97465 271
j3 0:bd523ea97465 272 case STATE_STOP_DATA:
j3 0:bd523ea97465 273 demoState = STATE_IDLE;
j3 0:bd523ea97465 274 break;
j3 0:bd523ea97465 275
j3 0:bd523ea97465 276 case STATE_IDLE:
j3 0:bd523ea97465 277 //do nothing
j3 0:bd523ea97465 278 break;
j3 0:bd523ea97465 279
j3 0:bd523ea97465 280 default:
j3 0:bd523ea97465 281 mbed_die();
j3 0:bd523ea97465 282 break;
j3 0:bd523ea97465 283 }
j3 0:bd523ea97465 284 }
j3 0:bd523ea97465 285 }