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