Modified for compatibility with Rev.E. hardware

Fork of AkmSensor by AKM Development Platform

Committer:
tkstreet
Date:
Fri Mar 24 23:01:27 2017 +0000
Revision:
21:966724730ce6
Parent:
15:1238993fd75f
Child:
23:50c98b286e41
Added class documentation

Who changed what in which revision?

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