Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
akmanalogsensor.cpp
- Committer:
- masahikofukasawa
- Date:
- 2016-10-28
- Revision:
- 15:1238993fd75f
- Parent:
- 13:d008249f0359
- Child:
- 16:d85be9bafb80
File content as of revision 15:1238993fd75f:
#include "akmanalogsensor.h" #include "debug.h" #define WAIT_ADC_MS 1 /** * Constructor. * */ AkmAnalogSensor::AkmAnalogSensor(){ event = false; ain = NULL; mcp3428 = NULL; sensorName = ""; } /** * Destructor. * */ AkmAnalogSensor::~AkmAnalogSensor(){ if(ain) delete ain; if(mcp3428) delete mcp3428; } AkmSensor::Status AkmAnalogSensor::init(const uint8_t id, const uint8_t subid){ primaryId = id; subId = subid; if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ430L) sensorName = "EQ-430L"; else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ431L) sensorName = "EQ-431L"; else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ432L) sensorName = "EQ-432L"; else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ433L) sensorName = "EQ-433L"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ3300) sensorName = "CQ-3300"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ3301) sensorName = "CQ-3301"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ3302) sensorName = "CQ-3302"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ3303) sensorName = "CQ-3303"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ330A) sensorName = "CQ-330A"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ330B) sensorName = "CQ-330B"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ330E) sensorName = "CQ-330E"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ330F) sensorName = "CQ-330F"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ330G) sensorName = "CQ-330G"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ330H) sensorName = "CQ-330H"; else if(primaryId == AKM_PRIMARY_ID_MISC_ANALOG && subId == AkmAnalogSensor::SUB_ID_EM3242) sensorName = "EM3242"; else if(primaryId == AKM_PRIMARY_ID_MISC_ANALOG && subId == AkmAnalogSensor::SUB_ID_AK9710) sensorName = "AK9710"; if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR_LEGACY){ // Internal ADC, Legacy ain = new AnalogIn(ANALOG_IN_PIN); sensorName = "EQ43XL"; } else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR || primaryId == AKM_PRIMARY_ID_MISC_ANALOG || primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR) { MCP342X::AdcChannel channel = MCP342X::ADC_CH3; // 0-5.0V out as default if( (primaryId == AKM_PRIMARY_ID_MISC_ANALOG) && (subId < 0x08) ){ channel = MCP342X::ADC_CH4; // 0-3.0V out } // 16bit ADC I2C* i2c = new I2C(I2C_SDA,I2C_SCL); i2c->frequency(I2C_SPEED_100KHZ); // ADC setting mcp3428 = new MCP342X(i2c, MCP342X::SLAVE_ADDRESS_6EH); mcp3428->setChannel(channel); mcp3428->setSampleSetting(MCP342X::SAMPLE_15HZ_16BIT); mcp3428->setConversionMode(MCP342X::CONTINUOUS); } else{ return AkmSensor::ERROR; } interval = SENSOR_SAMPLING_RATE; // 10Hz return AkmSensor::SUCCESS; } void AkmAnalogSensor::eventCallback(){ event = true; } bool AkmAnalogSensor::isEvent(){ return event; } AkmSensor::Status AkmAnalogSensor::startSensor(){ ticker.attach(this, &AkmAnalogSensor::eventCallback, interval); return AkmSensor::SUCCESS; } AkmSensor::Status AkmAnalogSensor::startSensor(const float sec){ interval = sec; ticker.attach(this, &AkmAnalogSensor::eventCallback, interval); return AkmSensor::SUCCESS; } AkmSensor::Status AkmAnalogSensor::stopSensor(){ ticker.detach(); return AkmSensor::SUCCESS; } AkmSensor::Status AkmAnalogSensor::readSensorData(Message* msg){ event = false; msg->setCommand(Message::CMD_START_MEASUREMENT); if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR_LEGACY){ uint16_t value; float s = *ain; value = s*1024; msg->setArgument( 0, (char)( value >> 8) ); msg->setArgument( 1, (char)( value & 0x00FF) ); }else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR || primaryId == AKM_PRIMARY_ID_MISC_ANALOG || primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR){ MCP342X::Data data; do { mcp3428->getData(&data); wait_ms(WAIT_ADC_MS); } while(data.st == MCP342X::DATA_NOT_UPDATED); msg->setArgument( 0, (char)( (data.value&0xFF00) >> 8) ); msg->setArgument( 1, (char)( data.value & 0x00FF) ); }else{ msg->setArgument( 0, 0); msg->setArgument( 1, 0); return AkmSensor::ERROR; } return AkmSensor::SUCCESS; } AkmSensor::Status AkmAnalogSensor::requestCommand(Message* in, Message* out){ return AkmSensor::ERROR; } char* AkmAnalogSensor::getSensorName(){ return sensorName; }