Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
akmsensormanager.cpp@30:5a241d9b3262, 2017-04-28 (annotated)
- Committer:
- masahikofukasawa
- Date:
- Fri Apr 28 20:32:31 2017 +0000
- Revision:
- 30:5a241d9b3262
- Parent:
- 29:b488d2c89fba
- Child:
- 34:1ea3357c8d9a
release RevD7.014. Add register access for AK7401/AK7451. Also for multi sensor control.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
masahikofukasawa | 0:7a00359e701e | 1 | #include "ble/services/UARTService.h" |
masahikofukasawa | 15:1238993fd75f | 2 | #include "akdphwinfo.h" |
masahikofukasawa | 0:7a00359e701e | 3 | #include "akmsensormanager.h" |
masahikofukasawa | 0:7a00359e701e | 4 | #include "akmhallswitch.h" |
masahikofukasawa | 11:cef8dc1cf010 | 5 | #include "akmanalogsensor.h" |
masahikofukasawa | 10:5c69b067d88a | 6 | #include "ak09970ctrl.h" |
masahikofukasawa | 10:5c69b067d88a | 7 | #include "ak9750ctrl.h" |
masahikofukasawa | 10:5c69b067d88a | 8 | #include "ak9752ctrl.h" |
masahikofukasawa | 11:cef8dc1cf010 | 9 | #include "ak7451ctrl.h" |
masahikofukasawa | 10:5c69b067d88a | 10 | #include "ak7401ctrl.h" |
masahikofukasawa | 0:7a00359e701e | 11 | #include "akmakd.h" |
masahikofukasawa | 0:7a00359e701e | 12 | #include "debug.h" |
masahikofukasawa | 0:7a00359e701e | 13 | #include "Message.h" |
coisme | 2:11fe67783c4c | 14 | #include "mcp342x.h" |
masahikofukasawa | 6:c4401549d68f | 15 | #include "I2CNano.h" |
masahikofukasawa | 0:7a00359e701e | 16 | |
masahikofukasawa | 0:7a00359e701e | 17 | #define MAGNETOMETER_ID 0x0A |
masahikofukasawa | 0:7a00359e701e | 18 | |
masahikofukasawa | 0:7a00359e701e | 19 | #define CONV16I(high,low) ((int16_t)(((high) << 8) | (low))) |
masahikofukasawa | 0:7a00359e701e | 20 | |
masahikofukasawa | 0:7a00359e701e | 21 | |
masahikofukasawa | 10:5c69b067d88a | 22 | const char* AKM_PRIMARY_ID_STR[] = { |
masahikofukasawa | 7:e269411568c9 | 23 | "AKD Daughter Cards(SPI)", |
masahikofukasawa | 0:7a00359e701e | 24 | "Switch, Unipolar", |
masahikofukasawa | 0:7a00359e701e | 25 | "Switch, Onmipolar", |
masahikofukasawa | 0:7a00359e701e | 26 | "Latch, Bipolar", |
masahikofukasawa | 0:7a00359e701e | 27 | "Switch, Dual Output", |
masahikofukasawa | 0:7a00359e701e | 28 | "Onechip Encoder", |
masahikofukasawa | 0:7a00359e701e | 29 | "TBD1", |
masahikofukasawa | 0:7a00359e701e | 30 | "TBD2", |
masahikofukasawa | 27:41aa9fb23a2f | 31 | "DEMO", |
masahikofukasawa | 0:7a00359e701e | 32 | "Current Sensor", |
masahikofukasawa | 0:7a00359e701e | 33 | "MISC(Analog)", |
masahikofukasawa | 0:7a00359e701e | 34 | "Linear Sensor", |
masahikofukasawa | 18:b7182d5ad8d5 | 35 | "Motor Drivers", |
masahikofukasawa | 1:b46b8653331f | 36 | "IR Sensor", |
masahikofukasawa | 7:e269411568c9 | 37 | "Angle Sensor(SPI)", |
masahikofukasawa | 10:5c69b067d88a | 38 | "AKD Daughter Cards(I2C)" |
masahikofukasawa | 0:7a00359e701e | 39 | }; |
masahikofukasawa | 0:7a00359e701e | 40 | |
masahikofukasawa | 13:d008249f0359 | 41 | AkmSensorManager::AkmSensorManager(SerialNano* com) |
masahikofukasawa | 0:7a00359e701e | 42 | { |
masahikofukasawa | 0:7a00359e701e | 43 | serial = com; |
masahikofukasawa | 0:7a00359e701e | 44 | isEnabledUsb = true; |
masahikofukasawa | 0:7a00359e701e | 45 | eventCommandReceived = false; |
masahikofukasawa | 0:7a00359e701e | 46 | eventConnected = false; |
masahikofukasawa | 0:7a00359e701e | 47 | eventDisconnected = false; |
masahikofukasawa | 29:b488d2c89fba | 48 | interrupt = NULL; |
masahikofukasawa | 29:b488d2c89fba | 49 | sensorIndex = 0; |
masahikofukasawa | 29:b488d2c89fba | 50 | sensorNum = 0; |
masahikofukasawa | 29:b488d2c89fba | 51 | drdyType = AkmAkd::INTERRUPT_DISABLED; |
masahikofukasawa | 0:7a00359e701e | 52 | } |
masahikofukasawa | 0:7a00359e701e | 53 | |
masahikofukasawa | 10:5c69b067d88a | 54 | AkmSensorManager::Status AkmSensorManager::init(uint8_t id, uint8_t subid) |
masahikofukasawa | 0:7a00359e701e | 55 | { |
masahikofukasawa | 10:5c69b067d88a | 56 | primaryId = id; |
masahikofukasawa | 10:5c69b067d88a | 57 | subId = subid; |
masahikofukasawa | 29:b488d2c89fba | 58 | sensorIndex = 0; |
masahikofukasawa | 29:b488d2c89fba | 59 | sensorNum = 0; |
masahikofukasawa | 27:41aa9fb23a2f | 60 | if(AkmSensorManager::checkAkmSensor()!= true) return AkmSensorManager::ERROR; |
masahikofukasawa | 0:7a00359e701e | 61 | return AkmSensorManager::SUCCESS; |
masahikofukasawa | 0:7a00359e701e | 62 | } |
masahikofukasawa | 0:7a00359e701e | 63 | |
masahikofukasawa | 13:d008249f0359 | 64 | |
masahikofukasawa | 13:d008249f0359 | 65 | void AkmSensorManager::setBleUartService(UARTService* service) |
masahikofukasawa | 13:d008249f0359 | 66 | { |
masahikofukasawa | 13:d008249f0359 | 67 | uartService = service; |
masahikofukasawa | 13:d008249f0359 | 68 | isEnabledBle = true; |
masahikofukasawa | 13:d008249f0359 | 69 | } |
masahikofukasawa | 13:d008249f0359 | 70 | |
masahikofukasawa | 0:7a00359e701e | 71 | void AkmSensorManager::setEventConnected() |
masahikofukasawa | 0:7a00359e701e | 72 | { |
masahikofukasawa | 0:7a00359e701e | 73 | eventConnected = true; |
masahikofukasawa | 0:7a00359e701e | 74 | } |
masahikofukasawa | 0:7a00359e701e | 75 | |
masahikofukasawa | 0:7a00359e701e | 76 | void AkmSensorManager::setEventDisconnected() |
masahikofukasawa | 0:7a00359e701e | 77 | { |
masahikofukasawa | 0:7a00359e701e | 78 | eventDisconnected = true; |
masahikofukasawa | 0:7a00359e701e | 79 | } |
masahikofukasawa | 0:7a00359e701e | 80 | |
masahikofukasawa | 27:41aa9fb23a2f | 81 | bool AkmSensorManager::checkAkmSensor() |
masahikofukasawa | 29:b488d2c89fba | 82 | { |
masahikofukasawa | 29:b488d2c89fba | 83 | sensorNum = 0; |
masahikofukasawa | 29:b488d2c89fba | 84 | |
masahikofukasawa | 29:b488d2c89fba | 85 | // int i=0; |
masahikofukasawa | 29:b488d2c89fba | 86 | // while(sensor[i] != NULL){ |
masahikofukasawa | 29:b488d2c89fba | 87 | // delete(sensor[i]); |
masahikofukasawa | 29:b488d2c89fba | 88 | // i++; |
masahikofukasawa | 29:b488d2c89fba | 89 | // } |
masahikofukasawa | 29:b488d2c89fba | 90 | if(interrupt) delete interrupt; |
masahikofukasawa | 29:b488d2c89fba | 91 | drdyType = AkmAkd::INTERRUPT_DISABLED; |
masahikofukasawa | 0:7a00359e701e | 92 | |
masahikofukasawa | 10:5c69b067d88a | 93 | switch(primaryId){ |
masahikofukasawa | 17:9abb7c28709c | 94 | |
masahikofukasawa | 7:e269411568c9 | 95 | case AkmSensor::AKM_PRIMARY_ID_AKD_SPI: |
masahikofukasawa | 29:b488d2c89fba | 96 | { |
masahikofukasawa | 29:b488d2c89fba | 97 | MSG("#SPI Interface.\r\n"); |
masahikofukasawa | 29:b488d2c89fba | 98 | } |
masahikofukasawa | 0:7a00359e701e | 99 | case AkmSensor::AKM_PRIMARY_ID_AKD_I2C: |
masahikofukasawa | 10:5c69b067d88a | 100 | { |
masahikofukasawa | 29:b488d2c89fba | 101 | if(subId == Ak09970Ctrl::SUB_ID_AK09970){ |
masahikofukasawa | 29:b488d2c89fba | 102 | drdyType = AkmAkd::INTERRUPT_ENABLED_PP; // Only support push-pull |
masahikofukasawa | 29:b488d2c89fba | 103 | Ak09970Ctrl* ak09970 = new Ak09970Ctrl(); |
masahikofukasawa | 29:b488d2c89fba | 104 | sensor[0] = ak09970; |
masahikofukasawa | 29:b488d2c89fba | 105 | sensorNum = 1; |
masahikofukasawa | 29:b488d2c89fba | 106 | MSG("#AK09970.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 107 | } |
masahikofukasawa | 10:5c69b067d88a | 108 | else{ |
masahikofukasawa | 29:b488d2c89fba | 109 | AkmAkd* akd = new AkmAkd(); |
masahikofukasawa | 29:b488d2c89fba | 110 | drdyType = akd->getInterrupt(primaryId, subId); |
masahikofukasawa | 29:b488d2c89fba | 111 | sensor[0] = akd; |
masahikofukasawa | 29:b488d2c89fba | 112 | sensorNum = 1; |
masahikofukasawa | 29:b488d2c89fba | 113 | MSG("#e-Compass.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 114 | } |
masahikofukasawa | 0:7a00359e701e | 115 | break; |
masahikofukasawa | 29:b488d2c89fba | 116 | } |
masahikofukasawa | 0:7a00359e701e | 117 | case AkmSensor::AKM_PRIMARY_ID_ANGLE_SENSOR: |
masahikofukasawa | 10:5c69b067d88a | 118 | { |
masahikofukasawa | 11:cef8dc1cf010 | 119 | if(subId == Ak7451Ctrl::SUB_ID_AK7451){ |
masahikofukasawa | 11:cef8dc1cf010 | 120 | Ak7451Ctrl* ak7451ctrl = new Ak7451Ctrl(); |
masahikofukasawa | 27:41aa9fb23a2f | 121 | sensor[0] = ak7451ctrl; |
masahikofukasawa | 29:b488d2c89fba | 122 | sensorNum = 1; |
masahikofukasawa | 10:5c69b067d88a | 123 | } |
masahikofukasawa | 10:5c69b067d88a | 124 | else if(subId == Ak7401Ctrl::SUB_ID_AK7401){ |
masahikofukasawa | 10:5c69b067d88a | 125 | Ak7401Ctrl* ak7401ctrl = new Ak7401Ctrl(); |
masahikofukasawa | 27:41aa9fb23a2f | 126 | sensor[0] = ak7401ctrl; |
masahikofukasawa | 29:b488d2c89fba | 127 | sensorNum = 1; |
masahikofukasawa | 10:5c69b067d88a | 128 | } |
masahikofukasawa | 0:7a00359e701e | 129 | break; |
masahikofukasawa | 29:b488d2c89fba | 130 | } |
masahikofukasawa | 17:9abb7c28709c | 131 | |
masahikofukasawa | 0:7a00359e701e | 132 | case AkmSensor::AKM_PRIMARY_ID_UNIPOLAR: |
masahikofukasawa | 0:7a00359e701e | 133 | case AkmSensor::AKM_PRIMARY_ID_OMNIPOLAR: |
masahikofukasawa | 0:7a00359e701e | 134 | case AkmSensor::AKM_PRIMARY_ID_LATCH: |
masahikofukasawa | 0:7a00359e701e | 135 | case AkmSensor::AKM_PRIMARY_ID_DUAL_OUTPUT: |
masahikofukasawa | 0:7a00359e701e | 136 | case AkmSensor::AKM_PRIMARY_ID_ONECHIP_ENCODER: |
masahikofukasawa | 10:5c69b067d88a | 137 | { |
masahikofukasawa | 10:5c69b067d88a | 138 | AkmHallSwitch* hallswitch = new AkmHallSwitch(); |
masahikofukasawa | 27:41aa9fb23a2f | 139 | sensor[0] = hallswitch; |
masahikofukasawa | 29:b488d2c89fba | 140 | sensorNum = 1; |
masahikofukasawa | 0:7a00359e701e | 141 | break; |
masahikofukasawa | 10:5c69b067d88a | 142 | } |
masahikofukasawa | 27:41aa9fb23a2f | 143 | case AkmSensor::AKM_PRIMARY_ID_DEMO: |
masahikofukasawa | 27:41aa9fb23a2f | 144 | { |
masahikofukasawa | 29:b488d2c89fba | 145 | if(subId == 0x08){ |
masahikofukasawa | 29:b488d2c89fba | 146 | drdyType = AkmAkd::INTERRUPT_ENABLED_OD; // Only support open drain type DRDY |
masahikofukasawa | 29:b488d2c89fba | 147 | |
masahikofukasawa | 29:b488d2c89fba | 148 | Ak09970Ctrl* ak09970 = new Ak09970Ctrl(); |
masahikofukasawa | 29:b488d2c89fba | 149 | sensor[0] = ak09970; |
masahikofukasawa | 29:b488d2c89fba | 150 | if(sensor[0]->init(AkmSensor::AKM_PRIMARY_ID_AKD_I2C, Ak09970Ctrl::SUB_ID_AK09970) != AkmSensor::SUCCESS){ |
masahikofukasawa | 29:b488d2c89fba | 151 | MSG("#sensor[0]->init failed.\r\n"); |
masahikofukasawa | 27:41aa9fb23a2f | 152 | return false; // couldn't find |
masahikofukasawa | 29:b488d2c89fba | 153 | } |
masahikofukasawa | 29:b488d2c89fba | 154 | |
masahikofukasawa | 29:b488d2c89fba | 155 | Ak9752Ctrl* ak9752ctrl = new Ak9752Ctrl(); |
masahikofukasawa | 29:b488d2c89fba | 156 | sensor[1] = ak9752ctrl; |
masahikofukasawa | 29:b488d2c89fba | 157 | if(sensor[1]->init(AkmSensor::AKM_PRIMARY_ID_IR_SENSOR, Ak9752Ctrl::SUB_ID_AK9752) != AkmSensor::SUCCESS){ |
masahikofukasawa | 29:b488d2c89fba | 158 | MSG("#sensor[1]->init failed.\r\n"); |
masahikofukasawa | 27:41aa9fb23a2f | 159 | return false; // couldn't find |
masahikofukasawa | 29:b488d2c89fba | 160 | } |
masahikofukasawa | 29:b488d2c89fba | 161 | sensorNum = 2; |
masahikofukasawa | 27:41aa9fb23a2f | 162 | } |
masahikofukasawa | 29:b488d2c89fba | 163 | break; |
masahikofukasawa | 27:41aa9fb23a2f | 164 | } |
masahikofukasawa | 4:af13b985c689 | 165 | case AkmSensor::AKM_PRIMARY_ID_LINEAR_SENSOR: |
masahikofukasawa | 10:5c69b067d88a | 166 | case AkmSensor::AKM_PRIMARY_ID_CURRENT_SENSOR: |
masahikofukasawa | 10:5c69b067d88a | 167 | case AkmSensor::AKM_PRIMARY_ID_MISC_ANALOG: |
masahikofukasawa | 10:5c69b067d88a | 168 | { |
masahikofukasawa | 11:cef8dc1cf010 | 169 | AkmAnalogSensor* analogsensor = new AkmAnalogSensor(); |
masahikofukasawa | 27:41aa9fb23a2f | 170 | sensor[0] = analogsensor; |
masahikofukasawa | 29:b488d2c89fba | 171 | sensorNum = 1; |
masahikofukasawa | 0:7a00359e701e | 172 | break; |
masahikofukasawa | 10:5c69b067d88a | 173 | } |
masahikofukasawa | 17:9abb7c28709c | 174 | |
masahikofukasawa | 1:b46b8653331f | 175 | case AkmSensor::AKM_PRIMARY_ID_IR_SENSOR: |
masahikofukasawa | 10:5c69b067d88a | 176 | { |
masahikofukasawa | 29:b488d2c89fba | 177 | drdyType = AkmAkd::INTERRUPT_ENABLED_OD; |
masahikofukasawa | 29:b488d2c89fba | 178 | |
masahikofukasawa | 10:5c69b067d88a | 179 | if(subId == Ak9750Ctrl::SUB_ID_AK9750){ |
masahikofukasawa | 10:5c69b067d88a | 180 | Ak9750Ctrl* ak9750ctrl = new Ak9750Ctrl(); |
masahikofukasawa | 27:41aa9fb23a2f | 181 | sensor[0] = ak9750ctrl; |
masahikofukasawa | 29:b488d2c89fba | 182 | sensorNum = 1; |
masahikofukasawa | 15:1238993fd75f | 183 | }else if(subId == Ak9750Ctrl::SUB_ID_AK9753){ |
masahikofukasawa | 15:1238993fd75f | 184 | Ak9750Ctrl* ak9753ctrl = new Ak9750Ctrl(); |
masahikofukasawa | 27:41aa9fb23a2f | 185 | sensor[0] = ak9753ctrl; |
masahikofukasawa | 29:b488d2c89fba | 186 | sensorNum = 1; |
masahikofukasawa | 10:5c69b067d88a | 187 | }else if(subId == Ak9752Ctrl::SUB_ID_AK9752){ |
masahikofukasawa | 10:5c69b067d88a | 188 | Ak9752Ctrl* ak9752ctrl = new Ak9752Ctrl(); |
masahikofukasawa | 27:41aa9fb23a2f | 189 | sensor[0] = ak9752ctrl; |
masahikofukasawa | 29:b488d2c89fba | 190 | sensorNum = 1; |
masahikofukasawa | 10:5c69b067d88a | 191 | }else{ |
masahikofukasawa | 27:41aa9fb23a2f | 192 | return false; // couldn't find |
masahikofukasawa | 10:5c69b067d88a | 193 | } |
masahikofukasawa | 1:b46b8653331f | 194 | break; |
masahikofukasawa | 10:5c69b067d88a | 195 | } |
masahikofukasawa | 17:9abb7c28709c | 196 | |
masahikofukasawa | 0:7a00359e701e | 197 | default: |
masahikofukasawa | 10:5c69b067d88a | 198 | { |
masahikofukasawa | 17:9abb7c28709c | 199 | MSG("#Can't find ID=%d SubID=%d %s\r\n", primaryId, subId, AKM_PRIMARY_ID_STR[primaryId]); |
masahikofukasawa | 27:41aa9fb23a2f | 200 | return false; // couldn't find |
masahikofukasawa | 10:5c69b067d88a | 201 | } |
masahikofukasawa | 0:7a00359e701e | 202 | } |
masahikofukasawa | 29:b488d2c89fba | 203 | |
masahikofukasawa | 29:b488d2c89fba | 204 | if(primaryId != AkmSensor::AKM_PRIMARY_ID_DEMO){ |
masahikofukasawa | 29:b488d2c89fba | 205 | for(int i=0; i<sensorNum; i++){ |
masahikofukasawa | 29:b488d2c89fba | 206 | if(sensor[i]->init(primaryId, subId) != AkmSensor::SUCCESS){ |
masahikofukasawa | 29:b488d2c89fba | 207 | MSG("#sensor[i]->init failed. ID=%d SubID=%d %s\r\n", primaryId, subId, AKM_PRIMARY_ID_STR[primaryId]); |
masahikofukasawa | 29:b488d2c89fba | 208 | return false; // couldn't find |
masahikofukasawa | 29:b488d2c89fba | 209 | } |
masahikofukasawa | 29:b488d2c89fba | 210 | } |
masahikofukasawa | 27:41aa9fb23a2f | 211 | MSG("#ID=%d SubID=%d %s\r\n", primaryId, subId, AKM_PRIMARY_ID_STR[primaryId]); |
masahikofukasawa | 0:7a00359e701e | 212 | } |
masahikofukasawa | 29:b488d2c89fba | 213 | |
masahikofukasawa | 27:41aa9fb23a2f | 214 | return true; |
masahikofukasawa | 0:7a00359e701e | 215 | } |
masahikofukasawa | 0:7a00359e701e | 216 | |
masahikofukasawa | 29:b488d2c89fba | 217 | void AkmSensorManager::detectDRDY(){ |
masahikofukasawa | 29:b488d2c89fba | 218 | // MSG("#detect DRDY.\r\n"); |
masahikofukasawa | 29:b488d2c89fba | 219 | for(int i=0; i<sensorNum; i++){ |
masahikofukasawa | 29:b488d2c89fba | 220 | sensor[i]->setEvent(); |
masahikofukasawa | 29:b488d2c89fba | 221 | } |
masahikofukasawa | 29:b488d2c89fba | 222 | } |
masahikofukasawa | 29:b488d2c89fba | 223 | |
masahikofukasawa | 16:d85be9bafb80 | 224 | void AkmSensorManager::dummyCallbackForCommandReceived(){} |
masahikofukasawa | 16:d85be9bafb80 | 225 | |
masahikofukasawa | 0:7a00359e701e | 226 | AkmSensorManager::Status AkmSensorManager::commandReceived(char* buf){ |
masahikofukasawa | 0:7a00359e701e | 227 | // Construct message |
masahikofukasawa | 0:7a00359e701e | 228 | Status status = SUCCESS; |
masahikofukasawa | 0:7a00359e701e | 229 | |
masahikofukasawa | 10:5c69b067d88a | 230 | if ((Message::parse(&msg, buf)) != Message::SUCCESS) { |
masahikofukasawa | 11:cef8dc1cf010 | 231 | MSG("#Failed to parse message. %s\r\n", buf); |
masahikofukasawa | 0:7a00359e701e | 232 | status = ERROR; |
masahikofukasawa | 0:7a00359e701e | 233 | eventCommandReceived = false; |
masahikofukasawa | 15:1238993fd75f | 234 | }else{ |
masahikofukasawa | 15:1238993fd75f | 235 | eventCommandReceived = true; |
masahikofukasawa | 16:d85be9bafb80 | 236 | // MSG("#Parsed message. %s\r\n", buf); |
masahikofukasawa | 0:7a00359e701e | 237 | } |
masahikofukasawa | 16:d85be9bafb80 | 238 | t.attach(callback(this, &AkmSensorManager::dummyCallbackForCommandReceived),0); // wake-up from ble.waitForEvent |
masahikofukasawa | 0:7a00359e701e | 239 | return status; |
masahikofukasawa | 0:7a00359e701e | 240 | } |
masahikofukasawa | 0:7a00359e701e | 241 | |
coisme | 2:11fe67783c4c | 242 | int16_t AkmSensorManager::getAdcData(MCP342X *mcp3428, MCP342X::AdcChannel ch, MCP342X::SampleSetting s) { |
coisme | 2:11fe67783c4c | 243 | const int WAIT_ADC_MS = 1; |
coisme | 2:11fe67783c4c | 244 | |
coisme | 2:11fe67783c4c | 245 | // Configure channel and trigger. |
coisme | 2:11fe67783c4c | 246 | mcp3428->setChannel(ch); |
coisme | 2:11fe67783c4c | 247 | mcp3428->setSampleSetting(s); |
coisme | 2:11fe67783c4c | 248 | mcp3428->trigger(); |
coisme | 2:11fe67783c4c | 249 | |
coisme | 2:11fe67783c4c | 250 | // polling data (!blocking) |
coisme | 2:11fe67783c4c | 251 | MCP342X::Data data; |
coisme | 2:11fe67783c4c | 252 | do { |
coisme | 2:11fe67783c4c | 253 | wait_ms(WAIT_ADC_MS); |
coisme | 2:11fe67783c4c | 254 | mcp3428->getData(&data); |
coisme | 2:11fe67783c4c | 255 | } while(data.st == MCP342X::DATA_NOT_UPDATED); |
coisme | 2:11fe67783c4c | 256 | |
coisme | 2:11fe67783c4c | 257 | return data.value; |
coisme | 2:11fe67783c4c | 258 | } |
masahikofukasawa | 0:7a00359e701e | 259 | |
masahikofukasawa | 0:7a00359e701e | 260 | uint8_t AkmSensorManager::getId(PinName pin, uint8_t bits) |
masahikofukasawa | 0:7a00359e701e | 261 | { |
masahikofukasawa | 11:cef8dc1cf010 | 262 | MSG("#GetID\r\n"); |
masahikofukasawa | 6:c4401549d68f | 263 | |
masahikofukasawa | 6:c4401549d68f | 264 | I2C i2c(I2C_SDA, I2C_SCL); |
coisme | 2:11fe67783c4c | 265 | // ADC |
coisme | 2:11fe67783c4c | 266 | MCP342X mcp342x(&i2c, MCP342X::SLAVE_ADDRESS_6EH); |
coisme | 2:11fe67783c4c | 267 | mcp342x.setConversionMode(MCP342X::ONE_SHOT); |
coisme | 2:11fe67783c4c | 268 | MCP342X::AdcChannel ch; |
coisme | 2:11fe67783c4c | 269 | if (pin == ANALOG_SENSOR_ID) { |
coisme | 2:11fe67783c4c | 270 | ch = MCP342X::ADC_CH1; |
coisme | 2:11fe67783c4c | 271 | } else { // pin == ANALOG_SENSOR_ID_SUB |
coisme | 2:11fe67783c4c | 272 | ch = MCP342X::ADC_CH2; |
coisme | 2:11fe67783c4c | 273 | } |
coisme | 2:11fe67783c4c | 274 | int16_t val = getAdcData(&mcp342x, ch, MCP342X::SAMPLE_240HZ_12BIT); |
masahikofukasawa | 11:cef8dc1cf010 | 275 | MSG("#12bit ADC Val = %d.\r\n", val); |
coisme | 2:11fe67783c4c | 276 | |
coisme | 2:11fe67783c4c | 277 | const int16_t VAL_MAX = 3000-2048; // Corresponds to 3V |
coisme | 2:11fe67783c4c | 278 | const int16_t VAL_MIN = -2048; // Corresponds to 0V |
coisme | 2:11fe67783c4c | 279 | |
masahikofukasawa | 6:c4401549d68f | 280 | uint8_t value = (uint8_t)((val - VAL_MIN)/(float)(VAL_MAX - VAL_MIN) * (1 << bits) + 0.5); |
masahikofukasawa | 11:cef8dc1cf010 | 281 | MSG("#ID = %d.\r\n", value); |
masahikofukasawa | 6:c4401549d68f | 282 | |
masahikofukasawa | 0:7a00359e701e | 283 | return value; |
masahikofukasawa | 0:7a00359e701e | 284 | } |
masahikofukasawa | 0:7a00359e701e | 285 | |
masahikofukasawa | 0:7a00359e701e | 286 | bool AkmSensorManager::isEvent() |
masahikofukasawa | 0:7a00359e701e | 287 | { |
masahikofukasawa | 29:b488d2c89fba | 288 | // check sensor related event |
masahikofukasawa | 29:b488d2c89fba | 289 | for(int i=0; i<sensorNum; i++){ |
masahikofukasawa | 29:b488d2c89fba | 290 | if(sensor[i]->isEvent()){ |
masahikofukasawa | 29:b488d2c89fba | 291 | return true; |
masahikofukasawa | 29:b488d2c89fba | 292 | } |
masahikofukasawa | 29:b488d2c89fba | 293 | } |
masahikofukasawa | 29:b488d2c89fba | 294 | |
masahikofukasawa | 29:b488d2c89fba | 295 | // other events |
masahikofukasawa | 29:b488d2c89fba | 296 | return ( |
masahikofukasawa | 0:7a00359e701e | 297 | eventCommandReceived || |
masahikofukasawa | 0:7a00359e701e | 298 | eventConnected || |
masahikofukasawa | 0:7a00359e701e | 299 | eventDisconnected); |
masahikofukasawa | 0:7a00359e701e | 300 | } |
masahikofukasawa | 0:7a00359e701e | 301 | |
masahikofukasawa | 0:7a00359e701e | 302 | void AkmSensorManager::processCommand() |
masahikofukasawa | 0:7a00359e701e | 303 | { |
masahikofukasawa | 0:7a00359e701e | 304 | // Gets command in the message |
masahikofukasawa | 0:7a00359e701e | 305 | Message::Command cmd = msg.getCommand(); |
masahikofukasawa | 0:7a00359e701e | 306 | |
masahikofukasawa | 0:7a00359e701e | 307 | // Creates an message object to return |
masahikofukasawa | 0:7a00359e701e | 308 | Message resMsg; |
masahikofukasawa | 0:7a00359e701e | 309 | |
masahikofukasawa | 0:7a00359e701e | 310 | // Return message has the same command as input |
masahikofukasawa | 0:7a00359e701e | 311 | resMsg.setCommand(cmd); |
masahikofukasawa | 0:7a00359e701e | 312 | |
masahikofukasawa | 0:7a00359e701e | 313 | switch(cmd) |
masahikofukasawa | 0:7a00359e701e | 314 | { |
masahikofukasawa | 0:7a00359e701e | 315 | case Message::CMD_GET_FW_VERSION: |
masahikofukasawa | 11:cef8dc1cf010 | 316 | { |
masahikofukasawa | 0:7a00359e701e | 317 | resMsg.setArgument(0, FIRMWARE_VERSION); |
masahikofukasawa | 0:7a00359e701e | 318 | throwMessage(&resMsg); |
masahikofukasawa | 11:cef8dc1cf010 | 319 | MSG("#FW version is reported.\r\n"); |
masahikofukasawa | 0:7a00359e701e | 320 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 321 | } |
masahikofukasawa | 0:7a00359e701e | 322 | case Message::CMD_GET_MAG_PART: |
masahikofukasawa | 11:cef8dc1cf010 | 323 | { |
masahikofukasawa | 0:7a00359e701e | 324 | resMsg.setArgument(0, MAGNETOMETER_ID); |
masahikofukasawa | 0:7a00359e701e | 325 | throwMessage(&resMsg); |
masahikofukasawa | 11:cef8dc1cf010 | 326 | MSG("#Mag ID is reported.\r\n"); |
masahikofukasawa | 0:7a00359e701e | 327 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 328 | } |
masahikofukasawa | 0:7a00359e701e | 329 | case Message::CMD_SET_SERIAL_TARGET: |
masahikofukasawa | 11:cef8dc1cf010 | 330 | { |
masahikofukasawa | 0:7a00359e701e | 331 | isEnabledBle = msg.getArgument(0)==Message::SW_ON ? true : false; |
masahikofukasawa | 0:7a00359e701e | 332 | isEnabledUsb = msg.getArgument(1)==Message::SW_ON ? true : false; |
masahikofukasawa | 0:7a00359e701e | 333 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 334 | } |
masahikofukasawa | 0:7a00359e701e | 335 | case Message::CMD_GET_ID: // return Primary ID and Sub ID |
masahikofukasawa | 11:cef8dc1cf010 | 336 | { |
masahikofukasawa | 10:5c69b067d88a | 337 | resMsg.setArgument(0, primaryId); |
masahikofukasawa | 0:7a00359e701e | 338 | resMsg.setArgument(1, subId); |
masahikofukasawa | 0:7a00359e701e | 339 | throwMessage(&resMsg); |
masahikofukasawa | 29:b488d2c89fba | 340 | MSG("#ID is reported.\r\n"); |
masahikofukasawa | 0:7a00359e701e | 341 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 342 | } |
masahikofukasawa | 29:b488d2c89fba | 343 | case Message::CMD_GET_SENSOR_INDEX: |
masahikofukasawa | 27:41aa9fb23a2f | 344 | { |
masahikofukasawa | 29:b488d2c89fba | 345 | resMsg.setArgument(0, sensorIndex); |
masahikofukasawa | 27:41aa9fb23a2f | 346 | throwMessage(&resMsg); |
masahikofukasawa | 29:b488d2c89fba | 347 | MSG("#Get Sensor Index=%d.\r\n", sensorIndex); |
masahikofukasawa | 27:41aa9fb23a2f | 348 | break; |
masahikofukasawa | 27:41aa9fb23a2f | 349 | } |
masahikofukasawa | 29:b488d2c89fba | 350 | case Message::CMD_SET_SENSOR_INDEX: |
masahikofukasawa | 27:41aa9fb23a2f | 351 | { |
masahikofukasawa | 29:b488d2c89fba | 352 | uint8_t index = (uint8_t)msg.getArgument(0); |
masahikofukasawa | 30:5a241d9b3262 | 353 | if(msg.getArgNum() == 1 && index<sensorNum){ |
masahikofukasawa | 28:dc4eb14e4d7e | 354 | resMsg.setArgument(0, 0); |
masahikofukasawa | 29:b488d2c89fba | 355 | sensorIndex = index; |
masahikofukasawa | 29:b488d2c89fba | 356 | MSG("#Set Sensor Index=%d.\r\n", sensorIndex); |
masahikofukasawa | 30:5a241d9b3262 | 357 | }else{ |
masahikofukasawa | 30:5a241d9b3262 | 358 | resMsg.setArgument(0, 1); |
masahikofukasawa | 30:5a241d9b3262 | 359 | MSG("#Error: Set Sensor Index=%d.\r\n", sensorIndex); |
masahikofukasawa | 29:b488d2c89fba | 360 | } |
masahikofukasawa | 28:dc4eb14e4d7e | 361 | throwMessage(&resMsg); |
masahikofukasawa | 27:41aa9fb23a2f | 362 | break; |
masahikofukasawa | 27:41aa9fb23a2f | 363 | } |
masahikofukasawa | 29:b488d2c89fba | 364 | case Message::CMD_GET_TOTAL_SENSOR_NUM: |
masahikofukasawa | 27:41aa9fb23a2f | 365 | { |
masahikofukasawa | 29:b488d2c89fba | 366 | resMsg.setArgument(0, sensorNum); |
masahikofukasawa | 27:41aa9fb23a2f | 367 | throwMessage(&resMsg); |
masahikofukasawa | 29:b488d2c89fba | 368 | MSG("#Get Sensor Total Num=%d.\r\n", sensorNum); |
masahikofukasawa | 27:41aa9fb23a2f | 369 | break; |
masahikofukasawa | 27:41aa9fb23a2f | 370 | } |
masahikofukasawa | 0:7a00359e701e | 371 | case Message::CMD_STOP_MEASUREMENT: |
masahikofukasawa | 11:cef8dc1cf010 | 372 | { |
masahikofukasawa | 29:b488d2c89fba | 373 | if( sensor[sensorIndex]->stopSensor() != AkmSensor::SUCCESS){ |
masahikofukasawa | 15:1238993fd75f | 374 | resMsg.setArgument(0, 1); |
masahikofukasawa | 15:1238993fd75f | 375 | }else{ |
masahikofukasawa | 27:41aa9fb23a2f | 376 | resMsg.setArgument(0, 0); |
masahikofukasawa | 15:1238993fd75f | 377 | } |
masahikofukasawa | 0:7a00359e701e | 378 | throwMessage(&resMsg); |
masahikofukasawa | 29:b488d2c89fba | 379 | MSG("#Stop measurement:%s.\r\n",sensor[sensorIndex]->getSensorName()); |
masahikofukasawa | 0:7a00359e701e | 380 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 381 | } |
masahikofukasawa | 0:7a00359e701e | 382 | case Message::CMD_START_MEASUREMENT: |
masahikofukasawa | 11:cef8dc1cf010 | 383 | { |
masahikofukasawa | 11:cef8dc1cf010 | 384 | int error_code = AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 385 | if(msg.getArgNum() == 0){ |
masahikofukasawa | 29:b488d2c89fba | 386 | error_code = sensor[sensorIndex]->startSensor(); |
masahikofukasawa | 11:cef8dc1cf010 | 387 | if( error_code != AkmSensor::SUCCESS ){ |
masahikofukasawa | 11:cef8dc1cf010 | 388 | MSG("#StartSensor Error. Code=%d\r\n",error_code); |
masahikofukasawa | 11:cef8dc1cf010 | 389 | } |
masahikofukasawa | 11:cef8dc1cf010 | 390 | else{ |
masahikofukasawa | 29:b488d2c89fba | 391 | switch(drdyType){ |
masahikofukasawa | 29:b488d2c89fba | 392 | case AkmAkd::INTERRUPT_ENABLED_PP: |
masahikofukasawa | 29:b488d2c89fba | 393 | { |
masahikofukasawa | 29:b488d2c89fba | 394 | if(primaryId == AkmSensor::AKM_PRIMARY_ID_AKD_SPI) interrupt = new InterruptIn(SPI_DRDY); |
masahikofukasawa | 29:b488d2c89fba | 395 | else interrupt = new InterruptIn(I2C_DRDY); |
masahikofukasawa | 29:b488d2c89fba | 396 | interrupt->rise(callback(this, &AkmSensorManager::detectDRDY)); |
masahikofukasawa | 29:b488d2c89fba | 397 | break; |
masahikofukasawa | 29:b488d2c89fba | 398 | } |
masahikofukasawa | 29:b488d2c89fba | 399 | case AkmAkd::INTERRUPT_ENABLED_OD: |
masahikofukasawa | 29:b488d2c89fba | 400 | { |
masahikofukasawa | 29:b488d2c89fba | 401 | if(primaryId == AkmSensor::AKM_PRIMARY_ID_AKD_SPI) interrupt = new InterruptIn(SPI_DRDY); |
masahikofukasawa | 29:b488d2c89fba | 402 | else interrupt = new InterruptIn(I2C_DRDY); |
masahikofukasawa | 29:b488d2c89fba | 403 | interrupt->fall(callback(this, &AkmSensorManager::detectDRDY)); |
masahikofukasawa | 29:b488d2c89fba | 404 | break; |
masahikofukasawa | 29:b488d2c89fba | 405 | } |
masahikofukasawa | 29:b488d2c89fba | 406 | default: |
masahikofukasawa | 29:b488d2c89fba | 407 | { |
masahikofukasawa | 29:b488d2c89fba | 408 | // nothing. |
masahikofukasawa | 29:b488d2c89fba | 409 | } |
masahikofukasawa | 29:b488d2c89fba | 410 | } |
masahikofukasawa | 11:cef8dc1cf010 | 411 | } |
masahikofukasawa | 11:cef8dc1cf010 | 412 | }else if(msg.getArgNum() == 1){ |
masahikofukasawa | 11:cef8dc1cf010 | 413 | float interval = (float)(1.0 / (float)msg.getArgument(0)); |
masahikofukasawa | 29:b488d2c89fba | 414 | error_code = sensor[sensorIndex]->startSensor(interval); |
masahikofukasawa | 11:cef8dc1cf010 | 415 | if( error_code != AkmSensor::SUCCESS ){ |
masahikofukasawa | 11:cef8dc1cf010 | 416 | MSG("#StartSensor Error. Code=%d\r\n",error_code); |
masahikofukasawa | 11:cef8dc1cf010 | 417 | } |
masahikofukasawa | 11:cef8dc1cf010 | 418 | else{ |
masahikofukasawa | 29:b488d2c89fba | 419 | if(interrupt) delete interrupt; |
masahikofukasawa | 11:cef8dc1cf010 | 420 | } |
masahikofukasawa | 11:cef8dc1cf010 | 421 | }else{ |
masahikofukasawa | 11:cef8dc1cf010 | 422 | MSG("#StartSensor Error. Wrong Argument num.\r\n"); |
masahikofukasawa | 11:cef8dc1cf010 | 423 | } |
masahikofukasawa | 11:cef8dc1cf010 | 424 | if(error_code == AkmSensor::SUCCESS){ |
masahikofukasawa | 11:cef8dc1cf010 | 425 | // get initial sensor state for switch type sensors |
masahikofukasawa | 11:cef8dc1cf010 | 426 | if( primaryId == AkmSensor::AKM_PRIMARY_ID_UNIPOLAR || |
masahikofukasawa | 11:cef8dc1cf010 | 427 | primaryId == AkmSensor::AKM_PRIMARY_ID_OMNIPOLAR || |
masahikofukasawa | 11:cef8dc1cf010 | 428 | primaryId == AkmSensor::AKM_PRIMARY_ID_LATCH || |
masahikofukasawa | 11:cef8dc1cf010 | 429 | primaryId == AkmSensor::AKM_PRIMARY_ID_DUAL_OUTPUT || |
masahikofukasawa | 11:cef8dc1cf010 | 430 | primaryId == AkmSensor::AKM_PRIMARY_ID_ONECHIP_ENCODER ){ |
masahikofukasawa | 11:cef8dc1cf010 | 431 | Message temp; |
masahikofukasawa | 29:b488d2c89fba | 432 | sensor[sensorIndex]->readSensorData(&temp); |
masahikofukasawa | 11:cef8dc1cf010 | 433 | throwMessage(&temp); |
masahikofukasawa | 11:cef8dc1cf010 | 434 | } |
masahikofukasawa | 0:7a00359e701e | 435 | } |
masahikofukasawa | 0:7a00359e701e | 436 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 437 | } |
masahikofukasawa | 9:6fa3e7b17c27 | 438 | case Message::CMD_PROGSW_GET_THRESHOLD: |
masahikofukasawa | 9:6fa3e7b17c27 | 439 | case Message::CMD_PROGSW_SET_THRESHOLD: |
masahikofukasawa | 9:6fa3e7b17c27 | 440 | case Message::CMD_PROGSW_GET_READ_COFIGURATION: |
masahikofukasawa | 9:6fa3e7b17c27 | 441 | case Message::CMD_PROGSW_SET_READ_COFIGURATION: |
masahikofukasawa | 9:6fa3e7b17c27 | 442 | case Message::CMD_PROGSW_GET_SWITCH_COFIGURATION: |
masahikofukasawa | 9:6fa3e7b17c27 | 443 | case Message::CMD_PROGSW_SET_SWITCH_COFIGURATION: |
masahikofukasawa | 9:6fa3e7b17c27 | 444 | case Message::CMD_PROGSW_GET_OPERATION_MODE: |
masahikofukasawa | 9:6fa3e7b17c27 | 445 | case Message::CMD_PROGSW_SET_OPERATION_MODE: |
masahikofukasawa | 1:b46b8653331f | 446 | case Message::CMD_IR_GET_THRESHOLD: |
masahikofukasawa | 1:b46b8653331f | 447 | case Message::CMD_IR_SET_THRESHOLD: |
masahikofukasawa | 1:b46b8653331f | 448 | case Message::CMD_IR_GET_HYSTERESIS: |
masahikofukasawa | 1:b46b8653331f | 449 | case Message::CMD_IR_SET_HYSTERESIS: |
masahikofukasawa | 1:b46b8653331f | 450 | case Message::CMD_IR_GET_INTERRUPT: |
masahikofukasawa | 1:b46b8653331f | 451 | case Message::CMD_IR_SET_INTERRUPT: |
masahikofukasawa | 1:b46b8653331f | 452 | case Message::CMD_IR_GET_OPERATION_MODE: |
masahikofukasawa | 1:b46b8653331f | 453 | case Message::CMD_IR_SET_OPERATION_MODE: |
masahikofukasawa | 1:b46b8653331f | 454 | case Message::CMD_IR_GET_THRESHOLD_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 455 | case Message::CMD_IR_SET_THRESHOLD_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 456 | case Message::CMD_IR_GET_HYSTERESIS_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 457 | case Message::CMD_IR_SET_HYSTERESIS_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 458 | case Message::CMD_IR_GET_INTERRUPT_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 459 | case Message::CMD_IR_SET_INTERRUPT_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 460 | case Message::CMD_IR_GET_OPERATION_MODE_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 461 | case Message::CMD_IR_SET_OPERATION_MODE_EEPROM: |
masahikofukasawa | 9:6fa3e7b17c27 | 462 | case Message::CMD_ANGLE_ZERO_RESET: |
masahikofukasawa | 30:5a241d9b3262 | 463 | case Message::CMD_ANGLE_READ: |
masahikofukasawa | 9:6fa3e7b17c27 | 464 | case Message::CMD_REG_WRITE: |
masahikofukasawa | 9:6fa3e7b17c27 | 465 | case Message::CMD_REG_WRITEN: |
masahikofukasawa | 9:6fa3e7b17c27 | 466 | case Message::CMD_REG_READ: |
masahikofukasawa | 9:6fa3e7b17c27 | 467 | case Message::CMD_REG_READN: |
masahikofukasawa | 9:6fa3e7b17c27 | 468 | case Message::CMD_COMPASS_GET_OPERATION_MODE: |
masahikofukasawa | 9:6fa3e7b17c27 | 469 | case Message::CMD_COMPASS_SET_OPERATION_MODE: |
masahikofukasawa | 11:cef8dc1cf010 | 470 | { |
masahikofukasawa | 29:b488d2c89fba | 471 | AkmSensor::Status st = sensor[sensorIndex]->requestCommand(&msg,&resMsg); |
masahikofukasawa | 15:1238993fd75f | 472 | if( (resMsg.getArgNum() == 0) && (st != AkmSensor::SUCCESS) ) |
masahikofukasawa | 15:1238993fd75f | 473 | { |
masahikofukasawa | 15:1238993fd75f | 474 | MSG("#Command failed.\r\n"); |
masahikofukasawa | 15:1238993fd75f | 475 | }else{ |
masahikofukasawa | 15:1238993fd75f | 476 | throwMessage(&resMsg); |
masahikofukasawa | 15:1238993fd75f | 477 | } |
masahikofukasawa | 0:7a00359e701e | 478 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 479 | } |
masahikofukasawa | 0:7a00359e701e | 480 | default: |
masahikofukasawa | 11:cef8dc1cf010 | 481 | { |
masahikofukasawa | 15:1238993fd75f | 482 | MSG("#Can't find command.\r\n"); |
masahikofukasawa | 0:7a00359e701e | 483 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 484 | } |
masahikofukasawa | 0:7a00359e701e | 485 | } |
masahikofukasawa | 0:7a00359e701e | 486 | } |
masahikofukasawa | 0:7a00359e701e | 487 | |
masahikofukasawa | 0:7a00359e701e | 488 | AkmSensorManager::Status AkmSensorManager::processEvent() |
masahikofukasawa | 0:7a00359e701e | 489 | { |
masahikofukasawa | 0:7a00359e701e | 490 | AkmSensorManager::Status status = AkmSensorManager::SUCCESS; |
masahikofukasawa | 16:d85be9bafb80 | 491 | |
masahikofukasawa | 0:7a00359e701e | 492 | // command received from the host |
masahikofukasawa | 0:7a00359e701e | 493 | if(eventCommandReceived) |
masahikofukasawa | 0:7a00359e701e | 494 | { |
masahikofukasawa | 16:d85be9bafb80 | 495 | // MSG("#Command received.\r\n"); |
masahikofukasawa | 0:7a00359e701e | 496 | processCommand(); |
masahikofukasawa | 0:7a00359e701e | 497 | eventCommandReceived = false; |
masahikofukasawa | 0:7a00359e701e | 498 | } |
masahikofukasawa | 29:b488d2c89fba | 499 | |
masahikofukasawa | 29:b488d2c89fba | 500 | // check sensor event |
masahikofukasawa | 29:b488d2c89fba | 501 | for(int i=0; i<sensorNum; i++){ |
masahikofukasawa | 29:b488d2c89fba | 502 | if( sensor[i]->isEvent() ){ |
masahikofukasawa | 29:b488d2c89fba | 503 | Message msg; |
masahikofukasawa | 29:b488d2c89fba | 504 | if( sensor[i]->readSensorData(&msg) != AkmSensor::SUCCESS) status = AkmSensorManager::ERROR; |
masahikofukasawa | 29:b488d2c89fba | 505 | throwMessage(&msg); |
masahikofukasawa | 29:b488d2c89fba | 506 | } |
masahikofukasawa | 0:7a00359e701e | 507 | } |
masahikofukasawa | 29:b488d2c89fba | 508 | |
masahikofukasawa | 0:7a00359e701e | 509 | if(eventConnected) // BLE connected. Start sensor. |
masahikofukasawa | 0:7a00359e701e | 510 | { |
masahikofukasawa | 0:7a00359e701e | 511 | eventConnected = false; |
masahikofukasawa | 12:c06cd8b76358 | 512 | MSG("#BLE connected.\r\n"); |
masahikofukasawa | 0:7a00359e701e | 513 | } |
masahikofukasawa | 0:7a00359e701e | 514 | if(eventDisconnected) // BLE dis-connected. Stop sensor. |
masahikofukasawa | 0:7a00359e701e | 515 | { |
masahikofukasawa | 12:c06cd8b76358 | 516 | MSG("#BLE dis-connected.\r\n"); |
masahikofukasawa | 29:b488d2c89fba | 517 | for(int i=0; i<sensorNum; i++){ |
masahikofukasawa | 29:b488d2c89fba | 518 | if( sensor[i]->stopSensor() != AkmSensor::SUCCESS) status = AkmSensorManager::ERROR; |
masahikofukasawa | 29:b488d2c89fba | 519 | } |
masahikofukasawa | 12:c06cd8b76358 | 520 | eventDisconnected = false; |
masahikofukasawa | 0:7a00359e701e | 521 | } |
masahikofukasawa | 0:7a00359e701e | 522 | return status; |
masahikofukasawa | 0:7a00359e701e | 523 | } |
masahikofukasawa | 0:7a00359e701e | 524 | |
masahikofukasawa | 0:7a00359e701e | 525 | AkmSensorManager::Status AkmSensorManager::throwMessage(const Message *msg) { |
masahikofukasawa | 0:7a00359e701e | 526 | int len = Message::getMaxMessageLength(); |
masahikofukasawa | 0:7a00359e701e | 527 | char buf[len]; |
masahikofukasawa | 0:7a00359e701e | 528 | |
masahikofukasawa | 0:7a00359e701e | 529 | buf[0] = '$'; |
masahikofukasawa | 0:7a00359e701e | 530 | |
masahikofukasawa | 0:7a00359e701e | 531 | // Processes command |
masahikofukasawa | 0:7a00359e701e | 532 | char cmd = (char)msg->getCommand(); |
masahikofukasawa | 0:7a00359e701e | 533 | Message::charToAscii(&buf[1], &cmd); |
masahikofukasawa | 0:7a00359e701e | 534 | |
masahikofukasawa | 0:7a00359e701e | 535 | // Processes arguments |
masahikofukasawa | 0:7a00359e701e | 536 | for (int i=0; i < msg->getArgNum(); i++) { |
masahikofukasawa | 0:7a00359e701e | 537 | char arg = msg->getArgument(i); |
masahikofukasawa | 0:7a00359e701e | 538 | Message::charToAscii(&buf[3+2*i], &arg); |
masahikofukasawa | 0:7a00359e701e | 539 | } |
masahikofukasawa | 0:7a00359e701e | 540 | |
masahikofukasawa | 0:7a00359e701e | 541 | // Add termination characters, 0x0D(\r), \n and \0, to the end of string |
masahikofukasawa | 0:7a00359e701e | 542 | int tIdx = 3 + 2 * (msg->getArgNum()); |
masahikofukasawa | 0:7a00359e701e | 543 | int bufSize = sizeof(buf)/sizeof(buf[0]); |
masahikofukasawa | 0:7a00359e701e | 544 | if ((tIdx + 3) > (bufSize - 1)) { |
masahikofukasawa | 11:cef8dc1cf010 | 545 | MSG("#Error: Message data exceeds the buffer.\r\n"); |
masahikofukasawa | 0:7a00359e701e | 546 | return ERROR; |
masahikofukasawa | 0:7a00359e701e | 547 | } |
masahikofukasawa | 11:cef8dc1cf010 | 548 | buf[tIdx++] = CR; // '\r' |
masahikofukasawa | 11:cef8dc1cf010 | 549 | buf[tIdx++] = LF; // '\n' |
masahikofukasawa | 11:cef8dc1cf010 | 550 | buf[tIdx] = '\0'; |
masahikofukasawa | 0:7a00359e701e | 551 | if(isEnabledBle) uartService->writeString(buf); |
masahikofukasawa | 0:7a00359e701e | 552 | if(isEnabledUsb) serial->printf(buf); |
masahikofukasawa | 0:7a00359e701e | 553 | |
masahikofukasawa | 0:7a00359e701e | 554 | return SUCCESS; |
masahikofukasawa | 12:c06cd8b76358 | 555 | } |
masahikofukasawa | 13:d008249f0359 | 556 | |
masahikofukasawa | 29:b488d2c89fba | 557 | |
masahikofukasawa | 29:b488d2c89fba | 558 | |
masahikofukasawa | 27:41aa9fb23a2f | 559 | char* AkmSensorManager::my_strcat(char* str1, char* str2) |
masahikofukasawa | 27:41aa9fb23a2f | 560 | { |
masahikofukasawa | 27:41aa9fb23a2f | 561 | int num1; |
masahikofukasawa | 27:41aa9fb23a2f | 562 | char* str; |
masahikofukasawa | 27:41aa9fb23a2f | 563 | |
masahikofukasawa | 27:41aa9fb23a2f | 564 | num1=strlen(str1) + strlen(str2); |
masahikofukasawa | 27:41aa9fb23a2f | 565 | str = (char *)malloc(num1 + 1); |
masahikofukasawa | 27:41aa9fb23a2f | 566 | sprintf(str,"%s%s",str1,str2); |
masahikofukasawa | 27:41aa9fb23a2f | 567 | return str; |
masahikofukasawa | 27:41aa9fb23a2f | 568 | } |
masahikofukasawa | 27:41aa9fb23a2f | 569 | |
masahikofukasawa | 13:d008249f0359 | 570 | char* AkmSensorManager::getSensorName(){ |
masahikofukasawa | 29:b488d2c89fba | 571 | char* name = ""; |
masahikofukasawa | 29:b488d2c89fba | 572 | for(int i=0; i<sensorNum; i++){ |
masahikofukasawa | 29:b488d2c89fba | 573 | name = my_strcat(name, sensor[i]->getSensorName()); |
masahikofukasawa | 29:b488d2c89fba | 574 | if( sensorNum > (i+1) )name = my_strcat(name, "+"); |
masahikofukasawa | 27:41aa9fb23a2f | 575 | } |
masahikofukasawa | 27:41aa9fb23a2f | 576 | MSG("#Sensor Name='%s'.\r\n",name); |
masahikofukasawa | 27:41aa9fb23a2f | 577 | return name; |
masahikofukasawa | 13:d008249f0359 | 578 | } |