Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
Diff: akmsensormanager.cpp
- Revision:
- 27:41aa9fb23a2f
- Parent:
- 18:b7182d5ad8d5
- Child:
- 28:dc4eb14e4d7e
diff -r 2fca76521680 -r 41aa9fb23a2f akmsensormanager.cpp --- a/akmsensormanager.cpp Wed Feb 22 21:56:05 2017 +0000 +++ b/akmsensormanager.cpp Sat Mar 11 01:14:15 2017 +0000 @@ -28,7 +28,7 @@ "Onechip Encoder", "TBD1", "TBD2", - "Linear Sensor Legacy", + "DEMO", "Current Sensor", "MISC(Analog)", "Linear Sensor", @@ -51,8 +51,7 @@ { primaryId = id; subId = subid; - sensor = AkmSensorManager::getAkmSensor(); - if(sensor == NULL) return AkmSensorManager::ERROR; + if(AkmSensorManager::checkAkmSensor()!= true) return AkmSensorManager::ERROR; return AkmSensorManager::SUCCESS; } @@ -73,9 +72,13 @@ eventDisconnected = true; } -AkmSensor* AkmSensorManager::getAkmSensor() +bool AkmSensorManager::checkAkmSensor() { - AkmSensor* sensor = NULL; + currentSensorNumber = 0; + int i=0; + for(i=0; i<MAX_SENSOR_NUM; i++){ + sensor[i] = NULL; + } switch(primaryId){ @@ -84,11 +87,11 @@ { if(subId != Ak09970Ctrl::SUB_ID_AK09970){ AkmAkd* akd = new AkmAkd(); - sensor = akd; + sensor[0] = akd; } else{ Ak09970Ctrl* ak09970 = new Ak09970Ctrl(); - sensor = ak09970; + sensor[0] = ak09970; } break; } @@ -97,12 +100,12 @@ { if(subId == Ak7451Ctrl::SUB_ID_AK7451){ Ak7451Ctrl* ak7451ctrl = new Ak7451Ctrl(); - sensor = ak7451ctrl; + sensor[0] = ak7451ctrl; break; } else if(subId == Ak7401Ctrl::SUB_ID_AK7401){ Ak7401Ctrl* ak7401ctrl = new Ak7401Ctrl(); - sensor = ak7401ctrl; + sensor[0] = ak7401ctrl; break; } break; @@ -115,16 +118,34 @@ case AkmSensor::AKM_PRIMARY_ID_ONECHIP_ENCODER: { AkmHallSwitch* hallswitch = new AkmHallSwitch(); - sensor = hallswitch; + sensor[0] = hallswitch; break; } -// case AkmSensor::AKM_PRIMARY_ID_LINEAR_SENSOR_LEGACY: + case AkmSensor::AKM_PRIMARY_ID_DEMO: + { + if(subId == 8){ + Ak9752Ctrl* ak9752ctrl = new Ak9752Ctrl(); + sensor[0] = ak9752ctrl; + if(sensor[currentSensorNumber]->init(AkmSensor::AKM_PRIMARY_ID_IR_SENSOR, Ak9752Ctrl::SUB_ID_AK9752) != AkmSensor::SUCCESS){ + MSG("#sensor[0]->init failed. ID=%d SubID=%d %s\r\n", primaryId, subId, AKM_PRIMARY_ID_STR[primaryId]); + return false; // couldn't find + } + + Ak09970Ctrl* ak09970 = new Ak09970Ctrl(); + sensor[1] = ak09970; + if(sensor[1]->init(AkmSensor::AKM_PRIMARY_ID_AKD_I2C, Ak09970Ctrl::SUB_ID_AK09970) != AkmSensor::SUCCESS){ + MSG("#sensor[1]->init failed. ID=%d SubID=%d %s\r\n", primaryId, subId, AKM_PRIMARY_ID_STR[primaryId]); + return false; // couldn't find + } + } + break; + } case AkmSensor::AKM_PRIMARY_ID_LINEAR_SENSOR: case AkmSensor::AKM_PRIMARY_ID_CURRENT_SENSOR: case AkmSensor::AKM_PRIMARY_ID_MISC_ANALOG: { AkmAnalogSensor* analogsensor = new AkmAnalogSensor(); - sensor = analogsensor; + sensor[0] = analogsensor; break; } @@ -132,34 +153,34 @@ { if(subId == Ak9750Ctrl::SUB_ID_AK9750){ Ak9750Ctrl* ak9750ctrl = new Ak9750Ctrl(); - sensor = ak9750ctrl; + sensor[0] = ak9750ctrl; }else if(subId == Ak9750Ctrl::SUB_ID_AK9753){ Ak9750Ctrl* ak9753ctrl = new Ak9750Ctrl(); - sensor = ak9753ctrl; + sensor[0] = ak9753ctrl; }else if(subId == Ak9752Ctrl::SUB_ID_AK9752){ Ak9752Ctrl* ak9752ctrl = new Ak9752Ctrl(); - sensor = ak9752ctrl; + sensor[0] = ak9752ctrl; }else{ - return NULL; // couldn't find + return false; // couldn't find } - break; } default: { MSG("#Can't find ID=%d SubID=%d %s\r\n", primaryId, subId, AKM_PRIMARY_ID_STR[primaryId]); - return NULL; // couldn't find + return false; // couldn't find } } - if(sensor->init(primaryId, subId) != AkmSensor::SUCCESS){ - MSG("#sensor->init failed. ID=%d SubID=%d %s\r\n", primaryId, subId, AKM_PRIMARY_ID_STR[primaryId]); - return NULL; // couldn't find + if(primaryId != AkmSensor::AKM_PRIMARY_ID_DEMO){ + if(sensor[currentSensorNumber]->init(primaryId, subId) != AkmSensor::SUCCESS){ + MSG("#sensor[currentSensorNumber]->init failed. ID=%d SubID=%d %s\r\n", primaryId, subId, AKM_PRIMARY_ID_STR[primaryId]); + return false; // couldn't find + } + MSG("#ID=%d SubID=%d %s\r\n", primaryId, subId, AKM_PRIMARY_ID_STR[primaryId]); } - - MSG("#ID=%d SubID=%d %s\r\n", primaryId, subId, AKM_PRIMARY_ID_STR[primaryId]); - return sensor; + return true; } @@ -227,7 +248,7 @@ bool AkmSensorManager::isEvent() { - return (sensor->isEvent() || + return (sensor[currentSensorNumber]->isEvent() || eventCommandReceived || eventConnected || eventDisconnected); @@ -275,12 +296,36 @@ MSG("#Mag ID is reported.\r\n"); break; } + case Message::CMD_GET_SENSOR_NUM: + { + resMsg.setArgument(0, currentSensorNumber); + throwMessage(&resMsg); + MSG("#Current Sensor Number reported.\r\n"); + break; + } + case Message::CMD_SET_SENSOR_NUM: + { + currentSensorNumber = msg.getArgument(0); + MSG("#Current Sensor Number set to %d.\r\n", currentSensorNumber); + break; + } + case Message::CMD_GET_SENSOR_COUNT: + { + int i=0; + while(sensor[i] != NULL){ + i++; + } + resMsg.setArgument(0, i); + throwMessage(&resMsg); + MSG("#Sensor Total Count reported.\r\n"); + break; + } case Message::CMD_STOP_MEASUREMENT: { - if( sensor->stopSensor() != AkmSensor::SUCCESS){ + if( sensor[currentSensorNumber]->stopSensor() != AkmSensor::SUCCESS){ resMsg.setArgument(0, 1); }else{ - resMsg.setArgument(0, 0); + resMsg.setArgument(0, 0); } throwMessage(&resMsg); MSG("#Stop measurement.\r\n"); @@ -290,7 +335,7 @@ { int error_code = AkmSensor::SUCCESS; if(msg.getArgNum() == 0){ - error_code = sensor->startSensor(); + error_code = sensor[currentSensorNumber]->startSensor(); if( error_code != AkmSensor::SUCCESS ){ MSG("#StartSensor Error. Code=%d\r\n",error_code); } @@ -299,7 +344,7 @@ } }else if(msg.getArgNum() == 1){ float interval = (float)(1.0 / (float)msg.getArgument(0)); - error_code = sensor->startSensor(interval); + error_code = sensor[currentSensorNumber]->startSensor(interval); if( error_code != AkmSensor::SUCCESS ){ MSG("#StartSensor Error. Code=%d\r\n",error_code); } @@ -317,7 +362,7 @@ primaryId == AkmSensor::AKM_PRIMARY_ID_DUAL_OUTPUT || primaryId == AkmSensor::AKM_PRIMARY_ID_ONECHIP_ENCODER ){ Message temp; - sensor->readSensorData(&temp); + sensor[currentSensorNumber]->readSensorData(&temp); throwMessage(&temp); } } @@ -355,7 +400,7 @@ case Message::CMD_COMPASS_GET_OPERATION_MODE: case Message::CMD_COMPASS_SET_OPERATION_MODE: { - AkmSensor::Status st = sensor->requestCommand(&msg,&resMsg); + AkmSensor::Status st = sensor[currentSensorNumber]->requestCommand(&msg,&resMsg); if( (resMsg.getArgNum() == 0) && (st != AkmSensor::SUCCESS) ) { MSG("#Command failed.\r\n"); @@ -383,11 +428,11 @@ processCommand(); eventCommandReceived = false; } - if(sensor->isEvent()) // sensor read data event + if(sensor[currentSensorNumber]->isEvent()) // sensor read data event { // MSG("#Sensor event received.\r\n"); Message msg; - if( sensor->readSensorData(&msg) != AkmSensor::SUCCESS) status = AkmSensorManager::ERROR; + if( sensor[currentSensorNumber]->readSensorData(&msg) != AkmSensor::SUCCESS) status = AkmSensorManager::ERROR; throwMessage(&msg); } if(eventConnected) // BLE connected. Start sensor. @@ -398,7 +443,7 @@ if(eventDisconnected) // BLE dis-connected. Stop sensor. { MSG("#BLE dis-connected.\r\n"); - if( sensor->stopSensor() != AkmSensor::SUCCESS) status = AkmSensorManager::ERROR; + if( sensor[currentSensorNumber]->stopSensor() != AkmSensor::SUCCESS) status = AkmSensorManager::ERROR; eventDisconnected = false; } return status; @@ -436,6 +481,27 @@ return SUCCESS; } +char* AkmSensorManager::my_strcat(char* str1, char* str2) +{ + int num1; + char* str; + + num1=strlen(str1) + strlen(str2); + str = (char *)malloc(num1 + 1); + sprintf(str,"%s%s",str1,str2); + return str; +} + char* AkmSensorManager::getSensorName(){ - return sensor->getSensorName(); + char* name=""; + int i=0; + while(sensor[i] != NULL){ + name = my_strcat(name,sensor[i]->getSensorName()); + if(sensor[i+1] != NULL){ + name = my_strcat(name,"_"); + } + i++; + } + MSG("#Sensor Name='%s'.\r\n",name); + return name; } \ No newline at end of file