Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
akmsensormanager.cpp@13:d008249f0359, 2016-08-12 (annotated)
- Committer:
- masahikofukasawa
- Date:
- Fri Aug 12 00:04:48 2016 +0000
- Revision:
- 13:d008249f0359
- Parent:
- 12:c06cd8b76358
- Child:
- 15:1238993fd75f
Ver008. Modified for faster BLE connection, and sensor name in the BLE device name.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
masahikofukasawa | 0:7a00359e701e | 1 | #include "ble/services/UARTService.h" |
masahikofukasawa | 0:7a00359e701e | 2 | #include "akmsensormanager.h" |
masahikofukasawa | 0:7a00359e701e | 3 | #include "akmhallswitch.h" |
masahikofukasawa | 11:cef8dc1cf010 | 4 | #include "akmanalogsensor.h" |
masahikofukasawa | 10:5c69b067d88a | 5 | #include "ak09970ctrl.h" |
masahikofukasawa | 10:5c69b067d88a | 6 | #include "ak9750ctrl.h" |
masahikofukasawa | 10:5c69b067d88a | 7 | #include "ak9752ctrl.h" |
masahikofukasawa | 11:cef8dc1cf010 | 8 | #include "ak7451ctrl.h" |
masahikofukasawa | 10:5c69b067d88a | 9 | #include "ak7401ctrl.h" |
masahikofukasawa | 0:7a00359e701e | 10 | #include "akmakd.h" |
masahikofukasawa | 0:7a00359e701e | 11 | #include "debug.h" |
masahikofukasawa | 0:7a00359e701e | 12 | #include "Message.h" |
coisme | 2:11fe67783c4c | 13 | #ifdef REV_D |
coisme | 2:11fe67783c4c | 14 | #include "mcp342x.h" |
masahikofukasawa | 6:c4401549d68f | 15 | #include "I2CNano.h" |
masahikofukasawa | 7:e269411568c9 | 16 | #include "tca9554a.h" |
coisme | 2:11fe67783c4c | 17 | #endif |
masahikofukasawa | 0:7a00359e701e | 18 | |
masahikofukasawa | 11:cef8dc1cf010 | 19 | #define CR '\r' |
masahikofukasawa | 11:cef8dc1cf010 | 20 | #define LF '\n' |
masahikofukasawa | 0:7a00359e701e | 21 | #define FIRMWARE_VERSION 0x02 |
masahikofukasawa | 0:7a00359e701e | 22 | #define MAGNETOMETER_ID 0x0A |
masahikofukasawa | 0:7a00359e701e | 23 | |
masahikofukasawa | 0:7a00359e701e | 24 | #define CONV16I(high,low) ((int16_t)(((high) << 8) | (low))) |
masahikofukasawa | 0:7a00359e701e | 25 | |
masahikofukasawa | 0:7a00359e701e | 26 | |
masahikofukasawa | 10:5c69b067d88a | 27 | const char* AKM_PRIMARY_ID_STR[] = { |
masahikofukasawa | 7:e269411568c9 | 28 | "AKD Daughter Cards(SPI)", |
masahikofukasawa | 0:7a00359e701e | 29 | "Switch, Unipolar", |
masahikofukasawa | 0:7a00359e701e | 30 | "Switch, Onmipolar", |
masahikofukasawa | 0:7a00359e701e | 31 | "Latch, Bipolar", |
masahikofukasawa | 0:7a00359e701e | 32 | "Switch, Dual Output", |
masahikofukasawa | 0:7a00359e701e | 33 | "Onechip Encoder", |
masahikofukasawa | 0:7a00359e701e | 34 | "TBD1", |
masahikofukasawa | 0:7a00359e701e | 35 | "TBD2", |
masahikofukasawa | 0:7a00359e701e | 36 | "Linear Sensor Legacy", |
masahikofukasawa | 0:7a00359e701e | 37 | "Current Sensor", |
masahikofukasawa | 0:7a00359e701e | 38 | "MISC(Analog)", |
masahikofukasawa | 0:7a00359e701e | 39 | "Linear Sensor", |
masahikofukasawa | 4:af13b985c689 | 40 | "TBD3", |
masahikofukasawa | 1:b46b8653331f | 41 | "IR Sensor", |
masahikofukasawa | 7:e269411568c9 | 42 | "Angle Sensor(SPI)", |
masahikofukasawa | 10:5c69b067d88a | 43 | "AKD Daughter Cards(I2C)" |
masahikofukasawa | 0:7a00359e701e | 44 | }; |
masahikofukasawa | 0:7a00359e701e | 45 | |
masahikofukasawa | 13:d008249f0359 | 46 | AkmSensorManager::AkmSensorManager(SerialNano* com) |
masahikofukasawa | 0:7a00359e701e | 47 | { |
masahikofukasawa | 0:7a00359e701e | 48 | serial = com; |
masahikofukasawa | 0:7a00359e701e | 49 | isEnabledUsb = true; |
masahikofukasawa | 0:7a00359e701e | 50 | eventCommandReceived = false; |
masahikofukasawa | 0:7a00359e701e | 51 | eventConnected = false; |
masahikofukasawa | 0:7a00359e701e | 52 | eventDisconnected = false; |
masahikofukasawa | 0:7a00359e701e | 53 | } |
masahikofukasawa | 0:7a00359e701e | 54 | |
masahikofukasawa | 10:5c69b067d88a | 55 | AkmSensorManager::Status AkmSensorManager::init(uint8_t id, uint8_t subid) |
masahikofukasawa | 0:7a00359e701e | 56 | { |
masahikofukasawa | 10:5c69b067d88a | 57 | primaryId = id; |
masahikofukasawa | 10:5c69b067d88a | 58 | subId = subid; |
masahikofukasawa | 0:7a00359e701e | 59 | sensor = AkmSensorManager::getAkmSensor(); |
masahikofukasawa | 0:7a00359e701e | 60 | if(sensor == NULL) 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 | 0:7a00359e701e | 81 | AkmSensor* AkmSensorManager::getAkmSensor() |
masahikofukasawa | 0:7a00359e701e | 82 | { |
masahikofukasawa | 0:7a00359e701e | 83 | AkmSensor* sensor = NULL; |
masahikofukasawa | 0:7a00359e701e | 84 | |
masahikofukasawa | 10:5c69b067d88a | 85 | switch(primaryId){ |
masahikofukasawa | 7:e269411568c9 | 86 | case AkmSensor::AKM_PRIMARY_ID_AKD_SPI: |
masahikofukasawa | 0:7a00359e701e | 87 | case AkmSensor::AKM_PRIMARY_ID_AKD_I2C: |
masahikofukasawa | 10:5c69b067d88a | 88 | { |
masahikofukasawa | 10:5c69b067d88a | 89 | if(subId != Ak09970Ctrl::SUB_ID_AK09970){ |
masahikofukasawa | 10:5c69b067d88a | 90 | AkmAkd* akd = new AkmAkd(); |
masahikofukasawa | 10:5c69b067d88a | 91 | sensor = akd; |
masahikofukasawa | 10:5c69b067d88a | 92 | } |
masahikofukasawa | 10:5c69b067d88a | 93 | else{ |
masahikofukasawa | 10:5c69b067d88a | 94 | Ak09970Ctrl* ak09970 = new Ak09970Ctrl(); |
masahikofukasawa | 10:5c69b067d88a | 95 | sensor = ak09970; |
masahikofukasawa | 10:5c69b067d88a | 96 | } |
masahikofukasawa | 0:7a00359e701e | 97 | break; |
masahikofukasawa | 10:5c69b067d88a | 98 | } |
masahikofukasawa | 0:7a00359e701e | 99 | case AkmSensor::AKM_PRIMARY_ID_ANGLE_SENSOR: |
masahikofukasawa | 10:5c69b067d88a | 100 | { |
masahikofukasawa | 11:cef8dc1cf010 | 101 | if(subId == Ak7451Ctrl::SUB_ID_AK7451){ |
masahikofukasawa | 11:cef8dc1cf010 | 102 | Ak7451Ctrl* ak7451ctrl = new Ak7451Ctrl(); |
masahikofukasawa | 11:cef8dc1cf010 | 103 | sensor = ak7451ctrl; |
masahikofukasawa | 10:5c69b067d88a | 104 | break; |
masahikofukasawa | 10:5c69b067d88a | 105 | } |
masahikofukasawa | 10:5c69b067d88a | 106 | else if(subId == Ak7401Ctrl::SUB_ID_AK7401){ |
masahikofukasawa | 10:5c69b067d88a | 107 | Ak7401Ctrl* ak7401ctrl = new Ak7401Ctrl(); |
masahikofukasawa | 10:5c69b067d88a | 108 | sensor = ak7401ctrl; |
masahikofukasawa | 10:5c69b067d88a | 109 | break; |
masahikofukasawa | 10:5c69b067d88a | 110 | } |
masahikofukasawa | 0:7a00359e701e | 111 | break; |
masahikofukasawa | 10:5c69b067d88a | 112 | } |
masahikofukasawa | 0:7a00359e701e | 113 | case AkmSensor::AKM_PRIMARY_ID_UNIPOLAR: |
masahikofukasawa | 0:7a00359e701e | 114 | case AkmSensor::AKM_PRIMARY_ID_OMNIPOLAR: |
masahikofukasawa | 0:7a00359e701e | 115 | case AkmSensor::AKM_PRIMARY_ID_LATCH: |
masahikofukasawa | 0:7a00359e701e | 116 | case AkmSensor::AKM_PRIMARY_ID_DUAL_OUTPUT: |
masahikofukasawa | 0:7a00359e701e | 117 | case AkmSensor::AKM_PRIMARY_ID_ONECHIP_ENCODER: |
masahikofukasawa | 10:5c69b067d88a | 118 | { |
masahikofukasawa | 10:5c69b067d88a | 119 | AkmHallSwitch* hallswitch = new AkmHallSwitch(); |
masahikofukasawa | 0:7a00359e701e | 120 | sensor = hallswitch; |
masahikofukasawa | 0:7a00359e701e | 121 | break; |
masahikofukasawa | 10:5c69b067d88a | 122 | } |
masahikofukasawa | 0:7a00359e701e | 123 | case AkmSensor::AKM_PRIMARY_ID_LINEAR_SENSOR_LEGACY: |
masahikofukasawa | 4:af13b985c689 | 124 | case AkmSensor::AKM_PRIMARY_ID_LINEAR_SENSOR: |
masahikofukasawa | 10:5c69b067d88a | 125 | case AkmSensor::AKM_PRIMARY_ID_CURRENT_SENSOR: |
masahikofukasawa | 10:5c69b067d88a | 126 | case AkmSensor::AKM_PRIMARY_ID_MISC_ANALOG: |
masahikofukasawa | 10:5c69b067d88a | 127 | { |
masahikofukasawa | 11:cef8dc1cf010 | 128 | AkmAnalogSensor* analogsensor = new AkmAnalogSensor(); |
masahikofukasawa | 11:cef8dc1cf010 | 129 | sensor = analogsensor; |
masahikofukasawa | 0:7a00359e701e | 130 | break; |
masahikofukasawa | 10:5c69b067d88a | 131 | } |
masahikofukasawa | 1:b46b8653331f | 132 | case AkmSensor::AKM_PRIMARY_ID_IR_SENSOR: |
masahikofukasawa | 10:5c69b067d88a | 133 | { |
masahikofukasawa | 10:5c69b067d88a | 134 | if(subId == Ak9750Ctrl::SUB_ID_AK9750){ |
masahikofukasawa | 10:5c69b067d88a | 135 | Ak9750Ctrl* ak9750ctrl = new Ak9750Ctrl(); |
masahikofukasawa | 10:5c69b067d88a | 136 | sensor = ak9750ctrl; |
masahikofukasawa | 10:5c69b067d88a | 137 | }else if(subId == Ak9752Ctrl::SUB_ID_AK9752){ |
masahikofukasawa | 10:5c69b067d88a | 138 | Ak9752Ctrl* ak9752ctrl = new Ak9752Ctrl(); |
masahikofukasawa | 10:5c69b067d88a | 139 | sensor = ak9752ctrl; |
masahikofukasawa | 10:5c69b067d88a | 140 | }else{ |
masahikofukasawa | 10:5c69b067d88a | 141 | return NULL; // couldn't find |
masahikofukasawa | 10:5c69b067d88a | 142 | } |
masahikofukasawa | 10:5c69b067d88a | 143 | |
masahikofukasawa | 1:b46b8653331f | 144 | break; |
masahikofukasawa | 10:5c69b067d88a | 145 | } |
masahikofukasawa | 0:7a00359e701e | 146 | default: |
masahikofukasawa | 10:5c69b067d88a | 147 | { |
masahikofukasawa | 10:5c69b067d88a | 148 | MSG("#Can't find ID=%d SubID=%d %s\r\n", primaryId, subId, AKM_PRIMARY_ID_STR[primaryId]); |
masahikofukasawa | 0:7a00359e701e | 149 | return NULL; // couldn't find |
masahikofukasawa | 10:5c69b067d88a | 150 | } |
masahikofukasawa | 0:7a00359e701e | 151 | } |
masahikofukasawa | 11:cef8dc1cf010 | 152 | |
masahikofukasawa | 10:5c69b067d88a | 153 | if(sensor->init(primaryId, subId) != AkmSensor::SUCCESS){ |
masahikofukasawa | 10:5c69b067d88a | 154 | MSG("#sensor->init failed. ID=%d SubID=%d %s\r\n", primaryId, subId, AKM_PRIMARY_ID_STR[primaryId]); |
masahikofukasawa | 0:7a00359e701e | 155 | return NULL; // couldn't find |
masahikofukasawa | 0:7a00359e701e | 156 | } |
masahikofukasawa | 0:7a00359e701e | 157 | |
masahikofukasawa | 10:5c69b067d88a | 158 | MSG("#ID=%d SubID=%d %s\r\n", primaryId, subId, AKM_PRIMARY_ID_STR[primaryId]); |
masahikofukasawa | 0:7a00359e701e | 159 | return sensor; |
masahikofukasawa | 0:7a00359e701e | 160 | } |
masahikofukasawa | 0:7a00359e701e | 161 | |
masahikofukasawa | 0:7a00359e701e | 162 | AkmSensorManager::Status AkmSensorManager::commandReceived(char* buf){ |
masahikofukasawa | 0:7a00359e701e | 163 | // Construct message |
masahikofukasawa | 0:7a00359e701e | 164 | Status status = SUCCESS; |
masahikofukasawa | 0:7a00359e701e | 165 | |
masahikofukasawa | 10:5c69b067d88a | 166 | if ((Message::parse(&msg, buf)) != Message::SUCCESS) { |
masahikofukasawa | 11:cef8dc1cf010 | 167 | MSG("#Failed to parse message. %s\r\n", buf); |
masahikofukasawa | 0:7a00359e701e | 168 | status = ERROR; |
masahikofukasawa | 0:7a00359e701e | 169 | eventCommandReceived = false; |
masahikofukasawa | 0:7a00359e701e | 170 | } |
masahikofukasawa | 0:7a00359e701e | 171 | eventCommandReceived = true; |
masahikofukasawa | 0:7a00359e701e | 172 | return status; |
masahikofukasawa | 0:7a00359e701e | 173 | } |
masahikofukasawa | 0:7a00359e701e | 174 | |
coisme | 2:11fe67783c4c | 175 | #ifdef REV_D |
coisme | 2:11fe67783c4c | 176 | int16_t AkmSensorManager::getAdcData(MCP342X *mcp3428, MCP342X::AdcChannel ch, MCP342X::SampleSetting s) { |
coisme | 2:11fe67783c4c | 177 | const int WAIT_ADC_MS = 1; |
coisme | 2:11fe67783c4c | 178 | |
coisme | 2:11fe67783c4c | 179 | // Configure channel and trigger. |
coisme | 2:11fe67783c4c | 180 | mcp3428->setChannel(ch); |
coisme | 2:11fe67783c4c | 181 | mcp3428->setSampleSetting(s); |
coisme | 2:11fe67783c4c | 182 | mcp3428->trigger(); |
coisme | 2:11fe67783c4c | 183 | |
coisme | 2:11fe67783c4c | 184 | // polling data (!blocking) |
coisme | 2:11fe67783c4c | 185 | MCP342X::Data data; |
coisme | 2:11fe67783c4c | 186 | do { |
coisme | 2:11fe67783c4c | 187 | wait_ms(WAIT_ADC_MS); |
coisme | 2:11fe67783c4c | 188 | mcp3428->getData(&data); |
coisme | 2:11fe67783c4c | 189 | } while(data.st == MCP342X::DATA_NOT_UPDATED); |
coisme | 2:11fe67783c4c | 190 | |
coisme | 2:11fe67783c4c | 191 | return data.value; |
coisme | 2:11fe67783c4c | 192 | } |
coisme | 2:11fe67783c4c | 193 | #endif |
masahikofukasawa | 0:7a00359e701e | 194 | |
masahikofukasawa | 0:7a00359e701e | 195 | uint8_t AkmSensorManager::getId(PinName pin, uint8_t bits) |
masahikofukasawa | 0:7a00359e701e | 196 | { |
coisme | 2:11fe67783c4c | 197 | #ifndef REV_D |
coisme | 2:11fe67783c4c | 198 | /* Rev.C */ |
masahikofukasawa | 0:7a00359e701e | 199 | AnalogIn id(pin); |
masahikofukasawa | 11:cef8dc1cf010 | 200 | MSG("#Voltage=%5.2f[V]\r\n",id*3.0); |
masahikofukasawa | 0:7a00359e701e | 201 | double s = id + 1.0/(double)(pow(2.0,bits+1)); |
masahikofukasawa | 0:7a00359e701e | 202 | uint8_t value = (uint8_t)(s*pow(2.0,bits)); |
coisme | 2:11fe67783c4c | 203 | #else |
coisme | 2:11fe67783c4c | 204 | /* Rev.D */ |
masahikofukasawa | 11:cef8dc1cf010 | 205 | MSG("#GetID\r\n"); |
masahikofukasawa | 6:c4401549d68f | 206 | |
masahikofukasawa | 6:c4401549d68f | 207 | I2C i2c(I2C_SDA, I2C_SCL); |
coisme | 2:11fe67783c4c | 208 | // ADC |
coisme | 2:11fe67783c4c | 209 | MCP342X mcp342x(&i2c, MCP342X::SLAVE_ADDRESS_6EH); |
coisme | 2:11fe67783c4c | 210 | mcp342x.setConversionMode(MCP342X::ONE_SHOT); |
coisme | 2:11fe67783c4c | 211 | MCP342X::AdcChannel ch; |
coisme | 2:11fe67783c4c | 212 | if (pin == ANALOG_SENSOR_ID) { |
coisme | 2:11fe67783c4c | 213 | ch = MCP342X::ADC_CH1; |
coisme | 2:11fe67783c4c | 214 | } else { // pin == ANALOG_SENSOR_ID_SUB |
coisme | 2:11fe67783c4c | 215 | ch = MCP342X::ADC_CH2; |
coisme | 2:11fe67783c4c | 216 | } |
coisme | 2:11fe67783c4c | 217 | int16_t val = getAdcData(&mcp342x, ch, MCP342X::SAMPLE_240HZ_12BIT); |
masahikofukasawa | 11:cef8dc1cf010 | 218 | MSG("#12bit ADC Val = %d.\r\n", val); |
coisme | 2:11fe67783c4c | 219 | |
coisme | 2:11fe67783c4c | 220 | const int16_t VAL_MAX = 3000-2048; // Corresponds to 3V |
coisme | 2:11fe67783c4c | 221 | const int16_t VAL_MIN = -2048; // Corresponds to 0V |
coisme | 2:11fe67783c4c | 222 | |
masahikofukasawa | 6:c4401549d68f | 223 | uint8_t value = (uint8_t)((val - VAL_MIN)/(float)(VAL_MAX - VAL_MIN) * (1 << bits) + 0.5); |
masahikofukasawa | 11:cef8dc1cf010 | 224 | MSG("#ID = %d.\r\n", value); |
masahikofukasawa | 6:c4401549d68f | 225 | |
coisme | 2:11fe67783c4c | 226 | #endif |
masahikofukasawa | 0:7a00359e701e | 227 | return value; |
masahikofukasawa | 0:7a00359e701e | 228 | } |
masahikofukasawa | 0:7a00359e701e | 229 | |
masahikofukasawa | 0:7a00359e701e | 230 | bool AkmSensorManager::isEvent() |
masahikofukasawa | 0:7a00359e701e | 231 | { |
masahikofukasawa | 0:7a00359e701e | 232 | return (sensor->isEvent() || |
masahikofukasawa | 0:7a00359e701e | 233 | eventCommandReceived || |
masahikofukasawa | 0:7a00359e701e | 234 | eventConnected || |
masahikofukasawa | 0:7a00359e701e | 235 | eventDisconnected); |
masahikofukasawa | 0:7a00359e701e | 236 | } |
masahikofukasawa | 0:7a00359e701e | 237 | |
masahikofukasawa | 0:7a00359e701e | 238 | |
masahikofukasawa | 0:7a00359e701e | 239 | void AkmSensorManager::processCommand() |
masahikofukasawa | 0:7a00359e701e | 240 | { |
masahikofukasawa | 0:7a00359e701e | 241 | AkmSensorManager::Status status = AkmSensorManager::SUCCESS; |
masahikofukasawa | 0:7a00359e701e | 242 | |
masahikofukasawa | 0:7a00359e701e | 243 | // Gets command in the message |
masahikofukasawa | 0:7a00359e701e | 244 | Message::Command cmd = msg.getCommand(); |
masahikofukasawa | 0:7a00359e701e | 245 | |
masahikofukasawa | 0:7a00359e701e | 246 | // Creates an message object to return |
masahikofukasawa | 0:7a00359e701e | 247 | Message resMsg; |
masahikofukasawa | 0:7a00359e701e | 248 | |
masahikofukasawa | 0:7a00359e701e | 249 | // Return message has the same command as input |
masahikofukasawa | 0:7a00359e701e | 250 | resMsg.setCommand(cmd); |
masahikofukasawa | 0:7a00359e701e | 251 | |
masahikofukasawa | 0:7a00359e701e | 252 | switch(cmd) |
masahikofukasawa | 0:7a00359e701e | 253 | { |
masahikofukasawa | 0:7a00359e701e | 254 | case Message::CMD_GET_FW_VERSION: |
masahikofukasawa | 11:cef8dc1cf010 | 255 | { |
masahikofukasawa | 0:7a00359e701e | 256 | resMsg.setArgument(0, FIRMWARE_VERSION); |
masahikofukasawa | 0:7a00359e701e | 257 | throwMessage(&resMsg); |
masahikofukasawa | 11:cef8dc1cf010 | 258 | MSG("#FW version is reported.\r\n"); |
masahikofukasawa | 0:7a00359e701e | 259 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 260 | } |
masahikofukasawa | 0:7a00359e701e | 261 | case Message::CMD_GET_MAG_PART: |
masahikofukasawa | 11:cef8dc1cf010 | 262 | { |
masahikofukasawa | 0:7a00359e701e | 263 | resMsg.setArgument(0, MAGNETOMETER_ID); |
masahikofukasawa | 0:7a00359e701e | 264 | throwMessage(&resMsg); |
masahikofukasawa | 11:cef8dc1cf010 | 265 | MSG("#Mag ID is reported.\r\n"); |
masahikofukasawa | 0:7a00359e701e | 266 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 267 | } |
masahikofukasawa | 0:7a00359e701e | 268 | case Message::CMD_SET_SERIAL_TARGET: |
masahikofukasawa | 11:cef8dc1cf010 | 269 | { |
masahikofukasawa | 0:7a00359e701e | 270 | isEnabledBle = msg.getArgument(0)==Message::SW_ON ? true : false; |
masahikofukasawa | 0:7a00359e701e | 271 | isEnabledUsb = msg.getArgument(1)==Message::SW_ON ? true : false; |
masahikofukasawa | 0:7a00359e701e | 272 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 273 | } |
masahikofukasawa | 0:7a00359e701e | 274 | case Message::CMD_GET_ID: // return Primary ID and Sub ID |
masahikofukasawa | 11:cef8dc1cf010 | 275 | { |
masahikofukasawa | 10:5c69b067d88a | 276 | resMsg.setArgument(0, primaryId); |
masahikofukasawa | 0:7a00359e701e | 277 | resMsg.setArgument(1, subId); |
masahikofukasawa | 0:7a00359e701e | 278 | throwMessage(&resMsg); |
masahikofukasawa | 11:cef8dc1cf010 | 279 | MSG("#Mag ID is reported.\r\n"); |
masahikofukasawa | 0:7a00359e701e | 280 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 281 | } |
masahikofukasawa | 0:7a00359e701e | 282 | case Message::CMD_STOP_MEASUREMENT: |
masahikofukasawa | 11:cef8dc1cf010 | 283 | { |
masahikofukasawa | 0:7a00359e701e | 284 | if( sensor->stopSensor() != AkmSensor::SUCCESS) status = AkmSensorManager::ERROR; |
masahikofukasawa | 0:7a00359e701e | 285 | resMsg.setArgument(0, status==AkmSensorManager::SUCCESS ? 0 : 1); |
masahikofukasawa | 0:7a00359e701e | 286 | throwMessage(&resMsg); |
masahikofukasawa | 11:cef8dc1cf010 | 287 | MSG("#Stop measurement.\r\n"); |
masahikofukasawa | 0:7a00359e701e | 288 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 289 | } |
masahikofukasawa | 0:7a00359e701e | 290 | case Message::CMD_START_MEASUREMENT: |
masahikofukasawa | 11:cef8dc1cf010 | 291 | { |
masahikofukasawa | 11:cef8dc1cf010 | 292 | int error_code = AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 293 | if(msg.getArgNum() == 0){ |
masahikofukasawa | 11:cef8dc1cf010 | 294 | error_code = sensor->startSensor(); |
masahikofukasawa | 11:cef8dc1cf010 | 295 | if( error_code != AkmSensor::SUCCESS ){ |
masahikofukasawa | 11:cef8dc1cf010 | 296 | MSG("#StartSensor Error. Code=%d\r\n",error_code); |
masahikofukasawa | 11:cef8dc1cf010 | 297 | } |
masahikofukasawa | 11:cef8dc1cf010 | 298 | else{ |
masahikofukasawa | 11:cef8dc1cf010 | 299 | MSG("#Start measurement.\r\n"); |
masahikofukasawa | 11:cef8dc1cf010 | 300 | } |
masahikofukasawa | 11:cef8dc1cf010 | 301 | }else if(msg.getArgNum() == 1){ |
masahikofukasawa | 11:cef8dc1cf010 | 302 | float interval = (float)(1.0 / (float)msg.getArgument(0)); |
masahikofukasawa | 11:cef8dc1cf010 | 303 | error_code = sensor->startSensor(interval); |
masahikofukasawa | 11:cef8dc1cf010 | 304 | if( error_code != AkmSensor::SUCCESS ){ |
masahikofukasawa | 11:cef8dc1cf010 | 305 | MSG("#StartSensor Error. Code=%d\r\n",error_code); |
masahikofukasawa | 11:cef8dc1cf010 | 306 | } |
masahikofukasawa | 11:cef8dc1cf010 | 307 | else{ |
masahikofukasawa | 11:cef8dc1cf010 | 308 | MSG("#Start measurement.\r\n"); |
masahikofukasawa | 11:cef8dc1cf010 | 309 | } |
masahikofukasawa | 11:cef8dc1cf010 | 310 | }else{ |
masahikofukasawa | 11:cef8dc1cf010 | 311 | MSG("#StartSensor Error. Wrong Argument num.\r\n"); |
masahikofukasawa | 11:cef8dc1cf010 | 312 | } |
masahikofukasawa | 11:cef8dc1cf010 | 313 | if(error_code == AkmSensor::SUCCESS){ |
masahikofukasawa | 11:cef8dc1cf010 | 314 | // get initial sensor state for switch type sensors |
masahikofukasawa | 11:cef8dc1cf010 | 315 | if( primaryId == AkmSensor::AKM_PRIMARY_ID_UNIPOLAR || |
masahikofukasawa | 11:cef8dc1cf010 | 316 | primaryId == AkmSensor::AKM_PRIMARY_ID_OMNIPOLAR || |
masahikofukasawa | 11:cef8dc1cf010 | 317 | primaryId == AkmSensor::AKM_PRIMARY_ID_LATCH || |
masahikofukasawa | 11:cef8dc1cf010 | 318 | primaryId == AkmSensor::AKM_PRIMARY_ID_DUAL_OUTPUT || |
masahikofukasawa | 11:cef8dc1cf010 | 319 | primaryId == AkmSensor::AKM_PRIMARY_ID_ONECHIP_ENCODER ){ |
masahikofukasawa | 11:cef8dc1cf010 | 320 | Message temp; |
masahikofukasawa | 11:cef8dc1cf010 | 321 | sensor->readSensorData(&temp); |
masahikofukasawa | 11:cef8dc1cf010 | 322 | throwMessage(&temp); |
masahikofukasawa | 11:cef8dc1cf010 | 323 | } |
masahikofukasawa | 0:7a00359e701e | 324 | } |
masahikofukasawa | 0:7a00359e701e | 325 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 326 | } |
masahikofukasawa | 9:6fa3e7b17c27 | 327 | case Message::CMD_PROGSW_GET_THRESHOLD: |
masahikofukasawa | 9:6fa3e7b17c27 | 328 | case Message::CMD_PROGSW_SET_THRESHOLD: |
masahikofukasawa | 9:6fa3e7b17c27 | 329 | case Message::CMD_PROGSW_GET_READ_COFIGURATION: |
masahikofukasawa | 9:6fa3e7b17c27 | 330 | case Message::CMD_PROGSW_SET_READ_COFIGURATION: |
masahikofukasawa | 9:6fa3e7b17c27 | 331 | case Message::CMD_PROGSW_GET_SWITCH_COFIGURATION: |
masahikofukasawa | 9:6fa3e7b17c27 | 332 | case Message::CMD_PROGSW_SET_SWITCH_COFIGURATION: |
masahikofukasawa | 9:6fa3e7b17c27 | 333 | case Message::CMD_PROGSW_GET_OPERATION_MODE: |
masahikofukasawa | 9:6fa3e7b17c27 | 334 | case Message::CMD_PROGSW_SET_OPERATION_MODE: |
masahikofukasawa | 1:b46b8653331f | 335 | case Message::CMD_IR_GET_THRESHOLD: |
masahikofukasawa | 1:b46b8653331f | 336 | case Message::CMD_IR_SET_THRESHOLD: |
masahikofukasawa | 1:b46b8653331f | 337 | case Message::CMD_IR_GET_HYSTERESIS: |
masahikofukasawa | 1:b46b8653331f | 338 | case Message::CMD_IR_SET_HYSTERESIS: |
masahikofukasawa | 1:b46b8653331f | 339 | case Message::CMD_IR_GET_INTERRUPT: |
masahikofukasawa | 1:b46b8653331f | 340 | case Message::CMD_IR_SET_INTERRUPT: |
masahikofukasawa | 1:b46b8653331f | 341 | case Message::CMD_IR_GET_OPERATION_MODE: |
masahikofukasawa | 1:b46b8653331f | 342 | case Message::CMD_IR_SET_OPERATION_MODE: |
masahikofukasawa | 1:b46b8653331f | 343 | case Message::CMD_IR_GET_THRESHOLD_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 344 | case Message::CMD_IR_SET_THRESHOLD_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 345 | case Message::CMD_IR_GET_HYSTERESIS_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 346 | case Message::CMD_IR_SET_HYSTERESIS_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 347 | case Message::CMD_IR_GET_INTERRUPT_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 348 | case Message::CMD_IR_SET_INTERRUPT_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 349 | case Message::CMD_IR_GET_OPERATION_MODE_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 350 | case Message::CMD_IR_SET_OPERATION_MODE_EEPROM: |
masahikofukasawa | 9:6fa3e7b17c27 | 351 | case Message::CMD_ANGLE_ZERO_RESET: |
masahikofukasawa | 9:6fa3e7b17c27 | 352 | case Message::CMD_REG_WRITE: |
masahikofukasawa | 9:6fa3e7b17c27 | 353 | case Message::CMD_REG_WRITEN: |
masahikofukasawa | 9:6fa3e7b17c27 | 354 | case Message::CMD_REG_READ: |
masahikofukasawa | 9:6fa3e7b17c27 | 355 | case Message::CMD_REG_READN: |
masahikofukasawa | 9:6fa3e7b17c27 | 356 | case Message::CMD_COMPASS_GET_OPERATION_MODE: |
masahikofukasawa | 9:6fa3e7b17c27 | 357 | case Message::CMD_COMPASS_SET_OPERATION_MODE: |
masahikofukasawa | 11:cef8dc1cf010 | 358 | { |
masahikofukasawa | 0:7a00359e701e | 359 | sensor->requestCommand(&msg,&resMsg); |
masahikofukasawa | 0:7a00359e701e | 360 | throwMessage(&resMsg); |
masahikofukasawa | 0:7a00359e701e | 361 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 362 | } |
masahikofukasawa | 0:7a00359e701e | 363 | default: |
masahikofukasawa | 11:cef8dc1cf010 | 364 | { |
masahikofukasawa | 11:cef8dc1cf010 | 365 | MSG("#Can't find command:%s\r\n", (char)cmd); |
masahikofukasawa | 0:7a00359e701e | 366 | break; |
masahikofukasawa | 11:cef8dc1cf010 | 367 | } |
masahikofukasawa | 0:7a00359e701e | 368 | } |
masahikofukasawa | 0:7a00359e701e | 369 | } |
masahikofukasawa | 0:7a00359e701e | 370 | |
masahikofukasawa | 0:7a00359e701e | 371 | AkmSensorManager::Status AkmSensorManager::processEvent() |
masahikofukasawa | 0:7a00359e701e | 372 | { |
masahikofukasawa | 0:7a00359e701e | 373 | AkmSensorManager::Status status = AkmSensorManager::SUCCESS; |
masahikofukasawa | 0:7a00359e701e | 374 | |
masahikofukasawa | 0:7a00359e701e | 375 | // command received from the host |
masahikofukasawa | 0:7a00359e701e | 376 | if(eventCommandReceived) |
masahikofukasawa | 0:7a00359e701e | 377 | { |
masahikofukasawa | 0:7a00359e701e | 378 | processCommand(); |
masahikofukasawa | 0:7a00359e701e | 379 | eventCommandReceived = false; |
masahikofukasawa | 0:7a00359e701e | 380 | } |
masahikofukasawa | 0:7a00359e701e | 381 | if(sensor->isEvent()) // sensor read data event |
masahikofukasawa | 0:7a00359e701e | 382 | { |
masahikofukasawa | 0:7a00359e701e | 383 | Message msg; |
masahikofukasawa | 0:7a00359e701e | 384 | if( sensor->readSensorData(&msg) != AkmSensor::SUCCESS) status = AkmSensorManager::ERROR; |
masahikofukasawa | 0:7a00359e701e | 385 | throwMessage(&msg); |
masahikofukasawa | 0:7a00359e701e | 386 | } |
masahikofukasawa | 0:7a00359e701e | 387 | |
masahikofukasawa | 0:7a00359e701e | 388 | if(eventConnected) // BLE connected. Start sensor. |
masahikofukasawa | 0:7a00359e701e | 389 | { |
masahikofukasawa | 0:7a00359e701e | 390 | eventConnected = false; |
masahikofukasawa | 12:c06cd8b76358 | 391 | MSG("#BLE connected.\r\n"); |
masahikofukasawa | 0:7a00359e701e | 392 | } |
masahikofukasawa | 0:7a00359e701e | 393 | if(eventDisconnected) // BLE dis-connected. Stop sensor. |
masahikofukasawa | 0:7a00359e701e | 394 | { |
masahikofukasawa | 12:c06cd8b76358 | 395 | MSG("#BLE dis-connected.\r\n"); |
masahikofukasawa | 0:7a00359e701e | 396 | if( sensor->stopSensor() != AkmSensor::SUCCESS) status = AkmSensorManager::ERROR; |
masahikofukasawa | 12:c06cd8b76358 | 397 | eventDisconnected = false; |
masahikofukasawa | 0:7a00359e701e | 398 | } |
masahikofukasawa | 0:7a00359e701e | 399 | return status; |
masahikofukasawa | 0:7a00359e701e | 400 | } |
masahikofukasawa | 0:7a00359e701e | 401 | |
masahikofukasawa | 0:7a00359e701e | 402 | AkmSensorManager::Status AkmSensorManager::throwMessage(const Message *msg) { |
masahikofukasawa | 0:7a00359e701e | 403 | int len = Message::getMaxMessageLength(); |
masahikofukasawa | 0:7a00359e701e | 404 | char buf[len]; |
masahikofukasawa | 0:7a00359e701e | 405 | |
masahikofukasawa | 0:7a00359e701e | 406 | buf[0] = '$'; |
masahikofukasawa | 0:7a00359e701e | 407 | |
masahikofukasawa | 0:7a00359e701e | 408 | // Processes command |
masahikofukasawa | 0:7a00359e701e | 409 | char cmd = (char)msg->getCommand(); |
masahikofukasawa | 0:7a00359e701e | 410 | Message::charToAscii(&buf[1], &cmd); |
masahikofukasawa | 0:7a00359e701e | 411 | |
masahikofukasawa | 0:7a00359e701e | 412 | // Processes arguments |
masahikofukasawa | 0:7a00359e701e | 413 | for (int i=0; i < msg->getArgNum(); i++) { |
masahikofukasawa | 0:7a00359e701e | 414 | char arg = msg->getArgument(i); |
masahikofukasawa | 0:7a00359e701e | 415 | Message::charToAscii(&buf[3+2*i], &arg); |
masahikofukasawa | 0:7a00359e701e | 416 | } |
masahikofukasawa | 0:7a00359e701e | 417 | |
masahikofukasawa | 0:7a00359e701e | 418 | // Add termination characters, 0x0D(\r), \n and \0, to the end of string |
masahikofukasawa | 0:7a00359e701e | 419 | int tIdx = 3 + 2 * (msg->getArgNum()); |
masahikofukasawa | 0:7a00359e701e | 420 | int bufSize = sizeof(buf)/sizeof(buf[0]); |
masahikofukasawa | 0:7a00359e701e | 421 | if ((tIdx + 3) > (bufSize - 1)) { |
masahikofukasawa | 11:cef8dc1cf010 | 422 | MSG("#Error: Message data exceeds the buffer.\r\n"); |
masahikofukasawa | 0:7a00359e701e | 423 | return ERROR; |
masahikofukasawa | 0:7a00359e701e | 424 | } |
masahikofukasawa | 11:cef8dc1cf010 | 425 | buf[tIdx++] = CR; // '\r' |
masahikofukasawa | 11:cef8dc1cf010 | 426 | buf[tIdx++] = LF; // '\n' |
masahikofukasawa | 11:cef8dc1cf010 | 427 | buf[tIdx] = '\0'; |
masahikofukasawa | 0:7a00359e701e | 428 | if(isEnabledBle) uartService->writeString(buf); |
masahikofukasawa | 0:7a00359e701e | 429 | if(isEnabledUsb) serial->printf(buf); |
masahikofukasawa | 0:7a00359e701e | 430 | |
masahikofukasawa | 0:7a00359e701e | 431 | return SUCCESS; |
masahikofukasawa | 12:c06cd8b76358 | 432 | } |
masahikofukasawa | 13:d008249f0359 | 433 | |
masahikofukasawa | 13:d008249f0359 | 434 | char* AkmSensorManager::getSensorName(){ |
masahikofukasawa | 13:d008249f0359 | 435 | return sensor->getSensorName(); |
masahikofukasawa | 13:d008249f0359 | 436 | } |