Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
akmanalogsensor.cpp
- Committer:
- masahikofukasawa
- Date:
- 2017-07-19
- Revision:
- 40:42e48427e4b7
- Parent:
- 37:c76d2edf3426
- Child:
- 43:45225713cd58
File content as of revision 40:42e48427e4b7:
#include "akmanalogsensor.h" #include "debug.h" #define WAIT_ADC_MS 1 /** * Constructor. * */ AkmAnalogSensor::AkmAnalogSensor() : AkmSensor(){ ain = NULL; mcp3428 = NULL; } /** * 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_3V && subId == AkmAnalogSensor::SUB_ID_CQ3200) sensorName = "CQ-3200"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ3201) sensorName = "CQ-3201"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ3202) sensorName = "CQ-3202"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ3203) sensorName = "CQ-3203"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ3204) sensorName = "CQ-3204"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ320A) sensorName = "CQ-320A"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ320B) sensorName = "CQ-320B"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ3300) sensorName = "CQ-3300"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ3301) sensorName = "CQ-3301"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ3302) sensorName = "CQ-3302"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ3303) sensorName = "CQ-3303"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330A) sensorName = "CQ-330A"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330B) sensorName = "CQ-330B"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330E) sensorName = "CQ-330E"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330F) sensorName = "CQ-330F"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330G) sensorName = "CQ-330G"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330H) sensorName = "CQ-330H"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330J) sensorName = "CQ-330J"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CZ3813) sensorName = "CZ-3813"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CZ3814) sensorName = "CZ-3814"; else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CZ3815) sensorName = "CZ-3815"; 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"; else return AkmSensor::ERROR; MCP342X::AdcChannel channel = MCP342X::ADC_CH3; // 0-5.0V out as default if( (primaryId == AKM_PRIMARY_ID_MISC_ANALOG) && (subId < 0x08) || primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V){ channel = MCP342X::ADC_CH4; // 0-3.0V out } // 16bit ADC I2C* i2c = new I2C(I2C_SDA,I2C_SCL); i2c->frequency(I2C_SPEED); // ADC setting mcp3428 = new MCP342X(i2c, MCP342X::SLAVE_ADDRESS_6EH); mcp3428->setChannel(channel); mcp3428->setSampleSetting(MCP342X::SAMPLE_15HZ_16BIT); mcp3428->setConversionMode(MCP342X::CONTINUOUS); interval = SENSOR_SAMPLING_RATE; // 10Hz return AkmSensor::SUCCESS; } AkmSensor::Status AkmAnalogSensor::startSensor(){ ticker.attach(callback(this, &AkmSensor::setEvent), interval); return AkmSensor::SUCCESS; } AkmSensor::Status AkmAnalogSensor::startSensor(const float sec){ interval = sec; ticker.attach(callback(this, &AkmSensor::setEvent), interval); MSG("#Start sensor %s.\r\n",sensorName); return AkmSensor::SUCCESS; } AkmSensor::Status AkmAnalogSensor::stopSensor(){ ticker.detach(); return AkmSensor::SUCCESS; } AkmSensor::Status AkmAnalogSensor::readSensorData(Message* msg){ AkmSensor::clearEvent(); msg->setCommand(Message::CMD_START_MEASUREMENT); if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR || primaryId == AKM_PRIMARY_ID_MISC_ANALOG || primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V || primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V ){ 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; }