├── README.md ├── examples └── MLX90393_Debug_Helper │ ├── TODO │ ├── LICENSE │ └── MLX90393_Debug_Helper.ino ├── LICENSE ├── MLX90393.h └── MLX90393.cpp /README.md: -------------------------------------------------------------------------------- 1 | # arduino-MLX90393 2 | Arduino library for MLX90393 magnetometer sensor 3 | -------------------------------------------------------------------------------- /examples/MLX90393_Debug_Helper/TODO: -------------------------------------------------------------------------------- 1 | Known issues: 2 | - If the MLX90393 is in burst mode then reading often fails. 3 | Please do NOT report this issue unless you can help me with 4 | insights why this happens. Even better: provide a pull request 5 | with a fix. 6 | 7 | - Register access to MLX90393 does not work while in burst mode. 8 | This is how the chip operates and not a bug of the debug helper. 9 | From the point of view of the debug helper this is a feature. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016-2018 Theodore C. Yapo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, 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, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MLX90393.h: -------------------------------------------------------------------------------- 1 | // 2 | // MLX90393.cpp : arduino driver for MLX90393 magnetometer 3 | // 4 | // Copyright 2016 Theodore C. Yapo 5 | // 6 | // released under MIT License (see file) 7 | // 8 | 9 | #ifndef MLX90393_H_INCLUDED 10 | #define MLX90393_H_INCLUDED 11 | 12 | #include 13 | #include 14 | 15 | class MLX90393 16 | { 17 | public: 18 | enum { STATUS_OK = 0, STATUS_ERROR = 0xff } return_status_t; 19 | enum { Z_FLAG = 0x8, Y_FLAG = 0x4, X_FLAG = 0x2, T_FLAG = 0x1 } axis_flag_t; 20 | enum { I2C_BASE_ADDR = 0x0c }; 21 | enum { GAIN_SEL_REG = 0x0, GAIN_SEL_MASK = 0x0070, GAIN_SEL_SHIFT = 4 }; 22 | enum { HALLCONF_REG = 0x0, HALLCONF_MASK = 0x000f, HALLCONF_SHIFT = 0 }; 23 | enum { BURST_SEL_REG = 0x1, BURST_SEL_MASK = 0x03c0, BURST_SEL_SHIFT = 6}; 24 | enum { TRIG_INT_SEL_REG = 0x1, TRIG_INT_SEL_MASK = 0x8000, TRIG_INT_SEL_SHIFT = 15 }; 25 | enum { EXT_TRIG_REG = 0x1, EXT_TRIG_MASK = 0x0800, EXT_TRIG_SHIFT = 11 }; 26 | enum { OSR_REG = 0x2, OSR_MASK = 0x0003, OSR_SHIFT = 0 }; 27 | enum { OSR2_REG = 0x2, OSR2_MASK = 0x1800, OSR2_SHIFT = 11 }; 28 | enum { DIG_FLT_REG = 0x2, DIG_FLT_MASK = 0x001c, DIG_FLT_SHIFT = 2 }; 29 | enum { RES_XYZ_REG = 0x2, RES_XYZ_MASK = 0x07e0, RES_XYZ_SHIFT = 5 }; 30 | enum { TCMP_EN_REG = 0x1, TCMP_EN_MASK = 0x0400, TCMP_EN_SHIFT = 10 }; 31 | enum { X_OFFSET_REG = 4, Y_OFFSET_REG = 5, Z_OFFSET_REG = 6 }; 32 | enum { WOXY_THRESHOLD_REG = 7, WOZ_THRESHOLD_REG = 8, WOT_THRESHOLD_REG = 9 }; 33 | enum { BURST_MODE_BIT = 0x80, WAKE_ON_CHANGE_BIT = 0x40, 34 | POLLING_MODE_BIT = 0x20, ERROR_BIT = 0x10, EEC_BIT = 0x08, 35 | RESET_BIT = 0x04, D1_BIT = 0x02, D0_BIT = 0x01 }; 36 | 37 | enum { 38 | CMD_NOP = 0x00, 39 | CMD_EXIT = 0x80, 40 | CMD_START_BURST = 0x10, 41 | CMD_WAKE_ON_CHANGE = 0x20, 42 | CMD_START_MEASUREMENT = 0x30, 43 | CMD_READ_MEASUREMENT = 0x40, 44 | CMD_READ_REGISTER = 0x50, 45 | CMD_WRITE_REGISTER = 0x60, 46 | CMD_MEMORY_RECALL = 0xd0, 47 | CMD_MEMORY_STORE = 0xe0, 48 | CMD_RESET = 0xf0 49 | }; 50 | 51 | struct txyz 52 | { 53 | float t; 54 | float x; 55 | float y; 56 | float z; 57 | }; 58 | struct txyzRaw 59 | { 60 | uint16_t t; 61 | uint16_t x; 62 | uint16_t y; 63 | uint16_t z; 64 | }; 65 | MLX90393(); 66 | 67 | // raw device commands 68 | uint8_t exit(); 69 | uint8_t startBurst(uint8_t zyxt_flags); 70 | uint8_t startWakeOnChange(uint8_t zyxt_flags); 71 | uint8_t startMeasurement(uint8_t zyxt_flags); 72 | uint8_t readMeasurement(uint8_t zyxt_flags, txyzRaw& txyz_result); 73 | uint8_t readRegister(uint8_t address, uint16_t& data); 74 | uint8_t writeRegister(uint8_t address, uint16_t data); 75 | uint8_t reset(); 76 | uint8_t memoryRecall(); 77 | uint8_t memoryStore(); 78 | uint8_t nop(); 79 | uint8_t sendCommand(uint8_t cmd); 80 | uint8_t checkStatus(uint8_t status); 81 | bool isOK(uint8_t status); 82 | bool hasError(uint8_t status); 83 | txyz convertRaw(txyzRaw raw); 84 | uint16_t convDelayMillis(); 85 | 86 | // higher-level API 87 | uint8_t begin(uint8_t A1 = 0, uint8_t A0 = 0, int DRDY_pin = -1, TwoWire &wirePort = Wire); 88 | 89 | // returns B (x,y,z) in uT, temperature in C 90 | uint8_t readData(txyz& data); 91 | uint8_t setGainSel(uint8_t gain_sel); 92 | uint8_t getGainSel(uint8_t& gain_sel); 93 | uint8_t setHallConf(uint8_t hallconf); 94 | uint8_t getHallConf(uint8_t& hallconf); 95 | uint8_t setBurstSel(uint8_t burst_sel); 96 | uint8_t getBurstSel(uint8_t& burst_sel); 97 | uint8_t setExtTrig(int8_t ext_trig); 98 | uint8_t getExtTrig(uint8_t& ext_trig); 99 | uint8_t setTrigIntSel(uint8_t trig_int_sel); 100 | uint8_t getTrigIntSel(uint8_t& trig_int_sel); 101 | uint8_t setOverSampling(uint8_t osr); 102 | uint8_t getOverSampling(uint8_t& osr); 103 | uint8_t setTemperatureOverSampling(uint8_t osr2); 104 | uint8_t getTemperatureOverSampling(uint8_t& osr2); 105 | uint8_t setDigitalFiltering(uint8_t dig_flt); 106 | uint8_t getDigitalFiltering(uint8_t& dig_flt); 107 | uint8_t setResolution(uint8_t res_x, uint8_t res_y, uint8_t res_z); 108 | uint8_t getResolution(uint8_t& res_x, uint8_t& res_y, uint8_t& res_z); 109 | uint8_t setTemperatureCompensation(uint8_t enabled); 110 | uint8_t getTemperatureCompensation(uint8_t& enabled); 111 | uint8_t setOffsets(uint16_t x, uint16_t y, uint16_t z); 112 | uint8_t setWOXYThreshold(uint16_t woxy_thresh); 113 | uint8_t setWOZThreshold(uint16_t woz_thresh); 114 | uint8_t setWOTThreshold(uint16_t wot_thresh); 115 | 116 | private: 117 | uint8_t I2C_address; 118 | int DRDY_pin; 119 | 120 | // parameters are cached to avoid reading them from sensor unnecessarily 121 | struct cache_t { 122 | enum { SIZE = 3, ALL_DIRTY_MASK = 1 << (SIZE + 1) - 1}; 123 | uint8_t dirty; 124 | uint16_t reg[SIZE]; 125 | } cache; 126 | void cache_invalidate(); 127 | void cache_invalidate(uint8_t address); 128 | void cache_set(uint8_t address, uint16_t data); 129 | uint8_t cache_fill(); 130 | 131 | 132 | float gain_multipliers[8]; 133 | float base_xy_sens_hc0; 134 | float base_z_sens_hc0; 135 | float base_xy_sens_hc0xc; 136 | float base_z_sens_hc0xc; 137 | 138 | private: 139 | TwoWire *_i2cPort; //The generic connection to user's chosen I2C hardware 140 | 141 | }; 142 | #endif // #ifndef MLX90393_H_INCLUDED 143 | -------------------------------------------------------------------------------- /MLX90393.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // MLX90393.cpp : arduino driver for MLX90393 magnetometer 3 | // 4 | // Copyright 2016 Theodore C. Yapo 5 | // 6 | // released under MIT License (see file) 7 | // 8 | 9 | #include 10 | 11 | MLX90393:: 12 | MLX90393() 13 | { 14 | I2C_address = 0; 15 | 16 | cache_invalidate(); 17 | 18 | // gain steps derived from datasheet section 15.1.4 tables 19 | gain_multipliers[0] = 5.f; 20 | gain_multipliers[1] = 4.f; 21 | gain_multipliers[2] = 3.f; 22 | gain_multipliers[3] = 2.5f; 23 | gain_multipliers[4] = 2.f; 24 | gain_multipliers[5] = 1.66666667f; 25 | gain_multipliers[6] = 1.33333333f; 26 | gain_multipliers[7] = 1.f; 27 | 28 | // from datasheet 29 | // for hallconf = 0 30 | base_xy_sens_hc0 = 0.196f; 31 | base_z_sens_hc0 = 0.316f; 32 | // for hallconf = 0xc 33 | base_xy_sens_hc0xc = 0.150f; 34 | base_z_sens_hc0xc = 0.242f; 35 | } 36 | 37 | uint8_t 38 | MLX90393:: 39 | begin(uint8_t A1, uint8_t A0, int DRDY_pin, TwoWire &wirePort) 40 | { 41 | I2C_address = I2C_BASE_ADDR | (A1?2:0) | (A0?1:0); 42 | this->DRDY_pin = DRDY_pin; 43 | if (DRDY_pin >= 0){ 44 | pinMode(DRDY_pin, INPUT); 45 | } 46 | 47 | _i2cPort = &wirePort; //Grab which port the user wants us to use 48 | 49 | uint8_t status1 = checkStatus(reset()); 50 | uint8_t status2 = setGainSel(7); 51 | uint8_t status3 = setResolution(0, 0, 0); 52 | uint8_t status4 = setOverSampling(3); 53 | uint8_t status5 = setDigitalFiltering(7); 54 | uint8_t status6 = setTemperatureCompensation(0); 55 | 56 | return status1 | status2 | status3 | status4 | status5 | status6; 57 | } 58 | 59 | void 60 | MLX90393:: 61 | cache_invalidate() 62 | { 63 | cache.dirty = cache_t::ALL_DIRTY_MASK; 64 | } 65 | 66 | void 67 | MLX90393:: 68 | cache_invalidate(uint8_t address) 69 | { 70 | cache.dirty |= cache_t::ALL_DIRTY_MASK & (1<readRegister(address, cache.reg[address]))) { 89 | return STATUS_ERROR; 90 | } 91 | } 92 | } 93 | } 94 | return STATUS_OK; 95 | } 96 | 97 | uint8_t 98 | MLX90393:: 99 | checkStatus(uint8_t status) 100 | { 101 | return (status & ERROR_BIT) ? STATUS_ERROR : STATUS_OK; 102 | } 103 | 104 | bool 105 | MLX90393:: 106 | isOK(uint8_t status) 107 | { 108 | return (status & ERROR_BIT) == 0; 109 | } 110 | 111 | bool 112 | MLX90393:: 113 | hasError(uint8_t status) 114 | { 115 | return (status & ERROR_BIT) != 0; 116 | } 117 | 118 | 119 | uint8_t 120 | MLX90393:: 121 | sendCommand(uint8_t cmd) 122 | { 123 | _i2cPort->beginTransmission(I2C_address); 124 | if (_i2cPort->write(cmd) != 1){ return STATUS_ERROR; } 125 | if (_i2cPort->endTransmission()){ return STATUS_ERROR; } 126 | if (_i2cPort->requestFrom(I2C_address, uint8_t(1)) != 1){ return STATUS_ERROR; } 127 | 128 | return _i2cPort->read(); 129 | } 130 | 131 | uint8_t 132 | MLX90393:: 133 | nop() 134 | { 135 | return sendCommand(CMD_NOP); 136 | } 137 | 138 | uint8_t 139 | MLX90393:: 140 | exit() 141 | { 142 | return sendCommand(CMD_EXIT); 143 | } 144 | 145 | uint8_t 146 | MLX90393:: 147 | startBurst(uint8_t zyxt_flags) 148 | { 149 | cache_fill(); 150 | uint8_t cmd = CMD_START_BURST | (zyxt_flags & 0xf); 151 | return sendCommand(cmd); 152 | } 153 | 154 | uint8_t 155 | MLX90393:: 156 | startWakeOnChange(uint8_t zyxt_flags) 157 | { 158 | cache_fill(); 159 | uint8_t cmd = CMD_WAKE_ON_CHANGE | (zyxt_flags & 0xf); 160 | return sendCommand(cmd); 161 | } 162 | 163 | uint8_t 164 | MLX90393:: 165 | startMeasurement(uint8_t zyxt_flags) 166 | { 167 | cache_fill(); 168 | uint8_t cmd = CMD_START_MEASUREMENT | (zyxt_flags & 0xf); 169 | return sendCommand(cmd); 170 | } 171 | 172 | uint8_t 173 | MLX90393:: 174 | readMeasurement(uint8_t zyxt_flags, txyzRaw& txyz_result) 175 | { 176 | uint8_t cmd = CMD_READ_MEASUREMENT | (zyxt_flags & 0xf); 177 | _i2cPort->beginTransmission(I2C_address); 178 | if(_i2cPort->write(cmd) != 1){ 179 | return STATUS_ERROR; 180 | } 181 | if (_i2cPort->endTransmission()){ 182 | return STATUS_ERROR; 183 | } 184 | 185 | uint8_t buffer[9]; 186 | uint8_t count = 1 + (((zyxt_flags & Z_FLAG)?2:0) + 187 | ((zyxt_flags & Y_FLAG)?2:0) + 188 | ((zyxt_flags & X_FLAG)?2:0) + 189 | ((zyxt_flags & T_FLAG)?2:0) ); 190 | 191 | if(_i2cPort->requestFrom(I2C_address, count) != count){ 192 | return STATUS_ERROR; 193 | } 194 | for (uint8_t i=0; i < count; i++){ 195 | if (_i2cPort->available()){ 196 | buffer[i] = _i2cPort->read(); 197 | } else { 198 | return STATUS_ERROR; 199 | } 200 | } 201 | 202 | uint8_t i = 1; 203 | if (zyxt_flags & T_FLAG){ 204 | txyz_result.t = (uint16_t(buffer[i])<<8) | buffer[i+1]; 205 | i += 2; 206 | } else { 207 | txyz_result.t = 0; 208 | } 209 | if (zyxt_flags & X_FLAG){ 210 | txyz_result.x = (uint16_t(buffer[i])<<8) | buffer[i+1]; 211 | i += 2; 212 | } else { 213 | txyz_result.x = 0; 214 | } 215 | if (zyxt_flags & Y_FLAG){ 216 | txyz_result.y = (uint16_t(buffer[i])<<8) | buffer[i+1]; 217 | i += 2; 218 | } else { 219 | txyz_result.y = 0; 220 | } 221 | if (zyxt_flags & Z_FLAG){ 222 | txyz_result.z = (uint16_t(buffer[i])<<8) | buffer[i+1]; 223 | i += 2; 224 | } else { 225 | txyz_result.z = 0; 226 | } 227 | 228 | return buffer[0]; 229 | } 230 | 231 | uint8_t 232 | MLX90393:: 233 | readRegister(uint8_t address, uint16_t& data) 234 | { 235 | _i2cPort->beginTransmission(I2C_address); 236 | if (_i2cPort->write(CMD_READ_REGISTER) != 1){ return STATUS_ERROR; } 237 | if (_i2cPort->write((address & 0x3f)<<2) != 1){ return STATUS_ERROR; } 238 | if (_i2cPort->endTransmission()){ return STATUS_ERROR; } 239 | if (_i2cPort->requestFrom(I2C_address, uint8_t(3)) != 3){ return STATUS_ERROR; } 240 | 241 | uint8_t status; 242 | if (!_i2cPort->available()){ return STATUS_ERROR; } 243 | status = _i2cPort->read(); 244 | 245 | uint8_t b_h; 246 | if (!_i2cPort->available()){ return STATUS_ERROR; } 247 | b_h = _i2cPort->read(); 248 | 249 | uint8_t b_l; 250 | if (!_i2cPort->available()){ return STATUS_ERROR; } 251 | b_l = _i2cPort->read(); 252 | 253 | data = (uint16_t(b_h)<<8) | b_l; 254 | cache_set(address, data); 255 | return status; 256 | } 257 | 258 | uint8_t 259 | MLX90393:: 260 | writeRegister(uint8_t address, uint16_t data) 261 | { 262 | cache_invalidate(address); 263 | 264 | _i2cPort->beginTransmission(I2C_address); 265 | if (_i2cPort->write(CMD_WRITE_REGISTER) != 1){ return STATUS_ERROR; } 266 | if (_i2cPort->write((data & 0xff00) >> 8) != 1){ return STATUS_ERROR; } 267 | if (_i2cPort->write(data & 0x00ff) != 1){ return STATUS_ERROR; } 268 | if (_i2cPort->write((address & 0x3f)<<2) != 1){ return STATUS_ERROR; } 269 | if (_i2cPort->endTransmission()){ return STATUS_ERROR; } 270 | if (_i2cPort->requestFrom(I2C_address, uint8_t(1)) != 1){ return STATUS_ERROR; } 271 | if (!_i2cPort->available()){ return STATUS_ERROR; } 272 | 273 | const uint8_t status = _i2cPort->read(); 274 | if (isOK(status)) { 275 | cache_set(address, data); 276 | } 277 | return status; 278 | } 279 | 280 | uint8_t 281 | MLX90393:: 282 | reset() 283 | { 284 | cache_invalidate(); 285 | 286 | uint8_t status = sendCommand(CMD_RESET); 287 | //Device now resets. We must give it time to complete 288 | delay(2); 289 | // POR is 1.6ms max. Software reset time limit is not specified. 290 | // 2ms was found to be good. 291 | 292 | return status; 293 | } 294 | 295 | uint8_t 296 | MLX90393:: 297 | memoryRecall() 298 | { 299 | cache_invalidate(); 300 | return sendCommand(CMD_MEMORY_RECALL); 301 | } 302 | 303 | uint8_t 304 | MLX90393:: 305 | memoryStore() 306 | { 307 | return sendCommand(CMD_MEMORY_STORE); 308 | } 309 | 310 | MLX90393::txyz 311 | MLX90393:: 312 | convertRaw(MLX90393::txyzRaw raw) 313 | { 314 | const uint8_t gain_sel = (cache.reg[GAIN_SEL_REG] & GAIN_SEL_MASK) >> GAIN_SEL_SHIFT; 315 | const uint8_t hallconf = (cache.reg[HALLCONF_REG] & HALLCONF_MASK) >> HALLCONF_SHIFT; 316 | const uint8_t res_xyz = (cache.reg[RES_XYZ_REG] & RES_XYZ_MASK) >> RES_XYZ_SHIFT; 317 | const uint8_t res_x = (res_xyz >> 0) & 0x3; 318 | const uint8_t res_y = (res_xyz >> 2) & 0x3; 319 | const uint8_t res_z = (res_xyz >> 4) & 0x3; 320 | uint8_t tcmp_en = (cache.reg[TCMP_EN_REG] & TCMP_EN_MASK) >> TCMP_EN_SHIFT; 321 | 322 | txyz data; 323 | float xy_sens; 324 | float z_sens; 325 | 326 | switch(hallconf){ 327 | default: 328 | case 0: 329 | xy_sens = base_xy_sens_hc0; 330 | z_sens = base_z_sens_hc0; 331 | break; 332 | case 0xc: 333 | xy_sens = base_xy_sens_hc0xc; 334 | z_sens = base_z_sens_hc0xc; 335 | break; 336 | } 337 | 338 | float gain_factor = gain_multipliers[gain_sel & 0x7]; 339 | 340 | if (tcmp_en){ 341 | data.x = ( (raw.x - 32768.f) * xy_sens * 342 | gain_factor * (1 << res_x) ); 343 | } else { 344 | switch(res_x){ 345 | case 0: 346 | case 1: 347 | data.x = int16_t(raw.x) * xy_sens * gain_factor * (1 << res_x); 348 | break; 349 | case 2: 350 | data.x = ( (raw.x - 32768.f) * xy_sens * 351 | gain_factor * (1 << res_x) ); 352 | break; 353 | case 3: 354 | data.x = ( (raw.x - 16384.f) * xy_sens * 355 | gain_factor * (1 << res_x) ); 356 | break; 357 | } 358 | } 359 | 360 | if (tcmp_en){ 361 | data.y = ( (raw.y - 32768.f) * xy_sens * 362 | gain_factor * (1 << res_y) ); 363 | } else { 364 | switch(res_y){ 365 | case 0: 366 | case 1: 367 | data.y = int16_t(raw.y) * xy_sens * gain_factor * (1 << res_y); 368 | break; 369 | case 2: 370 | data.y = ( (raw.y - 32768.f) * xy_sens * 371 | gain_factor * (1 << res_y) ); 372 | break; 373 | case 3: 374 | data.y = ( (raw.y - 16384.f) * xy_sens * 375 | gain_factor * (1 << res_y) ); 376 | break; 377 | } 378 | } 379 | 380 | if (tcmp_en){ 381 | data.z = ( (raw.z - 32768.f) * z_sens * 382 | gain_factor * (1 << res_z) ); 383 | } else { 384 | switch(res_z){ 385 | case 0: 386 | case 1: 387 | data.z = int16_t(raw.z) * z_sens * gain_factor * (1 << res_z); 388 | break; 389 | case 2: 390 | data.z = ( (raw.z - 32768.f) * z_sens * 391 | gain_factor * (1 << res_z) ); 392 | break; 393 | case 3: 394 | data.z = ( (raw.z - 16384.f) * z_sens * 395 | gain_factor * (1 << res_z) ); 396 | break; 397 | } 398 | } 399 | 400 | data.t = 25 + (raw.t - 46244.f)/45.2f; 401 | return data; 402 | } 403 | 404 | uint16_t 405 | MLX90393:: 406 | convDelayMillis() { 407 | const uint8_t osr = (cache.reg[OSR_REG] & OSR_MASK) >> OSR_SHIFT; 408 | const uint8_t osr2 = (cache.reg[OSR2_REG] & OSR2_MASK) >> OSR2_SHIFT; 409 | const uint8_t dig_flt = (cache.reg[DIG_FLT_REG] & DIG_FLT_MASK) >> DIG_FLT_SHIFT; 410 | 411 | return 412 | (DRDY_pin >= 0)? 0 /* no delay if drdy pin present */ : 413 | // estimate conversion time from datasheet equations 414 | ( 3 * (2 + (1 << dig_flt)) * (1 << osr) *0.064f + 415 | (1 << osr2) * 0.192f ) * 416 | 1.3f; // 30% tolerance 417 | } 418 | 419 | uint8_t 420 | MLX90393:: 421 | readData(MLX90393::txyz& data) 422 | { 423 | uint8_t status1 = startMeasurement(X_FLAG | Y_FLAG | Z_FLAG | T_FLAG); 424 | 425 | // wait for DRDY signal if connected, otherwise delay appropriately 426 | if (DRDY_pin >= 0){ 427 | delayMicroseconds(600); 428 | while(!digitalRead(DRDY_pin)){ 429 | // busy wait 430 | } 431 | } else { 432 | delay(this->convDelayMillis()); 433 | } 434 | 435 | txyzRaw raw_txyz; 436 | uint8_t status2 = 437 | readMeasurement(X_FLAG | Y_FLAG | Z_FLAG | T_FLAG, raw_txyz); 438 | data = convertRaw(raw_txyz); 439 | return checkStatus(status1) | checkStatus(status2); 440 | } 441 | 442 | uint8_t 443 | MLX90393:: 444 | setGainSel(uint8_t gain_sel) 445 | { 446 | uint16_t old_val; 447 | uint8_t status1 = readRegister(GAIN_SEL_REG, old_val); 448 | 449 | uint8_t status2 = writeRegister(GAIN_SEL_REG, 450 | (old_val & ~GAIN_SEL_MASK) | 451 | ((uint16_t(gain_sel) << GAIN_SEL_SHIFT) & 452 | GAIN_SEL_MASK)); 453 | return checkStatus(status1) | checkStatus(status2); 454 | } 455 | 456 | uint8_t 457 | MLX90393:: 458 | getGainSel(uint8_t& gain_sel) 459 | { 460 | uint16_t reg_val; 461 | uint8_t status = readRegister(GAIN_SEL_REG, reg_val); 462 | gain_sel = (reg_val & GAIN_SEL_MASK) >> GAIN_SEL_SHIFT; 463 | return checkStatus(status); 464 | } 465 | 466 | uint8_t 467 | MLX90393:: 468 | setHallConf(uint8_t hallconf) 469 | { 470 | uint16_t old_val; 471 | uint8_t status1 = readRegister(HALLCONF_REG, old_val); 472 | uint8_t status2 = writeRegister(HALLCONF_REG, 473 | (old_val & ~HALLCONF_MASK) | 474 | ((uint16_t(hallconf) << HALLCONF_SHIFT) & 475 | HALLCONF_MASK)); 476 | return checkStatus(status1) | checkStatus(status2); 477 | } 478 | 479 | uint8_t 480 | MLX90393:: 481 | getHallConf(uint8_t& hallconf) 482 | { 483 | uint16_t reg_val; 484 | uint8_t status = readRegister(HALLCONF_REG, reg_val); 485 | hallconf = (reg_val & HALLCONF_MASK) >> HALLCONF_SHIFT; 486 | return checkStatus(status); 487 | } 488 | 489 | uint8_t 490 | MLX90393:: 491 | setOverSampling(uint8_t osr) 492 | { 493 | uint16_t old_val; 494 | uint8_t status1 = readRegister(OSR_REG, old_val); 495 | uint8_t status2 = writeRegister(OSR_REG, 496 | (old_val & ~OSR_MASK) | 497 | ((uint16_t(osr) << OSR_SHIFT) & OSR_MASK)); 498 | return checkStatus(status1) | checkStatus(status2); 499 | } 500 | 501 | uint8_t 502 | MLX90393:: 503 | getOverSampling(uint8_t& osr) 504 | { 505 | uint16_t reg_val; 506 | uint8_t status = readRegister(OSR_REG, reg_val); 507 | osr = (reg_val & OSR_MASK) >> OSR_SHIFT; 508 | return checkStatus(status); 509 | } 510 | 511 | uint8_t 512 | MLX90393:: 513 | setTemperatureOverSampling(uint8_t osr2) 514 | { 515 | uint16_t old_val; 516 | uint8_t status1 = readRegister(OSR2_REG, old_val); 517 | uint8_t status2 = writeRegister(OSR2_REG, 518 | (old_val & ~OSR2_MASK) | 519 | ((uint16_t(osr2) << OSR2_SHIFT) & OSR2_MASK)); 520 | return checkStatus(status1) | checkStatus(status2); 521 | } 522 | 523 | uint8_t 524 | MLX90393:: 525 | getTemperatureOverSampling(uint8_t& osr2) 526 | { 527 | uint16_t reg_val; 528 | uint8_t status = readRegister(OSR2_REG, reg_val); 529 | osr2 = (reg_val & OSR2_MASK) >> OSR2_SHIFT; 530 | return checkStatus(status); 531 | } 532 | 533 | uint8_t 534 | MLX90393:: 535 | setDigitalFiltering(uint8_t dig_flt) 536 | { 537 | uint16_t old_val; 538 | uint8_t status1 = readRegister(DIG_FLT_REG, old_val); 539 | uint8_t status2 = writeRegister(DIG_FLT_REG, 540 | (old_val & ~DIG_FLT_MASK) | 541 | ((uint16_t(dig_flt) << DIG_FLT_SHIFT) & 542 | DIG_FLT_MASK)); 543 | return checkStatus(status1) | checkStatus(status2); 544 | } 545 | 546 | uint8_t 547 | MLX90393:: 548 | getDigitalFiltering(uint8_t& dig_flt) 549 | { 550 | uint16_t reg_val; 551 | uint8_t status = readRegister(DIG_FLT_REG, reg_val); 552 | dig_flt = (reg_val & DIG_FLT_MASK) >> DIG_FLT_SHIFT; 553 | return checkStatus(status); 554 | } 555 | 556 | uint8_t 557 | MLX90393:: 558 | setResolution(uint8_t res_x, uint8_t res_y, uint8_t res_z) 559 | { 560 | uint16_t res_xyz = ((res_z & 0x3)<<4) | ((res_y & 0x3)<<2) | (res_x & 0x3); 561 | uint16_t old_val; 562 | uint8_t status1 = readRegister(RES_XYZ_REG, old_val); 563 | uint8_t status2 = writeRegister(RES_XYZ_REG, 564 | (old_val & ~RES_XYZ_MASK) | 565 | (res_xyz << RES_XYZ_SHIFT) & RES_XYZ_MASK); 566 | return checkStatus(status1) | checkStatus(status2); 567 | } 568 | 569 | uint8_t 570 | MLX90393:: 571 | getResolution(uint8_t& res_x, uint8_t& res_y, uint8_t& res_z) 572 | { 573 | uint16_t reg_val; 574 | uint8_t status = readRegister(RES_XYZ_REG, reg_val); 575 | uint8_t res_xyz = (reg_val & RES_XYZ_MASK) >> RES_XYZ_SHIFT; 576 | res_x = (res_xyz >> 0) & 0x3; 577 | res_y = (res_xyz >> 2) & 0x3; 578 | res_z = (res_xyz >> 4) & 0x3; 579 | return checkStatus(status); 580 | } 581 | 582 | uint8_t 583 | MLX90393:: 584 | setTemperatureCompensation(uint8_t enabled) 585 | { 586 | uint8_t tcmp_en = enabled?1:0; 587 | uint16_t old_val; 588 | uint8_t status1 = readRegister(TCMP_EN_REG, old_val); 589 | uint8_t status2 = writeRegister(TCMP_EN_REG, 590 | (old_val & ~TCMP_EN_MASK) | 591 | ((uint16_t(tcmp_en) << TCMP_EN_SHIFT) & 592 | TCMP_EN_MASK)); 593 | return checkStatus(status1) | checkStatus(status2); 594 | } 595 | 596 | uint8_t 597 | MLX90393:: 598 | getTemperatureCompensation(uint8_t& enabled) 599 | { 600 | uint16_t reg_val; 601 | uint8_t status = readRegister(TCMP_EN_REG, reg_val); 602 | enabled = (reg_val & TCMP_EN_MASK) >> TCMP_EN_SHIFT; 603 | return checkStatus(status); 604 | } 605 | 606 | uint8_t 607 | MLX90393:: 608 | setBurstSel(uint8_t burst_sel) 609 | { 610 | uint16_t old_val; 611 | uint8_t status1 = readRegister(BURST_SEL_REG, old_val); 612 | uint8_t status2 = writeRegister(BURST_SEL_REG, 613 | (old_val & ~BURST_SEL_MASK) | 614 | ((uint16_t(burst_sel) << BURST_SEL_SHIFT) & 615 | BURST_SEL_MASK)); 616 | return checkStatus(status1) | checkStatus(status2); 617 | } 618 | 619 | uint8_t 620 | MLX90393:: 621 | getBurstSel(uint8_t& burst_sel) 622 | { 623 | uint16_t reg_val; 624 | uint8_t status = readRegister(BURST_SEL_REG, reg_val); 625 | burst_sel = (reg_val & BURST_SEL_MASK) >> BURST_SEL_SHIFT; 626 | return checkStatus(status); 627 | } 628 | 629 | uint8_t 630 | MLX90393:: 631 | setExtTrig(int8_t ext_trig) 632 | { 633 | uint16_t old_val; 634 | uint8_t status1 = readRegister(EXT_TRIG_REG, old_val); 635 | uint8_t status2 = writeRegister(EXT_TRIG_REG, 636 | (old_val & ~EXT_TRIG_MASK) | 637 | ((uint16_t(ext_trig) << EXT_TRIG_SHIFT) & 638 | EXT_TRIG_MASK)); 639 | return checkStatus(status1) | checkStatus(status2); 640 | } 641 | 642 | uint8_t 643 | MLX90393:: 644 | getExtTrig(uint8_t& ext_trig) 645 | { 646 | uint16_t reg_val; 647 | uint8_t status = readRegister(EXT_TRIG_REG, reg_val); 648 | ext_trig = (reg_val & EXT_TRIG_MASK) >> EXT_TRIG_SHIFT; 649 | return checkStatus(status); 650 | 651 | } 652 | 653 | uint8_t 654 | MLX90393:: 655 | setTrigIntSel(uint8_t trig_int_sel) 656 | { 657 | uint16_t old_val; 658 | uint8_t status1 = readRegister(TRIG_INT_SEL_REG, old_val); 659 | uint8_t status2 = writeRegister(TRIG_INT_SEL_REG, 660 | (old_val & ~TRIG_INT_SEL_MASK) | 661 | ((uint16_t(trig_int_sel) << TRIG_INT_SEL_SHIFT) & 662 | TRIG_INT_SEL_MASK)); 663 | return checkStatus(status1) | checkStatus(status2); 664 | } 665 | 666 | uint8_t 667 | MLX90393:: 668 | getTrigIntSel(uint8_t& trig_int_sel) 669 | { 670 | uint16_t reg_val; 671 | uint8_t status = readRegister(TRIG_INT_SEL_REG, reg_val); 672 | trig_int_sel = (reg_val & TRIG_INT_SEL_MASK) >> TRIG_INT_SEL_SHIFT; 673 | return checkStatus(status); 674 | } 675 | 676 | 677 | // 678 | // Note: offsets are relative to 0x8000 679 | // the default value of 0 in these registers will give poor results 680 | // 681 | uint8_t 682 | MLX90393:: 683 | setOffsets(uint16_t x, uint16_t y, uint16_t z) 684 | { 685 | uint8_t status1 = writeRegister(X_OFFSET_REG, x); 686 | uint8_t status2 = writeRegister(Y_OFFSET_REG, y); 687 | uint8_t status3 = writeRegister(Z_OFFSET_REG, z); 688 | return checkStatus(status1) | checkStatus(status2) | checkStatus(status3); 689 | } 690 | 691 | uint8_t 692 | MLX90393:: 693 | setWOXYThreshold(uint16_t woxy_thresh) 694 | { 695 | uint8_t status = writeRegister(WOXY_THRESHOLD_REG, woxy_thresh); 696 | return checkStatus(status); 697 | } 698 | 699 | uint8_t 700 | MLX90393:: 701 | setWOZThreshold(uint16_t woz_thresh) 702 | { 703 | uint8_t status = writeRegister(WOZ_THRESHOLD_REG, woz_thresh); 704 | return checkStatus(status); 705 | } 706 | 707 | 708 | uint8_t 709 | MLX90393:: 710 | setWOTThreshold(uint16_t wot_thresh) 711 | { 712 | uint8_t status = writeRegister(WOT_THRESHOLD_REG, wot_thresh); 713 | return checkStatus(status); 714 | } 715 | -------------------------------------------------------------------------------- /examples/MLX90393_Debug_Helper/LICENSE: -------------------------------------------------------------------------------- 1 | ### GNU GENERAL PUBLIC LICENSE 2 | 3 | Version 3, 29 June 2007 4 | 5 | Copyright (C) 2007 Free Software Foundation, Inc. 6 | 7 | 8 | Everyone is permitted to copy and distribute verbatim copies of this 9 | license document, but changing it is not allowed. 10 | 11 | ### Preamble 12 | 13 | The GNU General Public License is a free, copyleft license for 14 | software and other kinds of works. 15 | 16 | The licenses for most software and other practical works are designed 17 | to take away your freedom to share and change the works. By contrast, 18 | the GNU General Public License is intended to guarantee your freedom 19 | to share and change all versions of a program--to make sure it remains 20 | free software for all its users. We, the Free Software Foundation, use 21 | the GNU General Public License for most of our software; it applies 22 | also to any other work released this way by its authors. You can apply 23 | it to your programs, too. 24 | 25 | When we speak of free software, we are referring to freedom, not 26 | price. Our General Public Licenses are designed to make sure that you 27 | have the freedom to distribute copies of free software (and charge for 28 | them if you wish), that you receive source code or can get it if you 29 | want it, that you can change the software or use pieces of it in new 30 | free programs, and that you know you can do these things. 31 | 32 | To protect your rights, we need to prevent others from denying you 33 | these rights or asking you to surrender the rights. Therefore, you 34 | have certain responsibilities if you distribute copies of the 35 | software, or if you modify it: responsibilities to respect the freedom 36 | of others. 37 | 38 | For example, if you distribute copies of such a program, whether 39 | gratis or for a fee, you must pass on to the recipients the same 40 | freedoms that you received. You must make sure that they, too, receive 41 | or can get the source code. And you must show them these terms so they 42 | know their rights. 43 | 44 | Developers that use the GNU GPL protect your rights with two steps: 45 | (1) assert copyright on the software, and (2) offer you this License 46 | giving you legal permission to copy, distribute and/or modify it. 47 | 48 | For the developers' and authors' protection, the GPL clearly explains 49 | that there is no warranty for this free software. For both users' and 50 | authors' sake, the GPL requires that modified versions be marked as 51 | changed, so that their problems will not be attributed erroneously to 52 | authors of previous versions. 53 | 54 | Some devices are designed to deny users access to install or run 55 | modified versions of the software inside them, although the 56 | manufacturer can do so. This is fundamentally incompatible with the 57 | aim of protecting users' freedom to change the software. The 58 | systematic pattern of such abuse occurs in the area of products for 59 | individuals to use, which is precisely where it is most unacceptable. 60 | Therefore, we have designed this version of the GPL to prohibit the 61 | practice for those products. If such problems arise substantially in 62 | other domains, we stand ready to extend this provision to those 63 | domains in future versions of the GPL, as needed to protect the 64 | freedom of users. 65 | 66 | Finally, every program is threatened constantly by software patents. 67 | States should not allow patents to restrict development and use of 68 | software on general-purpose computers, but in those that do, we wish 69 | to avoid the special danger that patents applied to a free program 70 | could make it effectively proprietary. To prevent this, the GPL 71 | assures that patents cannot be used to render the program non-free. 72 | 73 | The precise terms and conditions for copying, distribution and 74 | modification follow. 75 | 76 | ### TERMS AND CONDITIONS 77 | 78 | #### 0. Definitions. 79 | 80 | "This License" refers to version 3 of the GNU General Public License. 81 | 82 | "Copyright" also means copyright-like laws that apply to other kinds 83 | of works, such as semiconductor masks. 84 | 85 | "The Program" refers to any copyrightable work licensed under this 86 | License. Each licensee is addressed as "you". "Licensees" and 87 | "recipients" may be individuals or organizations. 88 | 89 | To "modify" a work means to copy from or adapt all or part of the work 90 | in a fashion requiring copyright permission, other than the making of 91 | an exact copy. The resulting work is called a "modified version" of 92 | the earlier work or a work "based on" the earlier work. 93 | 94 | A "covered work" means either the unmodified Program or a work based 95 | on the Program. 96 | 97 | To "propagate" a work means to do anything with it that, without 98 | permission, would make you directly or secondarily liable for 99 | infringement under applicable copyright law, except executing it on a 100 | computer or modifying a private copy. Propagation includes copying, 101 | distribution (with or without modification), making available to the 102 | public, and in some countries other activities as well. 103 | 104 | To "convey" a work means any kind of propagation that enables other 105 | parties to make or receive copies. Mere interaction with a user 106 | through a computer network, with no transfer of a copy, is not 107 | conveying. 108 | 109 | An interactive user interface displays "Appropriate Legal Notices" to 110 | the extent that it includes a convenient and prominently visible 111 | feature that (1) displays an appropriate copyright notice, and (2) 112 | tells the user that there is no warranty for the work (except to the 113 | extent that warranties are provided), that licensees may convey the 114 | work under this License, and how to view a copy of this License. If 115 | the interface presents a list of user commands or options, such as a 116 | menu, a prominent item in the list meets this criterion. 117 | 118 | #### 1. Source Code. 119 | 120 | The "source code" for a work means the preferred form of the work for 121 | making modifications to it. "Object code" means any non-source form of 122 | a work. 123 | 124 | A "Standard Interface" means an interface that either is an official 125 | standard defined by a recognized standards body, or, in the case of 126 | interfaces specified for a particular programming language, one that 127 | is widely used among developers working in that language. 128 | 129 | The "System Libraries" of an executable work include anything, other 130 | than the work as a whole, that (a) is included in the normal form of 131 | packaging a Major Component, but which is not part of that Major 132 | Component, and (b) serves only to enable use of the work with that 133 | Major Component, or to implement a Standard Interface for which an 134 | implementation is available to the public in source code form. A 135 | "Major Component", in this context, means a major essential component 136 | (kernel, window system, and so on) of the specific operating system 137 | (if any) on which the executable work runs, or a compiler used to 138 | produce the work, or an object code interpreter used to run it. 139 | 140 | The "Corresponding Source" for a work in object code form means all 141 | the source code needed to generate, install, and (for an executable 142 | work) run the object code and to modify the work, including scripts to 143 | control those activities. However, it does not include the work's 144 | System Libraries, or general-purpose tools or generally available free 145 | programs which are used unmodified in performing those activities but 146 | which are not part of the work. For example, Corresponding Source 147 | includes interface definition files associated with source files for 148 | the work, and the source code for shared libraries and dynamically 149 | linked subprograms that the work is specifically designed to require, 150 | such as by intimate data communication or control flow between those 151 | subprograms and other parts of the work. 152 | 153 | The Corresponding Source need not include anything that users can 154 | regenerate automatically from other parts of the Corresponding Source. 155 | 156 | The Corresponding Source for a work in source code form is that same 157 | work. 158 | 159 | #### 2. Basic Permissions. 160 | 161 | All rights granted under this License are granted for the term of 162 | copyright on the Program, and are irrevocable provided the stated 163 | conditions are met. This License explicitly affirms your unlimited 164 | permission to run the unmodified Program. The output from running a 165 | covered work is covered by this License only if the output, given its 166 | content, constitutes a covered work. This License acknowledges your 167 | rights of fair use or other equivalent, as provided by copyright law. 168 | 169 | You may make, run and propagate covered works that you do not convey, 170 | without conditions so long as your license otherwise remains in force. 171 | You may convey covered works to others for the sole purpose of having 172 | them make modifications exclusively for you, or provide you with 173 | facilities for running those works, provided that you comply with the 174 | terms of this License in conveying all material for which you do not 175 | control copyright. Those thus making or running the covered works for 176 | you must do so exclusively on your behalf, under your direction and 177 | control, on terms that prohibit them from making any copies of your 178 | copyrighted material outside their relationship with you. 179 | 180 | Conveying under any other circumstances is permitted solely under the 181 | conditions stated below. Sublicensing is not allowed; section 10 makes 182 | it unnecessary. 183 | 184 | #### 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 185 | 186 | No covered work shall be deemed part of an effective technological 187 | measure under any applicable law fulfilling obligations under article 188 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 189 | similar laws prohibiting or restricting circumvention of such 190 | measures. 191 | 192 | When you convey a covered work, you waive any legal power to forbid 193 | circumvention of technological measures to the extent such 194 | circumvention is effected by exercising rights under this License with 195 | respect to the covered work, and you disclaim any intention to limit 196 | operation or modification of the work as a means of enforcing, against 197 | the work's users, your or third parties' legal rights to forbid 198 | circumvention of technological measures. 199 | 200 | #### 4. Conveying Verbatim Copies. 201 | 202 | You may convey verbatim copies of the Program's source code as you 203 | receive it, in any medium, provided that you conspicuously and 204 | appropriately publish on each copy an appropriate copyright notice; 205 | keep intact all notices stating that this License and any 206 | non-permissive terms added in accord with section 7 apply to the code; 207 | keep intact all notices of the absence of any warranty; and give all 208 | recipients a copy of this License along with the Program. 209 | 210 | You may charge any price or no price for each copy that you convey, 211 | and you may offer support or warranty protection for a fee. 212 | 213 | #### 5. Conveying Modified Source Versions. 214 | 215 | You may convey a work based on the Program, or the modifications to 216 | produce it from the Program, in the form of source code under the 217 | terms of section 4, provided that you also meet all of these 218 | conditions: 219 | 220 | - a) The work must carry prominent notices stating that you modified 221 | it, and giving a relevant date. 222 | - b) The work must carry prominent notices stating that it is 223 | released under this License and any conditions added under 224 | section 7. This requirement modifies the requirement in section 4 225 | to "keep intact all notices". 226 | - c) You must license the entire work, as a whole, under this 227 | License to anyone who comes into possession of a copy. This 228 | License will therefore apply, along with any applicable section 7 229 | additional terms, to the whole of the work, and all its parts, 230 | regardless of how they are packaged. This License gives no 231 | permission to license the work in any other way, but it does not 232 | invalidate such permission if you have separately received it. 233 | - d) If the work has interactive user interfaces, each must display 234 | Appropriate Legal Notices; however, if the Program has interactive 235 | interfaces that do not display Appropriate Legal Notices, your 236 | work need not make them do so. 237 | 238 | A compilation of a covered work with other separate and independent 239 | works, which are not by their nature extensions of the covered work, 240 | and which are not combined with it such as to form a larger program, 241 | in or on a volume of a storage or distribution medium, is called an 242 | "aggregate" if the compilation and its resulting copyright are not 243 | used to limit the access or legal rights of the compilation's users 244 | beyond what the individual works permit. Inclusion of a covered work 245 | in an aggregate does not cause this License to apply to the other 246 | parts of the aggregate. 247 | 248 | #### 6. Conveying Non-Source Forms. 249 | 250 | You may convey a covered work in object code form under the terms of 251 | sections 4 and 5, provided that you also convey the machine-readable 252 | Corresponding Source under the terms of this License, in one of these 253 | ways: 254 | 255 | - a) Convey the object code in, or embodied in, a physical product 256 | (including a physical distribution medium), accompanied by the 257 | Corresponding Source fixed on a durable physical medium 258 | customarily used for software interchange. 259 | - b) Convey the object code in, or embodied in, a physical product 260 | (including a physical distribution medium), accompanied by a 261 | written offer, valid for at least three years and valid for as 262 | long as you offer spare parts or customer support for that product 263 | model, to give anyone who possesses the object code either (1) a 264 | copy of the Corresponding Source for all the software in the 265 | product that is covered by this License, on a durable physical 266 | medium customarily used for software interchange, for a price no 267 | more than your reasonable cost of physically performing this 268 | conveying of source, or (2) access to copy the Corresponding 269 | Source from a network server at no charge. 270 | - c) Convey individual copies of the object code with a copy of the 271 | written offer to provide the Corresponding Source. This 272 | alternative is allowed only occasionally and noncommercially, and 273 | only if you received the object code with such an offer, in accord 274 | with subsection 6b. 275 | - d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | - e) Convey the object code using peer-to-peer transmission, 288 | provided you inform other peers where the object code and 289 | Corresponding Source of the work are being offered to the general 290 | public at no charge under subsection 6d. 291 | 292 | A separable portion of the object code, whose source code is excluded 293 | from the Corresponding Source as a System Library, need not be 294 | included in conveying the object code work. 295 | 296 | A "User Product" is either (1) a "consumer product", which means any 297 | tangible personal property which is normally used for personal, 298 | family, or household purposes, or (2) anything designed or sold for 299 | incorporation into a dwelling. In determining whether a product is a 300 | consumer product, doubtful cases shall be resolved in favor of 301 | coverage. For a particular product received by a particular user, 302 | "normally used" refers to a typical or common use of that class of 303 | product, regardless of the status of the particular user or of the way 304 | in which the particular user actually uses, or expects or is expected 305 | to use, the product. A product is a consumer product regardless of 306 | whether the product has substantial commercial, industrial or 307 | non-consumer uses, unless such uses represent the only significant 308 | mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to 312 | install and execute modified versions of a covered work in that User 313 | Product from a modified version of its Corresponding Source. The 314 | information must suffice to ensure that the continued functioning of 315 | the modified object code is in no case prevented or interfered with 316 | solely because modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or 331 | updates for a work that has been modified or installed by the 332 | recipient, or for the User Product in which it has been modified or 333 | installed. Access to a network may be denied when the modification 334 | itself materially and adversely affects the operation of the network 335 | or violates the rules and protocols for communication across the 336 | network. 337 | 338 | Corresponding Source conveyed, and Installation Information provided, 339 | in accord with this section must be in a format that is publicly 340 | documented (and with an implementation available to the public in 341 | source code form), and must require no special password or key for 342 | unpacking, reading or copying. 343 | 344 | #### 7. Additional Terms. 345 | 346 | "Additional permissions" are terms that supplement the terms of this 347 | License by making exceptions from one or more of its conditions. 348 | Additional permissions that are applicable to the entire Program shall 349 | be treated as though they were included in this License, to the extent 350 | that they are valid under applicable law. If additional permissions 351 | apply only to part of the Program, that part may be used separately 352 | under those permissions, but the entire Program remains governed by 353 | this License without regard to the additional permissions. 354 | 355 | When you convey a copy of a covered work, you may at your option 356 | remove any additional permissions from that copy, or from any part of 357 | it. (Additional permissions may be written to require their own 358 | removal in certain cases when you modify the work.) You may place 359 | additional permissions on material, added by you to a covered work, 360 | for which you have or can give appropriate copyright permission. 361 | 362 | Notwithstanding any other provision of this License, for material you 363 | add to a covered work, you may (if authorized by the copyright holders 364 | of that material) supplement the terms of this License with terms: 365 | 366 | - a) Disclaiming warranty or limiting liability differently from the 367 | terms of sections 15 and 16 of this License; or 368 | - b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | - c) Prohibiting misrepresentation of the origin of that material, 372 | or requiring that modified versions of such material be marked in 373 | reasonable ways as different from the original version; or 374 | - d) Limiting the use for publicity purposes of names of licensors 375 | or authors of the material; or 376 | - e) Declining to grant rights under trademark law for use of some 377 | trade names, trademarks, or service marks; or 378 | - f) Requiring indemnification of licensors and authors of that 379 | material by anyone who conveys the material (or modified versions 380 | of it) with contractual assumptions of liability to the recipient, 381 | for any liability that these contractual assumptions directly 382 | impose on those licensors and authors. 383 | 384 | All other non-permissive additional terms are considered "further 385 | restrictions" within the meaning of section 10. If the Program as you 386 | received it, or any part of it, contains a notice stating that it is 387 | governed by this License along with a term that is a further 388 | restriction, you may remove that term. If a license document contains 389 | a further restriction but permits relicensing or conveying under this 390 | License, you may add to a covered work material governed by the terms 391 | of that license document, provided that the further restriction does 392 | not survive such relicensing or conveying. 393 | 394 | If you add terms to a covered work in accord with this section, you 395 | must place, in the relevant source files, a statement of the 396 | additional terms that apply to those files, or a notice indicating 397 | where to find the applicable terms. 398 | 399 | Additional terms, permissive or non-permissive, may be stated in the 400 | form of a separately written license, or stated as exceptions; the 401 | above requirements apply either way. 402 | 403 | #### 8. Termination. 404 | 405 | You may not propagate or modify a covered work except as expressly 406 | provided under this License. Any attempt otherwise to propagate or 407 | modify it is void, and will automatically terminate your rights under 408 | this License (including any patent licenses granted under the third 409 | paragraph of section 11). 410 | 411 | However, if you cease all violation of this License, then your license 412 | from a particular copyright holder is reinstated (a) provisionally, 413 | unless and until the copyright holder explicitly and finally 414 | terminates your license, and (b) permanently, if the copyright holder 415 | fails to notify you of the violation by some reasonable means prior to 416 | 60 days after the cessation. 417 | 418 | Moreover, your license from a particular copyright holder is 419 | reinstated permanently if the copyright holder notifies you of the 420 | violation by some reasonable means, this is the first time you have 421 | received notice of violation of this License (for any work) from that 422 | copyright holder, and you cure the violation prior to 30 days after 423 | your receipt of the notice. 424 | 425 | Termination of your rights under this section does not terminate the 426 | licenses of parties who have received copies or rights from you under 427 | this License. If your rights have been terminated and not permanently 428 | reinstated, you do not qualify to receive new licenses for the same 429 | material under section 10. 430 | 431 | #### 9. Acceptance Not Required for Having Copies. 432 | 433 | You are not required to accept this License in order to receive or run 434 | a copy of the Program. Ancillary propagation of a covered work 435 | occurring solely as a consequence of using peer-to-peer transmission 436 | to receive a copy likewise does not require acceptance. However, 437 | nothing other than this License grants you permission to propagate or 438 | modify any covered work. These actions infringe copyright if you do 439 | not accept this License. Therefore, by modifying or propagating a 440 | covered work, you indicate your acceptance of this License to do so. 441 | 442 | #### 10. Automatic Licensing of Downstream Recipients. 443 | 444 | Each time you convey a covered work, the recipient automatically 445 | receives a license from the original licensors, to run, modify and 446 | propagate that work, subject to this License. You are not responsible 447 | for enforcing compliance by third parties with this License. 448 | 449 | An "entity transaction" is a transaction transferring control of an 450 | organization, or substantially all assets of one, or subdividing an 451 | organization, or merging organizations. If propagation of a covered 452 | work results from an entity transaction, each party to that 453 | transaction who receives a copy of the work also receives whatever 454 | licenses to the work the party's predecessor in interest had or could 455 | give under the previous paragraph, plus a right to possession of the 456 | Corresponding Source of the work from the predecessor in interest, if 457 | the predecessor has it or can get it with reasonable efforts. 458 | 459 | You may not impose any further restrictions on the exercise of the 460 | rights granted or affirmed under this License. For example, you may 461 | not impose a license fee, royalty, or other charge for exercise of 462 | rights granted under this License, and you may not initiate litigation 463 | (including a cross-claim or counterclaim in a lawsuit) alleging that 464 | any patent claim is infringed by making, using, selling, offering for 465 | sale, or importing the Program or any portion of it. 466 | 467 | #### 11. Patents. 468 | 469 | A "contributor" is a copyright holder who authorizes use under this 470 | License of the Program or a work on which the Program is based. The 471 | work thus licensed is called the contributor's "contributor version". 472 | 473 | A contributor's "essential patent claims" are all patent claims owned 474 | or controlled by the contributor, whether already acquired or 475 | hereafter acquired, that would be infringed by some manner, permitted 476 | by this License, of making, using, or selling its contributor version, 477 | but do not include claims that would be infringed only as a 478 | consequence of further modification of the contributor version. For 479 | purposes of this definition, "control" includes the right to grant 480 | patent sublicenses in a manner consistent with the requirements of 481 | this License. 482 | 483 | Each contributor grants you a non-exclusive, worldwide, royalty-free 484 | patent license under the contributor's essential patent claims, to 485 | make, use, sell, offer for sale, import and otherwise run, modify and 486 | propagate the contents of its contributor version. 487 | 488 | In the following three paragraphs, a "patent license" is any express 489 | agreement or commitment, however denominated, not to enforce a patent 490 | (such as an express permission to practice a patent or covenant not to 491 | sue for patent infringement). To "grant" such a patent license to a 492 | party means to make such an agreement or commitment not to enforce a 493 | patent against the party. 494 | 495 | If you convey a covered work, knowingly relying on a patent license, 496 | and the Corresponding Source of the work is not available for anyone 497 | to copy, free of charge and under the terms of this License, through a 498 | publicly available network server or other readily accessible means, 499 | then you must either (1) cause the Corresponding Source to be so 500 | available, or (2) arrange to deprive yourself of the benefit of the 501 | patent license for this particular work, or (3) arrange, in a manner 502 | consistent with the requirements of this License, to extend the patent 503 | license to downstream recipients. "Knowingly relying" means you have 504 | actual knowledge that, but for the patent license, your conveying the 505 | covered work in a country, or your recipient's use of the covered work 506 | in a country, would infringe one or more identifiable patents in that 507 | country that you have reason to believe are valid. 508 | 509 | If, pursuant to or in connection with a single transaction or 510 | arrangement, you convey, or propagate by procuring conveyance of, a 511 | covered work, and grant a patent license to some of the parties 512 | receiving the covered work authorizing them to use, propagate, modify 513 | or convey a specific copy of the covered work, then the patent license 514 | you grant is automatically extended to all recipients of the covered 515 | work and works based on it. 516 | 517 | A patent license is "discriminatory" if it does not include within the 518 | scope of its coverage, prohibits the exercise of, or is conditioned on 519 | the non-exercise of one or more of the rights that are specifically 520 | granted under this License. You may not convey a covered work if you 521 | are a party to an arrangement with a third party that is in the 522 | business of distributing software, under which you make payment to the 523 | third party based on the extent of your activity of conveying the 524 | work, and under which the third party grants, to any of the parties 525 | who would receive the covered work from you, a discriminatory patent 526 | license (a) in connection with copies of the covered work conveyed by 527 | you (or copies made from those copies), or (b) primarily for and in 528 | connection with specific products or compilations that contain the 529 | covered work, unless you entered into that arrangement, or that patent 530 | license was granted, prior to 28 March 2007. 531 | 532 | Nothing in this License shall be construed as excluding or limiting 533 | any implied license or other defenses to infringement that may 534 | otherwise be available to you under applicable patent law. 535 | 536 | #### 12. No Surrender of Others' Freedom. 537 | 538 | If conditions are imposed on you (whether by court order, agreement or 539 | otherwise) that contradict the conditions of this License, they do not 540 | excuse you from the conditions of this License. If you cannot convey a 541 | covered work so as to satisfy simultaneously your obligations under 542 | this License and any other pertinent obligations, then as a 543 | consequence you may not convey it at all. For example, if you agree to 544 | terms that obligate you to collect a royalty for further conveying 545 | from those to whom you convey the Program, the only way you could 546 | satisfy both those terms and this License would be to refrain entirely 547 | from conveying the Program. 548 | 549 | #### 13. Use with the GNU Affero General Public License. 550 | 551 | Notwithstanding any other provision of this License, you have 552 | permission to link or combine any covered work with a work licensed 553 | under version 3 of the GNU Affero General Public License into a single 554 | combined work, and to convey the resulting work. The terms of this 555 | License will continue to apply to the part which is the covered work, 556 | but the special requirements of the GNU Affero General Public License, 557 | section 13, concerning interaction through a network will apply to the 558 | combination as such. 559 | 560 | #### 14. Revised Versions of this License. 561 | 562 | The Free Software Foundation may publish revised and/or new versions 563 | of the GNU General Public License from time to time. Such new versions 564 | will be similar in spirit to the present version, but may differ in 565 | detail to address new problems or concerns. 566 | 567 | Each version is given a distinguishing version number. If the Program 568 | specifies that a certain numbered version of the GNU General Public 569 | License "or any later version" applies to it, you have the option of 570 | following the terms and conditions either of that numbered version or 571 | of any later version published by the Free Software Foundation. If the 572 | Program does not specify a version number of the GNU General Public 573 | License, you may choose any version ever published by the Free 574 | Software Foundation. 575 | 576 | If the Program specifies that a proxy can decide which future versions 577 | of the GNU General Public License can be used, that proxy's public 578 | statement of acceptance of a version permanently authorizes you to 579 | choose that version for the Program. 580 | 581 | Later license versions may give you additional or different 582 | permissions. However, no additional obligations are imposed on any 583 | author or copyright holder as a result of your choosing to follow a 584 | later version. 585 | 586 | #### 15. Disclaimer of Warranty. 587 | 588 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 589 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 590 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT 591 | WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT 592 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 593 | A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND 594 | PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE 595 | DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR 596 | CORRECTION. 597 | 598 | #### 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR 602 | CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 603 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES 604 | ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT 605 | NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR 606 | LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM 607 | TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER 608 | PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 609 | 610 | #### 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | ### How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these 626 | terms. 627 | 628 | To do so, attach the following notices to the program. It is safest to 629 | attach them to the start of each source file to most effectively state 630 | the exclusion of warranty; and each file should have at least the 631 | "copyright" line and a pointer to where the full notice is found. 632 | 633 | 634 | Copyright (C) 635 | 636 | This program is free software: you can redistribute it and/or modify 637 | it under the terms of the GNU General Public License as published by 638 | the Free Software Foundation, either version 3 of the License, or 639 | (at your option) any later version. 640 | 641 | This program is distributed in the hope that it will be useful, 642 | but WITHOUT ANY WARRANTY; without even the implied warranty of 643 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 644 | GNU General Public License for more details. 645 | 646 | You should have received a copy of the GNU General Public License 647 | along with this program. If not, see . 648 | 649 | Also add information on how to contact you by electronic and paper 650 | mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands \`show w' and \`show c' should show the 661 | appropriate parts of the General Public License. Of course, your 662 | program's commands might be different; for a GUI interface, you would 663 | use an "about box". 664 | 665 | You should also get your employer (if you work as a programmer) or 666 | school, if any, to sign a "copyright disclaimer" for the program, if 667 | necessary. For more information on this, and how to apply and follow 668 | the GNU GPL, see . 669 | 670 | The GNU General Public License does not permit incorporating your 671 | program into proprietary programs. If your program is a subroutine 672 | library, you may consider it more useful to permit linking proprietary 673 | applications with the library. If this is what you want to do, use the 674 | GNU Lesser General Public License instead of this License. But first, 675 | please read . 676 | -------------------------------------------------------------------------------- /examples/MLX90393_Debug_Helper/MLX90393_Debug_Helper.ino: -------------------------------------------------------------------------------- 1 | // 2 | // www.blinkenlight.net 3 | // 4 | // Copyright 2018 Udo Klein 5 | // 6 | // This program is free software: you can redistribute it and/or modify 7 | // it under the terms of the GNU General Public License as published by 8 | // the Free Software Foundation, either version 3 of the License, or 9 | // (at your option) any later version. 10 | // 11 | // This program is distributed in the hope that it will be useful, 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | // GNU General Public License for more details. 15 | // 16 | // You should have received a copy of the GNU General Public License 17 | // along with this program. If not, see http://www.gnu.org/licenses/ 18 | 19 | 20 | #include 21 | 22 | #define print(...) Serial.print( __VA_ARGS__ ) 23 | #define println(...) Serial.println( __VA_ARGS__ ) 24 | 25 | 26 | MLX90393 mlx; 27 | 28 | struct output_mode_t { 29 | typedef enum { HEX_RAW = 0, HEX_CSV = 1, DEC_CSV = 2, FLOAT_CSV = 3 } style_t; 30 | 31 | // QUIET: suppress status 32 | // NORMAL: show status on error 33 | // VERBOSE: always show status (HEX) 34 | typedef enum { QUIET = 0, NORMAL = 1, VERBOSE = 2, VERY_VERBOSE = 3 } verbosity_t; 35 | 36 | // ON_DEMAND: reading must be explicitly triggered 37 | // AUTO: any measurement will trigger output (except WOC w/o DRDY pin) 38 | // CONTINUOUS: read even if there might be nothing to read 39 | typedef enum { ON_DEMAND = 0, AUTO = 1, CONTINUOUS = 2 } read_behaviour_t; 40 | 41 | typedef enum { IDLE = 0, READ = 1, START_MEASUREMENT = 2, BURST = 3, WAKE_ON_CHANGE = 4 } chip_state_t; 42 | 43 | style_t style; 44 | verbosity_t verbosity; 45 | read_behaviour_t read_behaviour; 46 | uint8_t timestamps; 47 | 48 | chip_state_t state; 49 | }; 50 | 51 | output_mode_t output_mode = {output_mode_t::HEX_CSV, 52 | output_mode_t::NORMAL, 53 | output_mode_t::ON_DEMAND, 54 | output_mode_t::IDLE }; 55 | 56 | int8_t drdy_pin = 10; 57 | 58 | uint8_t axis_mask = MLX90393::X_FLAG | MLX90393::Y_FLAG | MLX90393::Z_FLAG | MLX90393::T_FLAG; 59 | 60 | namespace timer { 61 | static auto old_ms = millis(); 62 | static auto new_ms = millis(); 63 | void set_old_ms() { old_ms = millis(); } 64 | void set_new_ms() { new_ms = millis(); } 65 | uint32_t elapsed() { return new_ms-old_ms; } 66 | } 67 | 68 | void space() { 69 | print(' '); 70 | } 71 | 72 | namespace dump { 73 | 74 | void nibble(uint8_t data) { 75 | print(data, HEX); 76 | } 77 | 78 | void hex(uint8_t data) { 79 | nibble(data >> 4); 80 | nibble(data & 0xf); 81 | } 82 | 83 | void hex(uint16_t data) { 84 | hex(highByte(data)); 85 | hex(lowByte(data)); 86 | } 87 | 88 | void bin(uint8_t data) { 89 | for (int8_t i=7; i>=0; --i) { 90 | print((data >> i) & 1); 91 | } 92 | } 93 | 94 | void bin(uint16_t data) { 95 | bin(highByte(data)); 96 | space(); 97 | bin(lowByte(data)); 98 | } 99 | 100 | void value(uint16_t data, uint8_t base) { 101 | switch (base) { 102 | case 2: bin(data); break; 103 | case 10: print(data); break; 104 | case 16: hex(data); break; 105 | default: print(F("wrong base")); 106 | } 107 | } 108 | void status(uint8_t s) { 109 | for (int8_t i=7; i>=0; --i) { 110 | if (i == 4) { 111 | print(' '); 112 | } 113 | print((s >> i) & 1); 114 | if (i == 4) { 115 | print(' '); 116 | } 117 | } 118 | if ((s >> 4) & 1) { 119 | print(F(" (ERROR)")); 120 | } else { 121 | print(F(" (OK)")); 122 | } 123 | print(F(" burst mode, woc mode, sm mode, error, single bit error, rs, d1, d0")); 124 | } 125 | 126 | void reg(uint8_t adr, uint16_t reg) { 127 | print(F("0x")); 128 | hex(adr); 129 | space(); 130 | space(); 131 | 132 | hex(reg); 133 | space(); 134 | space(); 135 | bin(reg); 136 | space(); 137 | space(); 138 | } 139 | 140 | void register_0(uint16_t reg) { 141 | dump::reg(0, reg); 142 | print(F("[15:9] ANA_RESERVED_LOW, [8] BIST, [7] Z_SERIES, [6:4] GAIN_SEL, [3:0] HALLCONF")); 143 | } 144 | 145 | void register_1(uint16_t reg) { 146 | dump::reg(1, reg); 147 | print(F("[15] TRIG_INT, [14:13] COMM_MODE, [12] WOC_DIFF, [11] EXT_TRIG, [10] TCMP_EN, [9:6] BURST_SEL (zyxt), [5-0] Burst data rate")); 148 | } 149 | 150 | void register_2(uint16_t reg) { 151 | dump::reg(2, reg); 152 | print(F("[15:13] -, [12:11] OSR2, [10:5] RES_XYZ, [4:2] DIG_FILT, [1:0] OSR")); 153 | } 154 | 155 | void register_3(uint16_t reg) { 156 | dump::reg(3, reg); 157 | print(F("[15:8] SENS_TC_HT, [7:0] SENS_TC_LT")); 158 | } 159 | 160 | void register_4(uint16_t reg) { 161 | dump::reg(4, reg); 162 | print(F("Offset X")); 163 | } 164 | 165 | void register_5(uint16_t reg) { 166 | dump::reg(5, reg); 167 | print(F("Offset Y")); 168 | } 169 | 170 | void register_6(uint16_t reg) { 171 | dump::reg(6, reg); 172 | print(F("Offset Z")); 173 | } 174 | 175 | void register_7(uint16_t reg) { 176 | dump::reg(7, reg); 177 | print(F("WOXY Threshold")); 178 | } 179 | 180 | void register_8(uint16_t reg) { 181 | dump::reg(8, reg); 182 | print(F("WOZ Threshold")); 183 | } 184 | 185 | void register_9(uint16_t reg) { 186 | dump::reg(9, reg); 187 | print(F("WOT Threshold")); 188 | } 189 | } 190 | 191 | namespace MLX { 192 | struct status_t { 193 | /** D1-D0: indicates the number of bytes (2D[1:0]) to follow the status byte after a read measurement 194 | * or a readregister command has been sent. 195 | */ 196 | uint8_t d0: 1; 197 | /** D1-D0: indicates the number of bytes (2D[1:0]) to follow the status byte after a read measurement 198 | * or a readregister command has been sent. 199 | */ 200 | uint8_t d1: 1; 201 | 202 | /** RS: indicates that the device has been reset successfully by a reset command. 203 | */ 204 | uint8_t rs: 1; 205 | 206 | /** SED: indicates that a single bit error has been corrected by the NVRAM 207 | */ 208 | uint8_t sed: 1; 209 | 210 | /** ERROR: indicates an error. 211 | * Can be set when reading out a measurement while the measurement is not yet completed or 212 | * when reading out the same measurement twice. 213 | */ 214 | uint8_t error: 1; 215 | 216 | /** SM_mode: if set, the IC is executing a measurement sequence in polling mode. 217 | * It can be initiated by a SM command or a pulse on the TRIG input. 218 | */ 219 | uint8_t sm_mode: 1; 220 | 221 | /** WOC_mode: if set, the IC is in wake-up-on-change mode. 222 | */ 223 | uint8_t woc_mode: 1; 224 | 225 | /** Burst_mode: if set, the IC is working in burst mode. 226 | */ 227 | uint8_t burst_mode:1; 228 | 229 | bool ok() const { return not error; } 230 | }; 231 | 232 | union status_u { 233 | status_t flags; 234 | uint8_t data; 235 | }; 236 | 237 | status_t status(uint8_t s) { 238 | MLX::status_u u; 239 | u.data = s; 240 | return u.flags; 241 | } 242 | } 243 | 244 | namespace explain { 245 | 246 | void status(uint8_t s) { 247 | auto status = MLX::status(s); 248 | 249 | print(F("burst mode: ")); 250 | println(status.burst_mode); 251 | print(F("woc mode: ")); 252 | println(status.woc_mode); 253 | print(F("sm mode: ")); 254 | println(status.sm_mode); 255 | print(F("error: ")); 256 | println(status.error); 257 | print(F("single bit error detected: ")); 258 | println(status.sed); 259 | print(F("rs: ")); 260 | println(status.rs); 261 | print(F("d1: ")); 262 | println(status.d1); 263 | print(F("d0: ")); 264 | println(status.d0); 265 | } 266 | 267 | void register_0(uint16_t reg) { 268 | uint16_t data = uint16_t(reg); 269 | 270 | print(F("ANA_RESERVED_LOW: ")); 271 | println(data >> 9); 272 | print(F("BIST: ")); 273 | println((data >> 8) & 1); 274 | print(F("Z_SERIES: ")); 275 | println((data >> 7) & 1); 276 | print(F("GAIN_SEL: ")); 277 | println((data >> 4) & 7); 278 | print(F("HALLCONF: ")); 279 | println(data & 0xf); 280 | } 281 | 282 | void register_1(uint16_t reg) { 283 | uint16_t data = uint16_t(reg); 284 | 285 | print(F("TRIG_INT: ")); 286 | println(data >> 15); 287 | print(F("COMM_MODE: ")); 288 | println((data >> 13) & 3); 289 | print(F("WOC_DIFF: ")); 290 | println((data >> 12) & 1); 291 | print(F("EXT_TRIG: ")); 292 | println((data >> 11) & 1); 293 | print(F("TCMP_EN: ")); 294 | println(data >> 10 & 1); 295 | print(F("BURST_SEL (zyxt): ")); 296 | print((data >> 9) & 1); 297 | print((data >> 8) & 1); 298 | print((data >> 7) & 1); 299 | println((data >> 6) & 1); 300 | print(F("Burst data rate: ")); 301 | println(data & 0x1f); 302 | } 303 | 304 | void register_2(uint16_t reg) { 305 | uint16_t data = uint16_t(reg); 306 | 307 | print(F("unused: ")); 308 | println((data >> 13) & 0x7); 309 | print(F("Temperature Oversampling OSR2: ")); 310 | println((data >> 11) & 3); 311 | print(F("Resolution X Y Z: ")); 312 | print((data >> 5) & 0x3); space(); 313 | print((data >> 7) & 0x3); space(); 314 | println((data >> 9) & 0x3); 315 | print(F("Digital Filtering: ")); 316 | println((data >> 2) & 0x7); 317 | print(F("Oversampling: ")); 318 | println(data & 3); 319 | } 320 | 321 | void register_3(uint16_t reg) { 322 | print(F("Sensitivity drift compensation factor for T > Tref: ")); 323 | println(highByte(reg)); 324 | print(F("Sensitivity drift compensation factor for T < Tref: ")); 325 | println(lowByte(reg)); 326 | } 327 | 328 | void register_4(uint16_t reg) { 329 | print(F("Offset X:")); 330 | println(reg); 331 | } 332 | 333 | void register_5(uint16_t reg) { 334 | print(F("Offset Y:")); 335 | println(reg); 336 | } 337 | 338 | void register_6(uint16_t reg) { 339 | print(F("Offset Z:")); 340 | println(reg); 341 | } 342 | 343 | void register_7(uint16_t reg) { 344 | print(F("WOXY Threshold:")); 345 | println(reg); 346 | } 347 | 348 | void register_8(uint16_t reg) { 349 | print(F("WOZ Threshold:")); 350 | println(reg); 351 | } 352 | 353 | void register_9(uint16_t reg) { 354 | print(F("WOT Threshold:")); 355 | println(reg); 356 | } 357 | 358 | void separator_as_needed(boolean has_predecessor) { 359 | if (has_predecessor and output_mode.style != output_mode_t::style_t::HEX_RAW) { 360 | print(','); 361 | } 362 | }; 363 | 364 | void axis(uint8_t zyxt) { 365 | boolean has_predecessor = false; 366 | 367 | if (zyxt & MLX90393::X_FLAG) { 368 | print('X'); 369 | has_predecessor = true; 370 | } 371 | if (zyxt & MLX90393::Y_FLAG) { 372 | separator_as_needed(has_predecessor); 373 | print('Y'); 374 | has_predecessor = true; 375 | } 376 | if (zyxt & MLX90393::Z_FLAG) { 377 | separator_as_needed(has_predecessor); 378 | print('Z'); 379 | has_predecessor = true; 380 | } 381 | if (zyxt & MLX90393::T_FLAG) { 382 | separator_as_needed(has_predecessor); 383 | print('T'); 384 | has_predecessor = true; 385 | } 386 | if (has_predecessor) { 387 | println(); 388 | } else { 389 | println('-'); 390 | } 391 | } 392 | 393 | void data(uint8_t zyxt, MLX90393::txyzRaw d) { 394 | const uint8_t base = output_mode.style == output_mode_t::DEC_CSV ? 10 : 395 | output_mode.style == output_mode_t::FLOAT_CSV? 0 : 396 | 16; 397 | const MLX90393::txyz data = mlx.convertRaw(d); 398 | 399 | boolean has_predecessor = false; 400 | if (zyxt & MLX90393::X_FLAG) { 401 | if (base) { dump::value(d.x, base); } else { print(data.x); } 402 | has_predecessor = true; 403 | } 404 | if (zyxt & MLX90393::Y_FLAG) { 405 | separator_as_needed(has_predecessor); 406 | if (base) { dump::value(d.y, base); } else { print(data.y); } 407 | has_predecessor = true; 408 | } 409 | if (zyxt & MLX90393::Z_FLAG) { 410 | separator_as_needed(has_predecessor); 411 | if (base) { dump::value(d.z, base); } else { print(data.z); } 412 | has_predecessor = true; 413 | } 414 | if (zyxt & MLX90393::T_FLAG) { 415 | separator_as_needed(has_predecessor); 416 | if (base) { dump::value(d.t, base); } else { print(data.t); } 417 | has_predecessor = true; 418 | } 419 | if (has_predecessor) { 420 | println(); 421 | } else { 422 | println('-'); 423 | } 424 | } 425 | } 426 | 427 | namespace show { 428 | 429 | void status(uint8_t s, boolean newline = true) { 430 | // Notice that newline will only be output IF AND ONLY IF 431 | // status prints output. So this is NOT the same as 432 | // calling status() and then println() 433 | 434 | if (output_mode.verbosity == output_mode_t::QUIET) { return; } 435 | 436 | MLX::status_t status = MLX::status(s); 437 | if (output_mode.verbosity == output_mode_t::VERBOSE || 438 | status.error) { 439 | dump::status(s); 440 | if (newline) { println(); } 441 | } 442 | } 443 | 444 | void registers(uint8_t lo, uint8_t hi, bool dump = true, bool explain = false) { 445 | // dump registers (Datasheet section 9) 446 | 447 | for (uint8_t i=lo; i <= hi and i < 0x40; ++i) { 448 | uint16_t reg; 449 | MLX::status_u status; 450 | status.data = mlx.readRegister(i, reg); 451 | if (!status.flags.error) { 452 | if (dump) { 453 | switch (i) { 454 | case 0: dump::register_0(reg); break; 455 | case 1: dump::register_1(reg); break; 456 | case 2: dump::register_2(reg); break; 457 | case 3: dump::register_3(reg); break; 458 | case 4: dump::register_4(reg); break; 459 | case 5: dump::register_5(reg); break; 460 | case 6: dump::register_6(reg); break; 461 | case 7: dump::register_7(reg); break; 462 | case 8: dump::register_8(reg); break; 463 | case 9: dump::register_9(reg); break; 464 | default: dump::reg(i, reg); 465 | } 466 | // no, we will not dump the status 467 | // this clutters the display to much 468 | } 469 | if (explain) { 470 | switch (i) { 471 | case 0: explain::register_0(reg); break; 472 | case 1: explain::register_1(reg); break; 473 | case 2: explain::register_2(reg); break; 474 | case 3: explain::register_3(reg); break; 475 | case 4: explain::register_4(reg); break; 476 | case 5: explain::register_5(reg); break; 477 | case 6: explain::register_6(reg); break; 478 | case 7: explain::register_7(reg); break; 479 | case 8: explain::register_8(reg); break; 480 | case 9: explain::register_9(reg); break; 481 | default: if (!dump) dump::reg(i, reg); println(); 482 | } 483 | show::status(status.data); 484 | } 485 | } else { 486 | dump::reg(i, reg); 487 | print(F("read failure ")); 488 | show::status(status.data, false); 489 | } 490 | println(); 491 | } 492 | } 493 | 494 | void measurement(uint8_t axis_mask) { 495 | MLX90393::txyzRaw data; 496 | if (output_mode.state == output_mode_t::IDLE) { return; } 497 | 498 | if (output_mode.state == output_mode_t::READ || 499 | output_mode.read_behaviour == output_mode_t::CONTINUOUS || 500 | output_mode.read_behaviour == output_mode_t::AUTO && 501 | output_mode.state != output_mode_t::IDLE && 502 | ( drdy_pin < 0 || 503 | drdy_pin >= 0 && digitalRead(drdy_pin)) 504 | ) { 505 | // attention if WOC is selected AND drdy_pin < 0 and 506 | // float output is selected we have to ensure that this is not going to block 507 | // however this is close to impossible without a drdy pin. 508 | // workaround: if AUTO / WOC is selected withoput drdy pin 509 | // then read the data but only output it if there is no error. 510 | 511 | if (drdy_pin < 0 && 512 | output_mode.read_behaviour == output_mode_t::AUTO && 513 | output_mode.state != output_mode_t::READ) { 514 | delay(mlx.convDelayMillis() ); 515 | } else { 516 | delayMicroseconds(600); 517 | } 518 | 519 | MLX90393::txyzRaw data; 520 | const uint8_t status = mlx.readMeasurement(axis_mask, data); 521 | 522 | if (output_mode.timestamps == 1) { 523 | timer::set_new_ms(); 524 | const uint32_t elapsed = timer::elapsed(); 525 | timer::set_old_ms(); 526 | 527 | print(millis()); 528 | print(F(", ")); 529 | println(elapsed); 530 | } 531 | 532 | show::status(status); 533 | explain::axis(axis_mask & 0x0f); 534 | explain::data(axis_mask, data); 535 | 536 | if (output_mode.state == output_mode_t::READ || 537 | output_mode.state == output_mode_t::START_MEASUREMENT && 538 | output_mode.read_behaviour != output_mode_t::CONTINUOUS) { 539 | output_mode.state = output_mode_t::IDLE; 540 | } 541 | } 542 | } 543 | } 544 | 545 | void help() { 546 | println(); 547 | println(F("H, ?: this help function\n")); 548 | 549 | println(F("D Dump Registers 0x0 - 0x9")); 550 | println(F("Dc Dump Customer Area (0x00 - 0x1F)")); 551 | println(F("Dm Dump Melexis Area (0x20 - 0x3F)")); 552 | println(F("Da All (0x00 - 0x3F)")); 553 | println(F("E Explain Registers 0x0 - 0x9\n")); 554 | 555 | println(F("S [xyzt*] Single Measurement")); 556 | println(F("B [xyzt*] Burst Measurement")); 557 | println(F("W [xyzt*] Wake on Change")); 558 | println(F("R [xyzt*] Read Measurement Result")); 559 | println(F("X eXit Burst or Wake on Change Mode\n")); 560 | 561 | println(F("@ MLX90393 Software Reset")); 562 | println(F(". NOP / read status\n")); 563 | 564 | println(F("$aa=XXxx set register at address aa value XXxx. XX = high byte.\n")); 565 | 566 | println(F("< Memory Recall")); 567 | println(F(">! Memory Store\n")); 568 | 569 | println(F("The following commands will set or get parameters")); 570 | println(F("+G [0-7] Gain")); 571 | println(F("+O [0-3] OverSampling")); 572 | println(F("+T [0-3] Temperature oversampling")); 573 | println(F("+F [0-7] digital Filtering")); 574 | println(F("+N [0-3]{3} resolutioN (xyz)")); 575 | println(F("+C [0-1] temperature Compensation")); 576 | println(F("+H [0-f] Hall configuration")); 577 | println(F("+A [xyzt]* Axis default choice for triggered reads")); 578 | println(F("+E [0-1] External trigger (1 = enable)")); 579 | println(F("+I [0-1] trigger / Interrupt (0 = trigger, 1 = interrupt)\n")); 580 | 581 | println(F("#V [0-2] Output Verbosity")); 582 | println(F("#S [0-3] Output Style HEX_RAW=0, HEX_CSV=1, DEC_CSV=2, FLOAT_CSV=3")); 583 | println(F("#B [0-2] Output Behaviour ON_DEMAND = 0, AUTO = 1, CONTINUOUS = 2\n")); 584 | println(F("#T [0-1] Timestamps\n")); 585 | 586 | println(F("Y [2-F] set and enable DRDY pin, 0 or 1 will disable\n")); 587 | } 588 | 589 | namespace parser { 590 | const char command_separator = ';'; 591 | const char option_separator = ','; 592 | 593 | boolean is_joker(const char c) { 594 | return (c == '*'); 595 | } 596 | 597 | uint8_t parse_joker(const char c) { 598 | return 0xf; 599 | } 600 | 601 | boolean is_quarternary_digit(const char c) { 602 | return (('0' <= c) && (c < '4')); 603 | } 604 | 605 | boolean is_octal_digit(const char c) { 606 | return (('0' <= c) && (c < '8')); 607 | } 608 | 609 | boolean is_decimal_digit(const char c) { 610 | return (('0' <= c) && (c <= '9')); 611 | } 612 | 613 | uint8_t parse_digit(const char c) { 614 | return c - '0'; 615 | } 616 | 617 | boolean is_decimal_digit_or_joker(const char c) { 618 | return (('0' <= c) && (c <= '9')) || (c == '*'); 619 | } 620 | 621 | uint8_t parse_decimal_digit_or_joker(const char c) { 622 | return 623 | is_joker(c) ? parse_joker(c) 624 | : parse_digit(c); 625 | } 626 | 627 | boolean is_hexadecimal_digit(const char c) { 628 | return ((('0' <= c) && (c <= '9')) || 629 | (('a' <= c) && (c <= 'f')) || 630 | (('A' <= c) && (c <= 'F'))); 631 | } 632 | 633 | uint8_t parse_hexadecimal_digit(const char c) { 634 | return 635 | is_decimal_digit(c) ? parse_digit(c) : 636 | ('a' <= c) && (c <= 'f') ? c - 'a' + 10 637 | : c - 'A' + 10; 638 | } 639 | 640 | boolean is_ternary_digit(const char c) { 641 | return (('0' <= c) && (c < '3')); 642 | } 643 | 644 | boolean is_binary_digit(const char c) { 645 | return (('0' <= c) && (c < '2')); 646 | } 647 | 648 | void static_parse(const char c, const char previous_char) { 649 | // static_parse is a co-routine for parsing. 650 | // That is all variables will be declared static 651 | // and the program counter will be stored in 652 | // static void * parser_state. 653 | // The idea is that the YIELD_NEXT_CHAR 654 | // macro can be used to return control to the 655 | // caller while keeping the internal state. 656 | // The caller in turn will push the 657 | // next available character into static_parse 658 | // whenever it has one character ready. 659 | // This is a means of cooperative multi tasking 660 | 661 | // For more background on what is going on you might want to 662 | // follow the two links below. 663 | // https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html 664 | // http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html 665 | 666 | static void * parser_state = &&l_parser_start; 667 | 668 | if (parser_state < &&l_generic_error) { 669 | print(c); 670 | } 671 | 672 | if (c == ' ') { 673 | return; 674 | } 675 | 676 | if (c == command_separator) { 677 | println(); 678 | } 679 | 680 | goto *parser_state; 681 | #define LABEL(N) label_ ## N 682 | #define XLABEL(N) LABEL(N) 683 | #define YIELD_NEXT_CHAR \ 684 | do { \ 685 | parser_state = &&XLABEL(__LINE__); return; XLABEL(__LINE__):; \ 686 | } while (0) 687 | 688 | l_parser_start: ; 689 | 690 | if (c == ' ') { 691 | // ignore leading space 692 | YIELD_NEXT_CHAR; 693 | } 694 | 695 | uint8_t zyxt = 0; 696 | static bool start_detetermine_axis; 697 | switch (c) { 698 | case '?': 699 | case 'h': { 700 | help(); 701 | // ignore all characters till command end 702 | while (c != command_separator) { 703 | YIELD_NEXT_CHAR; 704 | } 705 | goto l_done; 706 | } 707 | 708 | case 'd': { 709 | YIELD_NEXT_CHAR; 710 | static uint8_t dump_lo; 711 | static uint8_t dump_hi; 712 | switch (c) { 713 | case 'c': dump_lo = 0x00; dump_hi = 0x1F; YIELD_NEXT_CHAR; break; 714 | case 'm': dump_lo = 0x20; dump_hi = 0x3F; YIELD_NEXT_CHAR; break; 715 | case 'a': dump_lo = 0x00; dump_hi = 0x3F; YIELD_NEXT_CHAR; break; 716 | case command_separator: dump_lo = 0x00; dump_hi = 0x9; break; 717 | default: goto l_unknown_command; 718 | } 719 | if (c != command_separator) goto l_separator_error; 720 | show::registers(dump_lo, dump_hi); 721 | goto l_done; 722 | } 723 | 724 | case 'e': { 725 | YIELD_NEXT_CHAR; 726 | if (c != command_separator) goto l_separator_error; 727 | show::registers(0, 9, false, true); 728 | goto l_done; 729 | } 730 | 731 | case '@': { 732 | YIELD_NEXT_CHAR; 733 | if (c != command_separator) goto l_separator_error; 734 | println(F("Software reset")); 735 | explain::status(mlx.reset()); 736 | output_mode.state = output_mode_t::IDLE; 737 | goto l_done; 738 | } 739 | 740 | case '.': { 741 | YIELD_NEXT_CHAR; 742 | if (c != command_separator) goto l_separator_error; 743 | println(F("nop")); 744 | explain::status(mlx.nop()); 745 | goto l_done; 746 | } 747 | 748 | case 'x': { 749 | YIELD_NEXT_CHAR; 750 | if (c != command_separator) goto l_separator_error; 751 | println(F("exit mode")); 752 | explain::status(mlx.exit()); 753 | output_mode.state = output_mode_t::IDLE; 754 | goto l_done; 755 | } 756 | 757 | case '<': { 758 | YIELD_NEXT_CHAR; 759 | if (c != command_separator) goto l_separator_error; 760 | println(F("Memory Recall")); 761 | show::status(mlx.memoryRecall()); 762 | goto l_done; 763 | } 764 | 765 | case '>': { 766 | YIELD_NEXT_CHAR; 767 | if (c != '!') goto l_unknown_command; 768 | YIELD_NEXT_CHAR; 769 | if (c != command_separator) goto l_separator_error; 770 | println(F("Memory Store")); 771 | show::status(mlx.memoryStore()); 772 | goto l_done; 773 | } 774 | 775 | static uint8_t adr; 776 | static uint16_t val; 777 | case '$': { 778 | YIELD_NEXT_CHAR; // consume first address nibble 779 | if (!is_hexadecimal_digit(c)) goto l_hexadecimal_digit_expected; 780 | YIELD_NEXT_CHAR; // consume second, address nible or = or ; 781 | if (is_hexadecimal_digit(c)) { 782 | adr = ((parse_hexadecimal_digit(previous_char))<<4) | 783 | parse_hexadecimal_digit(c); 784 | if (adr > 0x3f) goto l_address_out_of_range; 785 | YIELD_NEXT_CHAR; // consume = or ; 786 | if (c == command_separator) { 787 | goto l_read_register; 788 | } else if (c != '=') { 789 | goto l_assignment_expected; 790 | } 791 | } else { // single digit case, treat = or ; 792 | adr = parse_hexadecimal_digit(previous_char); 793 | if (c == command_separator) { 794 | goto l_read_register; 795 | } else if (c != '=') { 796 | goto l_hexadecimal_digit_expected; 797 | } 798 | } 799 | // writing to MLX range is not supported by the chip, 800 | // but maybe we want to try anyway 801 | if (adr > 0x1f) goto l_address_out_of_range; 802 | YIELD_NEXT_CHAR; // consume highest value nibble 803 | val = 0; 804 | if (!is_hexadecimal_digit(c)) 805 | if (c == command_separator) goto l_write_register; 806 | else goto l_hexadecimal_digit_expected; 807 | val = parse_hexadecimal_digit(c); 808 | YIELD_NEXT_CHAR; 809 | if (!is_hexadecimal_digit(c)) 810 | if (c == command_separator) goto l_write_register; 811 | else goto l_hexadecimal_digit_expected; 812 | val = (val << 4) | parse_hexadecimal_digit(c); 813 | YIELD_NEXT_CHAR; 814 | if (!is_hexadecimal_digit(c)) 815 | if (c == command_separator) goto l_write_register; 816 | else goto l_hexadecimal_digit_expected; 817 | val = (val << 4) | parse_hexadecimal_digit(c); 818 | YIELD_NEXT_CHAR; 819 | if (!is_hexadecimal_digit(c)) 820 | if (c == command_separator) goto l_write_register; 821 | else goto l_hexadecimal_digit_expected; 822 | val = (val << 4) | parse_hexadecimal_digit(c); 823 | YIELD_NEXT_CHAR; 824 | if (c != command_separator) goto l_separator_error; 825 | 826 | l_write_register: 827 | show::status(mlx.writeRegister(adr, val)); 828 | goto l_done; 829 | 830 | l_read_register: 831 | show::status(mlx.readRegister(adr, val)); 832 | dump::reg(adr, val); 833 | goto l_done; 834 | } 835 | 836 | case 'y': { 837 | YIELD_NEXT_CHAR; 838 | if (c == command_separator) { 839 | print(F("DRDY pin: ")); 840 | println(drdy_pin); 841 | goto l_done; 842 | } 843 | if (!is_hexadecimal_digit(c)) goto l_hexadecimal_digit_expected; 844 | YIELD_NEXT_CHAR; 845 | if (c != command_separator) goto l_separator_error; 846 | drdy_pin = parse_hexadecimal_digit(previous_char); 847 | if (drdy_pin < 2) { drdy_pin = -1; } 848 | mlx.begin(0, 0, drdy_pin); 849 | print(F("DRDY pin: ")); 850 | println(drdy_pin, 16); 851 | println(F("\n ... reset ...")); 852 | show::status(mlx.reset()); 853 | println(); 854 | show::registers(0, 9); 855 | goto l_done; 856 | } 857 | 858 | case '+': { 859 | YIELD_NEXT_CHAR; 860 | switch(c) { 861 | case 'g': { 862 | YIELD_NEXT_CHAR; 863 | if (c==command_separator) { 864 | print(F("Gain: ")); 865 | uint8_t val; 866 | show::status(mlx.getGainSel(val)); 867 | println(val); 868 | } else { 869 | if (!is_octal_digit(c)) goto l_octal_digit_expected; 870 | YIELD_NEXT_CHAR; 871 | if (c != command_separator) goto l_separator_error; 872 | print(F("Gain: ")); 873 | println(previous_char); 874 | show::status(mlx.setGainSel(parse_digit(previous_char))); 875 | } 876 | goto l_done; 877 | } 878 | 879 | case 'o': { 880 | YIELD_NEXT_CHAR; 881 | if (c==command_separator) { 882 | print(F("Oversampling: ")); 883 | uint8_t val; 884 | show::status(mlx.getOverSampling(val)); 885 | println(val); 886 | } else { 887 | if (!is_quarternary_digit(c)) goto l_quarternary_digit_expected; 888 | YIELD_NEXT_CHAR; 889 | if (c != command_separator) goto l_separator_error; 890 | print(F("Oversampling: ")); 891 | println(previous_char); 892 | show::status(mlx.setOverSampling(parse_digit(previous_char))); 893 | } 894 | goto l_done; 895 | } 896 | 897 | case 't': { 898 | YIELD_NEXT_CHAR; 899 | if (c==command_separator) { 900 | print(F("Temperature oversampling: ")); 901 | uint8_t val; 902 | show::status(mlx.getTemperatureOverSampling(val)); 903 | println(val); 904 | } else { 905 | if (!is_quarternary_digit(c)) goto l_quarternary_digit_expected; 906 | YIELD_NEXT_CHAR; 907 | if (c != command_separator) goto l_separator_error; 908 | print(F("Temperature oversampling: ")); 909 | println(previous_char); 910 | show::status(mlx.setTemperatureOverSampling(parse_digit(previous_char))); 911 | } 912 | goto l_done; 913 | } 914 | 915 | case 'f': { 916 | YIELD_NEXT_CHAR; 917 | if (c==command_separator) { 918 | print(F("digital Filtering: ")); 919 | uint8_t val; 920 | show::status(mlx.getDigitalFiltering(val)); 921 | println(val); 922 | } else { 923 | if (!is_octal_digit(c)) goto l_octal_digit_expected; 924 | YIELD_NEXT_CHAR; 925 | if (c != command_separator) goto l_separator_error; 926 | print(F("digital Filtering: ")); 927 | println(previous_char); 928 | show::status(mlx.setDigitalFiltering(parse_digit(previous_char))); 929 | } 930 | goto l_done; 931 | } 932 | 933 | case 'n': { 934 | uint8_t x; 935 | uint8_t y; 936 | uint8_t z; 937 | 938 | YIELD_NEXT_CHAR; 939 | if (c == command_separator) { 940 | show::status(mlx.getResolution(x, y, z)); 941 | } else { 942 | static uint8_t val; 943 | if (!is_quarternary_digit(c)) goto l_quarternary_digit_expected; 944 | val = parse_digit(c); 945 | YIELD_NEXT_CHAR; 946 | if (!is_quarternary_digit(c)) goto l_quarternary_digit_expected; 947 | val = (val<<2) | parse_digit(c); 948 | YIELD_NEXT_CHAR; 949 | if (!is_quarternary_digit(c)) goto l_quarternary_digit_expected; 950 | val = (val<<2) | parse_digit(c); 951 | YIELD_NEXT_CHAR; 952 | if (c != command_separator) goto l_separator_error; 953 | 954 | z = val & 0x3; 955 | y = (val >> 2) & 0x3; 956 | x = (val >> 4) & 0x3; 957 | show::status(mlx.setResolution(x, y, z)); 958 | } 959 | print(F("resolutionN: ")); 960 | print(x); space(); 961 | print(y); space(); 962 | println(z); 963 | 964 | goto l_done; 965 | } 966 | 967 | case 'c': { 968 | YIELD_NEXT_CHAR; 969 | if (c==command_separator) { 970 | print(F("Temperature Compensation: ")); 971 | uint8_t enabled; 972 | show::status(mlx.getTemperatureCompensation(enabled)); 973 | println(enabled); 974 | } else { 975 | if (!is_binary_digit(c)) goto l_binary_digit_expected; 976 | YIELD_NEXT_CHAR; 977 | if (c != command_separator) goto l_separator_error; 978 | print(F("Temperature Compensation: ")); 979 | println(previous_char); 980 | show::status(mlx.setTemperatureCompensation(parse_digit(previous_char))); 981 | } 982 | goto l_done; 983 | } 984 | 985 | case 'h': { 986 | YIELD_NEXT_CHAR; 987 | if (c==command_separator) { 988 | print(F("Hall configuration: ")); 989 | uint8_t hallconf; 990 | show::status(mlx.getHallConf(hallconf)); 991 | println(hallconf,16); 992 | } else { 993 | if (!is_hexadecimal_digit(c)) goto l_hexadecimal_digit_expected; 994 | YIELD_NEXT_CHAR; 995 | if (c != command_separator) goto l_separator_error; 996 | print(F("Hall configuration: ")); 997 | println(previous_char); 998 | show::status(mlx.setHallConf(parse_hexadecimal_digit(previous_char))); 999 | } 1000 | goto l_done; 1001 | } 1002 | 1003 | case 'a': { 1004 | static uint8_t burst_sel; 1005 | YIELD_NEXT_CHAR; 1006 | if (c==command_separator) { 1007 | uint8_t burst_sel; 1008 | print(F("Axis default: ")); 1009 | show::status(mlx.getBurstSel(burst_sel)); 1010 | explain::axis(burst_sel); 1011 | println(); 1012 | } else { 1013 | burst_sel = 0; 1014 | goto l_det_axis_start; 1015 | l_det_axis: 1016 | YIELD_NEXT_CHAR; 1017 | l_det_axis_start: 1018 | if (start_detetermine_axis and (c != command_separator)) { 1019 | burst_sel = 0; 1020 | } 1021 | start_detetermine_axis = false; 1022 | switch (c) { 1023 | case 'x': burst_sel |= MLX90393::X_FLAG; goto l_det_axis; 1024 | case 'y': burst_sel |= MLX90393::Y_FLAG; goto l_det_axis; 1025 | case 'z': burst_sel |= MLX90393::Z_FLAG; goto l_det_axis; 1026 | case 't': burst_sel |= MLX90393::T_FLAG; goto l_det_axis; 1027 | case '*': burst_sel |= 0xf; goto l_det_axis; 1028 | case command_separator: break; 1029 | default: goto l_zyxt_expected; 1030 | } 1031 | print(F("Axis default: ")); 1032 | explain::axis(burst_sel); 1033 | show::status(mlx.setBurstSel(burst_sel)); 1034 | } 1035 | goto l_done; 1036 | } 1037 | 1038 | case 'e': { 1039 | YIELD_NEXT_CHAR; 1040 | if (c==command_separator) { 1041 | print(F("External trigger: ")); 1042 | uint8_t ext_trig; 1043 | show::status(mlx.getExtTrig(ext_trig)); 1044 | println(ext_trig); 1045 | } else { 1046 | if (!is_binary_digit(c)) goto l_binary_digit_expected; 1047 | YIELD_NEXT_CHAR; 1048 | if (c != command_separator) goto l_separator_error; 1049 | print(F("External trigger: ")); 1050 | println(previous_char); 1051 | show::status(mlx.setExtTrig(parse_digit(previous_char))); 1052 | } 1053 | goto l_done; 1054 | } 1055 | 1056 | case 'i': { 1057 | YIELD_NEXT_CHAR; 1058 | if (c==command_separator) { 1059 | print(F("trigger / Interrupt: ")); 1060 | uint8_t trig_int_sel; 1061 | show::status(mlx.getTrigIntSel(trig_int_sel)); 1062 | println(trig_int_sel); 1063 | } else { 1064 | if (!is_binary_digit(c)) goto l_binary_digit_expected; 1065 | YIELD_NEXT_CHAR; 1066 | if (c != command_separator) goto l_separator_error; 1067 | print(F("trigger / Interrupt: ")); 1068 | println(previous_char); 1069 | show::status(mlx.setTrigIntSel(parse_digit(previous_char))); 1070 | } 1071 | goto l_done; 1072 | } 1073 | 1074 | } 1075 | goto l_unknown_command; 1076 | } 1077 | 1078 | case '#': { 1079 | YIELD_NEXT_CHAR; 1080 | switch (c) { 1081 | case 'v': { 1082 | YIELD_NEXT_CHAR; 1083 | if (c == command_separator) goto l_output_verbosity; 1084 | if (!is_ternary_digit(c)) goto l_ternary_digit_expected; 1085 | YIELD_NEXT_CHAR; 1086 | if (c != command_separator) goto l_separator_error; 1087 | output_mode.verbosity = parse_digit(previous_char); 1088 | l_output_verbosity: 1089 | print(F("output Verbosity: ")); 1090 | println(output_mode.verbosity); 1091 | break; 1092 | } 1093 | case 's': { 1094 | YIELD_NEXT_CHAR; 1095 | if (c == command_separator) goto l_output_style; 1096 | if (!is_quarternary_digit(c)) goto l_quarternary_digit_expected; 1097 | YIELD_NEXT_CHAR; 1098 | if (c != command_separator) goto l_separator_error; 1099 | output_mode.style = parse_digit(previous_char); 1100 | l_output_style: 1101 | print(F("output Style: ")); 1102 | println(output_mode.style); 1103 | break; 1104 | } 1105 | case 'b': { 1106 | YIELD_NEXT_CHAR; 1107 | if (c == command_separator) goto l_output_behaviour; 1108 | if (!is_ternary_digit(c)) goto l_ternary_digit_expected; 1109 | YIELD_NEXT_CHAR; 1110 | if (c != command_separator) goto l_separator_error; 1111 | output_mode.read_behaviour = parse_digit(previous_char); 1112 | l_output_behaviour: 1113 | print(F("output Behaviour: ")); 1114 | println(output_mode.read_behaviour); 1115 | break; 1116 | } 1117 | case 't': { 1118 | YIELD_NEXT_CHAR; 1119 | if (c == command_separator) goto l_output_timestamps; 1120 | if (!is_binary_digit(c)) goto l_binary_digit_expected; 1121 | YIELD_NEXT_CHAR; 1122 | if (c != command_separator) goto l_separator_error; 1123 | output_mode.timestamps = parse_digit(previous_char); 1124 | l_output_timestamps: 1125 | print(F("Timestamps: ")); 1126 | println(output_mode.timestamps); 1127 | break; 1128 | } 1129 | default: goto l_unknown_command; 1130 | } 1131 | goto l_done; 1132 | } 1133 | 1134 | static uint8_t cmd; 1135 | case 'r': cmd = MLX90393::CMD_READ_MEASUREMENT; goto l_start_determine_axis; 1136 | case 'w': cmd = MLX90393::CMD_WAKE_ON_CHANGE; goto l_start_determine_axis; 1137 | case 'b': cmd = MLX90393::CMD_START_BURST; goto l_start_determine_axis; 1138 | case 's': cmd = MLX90393::CMD_START_MEASUREMENT; goto l_start_determine_axis; 1139 | 1140 | l_start_determine_axis: 1141 | start_detetermine_axis = true; 1142 | l_determine_axis: // default: start_detetermine_axis = false; 1143 | YIELD_NEXT_CHAR; 1144 | if (start_detetermine_axis and (c != command_separator)) { 1145 | // wipe outdated axis information 1146 | cmd &= 0xf0; 1147 | } 1148 | start_detetermine_axis = false; 1149 | switch (c) { 1150 | case 'x': zyxt = MLX90393::X_FLAG; break; 1151 | case 'y': zyxt = MLX90393::Y_FLAG; break; 1152 | case 'z': zyxt = MLX90393::Z_FLAG; break; 1153 | case 't': zyxt = MLX90393::T_FLAG; break; 1154 | case '*': zyxt = 0xf; break; 1155 | case command_separator: { 1156 | if ((cmd & 0xf0) != MLX90393::CMD_READ_MEASUREMENT) { 1157 | show::status(mlx.sendCommand(cmd)); 1158 | } 1159 | 1160 | timer::set_old_ms(); 1161 | switch (cmd & 0xf0) { 1162 | case MLX90393::CMD_READ_MEASUREMENT: 1163 | output_mode.state = output_mode_t::READ; 1164 | break; 1165 | case MLX90393::CMD_START_MEASUREMENT: 1166 | output_mode.state = output_mode_t::START_MEASUREMENT; 1167 | break; 1168 | case MLX90393::CMD_START_BURST: 1169 | output_mode.state = output_mode_t::BURST; 1170 | break; 1171 | case MLX90393::CMD_WAKE_ON_CHANGE: 1172 | output_mode.state = output_mode_t::WAKE_ON_CHANGE; 1173 | break; 1174 | } 1175 | 1176 | axis_mask = cmd & 0x0f; 1177 | show::measurement(axis_mask); 1178 | goto l_done; 1179 | } 1180 | default: goto l_zyxt_expected; 1181 | } 1182 | 1183 | cmd |= zyxt; 1184 | goto l_determine_axis; 1185 | 1186 | default: goto l_unknown_command; 1187 | } 1188 | 1189 | l_unknown_command: 1190 | print(F("\n Unknown command")); 1191 | goto l_generic_error; 1192 | 1193 | l_address_out_of_range: 1194 | print(F("\n Address out of range")); 1195 | goto l_generic_error; 1196 | 1197 | l_value_out_of_range: 1198 | print(F("\n Value out of range")); 1199 | goto l_generic_error; 1200 | 1201 | l_assignment_expected: 1202 | print(F(" \n Assignment '=' expected")); 1203 | goto l_generic_error; 1204 | 1205 | l_zyxt_expected: 1206 | print(F("\nAxis [txyz] or * expected")); 1207 | goto l_generic_error; 1208 | 1209 | l_digit_or_joker_expected: 1210 | print(F("\n Digit or * expected")); 1211 | goto l_generic_error; 1212 | 1213 | l_digit_expected: 1214 | print(F("\n Digit expected")); 1215 | goto l_generic_error; 1216 | 1217 | l_octal_digit_expected: 1218 | print(F("\n Octal digit expected")); 1219 | goto l_generic_error; 1220 | 1221 | l_quarternary_digit_expected: 1222 | print(F("\n Quarternary digit expected")); 1223 | goto l_generic_error; 1224 | 1225 | l_ternary_digit_expected: 1226 | print(F("\n Quarternary digit expected")); 1227 | goto l_generic_error; 1228 | 1229 | l_binary_digit_expected: 1230 | print(F("\n Binary digit expected")); 1231 | goto l_generic_error; 1232 | 1233 | l_hexadecimal_digit_expected: 1234 | print(F("\n Hexadecimal digit expected")); 1235 | goto l_generic_error; 1236 | 1237 | l_separator_error: 1238 | print(F("\n Unexpected or missing separator")); 1239 | goto l_generic_error; 1240 | 1241 | l_out_of_range_error: 1242 | print(F("\n Digit out of range error")); 1243 | goto l_generic_error; 1244 | 1245 | l_syntax_error: 1246 | println(); 1247 | goto l_generic_error; 1248 | 1249 | l_generic_error: 1250 | 1251 | print(F(" error at '")); 1252 | print(c); 1253 | if (c== command_separator) { 1254 | println('\''); 1255 | } else { 1256 | print(F("' before ")); 1257 | do { 1258 | YIELD_NEXT_CHAR; 1259 | print(c); 1260 | } while (c != command_separator); 1261 | 1262 | println(); 1263 | } 1264 | println(); 1265 | println(F("Use h for help!")); 1266 | goto l_done; 1267 | 1268 | l_done: 1269 | println(); 1270 | parser_state = &&l_parser_start; 1271 | return; 1272 | } 1273 | 1274 | 1275 | boolean parse() { // deliver true if some character was available 1276 | static char previous_char; 1277 | if (Serial.available()) { 1278 | char c = (char) Serial.read(); 1279 | 1280 | // interpret tab as whitespace 1281 | if (c == 0x09) { 1282 | c = ' '; 1283 | } 1284 | 1285 | // map newline and carriage return to command separator 1286 | if (c==0x0A || c==0x0D) { 1287 | c = command_separator; 1288 | } 1289 | 1290 | // ignore successive separators 1291 | if ((c == option_separator || c == command_separator) && (c == previous_char)) { 1292 | return true; 1293 | } 1294 | 1295 | // map everything to lower case 1296 | if (('A' <= c) && (c <= 'Z')) { 1297 | c+= 'a' - 'A'; 1298 | } 1299 | 1300 | static_parse(c, previous_char); 1301 | if (c != ' ') { 1302 | previous_char = c; 1303 | } 1304 | 1305 | return true; 1306 | } 1307 | return false; 1308 | } 1309 | } 1310 | 1311 | void boilerplate() { 1312 | println(F("\nMelexis 90393 debug helper")); 1313 | println(F("\n(C) Udo Klein 2018")); 1314 | println(F("License: GPL v3\n")); 1315 | 1316 | //help(); 1317 | 1318 | println(); 1319 | explain::status(mlx.nop()); 1320 | println(); 1321 | show::registers(0, 9); 1322 | 1323 | println(F("\n\nUse h for help!")); 1324 | } 1325 | 1326 | 1327 | void setup(){ 1328 | // I2C 1329 | // Arduino A4 = SDA 1330 | // Arduino A5 = SCL 1331 | // DRDY ("Data Ready"line connected to A3 (omit third parameter to used timed reads) 1332 | // uint8_t status = mlx.begin(0, 0, A3); 1333 | uint8_t status = mlx.begin(0, 0); 1334 | Serial.begin(57600); 1335 | boilerplate(); 1336 | } 1337 | 1338 | 1339 | void loop() { 1340 | while (parser::parse()) { 1341 | /* consume all available input characters */ 1342 | } 1343 | 1344 | show::measurement(axis_mask); 1345 | } 1346 | --------------------------------------------------------------------------------