Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
akmanalogsensor.cpp@13:d008249f0359, 2016-08-12 (annotated)
- Committer:
- masahikofukasawa
- Date:
- Fri Aug 12 00:04:48 2016 +0000
- Revision:
- 13:d008249f0359
- Parent:
- 11:cef8dc1cf010
- Child:
- 15:1238993fd75f
Ver008. Modified for faster BLE connection, and sensor name in the BLE device name.
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 | #include "debug.h" |
masahikofukasawa | 11:cef8dc1cf010 | 3 | |
masahikofukasawa | 11:cef8dc1cf010 | 4 | #define WAIT_ADC_MS 1 |
masahikofukasawa | 11:cef8dc1cf010 | 5 | |
masahikofukasawa | 11:cef8dc1cf010 | 6 | /** |
masahikofukasawa | 11:cef8dc1cf010 | 7 | * Constructor. |
masahikofukasawa | 11:cef8dc1cf010 | 8 | * |
masahikofukasawa | 11:cef8dc1cf010 | 9 | */ |
masahikofukasawa | 11:cef8dc1cf010 | 10 | AkmAnalogSensor::AkmAnalogSensor(){ |
masahikofukasawa | 11:cef8dc1cf010 | 11 | event = false; |
masahikofukasawa | 11:cef8dc1cf010 | 12 | ain = NULL; |
masahikofukasawa | 11:cef8dc1cf010 | 13 | mcp3428 = NULL; |
masahikofukasawa | 13:d008249f0359 | 14 | sensorName = ""; |
masahikofukasawa | 11:cef8dc1cf010 | 15 | } |
masahikofukasawa | 11:cef8dc1cf010 | 16 | |
masahikofukasawa | 11:cef8dc1cf010 | 17 | /** |
masahikofukasawa | 11:cef8dc1cf010 | 18 | * Destructor. |
masahikofukasawa | 11:cef8dc1cf010 | 19 | * |
masahikofukasawa | 11:cef8dc1cf010 | 20 | */ |
masahikofukasawa | 11:cef8dc1cf010 | 21 | AkmAnalogSensor::~AkmAnalogSensor(){ |
masahikofukasawa | 11:cef8dc1cf010 | 22 | if(ain) delete ain; |
masahikofukasawa | 11:cef8dc1cf010 | 23 | if(mcp3428) delete mcp3428; |
masahikofukasawa | 11:cef8dc1cf010 | 24 | } |
masahikofukasawa | 11:cef8dc1cf010 | 25 | |
masahikofukasawa | 11:cef8dc1cf010 | 26 | AkmSensor::Status AkmAnalogSensor::init(const uint8_t id, const uint8_t subid){ |
masahikofukasawa | 11:cef8dc1cf010 | 27 | primaryId = id; |
masahikofukasawa | 11:cef8dc1cf010 | 28 | subId = subid; |
masahikofukasawa | 11:cef8dc1cf010 | 29 | |
masahikofukasawa | 13:d008249f0359 | 30 | if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ430L) sensorName = "EQ-430L"; |
masahikofukasawa | 13:d008249f0359 | 31 | else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ431L) sensorName = "EQ-431L"; |
masahikofukasawa | 13:d008249f0359 | 32 | else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ432L) sensorName = "EQ-432L"; |
masahikofukasawa | 13:d008249f0359 | 33 | else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR && subId == AkmAnalogSensor::SUB_ID_EQ433L) sensorName = "EQ-433L"; |
masahikofukasawa | 13:d008249f0359 | 34 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ3300) sensorName = "CQ-3300"; |
masahikofukasawa | 13:d008249f0359 | 35 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ3301) sensorName = "CQ-3301"; |
masahikofukasawa | 13:d008249f0359 | 36 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ3302) sensorName = "CQ-3302"; |
masahikofukasawa | 13:d008249f0359 | 37 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ3303) sensorName = "CQ-3303"; |
masahikofukasawa | 13:d008249f0359 | 38 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ330A) sensorName = "CQ-330A"; |
masahikofukasawa | 13:d008249f0359 | 39 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ330B) sensorName = "CQ-330B"; |
masahikofukasawa | 13:d008249f0359 | 40 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ330E) sensorName = "CQ-330E"; |
masahikofukasawa | 13:d008249f0359 | 41 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ330F) sensorName = "CQ-330F"; |
masahikofukasawa | 13:d008249f0359 | 42 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ330G) sensorName = "CQ-330G"; |
masahikofukasawa | 13:d008249f0359 | 43 | else if(primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR && subId == AkmAnalogSensor::SUB_ID_CQ330H) sensorName = "CQ-330H"; |
masahikofukasawa | 13:d008249f0359 | 44 | else if(primaryId == AKM_PRIMARY_ID_MISC_ANALOG && subId == AkmAnalogSensor::SUB_ID_EM3242) sensorName = "EM3242"; |
masahikofukasawa | 13:d008249f0359 | 45 | else if(primaryId == AKM_PRIMARY_ID_MISC_ANALOG && subId == AkmAnalogSensor::SUB_ID_AK9710) sensorName = "AK9710"; |
masahikofukasawa | 13:d008249f0359 | 46 | |
masahikofukasawa | 11:cef8dc1cf010 | 47 | if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR_LEGACY){ |
masahikofukasawa | 11:cef8dc1cf010 | 48 | // Internal ADC, Legacy |
masahikofukasawa | 13:d008249f0359 | 49 | ain = new AnalogIn(ANALOG_IN_PIN); |
masahikofukasawa | 13:d008249f0359 | 50 | sensorName = "EQ43XL"; |
masahikofukasawa | 11:cef8dc1cf010 | 51 | } |
masahikofukasawa | 11:cef8dc1cf010 | 52 | else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR || |
masahikofukasawa | 11:cef8dc1cf010 | 53 | primaryId == AKM_PRIMARY_ID_MISC_ANALOG || |
masahikofukasawa | 11:cef8dc1cf010 | 54 | primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR) |
masahikofukasawa | 11:cef8dc1cf010 | 55 | { |
masahikofukasawa | 11:cef8dc1cf010 | 56 | MCP342X::AdcChannel channel = MCP342X::ADC_CH3; // 0-5.0V out as default |
masahikofukasawa | 11:cef8dc1cf010 | 57 | |
masahikofukasawa | 11:cef8dc1cf010 | 58 | if( (primaryId == AKM_PRIMARY_ID_MISC_ANALOG) && (subId < 0x08) ){ |
masahikofukasawa | 11:cef8dc1cf010 | 59 | channel = MCP342X::ADC_CH4; // 0-3.0V out |
masahikofukasawa | 11:cef8dc1cf010 | 60 | } |
masahikofukasawa | 11:cef8dc1cf010 | 61 | |
masahikofukasawa | 11:cef8dc1cf010 | 62 | // 16bit ADC |
masahikofukasawa | 11:cef8dc1cf010 | 63 | I2C* i2c = new I2C(I2C_SDA,I2C_SCL); |
masahikofukasawa | 11:cef8dc1cf010 | 64 | i2c->frequency(I2C_SPEED_100KHZ); |
masahikofukasawa | 11:cef8dc1cf010 | 65 | |
masahikofukasawa | 11:cef8dc1cf010 | 66 | // ADC setting |
masahikofukasawa | 11:cef8dc1cf010 | 67 | mcp3428 = new MCP342X(i2c, MCP342X::SLAVE_ADDRESS_6EH); |
masahikofukasawa | 11:cef8dc1cf010 | 68 | mcp3428->setChannel(channel); |
masahikofukasawa | 11:cef8dc1cf010 | 69 | mcp3428->setSampleSetting(MCP342X::SAMPLE_15HZ_16BIT); |
masahikofukasawa | 11:cef8dc1cf010 | 70 | mcp3428->setConversionMode(MCP342X::CONTINUOUS); |
masahikofukasawa | 11:cef8dc1cf010 | 71 | } |
masahikofukasawa | 11:cef8dc1cf010 | 72 | else{ |
masahikofukasawa | 11:cef8dc1cf010 | 73 | return AkmSensor::ERROR; |
masahikofukasawa | 11:cef8dc1cf010 | 74 | } |
masahikofukasawa | 11:cef8dc1cf010 | 75 | |
masahikofukasawa | 11:cef8dc1cf010 | 76 | interval = SENSOR_SAMPLING_RATE; // 10Hz |
masahikofukasawa | 11:cef8dc1cf010 | 77 | |
masahikofukasawa | 11:cef8dc1cf010 | 78 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 79 | } |
masahikofukasawa | 11:cef8dc1cf010 | 80 | |
masahikofukasawa | 11:cef8dc1cf010 | 81 | void AkmAnalogSensor::eventCallback(){ |
masahikofukasawa | 11:cef8dc1cf010 | 82 | event = true; |
masahikofukasawa | 11:cef8dc1cf010 | 83 | } |
masahikofukasawa | 11:cef8dc1cf010 | 84 | |
masahikofukasawa | 11:cef8dc1cf010 | 85 | bool AkmAnalogSensor::isEvent(){ |
masahikofukasawa | 11:cef8dc1cf010 | 86 | return event; |
masahikofukasawa | 11:cef8dc1cf010 | 87 | } |
masahikofukasawa | 11:cef8dc1cf010 | 88 | |
masahikofukasawa | 11:cef8dc1cf010 | 89 | AkmSensor::Status AkmAnalogSensor::startSensor(){ |
masahikofukasawa | 11:cef8dc1cf010 | 90 | ticker.attach(this, &AkmAnalogSensor::eventCallback, interval); |
masahikofukasawa | 11:cef8dc1cf010 | 91 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 92 | } |
masahikofukasawa | 11:cef8dc1cf010 | 93 | |
masahikofukasawa | 11:cef8dc1cf010 | 94 | AkmSensor::Status AkmAnalogSensor::startSensor(const float sec){ |
masahikofukasawa | 11:cef8dc1cf010 | 95 | interval = sec; |
masahikofukasawa | 11:cef8dc1cf010 | 96 | ticker.attach(this, &AkmAnalogSensor::eventCallback, interval); |
masahikofukasawa | 11:cef8dc1cf010 | 97 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 98 | } |
masahikofukasawa | 11:cef8dc1cf010 | 99 | |
masahikofukasawa | 11:cef8dc1cf010 | 100 | AkmSensor::Status AkmAnalogSensor::stopSensor(){ |
masahikofukasawa | 11:cef8dc1cf010 | 101 | ticker.detach(); |
masahikofukasawa | 11:cef8dc1cf010 | 102 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 103 | } |
masahikofukasawa | 11:cef8dc1cf010 | 104 | |
masahikofukasawa | 11:cef8dc1cf010 | 105 | AkmSensor::Status AkmAnalogSensor::readSensorData(Message* msg){ |
masahikofukasawa | 11:cef8dc1cf010 | 106 | event = false; |
masahikofukasawa | 11:cef8dc1cf010 | 107 | |
masahikofukasawa | 11:cef8dc1cf010 | 108 | msg->setCommand(Message::CMD_START_MEASUREMENT); |
masahikofukasawa | 11:cef8dc1cf010 | 109 | |
masahikofukasawa | 11:cef8dc1cf010 | 110 | if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR_LEGACY){ |
masahikofukasawa | 11:cef8dc1cf010 | 111 | uint16_t value; |
masahikofukasawa | 11:cef8dc1cf010 | 112 | float s = *ain; |
masahikofukasawa | 11:cef8dc1cf010 | 113 | value = s*1024; |
masahikofukasawa | 11:cef8dc1cf010 | 114 | msg->setArgument( 0, (char)( value >> 8) ); |
masahikofukasawa | 11:cef8dc1cf010 | 115 | msg->setArgument( 1, (char)( value & 0x00FF) ); |
masahikofukasawa | 11:cef8dc1cf010 | 116 | }else if(primaryId == AKM_PRIMARY_ID_LINEAR_SENSOR || |
masahikofukasawa | 11:cef8dc1cf010 | 117 | primaryId == AKM_PRIMARY_ID_MISC_ANALOG || |
masahikofukasawa | 11:cef8dc1cf010 | 118 | primaryId == AKM_PRIMARY_ID_CURRENT_SENSOR){ |
masahikofukasawa | 11:cef8dc1cf010 | 119 | MCP342X::Data data; |
masahikofukasawa | 11:cef8dc1cf010 | 120 | do { |
masahikofukasawa | 11:cef8dc1cf010 | 121 | mcp3428->getData(&data); |
masahikofukasawa | 11:cef8dc1cf010 | 122 | wait_ms(WAIT_ADC_MS); |
masahikofukasawa | 11:cef8dc1cf010 | 123 | } while(data.st == MCP342X::DATA_NOT_UPDATED); |
masahikofukasawa | 11:cef8dc1cf010 | 124 | |
masahikofukasawa | 11:cef8dc1cf010 | 125 | msg->setArgument( 0, (char)( (data.value&0xFF00) >> 8) ); |
masahikofukasawa | 11:cef8dc1cf010 | 126 | msg->setArgument( 1, (char)( data.value & 0x00FF) ); |
masahikofukasawa | 11:cef8dc1cf010 | 127 | }else{ |
masahikofukasawa | 11:cef8dc1cf010 | 128 | msg->setArgument( 0, 0); |
masahikofukasawa | 11:cef8dc1cf010 | 129 | msg->setArgument( 1, 0); |
masahikofukasawa | 11:cef8dc1cf010 | 130 | return AkmSensor::ERROR; |
masahikofukasawa | 11:cef8dc1cf010 | 131 | } |
masahikofukasawa | 11:cef8dc1cf010 | 132 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 133 | } |
masahikofukasawa | 11:cef8dc1cf010 | 134 | |
masahikofukasawa | 11:cef8dc1cf010 | 135 | AkmSensor::Status AkmAnalogSensor::requestCommand(Message* in, Message* out){ |
masahikofukasawa | 11:cef8dc1cf010 | 136 | return AkmSensor::SUCCESS; |
masahikofukasawa | 11:cef8dc1cf010 | 137 | } |
masahikofukasawa | 13:d008249f0359 | 138 | char* AkmAnalogSensor::getSensorName(){ |
masahikofukasawa | 13:d008249f0359 | 139 | return sensorName; |
masahikofukasawa | 13:d008249f0359 | 140 | } |