Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
akmsensormanager.cpp@6:c4401549d68f, 2016-05-13 (annotated)
- Committer:
- masahikofukasawa
- Date:
- Fri May 13 23:52:37 2016 +0000
- Revision:
- 6:c4401549d68f
- Parent:
- 4:af13b985c689
- Child:
- 7:e269411568c9
Added current sensor
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 "akmanglesensor.h" |
masahikofukasawa | 0:7a00359e701e | 4 | #include "akmhallswitch.h" |
masahikofukasawa | 0:7a00359e701e | 5 | #include "akmlinearsensor.h" |
masahikofukasawa | 6:c4401549d68f | 6 | #include "akmcurrentsensor.h" |
masahikofukasawa | 1:b46b8653331f | 7 | #include "akmirsensor.h" |
masahikofukasawa | 0:7a00359e701e | 8 | #include "akmakd.h" |
masahikofukasawa | 0:7a00359e701e | 9 | #include "debug.h" |
masahikofukasawa | 0:7a00359e701e | 10 | #include "Message.h" |
coisme | 2:11fe67783c4c | 11 | #ifdef REV_D |
coisme | 2:11fe67783c4c | 12 | #include "mcp342x.h" |
masahikofukasawa | 6:c4401549d68f | 13 | #include "I2CNano.h" |
coisme | 2:11fe67783c4c | 14 | #endif |
masahikofukasawa | 0:7a00359e701e | 15 | |
masahikofukasawa | 0:7a00359e701e | 16 | #define FIRMWARE_VERSION 0x02 |
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 | 0:7a00359e701e | 22 | static char* AKM_PRIMARY_ID_STR[] = { |
masahikofukasawa | 0:7a00359e701e | 23 | "AKD Daughter Cards(SPI)", // Temporary |
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 | 0:7a00359e701e | 31 | "Linear Sensor Legacy", |
masahikofukasawa | 0:7a00359e701e | 32 | "Current Sensor", |
masahikofukasawa | 0:7a00359e701e | 33 | "MISC(Analog)", |
masahikofukasawa | 0:7a00359e701e | 34 | "Linear Sensor", |
masahikofukasawa | 4:af13b985c689 | 35 | "TBD3", |
masahikofukasawa | 1:b46b8653331f | 36 | "IR Sensor", |
masahikofukasawa | 0:7a00359e701e | 37 | "Angle Sensor(SPI)", // Temporary |
masahikofukasawa | 0:7a00359e701e | 38 | "AKD Daughter Cards(I2C)", |
masahikofukasawa | 0:7a00359e701e | 39 | }; |
masahikofukasawa | 0:7a00359e701e | 40 | |
masahikofukasawa | 0:7a00359e701e | 41 | AkmSensorManager::AkmSensorManager(SerialNano* com, UARTService* service) |
masahikofukasawa | 0:7a00359e701e | 42 | { |
masahikofukasawa | 0:7a00359e701e | 43 | serial = com; |
masahikofukasawa | 0:7a00359e701e | 44 | uartService = service; |
masahikofukasawa | 0:7a00359e701e | 45 | isEnabledBle = true; |
masahikofukasawa | 0:7a00359e701e | 46 | isEnabledUsb = true; |
masahikofukasawa | 0:7a00359e701e | 47 | eventCommandReceived = false; |
masahikofukasawa | 0:7a00359e701e | 48 | eventConnected = false; |
masahikofukasawa | 0:7a00359e701e | 49 | eventDisconnected = false; |
masahikofukasawa | 0:7a00359e701e | 50 | } |
masahikofukasawa | 0:7a00359e701e | 51 | |
masahikofukasawa | 0:7a00359e701e | 52 | AkmSensorManager::Status AkmSensorManager::init() |
masahikofukasawa | 0:7a00359e701e | 53 | { |
masahikofukasawa | 0:7a00359e701e | 54 | sensor = AkmSensorManager::getAkmSensor(); |
masahikofukasawa | 0:7a00359e701e | 55 | if(sensor == NULL) return AkmSensorManager::ERROR; |
masahikofukasawa | 0:7a00359e701e | 56 | return AkmSensorManager::SUCCESS; |
masahikofukasawa | 0:7a00359e701e | 57 | } |
masahikofukasawa | 0:7a00359e701e | 58 | |
masahikofukasawa | 0:7a00359e701e | 59 | void AkmSensorManager::setEventConnected() |
masahikofukasawa | 0:7a00359e701e | 60 | { |
masahikofukasawa | 0:7a00359e701e | 61 | eventConnected = true; |
masahikofukasawa | 0:7a00359e701e | 62 | } |
masahikofukasawa | 0:7a00359e701e | 63 | |
masahikofukasawa | 0:7a00359e701e | 64 | void AkmSensorManager::setEventDisconnected() |
masahikofukasawa | 0:7a00359e701e | 65 | { |
masahikofukasawa | 0:7a00359e701e | 66 | eventDisconnected = true; |
masahikofukasawa | 0:7a00359e701e | 67 | } |
masahikofukasawa | 0:7a00359e701e | 68 | |
masahikofukasawa | 0:7a00359e701e | 69 | AkmSensor* AkmSensorManager::getAkmSensor() |
masahikofukasawa | 0:7a00359e701e | 70 | { |
masahikofukasawa | 0:7a00359e701e | 71 | AkmSensor* sensor = NULL; |
masahikofukasawa | 0:7a00359e701e | 72 | |
masahikofukasawa | 0:7a00359e701e | 73 | // primary id check |
masahikofukasawa | 0:7a00359e701e | 74 | id = AkmSensorManager::getId(ANALOG_SENSOR_ID,4); |
masahikofukasawa | 6:c4401549d68f | 75 | // id=14; |
masahikofukasawa | 6:c4401549d68f | 76 | // subId=1; |
masahikofukasawa | 0:7a00359e701e | 77 | |
masahikofukasawa | 0:7a00359e701e | 78 | switch(id){ |
masahikofukasawa | 0:7a00359e701e | 79 | case AkmSensor::AKM_PRIMARY_ID_AKD_I2C: |
masahikofukasawa | 0:7a00359e701e | 80 | subId = AkmSensorManager::getId(ANALOG_SENSOR_ID_SUB,5); // 5bit sub id |
masahikofukasawa | 0:7a00359e701e | 81 | AkmAkd* akd; |
masahikofukasawa | 0:7a00359e701e | 82 | akd = new AkmAkd(); |
masahikofukasawa | 0:7a00359e701e | 83 | sensor = akd; |
masahikofukasawa | 0:7a00359e701e | 84 | break; |
masahikofukasawa | 0:7a00359e701e | 85 | |
masahikofukasawa | 6:c4401549d68f | 86 | // case AkmSensor::AKM_PRIMARY_ID_AKD_SPI: // Temporary |
masahikofukasawa | 0:7a00359e701e | 87 | case AkmSensor::AKM_PRIMARY_ID_ANGLE_SENSOR: |
masahikofukasawa | 0:7a00359e701e | 88 | subId = AkmSensorManager::getId(ANALOG_SENSOR_ID_SUB,4); // 4bit sub id |
masahikofukasawa | 0:7a00359e701e | 89 | AkmAngleSensor* angleSensor; |
masahikofukasawa | 0:7a00359e701e | 90 | angleSensor = new AkmAngleSensor(); |
masahikofukasawa | 0:7a00359e701e | 91 | sensor = angleSensor; |
masahikofukasawa | 0:7a00359e701e | 92 | break; |
masahikofukasawa | 0:7a00359e701e | 93 | |
masahikofukasawa | 0:7a00359e701e | 94 | case AkmSensor::AKM_PRIMARY_ID_UNIPOLAR: |
masahikofukasawa | 0:7a00359e701e | 95 | case AkmSensor::AKM_PRIMARY_ID_OMNIPOLAR: |
masahikofukasawa | 0:7a00359e701e | 96 | case AkmSensor::AKM_PRIMARY_ID_LATCH: |
masahikofukasawa | 0:7a00359e701e | 97 | case AkmSensor::AKM_PRIMARY_ID_DUAL_OUTPUT: |
masahikofukasawa | 0:7a00359e701e | 98 | case AkmSensor::AKM_PRIMARY_ID_ONECHIP_ENCODER: |
masahikofukasawa | 0:7a00359e701e | 99 | subId = AkmSensorManager::getId(ANALOG_SENSOR_ID_SUB,4); // 4bit sub id |
masahikofukasawa | 0:7a00359e701e | 100 | AkmHallSwitch* hallswitch; |
masahikofukasawa | 0:7a00359e701e | 101 | hallswitch = new AkmHallSwitch(); |
masahikofukasawa | 0:7a00359e701e | 102 | sensor = hallswitch; |
masahikofukasawa | 0:7a00359e701e | 103 | break; |
masahikofukasawa | 0:7a00359e701e | 104 | |
masahikofukasawa | 0:7a00359e701e | 105 | case AkmSensor::AKM_PRIMARY_ID_LINEAR_SENSOR_LEGACY: |
masahikofukasawa | 4:af13b985c689 | 106 | case AkmSensor::AKM_PRIMARY_ID_LINEAR_SENSOR: |
masahikofukasawa | 0:7a00359e701e | 107 | subId = AkmSensorManager::getId(ANALOG_SENSOR_ID_SUB,4); // 4bit sub id |
masahikofukasawa | 0:7a00359e701e | 108 | AkmLinearSensor* linearsensor; |
masahikofukasawa | 0:7a00359e701e | 109 | linearsensor = new AkmLinearSensor(); |
masahikofukasawa | 0:7a00359e701e | 110 | sensor = linearsensor; |
masahikofukasawa | 0:7a00359e701e | 111 | break; |
masahikofukasawa | 0:7a00359e701e | 112 | |
masahikofukasawa | 6:c4401549d68f | 113 | case AkmSensor::AKM_PRIMARY_ID_CURRENT_SENSOR: |
masahikofukasawa | 6:c4401549d68f | 114 | subId = AkmSensorManager::getId(ANALOG_SENSOR_ID_SUB,4); // 4bit sub id |
masahikofukasawa | 6:c4401549d68f | 115 | AkmCurrentSensor* currentsensor; |
masahikofukasawa | 6:c4401549d68f | 116 | currentsensor = new AkmCurrentSensor(); |
masahikofukasawa | 6:c4401549d68f | 117 | sensor = currentsensor; |
masahikofukasawa | 6:c4401549d68f | 118 | break; |
masahikofukasawa | 6:c4401549d68f | 119 | |
masahikofukasawa | 1:b46b8653331f | 120 | case AkmSensor::AKM_PRIMARY_ID_IR_SENSOR: |
masahikofukasawa | 1:b46b8653331f | 121 | subId = AkmSensorManager::getId(ANALOG_SENSOR_ID_SUB,4); // 4bit sub id |
masahikofukasawa | 1:b46b8653331f | 122 | AkmIrSensor* irsensor; |
masahikofukasawa | 1:b46b8653331f | 123 | irsensor = new AkmIrSensor(); |
masahikofukasawa | 1:b46b8653331f | 124 | sensor = irsensor; |
masahikofukasawa | 1:b46b8653331f | 125 | break; |
masahikofukasawa | 1:b46b8653331f | 126 | |
masahikofukasawa | 0:7a00359e701e | 127 | default: |
masahikofukasawa | 0:7a00359e701e | 128 | subId = AkmSensorManager::getId(ANALOG_SENSOR_ID_SUB,4); // 4bit sub id |
masahikofukasawa | 0:7a00359e701e | 129 | MSG("#Can't find ID=%d SubID=%d %s\r\n", id, subId, AKM_PRIMARY_ID_STR[id]); |
masahikofukasawa | 0:7a00359e701e | 130 | return NULL; // couldn't find |
masahikofukasawa | 0:7a00359e701e | 131 | } |
masahikofukasawa | 0:7a00359e701e | 132 | |
masahikofukasawa | 0:7a00359e701e | 133 | if(sensor->init(id, subId) != AkmSensor::SUCCESS){ |
masahikofukasawa | 1:b46b8653331f | 134 | MSG("#sensor->init failed. ID=%d SubID=%d %s\r\n", id, subId, AKM_PRIMARY_ID_STR[id]); |
masahikofukasawa | 0:7a00359e701e | 135 | return NULL; // couldn't find |
masahikofukasawa | 0:7a00359e701e | 136 | } |
masahikofukasawa | 0:7a00359e701e | 137 | |
masahikofukasawa | 0:7a00359e701e | 138 | MSG("#ID=%d SubID=%d %s\r\n", id, subId, AKM_PRIMARY_ID_STR[id]); |
masahikofukasawa | 0:7a00359e701e | 139 | return sensor; |
masahikofukasawa | 0:7a00359e701e | 140 | } |
masahikofukasawa | 0:7a00359e701e | 141 | |
masahikofukasawa | 0:7a00359e701e | 142 | AkmSensorManager::Status AkmSensorManager::commandReceived(char* buf){ |
masahikofukasawa | 0:7a00359e701e | 143 | // Construct message |
masahikofukasawa | 0:7a00359e701e | 144 | Status status = SUCCESS; |
masahikofukasawa | 0:7a00359e701e | 145 | |
masahikofukasawa | 0:7a00359e701e | 146 | Message::Status st; |
masahikofukasawa | 0:7a00359e701e | 147 | if ((st=Message::parse(&msg, buf)) != Message::SUCCESS) { |
masahikofukasawa | 0:7a00359e701e | 148 | MSG("#Failed to parse message. status = %02x. %s\n", st, buf); |
masahikofukasawa | 0:7a00359e701e | 149 | status = ERROR; |
masahikofukasawa | 0:7a00359e701e | 150 | eventCommandReceived = false; |
masahikofukasawa | 0:7a00359e701e | 151 | } |
masahikofukasawa | 0:7a00359e701e | 152 | eventCommandReceived = true; |
masahikofukasawa | 0:7a00359e701e | 153 | return status; |
masahikofukasawa | 0:7a00359e701e | 154 | } |
masahikofukasawa | 0:7a00359e701e | 155 | |
coisme | 2:11fe67783c4c | 156 | #ifdef REV_D |
coisme | 2:11fe67783c4c | 157 | int16_t AkmSensorManager::getAdcData(MCP342X *mcp3428, MCP342X::AdcChannel ch, MCP342X::SampleSetting s) { |
coisme | 2:11fe67783c4c | 158 | const int WAIT_ADC_MS = 1; |
coisme | 2:11fe67783c4c | 159 | |
coisme | 2:11fe67783c4c | 160 | // Configure channel and trigger. |
coisme | 2:11fe67783c4c | 161 | mcp3428->setChannel(ch); |
coisme | 2:11fe67783c4c | 162 | mcp3428->setSampleSetting(s); |
coisme | 2:11fe67783c4c | 163 | mcp3428->trigger(); |
coisme | 2:11fe67783c4c | 164 | |
coisme | 2:11fe67783c4c | 165 | // polling data (!blocking) |
coisme | 2:11fe67783c4c | 166 | MCP342X::Data data; |
coisme | 2:11fe67783c4c | 167 | do { |
coisme | 2:11fe67783c4c | 168 | wait_ms(WAIT_ADC_MS); |
coisme | 2:11fe67783c4c | 169 | mcp3428->getData(&data); |
coisme | 2:11fe67783c4c | 170 | } while(data.st == MCP342X::DATA_NOT_UPDATED); |
coisme | 2:11fe67783c4c | 171 | |
coisme | 2:11fe67783c4c | 172 | return data.value; |
coisme | 2:11fe67783c4c | 173 | } |
coisme | 2:11fe67783c4c | 174 | #endif |
masahikofukasawa | 0:7a00359e701e | 175 | |
masahikofukasawa | 0:7a00359e701e | 176 | uint8_t AkmSensorManager::getId(PinName pin, uint8_t bits) |
masahikofukasawa | 0:7a00359e701e | 177 | { |
coisme | 2:11fe67783c4c | 178 | #ifndef REV_D |
coisme | 2:11fe67783c4c | 179 | /* Rev.C */ |
masahikofukasawa | 0:7a00359e701e | 180 | AnalogIn id(pin); |
masahikofukasawa | 1:b46b8653331f | 181 | MSG("#Voltage=%5.2f[V]\n",id*3.0); |
masahikofukasawa | 0:7a00359e701e | 182 | double s = id + 1.0/(double)(pow(2.0,bits+1)); |
masahikofukasawa | 0:7a00359e701e | 183 | uint8_t value = (uint8_t)(s*pow(2.0,bits)); |
coisme | 2:11fe67783c4c | 184 | #else |
coisme | 2:11fe67783c4c | 185 | /* Rev.D */ |
masahikofukasawa | 4:af13b985c689 | 186 | MSG("#GetID\n"); |
masahikofukasawa | 6:c4401549d68f | 187 | |
masahikofukasawa | 6:c4401549d68f | 188 | I2C i2c(I2C_SDA, I2C_SCL); |
coisme | 2:11fe67783c4c | 189 | // ADC |
coisme | 2:11fe67783c4c | 190 | MCP342X mcp342x(&i2c, MCP342X::SLAVE_ADDRESS_6EH); |
coisme | 2:11fe67783c4c | 191 | mcp342x.setConversionMode(MCP342X::ONE_SHOT); |
coisme | 2:11fe67783c4c | 192 | MCP342X::AdcChannel ch; |
coisme | 2:11fe67783c4c | 193 | if (pin == ANALOG_SENSOR_ID) { |
coisme | 2:11fe67783c4c | 194 | ch = MCP342X::ADC_CH1; |
coisme | 2:11fe67783c4c | 195 | } else { // pin == ANALOG_SENSOR_ID_SUB |
coisme | 2:11fe67783c4c | 196 | ch = MCP342X::ADC_CH2; |
coisme | 2:11fe67783c4c | 197 | } |
coisme | 2:11fe67783c4c | 198 | int16_t val = getAdcData(&mcp342x, ch, MCP342X::SAMPLE_240HZ_12BIT); |
coisme | 2:11fe67783c4c | 199 | MSG("#12bit ADC Val = %d.\n", val); |
coisme | 2:11fe67783c4c | 200 | |
coisme | 2:11fe67783c4c | 201 | const int16_t VAL_MAX = 3000-2048; // Corresponds to 3V |
coisme | 2:11fe67783c4c | 202 | const int16_t VAL_MIN = -2048; // Corresponds to 0V |
coisme | 2:11fe67783c4c | 203 | |
masahikofukasawa | 6:c4401549d68f | 204 | uint8_t value = (uint8_t)((val - VAL_MIN)/(float)(VAL_MAX - VAL_MIN) * (1 << bits) + 0.5); |
masahikofukasawa | 4:af13b985c689 | 205 | MSG("#ID = %d.\n", value); |
masahikofukasawa | 6:c4401549d68f | 206 | |
masahikofukasawa | 6:c4401549d68f | 207 | // I2C turn off |
masahikofukasawa | 6:c4401549d68f | 208 | releaseTWI(); |
masahikofukasawa | 6:c4401549d68f | 209 | |
coisme | 2:11fe67783c4c | 210 | #endif |
masahikofukasawa | 0:7a00359e701e | 211 | return value; |
masahikofukasawa | 0:7a00359e701e | 212 | } |
masahikofukasawa | 0:7a00359e701e | 213 | |
masahikofukasawa | 0:7a00359e701e | 214 | bool AkmSensorManager::isEvent() |
masahikofukasawa | 0:7a00359e701e | 215 | { |
masahikofukasawa | 0:7a00359e701e | 216 | return (sensor->isEvent() || |
masahikofukasawa | 0:7a00359e701e | 217 | eventCommandReceived || |
masahikofukasawa | 0:7a00359e701e | 218 | eventConnected || |
masahikofukasawa | 0:7a00359e701e | 219 | eventDisconnected); |
masahikofukasawa | 0:7a00359e701e | 220 | } |
masahikofukasawa | 0:7a00359e701e | 221 | |
masahikofukasawa | 0:7a00359e701e | 222 | |
masahikofukasawa | 0:7a00359e701e | 223 | void AkmSensorManager::processCommand() |
masahikofukasawa | 0:7a00359e701e | 224 | { |
masahikofukasawa | 0:7a00359e701e | 225 | AkmSensorManager::Status status = AkmSensorManager::SUCCESS; |
masahikofukasawa | 0:7a00359e701e | 226 | // MSG("#processCommand.\n"); |
masahikofukasawa | 0:7a00359e701e | 227 | |
masahikofukasawa | 0:7a00359e701e | 228 | // Gets command in the message |
masahikofukasawa | 0:7a00359e701e | 229 | Message::Command cmd = msg.getCommand(); |
masahikofukasawa | 0:7a00359e701e | 230 | |
masahikofukasawa | 0:7a00359e701e | 231 | // Creates an message object to return |
masahikofukasawa | 0:7a00359e701e | 232 | Message resMsg; |
masahikofukasawa | 0:7a00359e701e | 233 | |
masahikofukasawa | 0:7a00359e701e | 234 | // Return message has the same command as input |
masahikofukasawa | 0:7a00359e701e | 235 | resMsg.setCommand(cmd); |
masahikofukasawa | 0:7a00359e701e | 236 | |
masahikofukasawa | 0:7a00359e701e | 237 | switch(cmd) |
masahikofukasawa | 0:7a00359e701e | 238 | { |
masahikofukasawa | 0:7a00359e701e | 239 | case Message::CMD_GET_FW_VERSION: |
masahikofukasawa | 0:7a00359e701e | 240 | resMsg.setArgument(0, FIRMWARE_VERSION); |
masahikofukasawa | 0:7a00359e701e | 241 | throwMessage(&resMsg); |
masahikofukasawa | 0:7a00359e701e | 242 | MSG("#FW version is reported.\n"); |
masahikofukasawa | 0:7a00359e701e | 243 | break; |
masahikofukasawa | 0:7a00359e701e | 244 | |
masahikofukasawa | 0:7a00359e701e | 245 | case Message::CMD_GET_MAG_PART: |
masahikofukasawa | 0:7a00359e701e | 246 | resMsg.setArgument(0, MAGNETOMETER_ID); |
masahikofukasawa | 0:7a00359e701e | 247 | throwMessage(&resMsg); |
masahikofukasawa | 0:7a00359e701e | 248 | MSG("#Mag ID is reported.\n"); |
masahikofukasawa | 0:7a00359e701e | 249 | break; |
masahikofukasawa | 0:7a00359e701e | 250 | |
masahikofukasawa | 0:7a00359e701e | 251 | case Message::CMD_SET_SERIAL_TARGET: |
masahikofukasawa | 0:7a00359e701e | 252 | isEnabledBle = msg.getArgument(0)==Message::SW_ON ? true : false; |
masahikofukasawa | 0:7a00359e701e | 253 | isEnabledUsb = msg.getArgument(1)==Message::SW_ON ? true : false; |
masahikofukasawa | 0:7a00359e701e | 254 | resMsg.setArgument(0, 0); |
masahikofukasawa | 0:7a00359e701e | 255 | throwMessage(&resMsg); |
masahikofukasawa | 0:7a00359e701e | 256 | MSG("#Serial out is set.\n"); |
masahikofukasawa | 0:7a00359e701e | 257 | break; |
masahikofukasawa | 0:7a00359e701e | 258 | |
masahikofukasawa | 0:7a00359e701e | 259 | case Message::CMD_GET_ID: // return Primary ID and Sub ID |
masahikofukasawa | 0:7a00359e701e | 260 | if(id == 0) id = 0xE; // Temporary for Angle Sensor |
masahikofukasawa | 0:7a00359e701e | 261 | resMsg.setArgument(0, id); |
masahikofukasawa | 0:7a00359e701e | 262 | resMsg.setArgument(1, subId); |
masahikofukasawa | 0:7a00359e701e | 263 | throwMessage(&resMsg); |
masahikofukasawa | 0:7a00359e701e | 264 | wait(0.4); // wait for App initialization |
masahikofukasawa | 0:7a00359e701e | 265 | MSG("#Mag ID is reported.\n"); |
masahikofukasawa | 0:7a00359e701e | 266 | break; |
masahikofukasawa | 0:7a00359e701e | 267 | |
masahikofukasawa | 0:7a00359e701e | 268 | case Message::CMD_STOP_MEASUREMENT: |
masahikofukasawa | 0:7a00359e701e | 269 | if( sensor->stopSensor() != AkmSensor::SUCCESS) status = AkmSensorManager::ERROR; |
masahikofukasawa | 0:7a00359e701e | 270 | resMsg.setArgument(0, status==AkmSensorManager::SUCCESS ? 0 : 1); |
masahikofukasawa | 0:7a00359e701e | 271 | throwMessage(&resMsg); |
masahikofukasawa | 0:7a00359e701e | 272 | MSG("#Stop measurement.\n"); |
masahikofukasawa | 0:7a00359e701e | 273 | break; |
masahikofukasawa | 0:7a00359e701e | 274 | |
masahikofukasawa | 0:7a00359e701e | 275 | case Message::CMD_START_MEASUREMENT: |
masahikofukasawa | 0:7a00359e701e | 276 | MSG("#Start measurement.\n"); |
masahikofukasawa | 0:7a00359e701e | 277 | sensor->startSensor(); |
masahikofukasawa | 0:7a00359e701e | 278 | // get initial sensor state for switch type sensors |
masahikofukasawa | 0:7a00359e701e | 279 | if( id == AkmSensor::AKM_PRIMARY_ID_UNIPOLAR || |
masahikofukasawa | 0:7a00359e701e | 280 | id == AkmSensor::AKM_PRIMARY_ID_OMNIPOLAR || |
masahikofukasawa | 0:7a00359e701e | 281 | id == AkmSensor::AKM_PRIMARY_ID_LATCH || |
masahikofukasawa | 0:7a00359e701e | 282 | id == AkmSensor::AKM_PRIMARY_ID_DUAL_OUTPUT || |
masahikofukasawa | 0:7a00359e701e | 283 | id == AkmSensor::AKM_PRIMARY_ID_ONECHIP_ENCODER ){ |
masahikofukasawa | 0:7a00359e701e | 284 | Message temp; |
masahikofukasawa | 0:7a00359e701e | 285 | sensor->readSensorData(&temp); |
masahikofukasawa | 0:7a00359e701e | 286 | throwMessage(&temp); |
masahikofukasawa | 0:7a00359e701e | 287 | } |
masahikofukasawa | 0:7a00359e701e | 288 | break; |
masahikofukasawa | 0:7a00359e701e | 289 | |
masahikofukasawa | 0:7a00359e701e | 290 | case Message::CMD_ANGLE_ZERO_RESET: |
masahikofukasawa | 1:b46b8653331f | 291 | case Message::CMD_IR_GET_THRESHOLD: |
masahikofukasawa | 1:b46b8653331f | 292 | case Message::CMD_IR_SET_THRESHOLD: |
masahikofukasawa | 1:b46b8653331f | 293 | case Message::CMD_IR_GET_HYSTERESIS: |
masahikofukasawa | 1:b46b8653331f | 294 | case Message::CMD_IR_SET_HYSTERESIS: |
masahikofukasawa | 1:b46b8653331f | 295 | case Message::CMD_IR_GET_INTERRUPT: |
masahikofukasawa | 1:b46b8653331f | 296 | case Message::CMD_IR_SET_INTERRUPT: |
masahikofukasawa | 1:b46b8653331f | 297 | case Message::CMD_IR_GET_OPERATION_MODE: |
masahikofukasawa | 1:b46b8653331f | 298 | case Message::CMD_IR_SET_OPERATION_MODE: |
masahikofukasawa | 1:b46b8653331f | 299 | case Message::CMD_IR_GET_THRESHOLD_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 300 | case Message::CMD_IR_SET_THRESHOLD_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 301 | case Message::CMD_IR_GET_HYSTERESIS_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 302 | case Message::CMD_IR_SET_HYSTERESIS_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 303 | case Message::CMD_IR_GET_INTERRUPT_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 304 | case Message::CMD_IR_SET_INTERRUPT_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 305 | case Message::CMD_IR_GET_OPERATION_MODE_EEPROM: |
masahikofukasawa | 1:b46b8653331f | 306 | case Message::CMD_IR_SET_OPERATION_MODE_EEPROM: |
masahikofukasawa | 0:7a00359e701e | 307 | sensor->requestCommand(&msg,&resMsg); |
masahikofukasawa | 0:7a00359e701e | 308 | throwMessage(&resMsg); |
masahikofukasawa | 0:7a00359e701e | 309 | break; |
masahikofukasawa | 1:b46b8653331f | 310 | |
masahikofukasawa | 0:7a00359e701e | 311 | default: |
masahikofukasawa | 0:7a00359e701e | 312 | MSG("#Can't find command:%s\n", (char)cmd); |
masahikofukasawa | 0:7a00359e701e | 313 | break; |
masahikofukasawa | 0:7a00359e701e | 314 | } |
masahikofukasawa | 0:7a00359e701e | 315 | } |
masahikofukasawa | 0:7a00359e701e | 316 | |
masahikofukasawa | 0:7a00359e701e | 317 | AkmSensorManager::Status AkmSensorManager::processEvent() |
masahikofukasawa | 0:7a00359e701e | 318 | { |
masahikofukasawa | 0:7a00359e701e | 319 | AkmSensorManager::Status status = AkmSensorManager::SUCCESS; |
masahikofukasawa | 0:7a00359e701e | 320 | |
masahikofukasawa | 0:7a00359e701e | 321 | // command received from the host |
masahikofukasawa | 0:7a00359e701e | 322 | if(eventCommandReceived) |
masahikofukasawa | 0:7a00359e701e | 323 | { |
masahikofukasawa | 0:7a00359e701e | 324 | processCommand(); |
masahikofukasawa | 0:7a00359e701e | 325 | eventCommandReceived = false; |
masahikofukasawa | 0:7a00359e701e | 326 | } |
masahikofukasawa | 0:7a00359e701e | 327 | if(sensor->isEvent()) // sensor read data event |
masahikofukasawa | 0:7a00359e701e | 328 | { |
masahikofukasawa | 0:7a00359e701e | 329 | Message msg; |
masahikofukasawa | 0:7a00359e701e | 330 | if( sensor->readSensorData(&msg) != AkmSensor::SUCCESS) status = AkmSensorManager::ERROR; |
masahikofukasawa | 0:7a00359e701e | 331 | throwMessage(&msg); |
masahikofukasawa | 0:7a00359e701e | 332 | } |
masahikofukasawa | 0:7a00359e701e | 333 | |
masahikofukasawa | 0:7a00359e701e | 334 | if(eventConnected) // BLE connected. Start sensor. |
masahikofukasawa | 0:7a00359e701e | 335 | { |
masahikofukasawa | 0:7a00359e701e | 336 | // if( sensor->startSensor() != AkmSensor::SUCCESS) status = AkmSensorManager::ERROR; |
masahikofukasawa | 0:7a00359e701e | 337 | eventConnected = false; |
masahikofukasawa | 0:7a00359e701e | 338 | } |
masahikofukasawa | 0:7a00359e701e | 339 | if(eventDisconnected) // BLE dis-connected. Stop sensor. |
masahikofukasawa | 0:7a00359e701e | 340 | { |
masahikofukasawa | 0:7a00359e701e | 341 | if( sensor->stopSensor() != AkmSensor::SUCCESS) status = AkmSensorManager::ERROR; |
masahikofukasawa | 0:7a00359e701e | 342 | eventDisconnected = false; |
masahikofukasawa | 0:7a00359e701e | 343 | } |
masahikofukasawa | 0:7a00359e701e | 344 | return status; |
masahikofukasawa | 0:7a00359e701e | 345 | } |
masahikofukasawa | 0:7a00359e701e | 346 | |
masahikofukasawa | 0:7a00359e701e | 347 | AkmSensorManager::Status AkmSensorManager::throwMessage(const Message *msg) { |
masahikofukasawa | 0:7a00359e701e | 348 | int len = Message::getMaxMessageLength(); |
masahikofukasawa | 0:7a00359e701e | 349 | char buf[len]; |
masahikofukasawa | 0:7a00359e701e | 350 | |
masahikofukasawa | 0:7a00359e701e | 351 | buf[0] = '$'; |
masahikofukasawa | 0:7a00359e701e | 352 | |
masahikofukasawa | 0:7a00359e701e | 353 | // Processes command |
masahikofukasawa | 0:7a00359e701e | 354 | char cmd = (char)msg->getCommand(); |
masahikofukasawa | 0:7a00359e701e | 355 | Message::charToAscii(&buf[1], &cmd); |
masahikofukasawa | 0:7a00359e701e | 356 | |
masahikofukasawa | 0:7a00359e701e | 357 | // Processes arguments |
masahikofukasawa | 0:7a00359e701e | 358 | for (int i=0; i < msg->getArgNum(); i++) { |
masahikofukasawa | 0:7a00359e701e | 359 | char arg = msg->getArgument(i); |
masahikofukasawa | 0:7a00359e701e | 360 | Message::charToAscii(&buf[3+2*i], &arg); |
masahikofukasawa | 0:7a00359e701e | 361 | } |
masahikofukasawa | 0:7a00359e701e | 362 | |
masahikofukasawa | 0:7a00359e701e | 363 | // Add termination characters, 0x0D(\r), \n and \0, to the end of string |
masahikofukasawa | 0:7a00359e701e | 364 | int tIdx = 3 + 2 * (msg->getArgNum()); |
masahikofukasawa | 0:7a00359e701e | 365 | int bufSize = sizeof(buf)/sizeof(buf[0]); |
masahikofukasawa | 0:7a00359e701e | 366 | if ((tIdx + 3) > (bufSize - 1)) { |
masahikofukasawa | 0:7a00359e701e | 367 | MSG("#Error: Message data exceeds the buffer.\n"); |
masahikofukasawa | 0:7a00359e701e | 368 | return ERROR; |
masahikofukasawa | 0:7a00359e701e | 369 | } |
masahikofukasawa | 0:7a00359e701e | 370 | buf[tIdx++] = 0x0D; // '\r' |
masahikofukasawa | 0:7a00359e701e | 371 | buf[tIdx++] = '\n'; |
masahikofukasawa | 0:7a00359e701e | 372 | buf[tIdx] = '\0'; |
masahikofukasawa | 0:7a00359e701e | 373 | |
masahikofukasawa | 0:7a00359e701e | 374 | // Send to the BLE buffer |
masahikofukasawa | 0:7a00359e701e | 375 | // Hardware *hw = Hardware::getInstance(); |
masahikofukasawa | 0:7a00359e701e | 376 | // hw->uartService->writeString(buf); |
masahikofukasawa | 0:7a00359e701e | 377 | if(isEnabledBle) uartService->writeString(buf); |
masahikofukasawa | 0:7a00359e701e | 378 | if(isEnabledUsb) serial->printf(buf); |
masahikofukasawa | 0:7a00359e701e | 379 | |
masahikofukasawa | 0:7a00359e701e | 380 | // MSG("#A message thrown: \"%s\"\n", buf); |
masahikofukasawa | 0:7a00359e701e | 381 | |
masahikofukasawa | 0:7a00359e701e | 382 | return SUCCESS; |
masahikofukasawa | 0:7a00359e701e | 383 | } |
masahikofukasawa | 0:7a00359e701e | 384 | |
masahikofukasawa | 6:c4401549d68f | 385 | void AkmSensorManager::releaseTWI(){ |
masahikofukasawa | 6:c4401549d68f | 386 | NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; |
masahikofukasawa | 6:c4401549d68f | 387 | NRF_TWI0->POWER = 0; |
masahikofukasawa | 6:c4401549d68f | 388 | NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; |
masahikofukasawa | 6:c4401549d68f | 389 | NRF_TWI1->POWER = 0; |
masahikofukasawa | 6:c4401549d68f | 390 | } |
masahikofukasawa | 6:c4401549d68f | 391 | |
masahikofukasawa | 0:7a00359e701e | 392 | /* |
masahikofukasawa | 0:7a00359e701e | 393 | void AkmSensorManager::dataOut(char* str){ |
masahikofukasawa | 0:7a00359e701e | 394 | if(isBleMode) uartService->writeString(str); |
masahikofukasawa | 0:7a00359e701e | 395 | if(isSerialMode) serial->printf(str); |
masahikofukasawa | 0:7a00359e701e | 396 | } |
masahikofukasawa | 0:7a00359e701e | 397 | */ |