mbed code for MAXREFDES131 Qt GUI demo

Dependencies:   OWGridEye OneWire mbed

Committer:
j3
Date:
Mon Aug 08 01:52:29 2016 +0000
Revision:
1:2ad0ae1416d2
Parent:
0:bd523ea97465
Child:
3:f2194ecf91f5
Set DS2484 as default OW master for this project

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 0:bd523ea97465 110 #if defined(I2C_TO_OW_MASTER)
j3 0:bd523ea97465 111 //Instantiates a 1-wire master using the given I2C bus SDA/SCL pins
j3 0:bd523ea97465 112 //Requires a DS248X series of masters on I2C bus
j3 0:bd523ea97465 113 //https://para.maximintegrated.com/en/results.mvp?fam=1wire&tree=master
j3 0:bd523ea97465 114
j3 0:bd523ea97465 115 I2C i2cBus(D14, D15);
j3 0:bd523ea97465 116 i2cBus.frequency(400000);
j3 0:bd523ea97465 117 DS2484 owm(i2cBus);
j3 0:bd523ea97465 118
j3 0:bd523ea97465 119 #elif defined(SERIAL_TO_OW_MASTER)
j3 0:bd523ea97465 120 //Instantiates a 1-wire master using the given UART tx/rx pins
j3 0:bd523ea97465 121 //This master hasn't been implemented yet 17MAR16, happy St. Patricks day.
j3 0:bd523ea97465 122 //Requires a DS2480B serial to 1-wire master
j3 0:bd523ea97465 123 //https://www.maximintegrated.com/en/products/interface/controllers-expanders/DS2480B.html
j3 0:bd523ea97465 124
j3 0:bd523ea97465 125 Serial owUart(D1, D0);
j3 0:bd523ea97465 126 DS2480B owm(owUart);
j3 0:bd523ea97465 127
j3 0:bd523ea97465 128 #elif defined(BIT_BANG_MASTER)
j3 0:bd523ea97465 129 //Instantiates a 1-wire master on the given GPIO pin
j3 0:bd523ea97465 130 //Requires MAX32600MBED platform
j3 0:bd523ea97465 131 //https://www.maximintegrated.com/en/products/digital/microcontrollers/MAX32600MBED.html
j3 0:bd523ea97465 132
j3 0:bd523ea97465 133 OwGpio owm(D2);
j3 0:bd523ea97465 134
j3 0:bd523ea97465 135 #else
j3 0:bd523ea97465 136 #error One Wire Master Not Defined
j3 0:bd523ea97465 137 #endif
j3 0:bd523ea97465 138
j3 0:bd523ea97465 139 OneWireMaster::CmdResult ow_result;
j3 0:bd523ea97465 140 MultidropRomIterator selector(owm);
j3 0:bd523ea97465 141
j3 0:bd523ea97465 142 //look up table for sensors, element 0 is location 1
j3 0:bd523ea97465 143 OWGridEye * ow_grid_eye_array[10];
j3 0:bd523ea97465 144 uint8_t num_sensors = 0;
j3 0:bd523ea97465 145 OWGridEye * p_sensor;
j3 0:bd523ea97465 146 OWGridEye::CmdResult sensor_result;
j3 0:bd523ea97465 147
j3 0:bd523ea97465 148 uint8_t idx;
j3 0:bd523ea97465 149 int16_t recvBuff[64];
j3 0:bd523ea97465 150 int16_t imageBuff[64];
j3 0:bd523ea97465 151 float temperatureBuff[64];
j3 0:bd523ea97465 152 float thermistor;
j3 0:bd523ea97465 153
j3 0:bd523ea97465 154 bool sensorsEnumerated = false;
j3 0:bd523ea97465 155 bool sensorSelected = false;
j3 0:bd523ea97465 156
j3 0:bd523ea97465 157 while(demoState == STATE_POR);
j3 0:bd523ea97465 158
j3 0:bd523ea97465 159 while(1)
j3 1:2ad0ae1416d2 160 {
j3 0:bd523ea97465 161 switch(demoState)
j3 0:bd523ea97465 162 {
j3 0:bd523ea97465 163 case STATE_ESTABLISH_COMMS:
j3 0:bd523ea97465 164
j3 0:bd523ea97465 165 ow_result = owm.OWInitMaster();
j3 0:bd523ea97465 166 if(ow_result == OneWireMaster::Success)
j3 0:bd523ea97465 167 {
j3 0:bd523ea97465 168 comms.printf("MBED: 1-wire Master Initialized\n");
j3 0:bd523ea97465 169 demoState = STATE_IDLE;
j3 0:bd523ea97465 170 }
j3 0:bd523ea97465 171 else
j3 0:bd523ea97465 172 {
j3 0:bd523ea97465 173 comms.printf("MBED: Failed to initialize the 1-wire Master\n");
j3 0:bd523ea97465 174 wait_ms(500);
j3 0:bd523ea97465 175 }
j3 0:bd523ea97465 176
j3 0:bd523ea97465 177 break;
j3 0:bd523ea97465 178
j3 0:bd523ea97465 179 case STATE_ENUMERATE_SENSORS:
j3 0:bd523ea97465 180
j3 0:bd523ea97465 181 if(sensorsEnumerated)
j3 0:bd523ea97465 182 {
j3 0:bd523ea97465 183 for(uint8_t reset_idx = 0; reset_idx < num_sensors; reset_idx++)
j3 0:bd523ea97465 184 {
j3 0:bd523ea97465 185 delete ow_grid_eye_array[reset_idx];
j3 0:bd523ea97465 186 ow_grid_eye_array[reset_idx] = 0;
j3 0:bd523ea97465 187 }
j3 0:bd523ea97465 188 sensorsEnumerated = false;
j3 0:bd523ea97465 189 }
j3 0:bd523ea97465 190
j3 0:bd523ea97465 191 if(!sensorsEnumerated)
j3 0:bd523ea97465 192 {
j3 0:bd523ea97465 193 //ensure default state of DS2413 PIO
j3 0:bd523ea97465 194 num_sensors = init_sensor_state(owm, comms);
j3 0:bd523ea97465 195 comms.printf("MBED: %d sensors initialized\n", num_sensors);
j3 0:bd523ea97465 196
j3 0:bd523ea97465 197 num_sensors = enumerate_sensors(comms, owm, selector, ow_grid_eye_array);
j3 0:bd523ea97465 198 comms.printf("MBED: %d sensors enumerated\n", num_sensors);
j3 0:bd523ea97465 199
j3 0:bd523ea97465 200 comms.printf("MBED: NUM_Sensors %d\n", num_sensors);
j3 0:bd523ea97465 201 }
j3 0:bd523ea97465 202
j3 0:bd523ea97465 203 demoState = STATE_IDLE;
j3 0:bd523ea97465 204
j3 0:bd523ea97465 205 break;
j3 0:bd523ea97465 206
j3 0:bd523ea97465 207 case STATE_SELECT_SENSOR:
j3 0:bd523ea97465 208
j3 0:bd523ea97465 209 if(selectedSensor > 0)
j3 0:bd523ea97465 210 {
j3 0:bd523ea97465 211 if(sensorSelected)
j3 0:bd523ea97465 212 {
j3 0:bd523ea97465 213 sensor_result = p_sensor->disconnectGridEye();
j3 0:bd523ea97465 214 if(sensor_result != OWGridEye::Success)
j3 0:bd523ea97465 215 {
j3 0:bd523ea97465 216 comms.printf("MBED: Failed to disconnect sensor\n");
j3 0:bd523ea97465 217 }
j3 0:bd523ea97465 218 else
j3 0:bd523ea97465 219 {
j3 0:bd523ea97465 220 sensorSelected = false;
j3 0:bd523ea97465 221 }
j3 0:bd523ea97465 222 }
j3 0:bd523ea97465 223
j3 0:bd523ea97465 224 if(!sensorSelected)
j3 0:bd523ea97465 225 {
j3 0:bd523ea97465 226 p_sensor = ow_grid_eye_array[selectedSensor - 1];
j3 0:bd523ea97465 227 sensor_result = p_sensor->connectGridEye();
j3 0:bd523ea97465 228 if(sensor_result == OWGridEye::Success)
j3 0:bd523ea97465 229 {
j3 0:bd523ea97465 230 //wait for sensor to init
j3 0:bd523ea97465 231 wait_ms(50);
j3 0:bd523ea97465 232 sensorSelected = true;
j3 0:bd523ea97465 233 }
j3 0:bd523ea97465 234 else
j3 0:bd523ea97465 235 {
j3 0:bd523ea97465 236 comms.printf("MBED: Failed to connect sensor\n");
j3 0:bd523ea97465 237 sensorSelected = false;
j3 0:bd523ea97465 238 }
j3 0:bd523ea97465 239 }
j3 0:bd523ea97465 240 }
j3 0:bd523ea97465 241 else
j3 0:bd523ea97465 242 {
j3 0:bd523ea97465 243 comms.printf("MBED: Invalid Sensor Index\n");
j3 0:bd523ea97465 244 }
j3 0:bd523ea97465 245
j3 0:bd523ea97465 246 demoState = STATE_IDLE;
j3 0:bd523ea97465 247
j3 0:bd523ea97465 248 break;
j3 0:bd523ea97465 249
j3 0:bd523ea97465 250 case STATE_SEND_DATA:
j3 0:bd523ea97465 251
j3 0:bd523ea97465 252 if(sensorSelected)
j3 0:bd523ea97465 253 {
j3 0:bd523ea97465 254 //get raw data from GridEye
j3 0:bd523ea97465 255 sensor_result = p_sensor->gridEyeGetFrameTemperature(recvBuff);
j3 0:bd523ea97465 256 if(sensor_result == OWGridEye::Success)
j3 0:bd523ea97465 257 {
j3 0:bd523ea97465 258 //rotate it 180 degress
j3 0:bd523ea97465 259 vAMG_PUB_IMG_ConvertRotate180(8, 8, recvBuff, imageBuff);
j3 0:bd523ea97465 260
j3 0:bd523ea97465 261 //move rotated image to recvBuff so we can flip it and put it back
j3 0:bd523ea97465 262 //in imageBuff
j3 0:bd523ea97465 263 std::memcpy(recvBuff, imageBuff, 128);
j3 0:bd523ea97465 264
j3 0:bd523ea97465 265 //flip on X axis
j3 0:bd523ea97465 266 vAMG_PUB_IMG_ConvertFlipX(8, 8, recvBuff, imageBuff);
j3 0:bd523ea97465 267
j3 0:bd523ea97465 268 //convert to float
j3 0:bd523ea97465 269 for(idx = 0; idx < 64; idx++)
j3 0:bd523ea97465 270 {
j3 0:bd523ea97465 271 temperatureBuff[idx] = fAMG_PUB_CMN_ConvStoF(imageBuff[idx]);
j3 0:bd523ea97465 272 }
j3 0:bd523ea97465 273
j3 0:bd523ea97465 274 //get thermistor data
j3 0:bd523ea97465 275 p_sensor->gridEyeGetThermistor(recvBuff[0]);
j3 0:bd523ea97465 276
j3 0:bd523ea97465 277 //convert to float
j3 0:bd523ea97465 278 thermistor = fAMG_PUB_CMN_ConvStoF(recvBuff[0]);
j3 0:bd523ea97465 279
j3 0:bd523ea97465 280 //send to GUI
j3 0:bd523ea97465 281 send_data(comms, temperatureBuff, thermistor);
j3 0:bd523ea97465 282
j3 0:bd523ea97465 283 demoState = STATE_IDLE;
j3 0:bd523ea97465 284 }
j3 0:bd523ea97465 285 else
j3 0:bd523ea97465 286 {
j3 0:bd523ea97465 287 comms.printf("MBED: Failed to Get Frame\n");
j3 0:bd523ea97465 288 }
j3 0:bd523ea97465 289 }
j3 0:bd523ea97465 290 else
j3 0:bd523ea97465 291 {
j3 0:bd523ea97465 292 comms.printf("MBED: Please Select Sensor\n");
j3 0:bd523ea97465 293 }
j3 0:bd523ea97465 294
j3 0:bd523ea97465 295 break;
j3 0:bd523ea97465 296
j3 0:bd523ea97465 297 case STATE_STOP_DATA:
j3 0:bd523ea97465 298 demoState = STATE_IDLE;
j3 0:bd523ea97465 299 break;
j3 0:bd523ea97465 300
j3 0:bd523ea97465 301 case STATE_IDLE:
j3 0:bd523ea97465 302 //do nothing
j3 0:bd523ea97465 303 break;
j3 0:bd523ea97465 304
j3 0:bd523ea97465 305 default:
j3 0:bd523ea97465 306 mbed_die();
j3 0:bd523ea97465 307 break;
j3 0:bd523ea97465 308 }
j3 0:bd523ea97465 309 }
j3 0:bd523ea97465 310 }