51 52 with same code
Dependencies: MtSense07
Fork of MtConnect04S_MtSense07 by
main.cpp@4:4ca64a863fbf, 2018-04-27 (annotated)
- Committer:
- johnathanlyu
- Date:
- Fri Apr 27 10:00:21 2018 +0000
- Revision:
- 4:4ca64a863fbf
- Parent:
- 2:87f1b98c0d09
51 52 with same code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcc6 | 0:418880413158 | 1 | /* Copyright (c) 2016 MtM Technology Corporation, MIT License |
bcc6 | 0:418880413158 | 2 | * |
bcc6 | 0:418880413158 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
bcc6 | 0:418880413158 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
bcc6 | 0:418880413158 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
bcc6 | 0:418880413158 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
bcc6 | 0:418880413158 | 7 | * furnished to do so, subject to the following conditions: |
bcc6 | 0:418880413158 | 8 | * |
bcc6 | 0:418880413158 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
bcc6 | 0:418880413158 | 10 | * substantial portions of the Software. |
bcc6 | 0:418880413158 | 11 | * |
bcc6 | 0:418880413158 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
bcc6 | 0:418880413158 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
bcc6 | 0:418880413158 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
bcc6 | 0:418880413158 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
bcc6 | 0:418880413158 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
bcc6 | 0:418880413158 | 17 | */ |
bcc6 | 0:418880413158 | 18 | #include "mbed.h" |
bcc6 | 0:418880413158 | 19 | #include "AK9750.h" |
bcc6 | 0:418880413158 | 20 | #include "AK09912.h" |
bcc6 | 0:418880413158 | 21 | #include "AK09970.h" |
bcc6 | 1:40c18f027e6c | 22 | #include "config.h" |
bcc6 | 1:40c18f027e6c | 23 | |
johnathanlyu | 4:4ca64a863fbf | 24 | I2C i2c(p3, p2); |
johnathanlyu | 4:4ca64a863fbf | 25 | |
johnathanlyu | 4:4ca64a863fbf | 26 | #ifdef NRF52 |
johnathanlyu | 4:4ca64a863fbf | 27 | Serial pc(p18, p16); |
johnathanlyu | 4:4ca64a863fbf | 28 | DigitalOut reset_sensor(p24, 1); // Reset pin for AK09970 (Low active) |
johnathanlyu | 4:4ca64a863fbf | 29 | AK9750 ak9750(i2c, p5); |
johnathanlyu | 4:4ca64a863fbf | 30 | #else |
bcc6 | 0:418880413158 | 31 | Serial pc(p15, p16); |
bcc6 | 0:418880413158 | 32 | DigitalOut reset_sensor(p4, 1); // Reset pin for AK09970 (Low active) |
bcc6 | 2:87f1b98c0d09 | 33 | AK9750 ak9750(i2c, p0); |
johnathanlyu | 4:4ca64a863fbf | 34 | #endif |
johnathanlyu | 4:4ca64a863fbf | 35 | |
bcc6 | 0:418880413158 | 36 | AK09912 ak09912(i2c); |
bcc6 | 0:418880413158 | 37 | AK09970 ak09970(i2c); |
bcc6 | 0:418880413158 | 38 | |
bcc6 | 2:87f1b98c0d09 | 39 | volatile bool isIntEvent = false; |
johnathanlyu | 4:4ca64a863fbf | 40 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
johnathanlyu | 4:4ca64a863fbf | 41 | int32_t count = 0; |
bcc6 | 2:87f1b98c0d09 | 42 | |
bcc6 | 2:87f1b98c0d09 | 43 | //============================================================================== |
bcc6 | 2:87f1b98c0d09 | 44 | static void IntEventCallback() { |
bcc6 | 2:87f1b98c0d09 | 45 | isIntEvent = true; |
bcc6 | 2:87f1b98c0d09 | 46 | } |
johnathanlyu | 4:4ca64a863fbf | 47 | void readSensorCallback() { |
johnathanlyu | 4:4ca64a863fbf | 48 | #if (CONFIG_RUN_MODE == RUN_ALL_SENSOR_COM) |
johnathanlyu | 4:4ca64a863fbf | 49 | AK9750::Data ak9750_data; |
johnathanlyu | 4:4ca64a863fbf | 50 | AK09912::Data ak09912_data; |
johnathanlyu | 4:4ca64a863fbf | 51 | AK09970::Data ak09970_data; |
johnathanlyu | 4:4ca64a863fbf | 52 | int32_t triggered_area; |
johnathanlyu | 4:4ca64a863fbf | 53 | ak9750.GetData(&ak9750_data); |
johnathanlyu | 4:4ca64a863fbf | 54 | triggered_area = ak9750.GetTriggeredAreaNum(&ak9750_data); |
johnathanlyu | 4:4ca64a863fbf | 55 | pc.printf("AK9750(%5.1fpA, %5.1fpA, %5.1fpA, %5.1fpA, %2.1f'C, Area%d)\n", |
johnathanlyu | 4:4ca64a863fbf | 56 | ak9750_data.ir1, ak9750_data.ir2, ak9750_data.ir3, ak9750_data.ir4, ak9750_data.tmp, triggered_area); |
bcc6 | 2:87f1b98c0d09 | 57 | |
johnathanlyu | 4:4ca64a863fbf | 58 | ak09912.GetData(&ak09912_data); |
johnathanlyu | 4:4ca64a863fbf | 59 | pc.printf("AK09912(%5.1fuT/LSB, %5.1fuT/LSB, %5.1fuT/LSB, %2.1f'C)\n", |
johnathanlyu | 4:4ca64a863fbf | 60 | ak09912_data.x, ak09912_data.y, ak09912_data.z, ak09912_data.t); |
johnathanlyu | 4:4ca64a863fbf | 61 | |
johnathanlyu | 4:4ca64a863fbf | 62 | ak09970.GetData(&ak09970_data); |
johnathanlyu | 4:4ca64a863fbf | 63 | pc.printf("AK09970(%6.1fuT/LSB, %6.1fuT/LSB, %6.1fuT/LSB)\n", |
johnathanlyu | 4:4ca64a863fbf | 64 | ak09970_data.x, ak09970_data.y, ak09970_data.z); |
johnathanlyu | 4:4ca64a863fbf | 65 | |
johnathanlyu | 4:4ca64a863fbf | 66 | pc.printf("\n"); |
johnathanlyu | 4:4ca64a863fbf | 67 | #elif (CONFIG_RUN_MODE == RUN_IR_AK9750_APP) |
johnathanlyu | 4:4ca64a863fbf | 68 | AK9750::Data ak9750_data; |
johnathanlyu | 4:4ca64a863fbf | 69 | ak9750.GetData(&ak9750_data); |
johnathanlyu | 4:4ca64a863fbf | 70 | pc.printf(" 3:%d,%d,%d,%d,%d,\r", count, (int32_t)ak9750_data.ir1, (int32_t)ak9750_data.ir2, (int32_t)ak9750_data.ir3, (int32_t)ak9750_data.ir4); |
johnathanlyu | 4:4ca64a863fbf | 71 | count++; |
johnathanlyu | 4:4ca64a863fbf | 72 | |
johnathanlyu | 4:4ca64a863fbf | 73 | #elif (CONFIG_RUN_MODE == RUN_COMPASS_AK09912_APP) |
johnathanlyu | 4:4ca64a863fbf | 74 | AK09912::Data ak09912_data; |
johnathanlyu | 4:4ca64a863fbf | 75 | ak09912.GetData(&ak09912_data); |
johnathanlyu | 4:4ca64a863fbf | 76 | pc.printf("C:%d,%d,%d,%d\r", count, (int32_t)ak09912_data.x, (int32_t)ak09912_data.y, (int32_t)ak09912_data.z); |
johnathanlyu | 4:4ca64a863fbf | 77 | count++; |
johnathanlyu | 4:4ca64a863fbf | 78 | |
johnathanlyu | 4:4ca64a863fbf | 79 | #elif (CONFIG_RUN_MODE == RUN_MAGNETIC_AK09970_APP) |
johnathanlyu | 4:4ca64a863fbf | 80 | AK09970::Data ak09970_data; |
johnathanlyu | 4:4ca64a863fbf | 81 | ak09970.GetData(&ak09970_data); |
johnathanlyu | 4:4ca64a863fbf | 82 | pc.printf("C:%d,%d,%d,%d\r", count, (int32_t)ak09970_data.x, (int32_t)ak09970_data.y, (int32_t)ak09970_data.z); |
johnathanlyu | 4:4ca64a863fbf | 83 | count++; |
johnathanlyu | 4:4ca64a863fbf | 84 | #endif |
johnathanlyu | 4:4ca64a863fbf | 85 | } |
bcc6 | 2:87f1b98c0d09 | 86 | //============================================================================== |
bcc6 | 0:418880413158 | 87 | int main() { |
bcc6 | 0:418880413158 | 88 | /* Disable the hardware flow control of Serial, then show the mbed version */ |
bcc6 | 1:40c18f027e6c | 89 | pc.set_flow_control(SerialBase::Disabled); |
bcc6 | 1:40c18f027e6c | 90 | pc.baud(CONFIG_BAUDRATE); |
bcc6 | 0:418880413158 | 91 | pc.printf("\n"); |
bcc6 | 0:418880413158 | 92 | pc.printf("mbed version(%d.%d.%d)\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
bcc6 | 0:418880413158 | 93 | pc.printf("\n"); |
bcc6 | 0:418880413158 | 94 | |
bcc6 | 0:418880413158 | 95 | /* Reset sensor */ |
bcc6 | 0:418880413158 | 96 | reset_sensor = 1; |
bcc6 | 0:418880413158 | 97 | wait_ms(10); |
bcc6 | 0:418880413158 | 98 | reset_sensor = 0; |
bcc6 | 0:418880413158 | 99 | wait_ms(10); |
bcc6 | 0:418880413158 | 100 | reset_sensor = 1; |
bcc6 | 0:418880413158 | 101 | wait_ms(10); |
bcc6 | 0:418880413158 | 102 | |
bcc6 | 0:418880413158 | 103 | /* Config device and check device ID */ |
bcc6 | 0:418880413158 | 104 | uint8_t id; |
bcc6 | 0:418880413158 | 105 | |
bcc6 | 0:418880413158 | 106 | ak9750.ConfigDevice(); |
bcc6 | 0:418880413158 | 107 | ak9750.GetDeviceID(&id); |
bcc6 | 0:418880413158 | 108 | pc.printf("AK9750_DEVICE_ID(0x%02X)\n", id); |
bcc6 | 0:418880413158 | 109 | if(id != ak9750.DEVICE_ID){ |
bcc6 | 0:418880413158 | 110 | pc.printf("Read Device ID fail !\n"); |
bcc6 | 0:418880413158 | 111 | while(1); |
bcc6 | 0:418880413158 | 112 | } |
bcc6 | 0:418880413158 | 113 | |
bcc6 | 0:418880413158 | 114 | ak09912.ConfigDevice(); |
bcc6 | 0:418880413158 | 115 | ak09912.GetDeviceID(&id); |
bcc6 | 0:418880413158 | 116 | pc.printf("AK09912_DEVICE_ID(0x%02X)\n", id); |
bcc6 | 0:418880413158 | 117 | if(id != ak09912.DEVICE_ID){ |
bcc6 | 0:418880413158 | 118 | pc.printf("Read Device ID fail !\n"); |
bcc6 | 0:418880413158 | 119 | while(1); |
bcc6 | 0:418880413158 | 120 | } |
bcc6 | 0:418880413158 | 121 | |
bcc6 | 0:418880413158 | 122 | ak09970.ConfigDevice(); |
bcc6 | 0:418880413158 | 123 | ak09970.GetDeviceID(&id); |
bcc6 | 0:418880413158 | 124 | pc.printf("AK09970_DEVICE_ID(0x%02X)\n", id); |
bcc6 | 0:418880413158 | 125 | if(id != ak09970.DEVICE_ID){ |
bcc6 | 0:418880413158 | 126 | pc.printf("Read Device ID fail !\n"); |
bcc6 | 0:418880413158 | 127 | while(1); |
bcc6 | 0:418880413158 | 128 | } |
bcc6 | 0:418880413158 | 129 | |
bcc6 | 2:87f1b98c0d09 | 130 | |
bcc6 | 0:418880413158 | 131 | /* Main loop */ |
bcc6 | 1:40c18f027e6c | 132 | #if (CONFIG_RUN_MODE == RUN_ALL_SENSOR_COM) |
johnathanlyu | 4:4ca64a863fbf | 133 | eventQueue.call_every(200, readSensorCallback); |
johnathanlyu | 4:4ca64a863fbf | 134 | eventQueue.dispatch_forever(); |
johnathanlyu | 4:4ca64a863fbf | 135 | |
bcc6 | 1:40c18f027e6c | 136 | #elif (CONFIG_RUN_MODE == RUN_IR_AK9750_APP) |
johnathanlyu | 4:4ca64a863fbf | 137 | eventQueue.call_every(200, readASensorCallback); |
johnathanlyu | 4:4ca64a863fbf | 138 | eventQueue.dispatch_forever(); |
johnathanlyu | 4:4ca64a863fbf | 139 | |
bcc6 | 1:40c18f027e6c | 140 | #elif (CONFIG_RUN_MODE == RUN_COMPASS_AK09912_APP) |
johnathanlyu | 4:4ca64a863fbf | 141 | eventQueue.call_every(200, readASensorCallback); |
johnathanlyu | 4:4ca64a863fbf | 142 | eventQueue.dispatch_forever(); |
johnathanlyu | 4:4ca64a863fbf | 143 | |
bcc6 | 1:40c18f027e6c | 144 | #elif (CONFIG_RUN_MODE == RUN_MAGNETIC_AK09970_APP) |
johnathanlyu | 4:4ca64a863fbf | 145 | eventQueue.call_every(200, readASensorCallback); |
johnathanlyu | 4:4ca64a863fbf | 146 | eventQueue.dispatch_forever(); |
johnathanlyu | 4:4ca64a863fbf | 147 | |
bcc6 | 2:87f1b98c0d09 | 148 | #elif (CONFIG_RUN_MODE == RUN_IR_AK9750_INT_COM) |
bcc6 | 2:87f1b98c0d09 | 149 | // Enable interrupt sources and set callback function |
bcc6 | 2:87f1b98c0d09 | 150 | ak9750.SetIntEvent(AK9750::INT_DR, &IntEventCallback); |
bcc6 | 2:87f1b98c0d09 | 151 | |
bcc6 | 2:87f1b98c0d09 | 152 | while (1) { |
bcc6 | 2:87f1b98c0d09 | 153 | /* If any interrupt occurred */ |
bcc6 | 2:87f1b98c0d09 | 154 | if (isIntEvent) { |
bcc6 | 2:87f1b98c0d09 | 155 | /* Read interrupt status */ |
bcc6 | 2:87f1b98c0d09 | 156 | uint8_t status = ak9750.GetIntStatus(); |
bcc6 | 2:87f1b98c0d09 | 157 | if (status & AK9750::INT_DR) { |
bcc6 | 2:87f1b98c0d09 | 158 | pc.printf("INT_DR\n"); |
bcc6 | 2:87f1b98c0d09 | 159 | /* Read data */ |
bcc6 | 2:87f1b98c0d09 | 160 | AK9750::Data data; |
bcc6 | 2:87f1b98c0d09 | 161 | ak9750.GetData(&data); |
bcc6 | 2:87f1b98c0d09 | 162 | pc.printf("AK9750(%5.1fpA, %5.1fpA, %5.1fpA, %5.1fpA)\n", data.ir1, data.ir2, data.ir3, data.ir4); |
bcc6 | 2:87f1b98c0d09 | 163 | } |
bcc6 | 2:87f1b98c0d09 | 164 | if (status & AK9750::INT_IR24L) pc.printf("INT_IR24L\n"); |
bcc6 | 2:87f1b98c0d09 | 165 | if (status & AK9750::INT_IR24H) pc.printf("INT_IR24H\n"); |
bcc6 | 2:87f1b98c0d09 | 166 | if (status & AK9750::INT_IR13L) pc.printf("INT_IR13L\n"); |
bcc6 | 2:87f1b98c0d09 | 167 | if (status & AK9750::INT_IR13H) pc.printf("INT_IR13H\n"); |
bcc6 | 2:87f1b98c0d09 | 168 | isIntEvent = false; |
bcc6 | 2:87f1b98c0d09 | 169 | } |
bcc6 | 2:87f1b98c0d09 | 170 | } |
bcc6 | 1:40c18f027e6c | 171 | #else |
bcc6 | 1:40c18f027e6c | 172 | #errror "Config run mode ...FAIL" |
bcc6 | 1:40c18f027e6c | 173 | #endif |
bcc6 | 0:418880413158 | 174 | } |