Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
akmanalogsensor.cpp@43:45225713cd58, 2017-11-08 (annotated)
- Committer:
- tkstreet
- Date:
- Wed Nov 08 21:56:17 2017 +0000
- Revision:
- 43:45225713cd58
- Parent:
- 40:42e48427e4b7
- Child:
- 44:0e251c736282
Moved all pin definitions to akdphwinfo header. Removed redundant library inclusions.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
masahikofukasawa | 11:cef8dc1cf010 | 1 | #include "akmanalogsensor.h" |
masahikofukasawa | 11:cef8dc1cf010 | 2 | |
masahikofukasawa | 11:cef8dc1cf010 | 3 | #define WAIT_ADC_MS 1 |
masahikofukasawa | 11:cef8dc1cf010 | 4 | |
masahikofukasawa | 11:cef8dc1cf010 | 5 | /** |
masahikofukasawa | 11:cef8dc1cf010 | 6 | * Constructor. |
masahikofukasawa | 11:cef8dc1cf010 | 7 | * |
masahikofukasawa | 11:cef8dc1cf010 | 8 | */ |
masahikofukasawa | 27:41aa9fb23a2f | 9 | AkmAnalogSensor::AkmAnalogSensor() : AkmSensor(){ |
masahikofukasawa | 11:cef8dc1cf010 | 10 | ain = NULL; |
masahikofukasawa | 11:cef8dc1cf010 | 11 | mcp3428 = NULL; |
masahikofukasawa | 11:cef8dc1cf010 | 12 | } |
masahikofukasawa | 11:cef8dc1cf010 | 13 | |
masahikofukasawa | 11:cef8dc1cf010 | 14 | /** |
masahikofukasawa | 11:cef8dc1cf010 | 15 | * Destructor. |
masahikofukasawa | 11:cef8dc1cf010 | 16 | * |
masahikofukasawa | 11:cef8dc1cf010 | 17 | */ |
masahikofukasawa | 11:cef8dc1cf010 | 18 | AkmAnalogSensor::~AkmAnalogSensor(){ |
masahikofukasawa | 11:cef8dc1cf010 | 19 | if(ain) delete ain; |
masahikofukasawa | 11:cef8dc1cf010 | 20 | if(mcp3428) delete mcp3428; |
masahikofukasawa | 11:cef8dc1cf010 | 21 | } |
masahikofukasawa | 11:cef8dc1cf010 | 22 | |
masahikofukasawa | 11:cef8dc1cf010 | 23 | AkmSensor::Status AkmAnalogSensor::init(const uint8_t id, const uint8_t subid){ |
masahikofukasawa | 11:cef8dc1cf010 | 24 | primaryId = id; |
masahikofukasawa | 11:cef8dc1cf010 | 25 | subId = subid; |
masahikofukasawa | 11:cef8dc1cf010 | 26 | |
masahikofukasawa | 37:c76d2edf3426 | 27 | if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ430L) sensorName = "EQ-430L"; |
masahikofukasawa | 37:c76d2edf3426 | 28 | else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ431L) sensorName = "EQ-431L"; |
masahikofukasawa | 37:c76d2edf3426 | 29 | else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ432L) sensorName = "EQ-432L"; |
masahikofukasawa | 37:c76d2edf3426 | 30 | else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ433L) sensorName = "EQ-433L"; |
masahikofukasawa | 37:c76d2edf3426 | 31 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ3200) sensorName = "CQ-3200"; |
masahikofukasawa | 37:c76d2edf3426 | 32 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ3201) sensorName = "CQ-3201"; |
masahikofukasawa | 37:c76d2edf3426 | 33 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ3202) sensorName = "CQ-3202"; |
masahikofukasawa | 37:c76d2edf3426 | 34 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ3203) sensorName = "CQ-3203"; |
masahikofukasawa | 37:c76d2edf3426 | 35 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ3204) sensorName = "CQ-3204"; |
masahikofukasawa | 37:c76d2edf3426 | 36 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ320A) sensorName = "CQ-320A"; |
masahikofukasawa | 37:c76d2edf3426 | 37 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V && subId == AkmAnalogSensor::SUB_ID_CQ320B) sensorName = "CQ-320B"; |
masahikofukasawa | 37:c76d2edf3426 | 38 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ3300) sensorName = "CQ-3300"; |
masahikofukasawa | 37:c76d2edf3426 | 39 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ3301) sensorName = "CQ-3301"; |
masahikofukasawa | 37:c76d2edf3426 | 40 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ3302) sensorName = "CQ-3302"; |
masahikofukasawa | 37:c76d2edf3426 | 41 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ3303) sensorName = "CQ-3303"; |
masahikofukasawa | 37:c76d2edf3426 | 42 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330A) sensorName = "CQ-330A"; |
masahikofukasawa | 37:c76d2edf3426 | 43 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330B) sensorName = "CQ-330B"; |
masahikofukasawa | 37:c76d2edf3426 | 44 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330E) sensorName = "CQ-330E"; |
masahikofukasawa | 37:c76d2edf3426 | 45 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330F) sensorName = "CQ-330F"; |
masahikofukasawa | 37:c76d2edf3426 | 46 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330G) sensorName = "CQ-330G"; |
masahikofukasawa | 37:c76d2edf3426 | 47 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330H) sensorName = "CQ-330H"; |
masahikofukasawa | 37:c76d2edf3426 | 48 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CQ330J) sensorName = "CQ-330J"; |
masahikofukasawa | 37:c76d2edf3426 | 49 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CZ3813) sensorName = "CZ-3813"; |
masahikofukasawa | 37:c76d2edf3426 | 50 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CZ3814) sensorName = "CZ-3814"; |
masahikofukasawa | 37:c76d2edf3426 | 51 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V && subId == AkmAnalogSensor::SUB_ID_CZ3815) sensorName = "CZ-3815"; |
masahikofukasawa | 37:c76d2edf3426 | 52 | else if(primaryId == AKM_PRIMARY_ID_MISC_ANALOG && subId == AkmAnalogSensor::SUB_ID_EM3242) sensorName = "EM3242"; |
masahikofukasawa | 37:c76d2edf3426 | 53 | else if(primaryId == AKM_PRIMARY_ID_MISC_ANALOG && subId == AkmAnalogSensor::SUB_ID_AK9710) sensorName = "AK9710"; |
masahikofukasawa | 37:c76d2edf3426 | 54 | else return AkmSensor::ERROR; |
masahikofukasawa | 37:c76d2edf3426 | 55 | |
masahikofukasawa | 37:c76d2edf3426 | 56 | MCP342X::AdcChannel channel = MCP342X::ADC_CH3; // 0-5.0V out as default |
masahikofukasawa | 37:c76d2edf3426 | 57 | if( (primaryId == AKM_PRIMARY_ID_MISC_ANALOG) && (subId < 0x08) || primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V){ |
masahikofukasawa | 37:c76d2edf3426 | 58 | channel = MCP342X::ADC_CH4; // 0-3.0V out |
masahikofukasawa | 37:c76d2edf3426 | 59 | } |
masahikofukasawa | 13:d008249f0359 | 60 | |
masahikofukasawa | 37:c76d2edf3426 | 61 | // 16bit ADC |
masahikofukasawa | 37:c76d2edf3426 | 62 | I2C* i2c = new I2C(I2C_SDA,I2C_SCL); |
masahikofukasawa | 40:42e48427e4b7 | 63 | i2c->frequency(I2C_SPEED); |
masahikofukasawa | 37:c76d2edf3426 | 64 | |
masahikofukasawa | 37:c76d2edf3426 | 65 | // ADC setting |
masahikofukasawa | 37:c76d2edf3426 | 66 | mcp3428 = new MCP342X(i2c, MCP342X::SLAVE_ADDRESS_6EH); |
masahikofukasawa | 37:c76d2edf3426 | 67 | mcp3428->setChannel(channel); |
masahikofukasawa | 37:c76d2edf3426 | 68 | mcp3428->setSampleSetting(MCP342X::SAMPLE_15HZ_16BIT); |
masahikofukasawa | 37:c76d2edf3426 | 69 | mcp3428->setConversionMode(MCP342X::CONTINUOUS); |
masahikofukasawa | 11:cef8dc1cf010 | 70 | |
masahikofukasawa | 11:cef8dc1cf010 | 71 | interval = SENSOR_SAMPLING_RATE; // 10Hz |
masahikofukasawa | 11:cef8dc1cf010 | 72 | |
masahikofukasawa | 11:cef8dc1cf010 | 73 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 74 | } |
masahikofukasawa | 11:cef8dc1cf010 | 75 | |
masahikofukasawa | 11:cef8dc1cf010 | 76 | AkmSensor::Status AkmAnalogSensor::startSensor(){ |
masahikofukasawa | 29:b488d2c89fba | 77 | ticker.attach(callback(this, &AkmSensor::setEvent), interval); |
masahikofukasawa | 11:cef8dc1cf010 | 78 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 79 | } |
masahikofukasawa | 11:cef8dc1cf010 | 80 | |
masahikofukasawa | 11:cef8dc1cf010 | 81 | AkmSensor::Status AkmAnalogSensor::startSensor(const float sec){ |
masahikofukasawa | 11:cef8dc1cf010 | 82 | interval = sec; |
masahikofukasawa | 29:b488d2c89fba | 83 | ticker.attach(callback(this, &AkmSensor::setEvent), interval); |
masahikofukasawa | 29:b488d2c89fba | 84 | MSG("#Start sensor %s.\r\n",sensorName); |
masahikofukasawa | 11:cef8dc1cf010 | 85 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 86 | } |
masahikofukasawa | 11:cef8dc1cf010 | 87 | |
masahikofukasawa | 11:cef8dc1cf010 | 88 | AkmSensor::Status AkmAnalogSensor::stopSensor(){ |
masahikofukasawa | 11:cef8dc1cf010 | 89 | ticker.detach(); |
masahikofukasawa | 11:cef8dc1cf010 | 90 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 91 | } |
masahikofukasawa | 11:cef8dc1cf010 | 92 | |
masahikofukasawa | 11:cef8dc1cf010 | 93 | AkmSensor::Status AkmAnalogSensor::readSensorData(Message* msg){ |
masahikofukasawa | 29:b488d2c89fba | 94 | AkmSensor::clearEvent(); |
masahikofukasawa | 11:cef8dc1cf010 | 95 | |
masahikofukasawa | 11:cef8dc1cf010 | 96 | msg->setCommand(Message::CMD_START_MEASUREMENT); |
masahikofukasawa | 11:cef8dc1cf010 | 97 | |
masahikofukasawa | 27:41aa9fb23a2f | 98 | if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR || |
masahikofukasawa | 11:cef8dc1cf010 | 99 | primaryId == AKM_PRIMARY_ID_MISC_ANALOG || |
masahikofukasawa | 37:c76d2edf3426 | 100 | primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_3V || |
masahikofukasawa | 37:c76d2edf3426 | 101 | primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR_5V ){ |
masahikofukasawa | 11:cef8dc1cf010 | 102 | MCP342X::Data data; |
masahikofukasawa | 11:cef8dc1cf010 | 103 | do { |
masahikofukasawa | 11:cef8dc1cf010 | 104 | mcp3428->getData(&data); |
masahikofukasawa | 11:cef8dc1cf010 | 105 | wait_ms(WAIT_ADC_MS); |
masahikofukasawa | 11:cef8dc1cf010 | 106 | } while(data.st == MCP342X::DATA_NOT_UPDATED); |
masahikofukasawa | 11:cef8dc1cf010 | 107 | |
masahikofukasawa | 11:cef8dc1cf010 | 108 | msg->setArgument( 0, (char)( (data.value&0xFF00) >> 8) ); |
masahikofukasawa | 11:cef8dc1cf010 | 109 | msg->setArgument( 1, (char)( data.value & 0x00FF) ); |
masahikofukasawa | 11:cef8dc1cf010 | 110 | }else{ |
masahikofukasawa | 11:cef8dc1cf010 | 111 | msg->setArgument( 0, 0); |
masahikofukasawa | 11:cef8dc1cf010 | 112 | msg->setArgument( 1, 0); |
masahikofukasawa | 11:cef8dc1cf010 | 113 | return AkmSensor::ERROR; |
masahikofukasawa | 11:cef8dc1cf010 | 114 | } |
masahikofukasawa | 11:cef8dc1cf010 | 115 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 116 | } |
masahikofukasawa | 11:cef8dc1cf010 | 117 | |
masahikofukasawa | 11:cef8dc1cf010 | 118 | AkmSensor::Status AkmAnalogSensor::requestCommand(Message* in, Message* out){ |
masahikofukasawa | 15:1238993fd75f | 119 | return AkmSensor::ERROR; |
masahikofukasawa | 11:cef8dc1cf010 | 120 | } |