Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
akmakd.cpp@42:b48b3ab8690e, 2017-11-03 (annotated)
- Committer:
- masahikofukasawa
- Date:
- Fri Nov 03 19:45:33 2017 +0000
- Revision:
- 42:b48b3ab8690e
- Parent:
- 41:a3ea80c594ec
- Child:
- 43:45225713cd58
TBD. Try to change serial baud rate to 1Mbps or 921600bps.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
masahikofukasawa | 0:7a00359e701e | 1 | #include "akmakd.h" |
masahikofukasawa | 0:7a00359e701e | 2 | #include "ak8963.h" |
masahikofukasawa | 0:7a00359e701e | 3 | #include "ak099xx.h" |
masahikofukasawa | 42:b48b3ab8690e | 4 | #include "ak09940.h" |
masahikofukasawa | 0:7a00359e701e | 5 | #include "debug.h" |
masahikofukasawa | 0:7a00359e701e | 6 | |
masahikofukasawa | 0:7a00359e701e | 7 | /** |
masahikofukasawa | 0:7a00359e701e | 8 | * Constructor. |
masahikofukasawa | 0:7a00359e701e | 9 | * |
masahikofukasawa | 0:7a00359e701e | 10 | */ |
masahikofukasawa | 27:41aa9fb23a2f | 11 | AkmAkd::AkmAkd() : AkmSensor(){ |
masahikofukasawa | 0:7a00359e701e | 12 | compass = NULL; |
masahikofukasawa | 0:7a00359e701e | 13 | } |
masahikofukasawa | 0:7a00359e701e | 14 | |
masahikofukasawa | 0:7a00359e701e | 15 | /** |
masahikofukasawa | 0:7a00359e701e | 16 | * Destructor. |
masahikofukasawa | 0:7a00359e701e | 17 | * |
masahikofukasawa | 0:7a00359e701e | 18 | */ |
masahikofukasawa | 0:7a00359e701e | 19 | AkmAkd::~AkmAkd(){ |
masahikofukasawa | 0:7a00359e701e | 20 | if (compass) delete compass; |
masahikofukasawa | 0:7a00359e701e | 21 | } |
masahikofukasawa | 0:7a00359e701e | 22 | |
masahikofukasawa | 20:2fca76521680 | 23 | AkmSensor::Status AkmAkd::checkSensor( const uint8_t primaryid, const uint8_t subid, AkmECompass::DeviceId* devid){ |
masahikofukasawa | 19:8dcc4f323bdc | 24 | AK099XX* ak099xx; |
masahikofukasawa | 42:b48b3ab8690e | 25 | AK09940* ak09940; |
masahikofukasawa | 19:8dcc4f323bdc | 26 | AK8963* ak8963; |
masahikofukasawa | 42:b48b3ab8690e | 27 | lenOptions = 0; |
masahikofukasawa | 42:b48b3ab8690e | 28 | |
masahikofukasawa | 19:8dcc4f323bdc | 29 | switch(subid){ |
masahikofukasawa | 19:8dcc4f323bdc | 30 | case AkmAkd::SUB_ID_AK8963N: |
masahikofukasawa | 19:8dcc4f323bdc | 31 | case AkmAkd::SUB_ID_AK8963C: |
masahikofukasawa | 20:2fca76521680 | 32 | *devid = AkmECompass::AK8963; |
masahikofukasawa | 19:8dcc4f323bdc | 33 | AkmAkd::sensorName = "AK8963"; |
masahikofukasawa | 19:8dcc4f323bdc | 34 | ak8963 = new AK8963(); |
masahikofukasawa | 19:8dcc4f323bdc | 35 | compass = ak8963; |
masahikofukasawa | 19:8dcc4f323bdc | 36 | break; |
masahikofukasawa | 19:8dcc4f323bdc | 37 | case AkmAkd::SUB_ID_AK09911C: |
masahikofukasawa | 20:2fca76521680 | 38 | *devid = AkmECompass::AK09911; |
masahikofukasawa | 19:8dcc4f323bdc | 39 | AkmAkd::sensorName = "AK09911C"; |
masahikofukasawa | 19:8dcc4f323bdc | 40 | if(primaryId == AKM_PRIMARY_ID_AKD_SPI) |
masahikofukasawa | 19:8dcc4f323bdc | 41 | return AkmSensor::ERROR; // No SPI support |
masahikofukasawa | 19:8dcc4f323bdc | 42 | ak099xx = new AK099XX(); |
masahikofukasawa | 19:8dcc4f323bdc | 43 | compass = ak099xx; |
masahikofukasawa | 19:8dcc4f323bdc | 44 | break; |
masahikofukasawa | 19:8dcc4f323bdc | 45 | case AkmAkd::SUB_ID_AK09912C: |
masahikofukasawa | 20:2fca76521680 | 46 | *devid = AkmECompass::AK09912; |
masahikofukasawa | 19:8dcc4f323bdc | 47 | AkmAkd::sensorName = "AK09912C"; |
masahikofukasawa | 19:8dcc4f323bdc | 48 | ak099xx = new AK099XX(); |
masahikofukasawa | 19:8dcc4f323bdc | 49 | compass = ak099xx; |
masahikofukasawa | 41:a3ea80c594ec | 50 | lenOptions = 1; |
masahikofukasawa | 19:8dcc4f323bdc | 51 | break; |
masahikofukasawa | 19:8dcc4f323bdc | 52 | case AkmAkd::SUB_ID_AK09915C: |
masahikofukasawa | 20:2fca76521680 | 53 | *devid = AkmECompass::AK09915; |
masahikofukasawa | 19:8dcc4f323bdc | 54 | AkmAkd::sensorName = "AK09915C"; |
masahikofukasawa | 19:8dcc4f323bdc | 55 | ak099xx = new AK099XX(); |
masahikofukasawa | 19:8dcc4f323bdc | 56 | compass = ak099xx; |
masahikofukasawa | 41:a3ea80c594ec | 57 | lenOptions = 2; |
masahikofukasawa | 19:8dcc4f323bdc | 58 | break; |
masahikofukasawa | 19:8dcc4f323bdc | 59 | case AkmAkd::SUB_ID_AK09915D: |
masahikofukasawa | 20:2fca76521680 | 60 | *devid = AkmECompass::AK09915; |
masahikofukasawa | 19:8dcc4f323bdc | 61 | AkmAkd::sensorName = "AK09915D"; |
masahikofukasawa | 19:8dcc4f323bdc | 62 | ak099xx = new AK099XX(); |
masahikofukasawa | 19:8dcc4f323bdc | 63 | compass = ak099xx; |
masahikofukasawa | 41:a3ea80c594ec | 64 | lenOptions = 2; |
masahikofukasawa | 19:8dcc4f323bdc | 65 | break; |
masahikofukasawa | 19:8dcc4f323bdc | 66 | case AkmAkd::SUB_ID_AK09916C: |
masahikofukasawa | 20:2fca76521680 | 67 | *devid = AkmECompass::AK09916C; |
masahikofukasawa | 19:8dcc4f323bdc | 68 | AkmAkd::sensorName = "AK09916C"; |
masahikofukasawa | 19:8dcc4f323bdc | 69 | if(primaryId == AKM_PRIMARY_ID_AKD_SPI) |
masahikofukasawa | 19:8dcc4f323bdc | 70 | return AkmSensor::ERROR; // No SPI support |
masahikofukasawa | 19:8dcc4f323bdc | 71 | ak099xx = new AK099XX(); |
masahikofukasawa | 19:8dcc4f323bdc | 72 | compass = ak099xx; |
masahikofukasawa | 19:8dcc4f323bdc | 73 | break; |
masahikofukasawa | 19:8dcc4f323bdc | 74 | case AkmAkd::SUB_ID_AK09916D: |
masahikofukasawa | 20:2fca76521680 | 75 | *devid = AkmECompass::AK09916D; |
masahikofukasawa | 19:8dcc4f323bdc | 76 | AkmAkd::sensorName = "AK09916D"; |
masahikofukasawa | 19:8dcc4f323bdc | 77 | if(primaryId == AKM_PRIMARY_ID_AKD_SPI) |
masahikofukasawa | 19:8dcc4f323bdc | 78 | return AkmSensor::ERROR; // No SPI support |
masahikofukasawa | 19:8dcc4f323bdc | 79 | ak099xx = new AK099XX(); |
masahikofukasawa | 19:8dcc4f323bdc | 80 | compass = ak099xx; |
masahikofukasawa | 19:8dcc4f323bdc | 81 | break; |
masahikofukasawa | 19:8dcc4f323bdc | 82 | case AkmAkd::SUB_ID_AK09918: |
masahikofukasawa | 20:2fca76521680 | 83 | *devid = AkmECompass::AK09918; |
masahikofukasawa | 19:8dcc4f323bdc | 84 | AkmAkd::sensorName = "AK09918"; |
masahikofukasawa | 19:8dcc4f323bdc | 85 | if(primaryId == AKM_PRIMARY_ID_AKD_SPI) |
masahikofukasawa | 19:8dcc4f323bdc | 86 | return AkmSensor::ERROR; // No SPI support |
masahikofukasawa | 19:8dcc4f323bdc | 87 | ak099xx = new AK099XX(); |
masahikofukasawa | 19:8dcc4f323bdc | 88 | compass = ak099xx; |
masahikofukasawa | 19:8dcc4f323bdc | 89 | break; |
masahikofukasawa | 38:e865dadfe54d | 90 | case AkmAkd::SUB_ID_AK09917: |
masahikofukasawa | 38:e865dadfe54d | 91 | *devid = AkmECompass::AK09917; |
masahikofukasawa | 38:e865dadfe54d | 92 | AkmAkd::sensorName = "AK09917"; |
masahikofukasawa | 38:e865dadfe54d | 93 | if(primaryId == AKM_PRIMARY_ID_AKD_SPI) |
masahikofukasawa | 38:e865dadfe54d | 94 | return AkmSensor::ERROR; // No SPI support |
masahikofukasawa | 38:e865dadfe54d | 95 | ak099xx = new AK099XX(); |
masahikofukasawa | 38:e865dadfe54d | 96 | compass = ak099xx; |
masahikofukasawa | 41:a3ea80c594ec | 97 | lenOptions = 2; |
masahikofukasawa | 41:a3ea80c594ec | 98 | break; |
masahikofukasawa | 41:a3ea80c594ec | 99 | case AkmAkd::SUB_ID_AK09940: |
masahikofukasawa | 41:a3ea80c594ec | 100 | *devid = AkmECompass::AK09940; |
masahikofukasawa | 41:a3ea80c594ec | 101 | AkmAkd::sensorName = "AK09940"; |
masahikofukasawa | 42:b48b3ab8690e | 102 | ak09940 = new AK09940(); |
masahikofukasawa | 42:b48b3ab8690e | 103 | compass = ak09940; |
masahikofukasawa | 41:a3ea80c594ec | 104 | lenOptions = 5; |
masahikofukasawa | 38:e865dadfe54d | 105 | break; |
masahikofukasawa | 19:8dcc4f323bdc | 106 | default: |
masahikofukasawa | 19:8dcc4f323bdc | 107 | return AkmSensor::ERROR; |
masahikofukasawa | 19:8dcc4f323bdc | 108 | } |
masahikofukasawa | 41:a3ea80c594ec | 109 | |
masahikofukasawa | 41:a3ea80c594ec | 110 | uint8_t options[lenOptions]; |
masahikofukasawa | 41:a3ea80c594ec | 111 | mode.options = options; |
masahikofukasawa | 41:a3ea80c594ec | 112 | |
masahikofukasawa | 19:8dcc4f323bdc | 113 | return AkmSensor::SUCCESS; |
masahikofukasawa | 19:8dcc4f323bdc | 114 | } |
masahikofukasawa | 19:8dcc4f323bdc | 115 | |
masahikofukasawa | 20:2fca76521680 | 116 | AkmSensor::Status AkmAkd::init(const uint8_t primaryid, const uint8_t subid){ |
masahikofukasawa | 20:2fca76521680 | 117 | primaryId = primaryid; |
masahikofukasawa | 0:7a00359e701e | 118 | subId = subid; |
masahikofukasawa | 42:b48b3ab8690e | 119 | |
masahikofukasawa | 20:2fca76521680 | 120 | AkmECompass::DeviceId devid; |
masahikofukasawa | 19:8dcc4f323bdc | 121 | |
masahikofukasawa | 41:a3ea80c594ec | 122 | mode.mode = AkmECompass::MODE_POWER_DOWN; |
masahikofukasawa | 9:6fa3e7b17c27 | 123 | |
masahikofukasawa | 0:7a00359e701e | 124 | if(primaryId == AKM_PRIMARY_ID_AKD_I2C){ |
masahikofukasawa | 0:7a00359e701e | 125 | |
masahikofukasawa | 0:7a00359e701e | 126 | I2C* i2c = new I2C(I2C_SDA,I2C_SCL); |
masahikofukasawa | 40:42e48427e4b7 | 127 | i2c->frequency(I2C_SPEED); |
masahikofukasawa | 0:7a00359e701e | 128 | |
masahikofukasawa | 20:2fca76521680 | 129 | if( checkSensor(primaryId,subId,&devid) != AkmSensor::SUCCESS ){ |
masahikofukasawa | 19:8dcc4f323bdc | 130 | return AkmSensor::ERROR; |
masahikofukasawa | 0:7a00359e701e | 131 | } |
masahikofukasawa | 0:7a00359e701e | 132 | |
masahikofukasawa | 0:7a00359e701e | 133 | AkmECompass::SlaveAddress slaveAddr[] |
masahikofukasawa | 0:7a00359e701e | 134 | = { AkmECompass::SLAVE_ADDR_1, |
masahikofukasawa | 0:7a00359e701e | 135 | AkmECompass::SLAVE_ADDR_2, |
masahikofukasawa | 0:7a00359e701e | 136 | AkmECompass::SLAVE_ADDR_3, |
masahikofukasawa | 0:7a00359e701e | 137 | AkmECompass::SLAVE_ADDR_4}; |
masahikofukasawa | 0:7a00359e701e | 138 | |
masahikofukasawa | 0:7a00359e701e | 139 | for(int i=0; i<sizeof(slaveAddr); i++) |
masahikofukasawa | 0:7a00359e701e | 140 | { |
masahikofukasawa | 42:b48b3ab8690e | 141 | MSG("#look for e-compass.\r\n"); |
masahikofukasawa | 20:2fca76521680 | 142 | compass->init(i2c, slaveAddr[i], devid); |
masahikofukasawa | 0:7a00359e701e | 143 | // Checks connectivity |
masahikofukasawa | 42:b48b3ab8690e | 144 | MSG("#checkConnection.\r\n"); |
masahikofukasawa | 0:7a00359e701e | 145 | if(compass->checkConnection() == AkmECompass::SUCCESS) { |
masahikofukasawa | 19:8dcc4f323bdc | 146 | MSG("#%s detected.\r\n", AkmAkd::sensorName); |
masahikofukasawa | 19:8dcc4f323bdc | 147 | return AkmSensor::SUCCESS; |
masahikofukasawa | 0:7a00359e701e | 148 | } |
masahikofukasawa | 0:7a00359e701e | 149 | } |
masahikofukasawa | 0:7a00359e701e | 150 | }else if(primaryId == AKM_PRIMARY_ID_AKD_SPI){ |
masahikofukasawa | 7:e269411568c9 | 151 | |
masahikofukasawa | 7:e269411568c9 | 152 | SPI* spi = new SPI(SPI_MOSI, SPI_MISO, SPI_SCK); |
masahikofukasawa | 7:e269411568c9 | 153 | spi->format(8,3); // 8bit, Mode=3 |
masahikofukasawa | 41:a3ea80c594ec | 154 | spi->frequency(SPI_SPEED); |
masahikofukasawa | 7:e269411568c9 | 155 | |
masahikofukasawa | 7:e269411568c9 | 156 | DigitalOut* cs = new DigitalOut(SPI_CS); |
masahikofukasawa | 19:8dcc4f323bdc | 157 | |
masahikofukasawa | 20:2fca76521680 | 158 | if( checkSensor(primaryId,subId,&devid) != AkmSensor::SUCCESS ){ |
masahikofukasawa | 19:8dcc4f323bdc | 159 | return AkmSensor::ERROR; |
masahikofukasawa | 7:e269411568c9 | 160 | } |
masahikofukasawa | 7:e269411568c9 | 161 | |
masahikofukasawa | 20:2fca76521680 | 162 | compass->init(spi, cs, devid); |
masahikofukasawa | 7:e269411568c9 | 163 | if(compass->checkConnection() == AkmECompass::SUCCESS) { |
masahikofukasawa | 19:8dcc4f323bdc | 164 | MSG("#%s detected.\r\n", AkmAkd::sensorName); |
masahikofukasawa | 19:8dcc4f323bdc | 165 | return AkmSensor::SUCCESS; |
masahikofukasawa | 7:e269411568c9 | 166 | } |
masahikofukasawa | 0:7a00359e701e | 167 | } |
masahikofukasawa | 42:b48b3ab8690e | 168 | MSG("#ERROR: couldn't detected.\r\n"); |
masahikofukasawa | 19:8dcc4f323bdc | 169 | return AkmSensor::ERROR; |
masahikofukasawa | 0:7a00359e701e | 170 | } |
masahikofukasawa | 0:7a00359e701e | 171 | |
masahikofukasawa | 29:b488d2c89fba | 172 | void AkmAkd::setEvent(){ |
masahikofukasawa | 41:a3ea80c594ec | 173 | AkmECompass::Status status = compass->isDataReady(); |
masahikofukasawa | 41:a3ea80c594ec | 174 | if( status == AkmECompass::DATA_READY ){ |
masahikofukasawa | 29:b488d2c89fba | 175 | base::setEvent(); |
masahikofukasawa | 29:b488d2c89fba | 176 | } |
masahikofukasawa | 0:7a00359e701e | 177 | } |
masahikofukasawa | 0:7a00359e701e | 178 | |
masahikofukasawa | 19:8dcc4f323bdc | 179 | AkmAkd::InterruptMode AkmAkd::getInterrupt(uint8_t primaryId, uint8_t subId){ |
masahikofukasawa | 19:8dcc4f323bdc | 180 | AkmAkd::InterruptMode ret = AkmAkd::INTERRUPT_DISABLED; |
masahikofukasawa | 19:8dcc4f323bdc | 181 | |
masahikofukasawa | 19:8dcc4f323bdc | 182 | if( primaryId == AKM_PRIMARY_ID_AKD_I2C && |
masahikofukasawa | 19:8dcc4f323bdc | 183 | (subId == AkmAkd::SUB_ID_AK8963N || |
masahikofukasawa | 19:8dcc4f323bdc | 184 | subId == AkmAkd::SUB_ID_AK8963C || |
masahikofukasawa | 19:8dcc4f323bdc | 185 | subId == AkmAkd::SUB_ID_AK09912C || |
masahikofukasawa | 41:a3ea80c594ec | 186 | subId == AkmAkd::SUB_ID_AK09915C || |
masahikofukasawa | 41:a3ea80c594ec | 187 | subId == AkmAkd::SUB_ID_AK09940 ) ){ |
masahikofukasawa | 19:8dcc4f323bdc | 188 | ret = AkmAkd::INTERRUPT_ENABLED_PP; |
masahikofukasawa | 19:8dcc4f323bdc | 189 | } |
masahikofukasawa | 19:8dcc4f323bdc | 190 | else if( primaryId == AKM_PRIMARY_ID_AKD_I2C && |
masahikofukasawa | 19:8dcc4f323bdc | 191 | (subId == AkmAkd::SUB_ID_AK09915D || |
masahikofukasawa | 38:e865dadfe54d | 192 | subId == AkmAkd::SUB_ID_AK09916D || |
masahikofukasawa | 38:e865dadfe54d | 193 | subId == AkmAkd::SUB_ID_AK09917 ) ){ |
masahikofukasawa | 19:8dcc4f323bdc | 194 | ret = AkmAkd::INTERRUPT_ENABLED_OD; |
masahikofukasawa | 19:8dcc4f323bdc | 195 | } |
masahikofukasawa | 19:8dcc4f323bdc | 196 | else{ |
masahikofukasawa | 19:8dcc4f323bdc | 197 | // No DRDY |
masahikofukasawa | 19:8dcc4f323bdc | 198 | // No DRDY use for SPI interface |
masahikofukasawa | 19:8dcc4f323bdc | 199 | } |
masahikofukasawa | 19:8dcc4f323bdc | 200 | return ret; |
masahikofukasawa | 19:8dcc4f323bdc | 201 | } |
masahikofukasawa | 19:8dcc4f323bdc | 202 | |
masahikofukasawa | 0:7a00359e701e | 203 | AkmSensor::Status AkmAkd::startSensor(){ |
masahikofukasawa | 11:cef8dc1cf010 | 204 | // read one data to clear DRDY |
masahikofukasawa | 41:a3ea80c594ec | 205 | AkmECompass::MagneticVectorLsb mag; |
masahikofukasawa | 41:a3ea80c594ec | 206 | compass->getMagneticVectorLsb(&mag); |
masahikofukasawa | 9:6fa3e7b17c27 | 207 | |
masahikofukasawa | 19:8dcc4f323bdc | 208 | AkmAkd::InterruptMode int_mode = getInterrupt(primaryId,subId); |
masahikofukasawa | 39:3821886c902e | 209 | if( int_mode == AkmAkd::INTERRUPT_DISABLED ){ |
masahikofukasawa | 39:3821886c902e | 210 | // No DRDY, attach timer and start |
masahikofukasawa | 29:b488d2c89fba | 211 | ticker.attach(callback(this, &AkmAkd::setEvent), 1.0/AKDP_POLLING_FREQUENCY); |
masahikofukasawa | 0:7a00359e701e | 212 | } |
masahikofukasawa | 11:cef8dc1cf010 | 213 | |
masahikofukasawa | 11:cef8dc1cf010 | 214 | // set operation mode |
masahikofukasawa | 41:a3ea80c594ec | 215 | if(compass->setOperationMode(mode) != AkmECompass::SUCCESS) { |
masahikofukasawa | 41:a3ea80c594ec | 216 | MSG("#Error: Start sensor failed %s\r\n", sensorName); |
masahikofukasawa | 41:a3ea80c594ec | 217 | return AkmSensor::ERROR; |
masahikofukasawa | 11:cef8dc1cf010 | 218 | } |
masahikofukasawa | 11:cef8dc1cf010 | 219 | |
masahikofukasawa | 29:b488d2c89fba | 220 | MSG("#Start sensor %s.\r\n",sensorName); |
masahikofukasawa | 0:7a00359e701e | 221 | return AkmSensor::SUCCESS; |
masahikofukasawa | 0:7a00359e701e | 222 | } |
masahikofukasawa | 0:7a00359e701e | 223 | |
masahikofukasawa | 0:7a00359e701e | 224 | AkmSensor::Status AkmAkd::startSensor(const float sec){ |
masahikofukasawa | 11:cef8dc1cf010 | 225 | return AkmSensor::ERROR; |
masahikofukasawa | 0:7a00359e701e | 226 | } |
masahikofukasawa | 0:7a00359e701e | 227 | |
masahikofukasawa | 0:7a00359e701e | 228 | AkmSensor::Status AkmAkd::stopSensor(){ |
masahikofukasawa | 0:7a00359e701e | 229 | ticker.detach(); |
masahikofukasawa | 29:b488d2c89fba | 230 | AkmSensor::clearEvent(); |
masahikofukasawa | 41:a3ea80c594ec | 231 | |
masahikofukasawa | 41:a3ea80c594ec | 232 | AkmECompass::Mode temp; |
masahikofukasawa | 41:a3ea80c594ec | 233 | temp.mode = AkmECompass::MODE_POWER_DOWN; |
masahikofukasawa | 42:b48b3ab8690e | 234 | uint8_t options[lenOptions]; |
masahikofukasawa | 41:a3ea80c594ec | 235 | for(int i=0; i<lenOptions; i++){ |
masahikofukasawa | 42:b48b3ab8690e | 236 | options[i] = mode.options[i]; |
masahikofukasawa | 41:a3ea80c594ec | 237 | } |
masahikofukasawa | 42:b48b3ab8690e | 238 | temp.options = options; |
masahikofukasawa | 41:a3ea80c594ec | 239 | if(compass->setOperationMode(temp) != AkmECompass::SUCCESS) { |
masahikofukasawa | 41:a3ea80c594ec | 240 | return AkmSensor::ERROR; |
masahikofukasawa | 0:7a00359e701e | 241 | } |
masahikofukasawa | 20:2fca76521680 | 242 | |
masahikofukasawa | 11:cef8dc1cf010 | 243 | // read one data to clear DRDY |
masahikofukasawa | 41:a3ea80c594ec | 244 | AkmECompass::MagneticVectorLsb mag; |
masahikofukasawa | 41:a3ea80c594ec | 245 | compass->getMagneticVectorLsb(&mag); |
masahikofukasawa | 11:cef8dc1cf010 | 246 | |
masahikofukasawa | 0:7a00359e701e | 247 | return AkmSensor::SUCCESS; |
masahikofukasawa | 0:7a00359e701e | 248 | } |
masahikofukasawa | 0:7a00359e701e | 249 | |
masahikofukasawa | 0:7a00359e701e | 250 | AkmSensor::Status AkmAkd::readSensorData(Message* msg){ |
masahikofukasawa | 29:b488d2c89fba | 251 | AkmSensor::clearEvent(); |
masahikofukasawa | 0:7a00359e701e | 252 | |
masahikofukasawa | 41:a3ea80c594ec | 253 | AkmECompass::MagneticVectorLsb lsb; |
masahikofukasawa | 41:a3ea80c594ec | 254 | AkmECompass::Status status = compass->getMagneticVectorLsb(&lsb); |
masahikofukasawa | 0:7a00359e701e | 255 | if( status != AkmECompass::SUCCESS){ |
masahikofukasawa | 0:7a00359e701e | 256 | return AkmSensor::ERROR; |
masahikofukasawa | 0:7a00359e701e | 257 | } |
masahikofukasawa | 0:7a00359e701e | 258 | msg->setCommand(Message::CMD_START_MEASUREMENT); |
masahikofukasawa | 41:a3ea80c594ec | 259 | msg->setArgument( 0, (lsb.isOverflow ? 1 : 0) ); |
masahikofukasawa | 41:a3ea80c594ec | 260 | if(subId == AkmAkd::SUB_ID_AK09940) |
masahikofukasawa | 41:a3ea80c594ec | 261 | { |
masahikofukasawa | 41:a3ea80c594ec | 262 | msg->setArgument( 1,(char)( lsb.lsbX>>16 ) ); |
masahikofukasawa | 41:a3ea80c594ec | 263 | msg->setArgument( 2,(char)( lsb.lsbX>>8 ) ); |
masahikofukasawa | 41:a3ea80c594ec | 264 | msg->setArgument( 3,(char)( lsb.lsbX ) ); |
masahikofukasawa | 41:a3ea80c594ec | 265 | msg->setArgument( 4,(char)( lsb.lsbY>>16 ) ); |
masahikofukasawa | 41:a3ea80c594ec | 266 | msg->setArgument( 5,(char)( lsb.lsbY>>8 ) ); |
masahikofukasawa | 41:a3ea80c594ec | 267 | msg->setArgument( 6,(char)( lsb.lsbY ) ); |
masahikofukasawa | 41:a3ea80c594ec | 268 | msg->setArgument( 7,(char)( lsb.lsbZ>>16 ) ); |
masahikofukasawa | 41:a3ea80c594ec | 269 | msg->setArgument( 8,(char)( lsb.lsbZ>>8 ) ); |
masahikofukasawa | 41:a3ea80c594ec | 270 | msg->setArgument( 9,(char)( lsb.lsbZ ) ); |
masahikofukasawa | 41:a3ea80c594ec | 271 | msg->setArgument(10,(char)( lsb.lsbTemp ) ); |
masahikofukasawa | 41:a3ea80c594ec | 272 | } |
masahikofukasawa | 41:a3ea80c594ec | 273 | else |
masahikofukasawa | 41:a3ea80c594ec | 274 | { |
masahikofukasawa | 41:a3ea80c594ec | 275 | msg->setArgument( 1,(char)( lsb.lsbX>>8 ) ); |
masahikofukasawa | 41:a3ea80c594ec | 276 | msg->setArgument( 2,(char)( lsb.lsbX ) ); |
masahikofukasawa | 41:a3ea80c594ec | 277 | msg->setArgument( 3,(char)( lsb.lsbY>>8 ) ); |
masahikofukasawa | 41:a3ea80c594ec | 278 | msg->setArgument( 4,(char)( lsb.lsbY ) ); |
masahikofukasawa | 41:a3ea80c594ec | 279 | msg->setArgument( 5,(char)( lsb.lsbZ>>8 ) ); |
masahikofukasawa | 41:a3ea80c594ec | 280 | msg->setArgument( 6,(char)( lsb.lsbZ ) ); |
masahikofukasawa | 41:a3ea80c594ec | 281 | } |
masahikofukasawa | 0:7a00359e701e | 282 | return AkmSensor::SUCCESS; |
masahikofukasawa | 0:7a00359e701e | 283 | } |
masahikofukasawa | 0:7a00359e701e | 284 | |
masahikofukasawa | 0:7a00359e701e | 285 | AkmSensor::Status AkmAkd::requestCommand(Message* in, Message* out){ |
masahikofukasawa | 9:6fa3e7b17c27 | 286 | AkmSensor::Status status = AkmSensor::SUCCESS; |
masahikofukasawa | 9:6fa3e7b17c27 | 287 | Message::Command cmd = in->getCommand(); |
masahikofukasawa | 9:6fa3e7b17c27 | 288 | out->setCommand(cmd); |
masahikofukasawa | 9:6fa3e7b17c27 | 289 | |
masahikofukasawa | 9:6fa3e7b17c27 | 290 | switch(cmd){ |
masahikofukasawa | 9:6fa3e7b17c27 | 291 | case Message::CMD_COMPASS_GET_OPERATION_MODE: |
masahikofukasawa | 41:a3ea80c594ec | 292 | { |
masahikofukasawa | 41:a3ea80c594ec | 293 | if(compass->getOperationMode(&mode) != AkmECompass::SUCCESS) { |
masahikofukasawa | 41:a3ea80c594ec | 294 | status = AkmSensor::ERROR; |
masahikofukasawa | 41:a3ea80c594ec | 295 | MSG("#Error: set operation mode.\r\n"); |
masahikofukasawa | 15:1238993fd75f | 296 | }else{ |
masahikofukasawa | 41:a3ea80c594ec | 297 | out->setArgument(0,mode.mode); |
masahikofukasawa | 41:a3ea80c594ec | 298 | for(int i=0; i<lenOptions; i++){ |
masahikofukasawa | 41:a3ea80c594ec | 299 | out->setArgument(i+1,mode.options[i]); |
masahikofukasawa | 41:a3ea80c594ec | 300 | } |
masahikofukasawa | 9:6fa3e7b17c27 | 301 | } |
masahikofukasawa | 9:6fa3e7b17c27 | 302 | break; |
masahikofukasawa | 9:6fa3e7b17c27 | 303 | } |
masahikofukasawa | 9:6fa3e7b17c27 | 304 | case Message::CMD_COMPASS_SET_OPERATION_MODE: |
masahikofukasawa | 9:6fa3e7b17c27 | 305 | { |
masahikofukasawa | 41:a3ea80c594ec | 306 | mode.mode = (AkmECompass::OperationMode)in->getArgument(0); |
masahikofukasawa | 41:a3ea80c594ec | 307 | for(int i=0; i<lenOptions; i++){ |
masahikofukasawa | 41:a3ea80c594ec | 308 | mode.options[i] = in->getArgument(i+1); |
masahikofukasawa | 9:6fa3e7b17c27 | 309 | } |
masahikofukasawa | 41:a3ea80c594ec | 310 | if(compass->setOperationMode(mode) != AkmECompass::SUCCESS) { |
masahikofukasawa | 41:a3ea80c594ec | 311 | status = AkmSensor::ERROR; |
masahikofukasawa | 41:a3ea80c594ec | 312 | MSG("#Error: set operation mode.\r\n"); |
masahikofukasawa | 9:6fa3e7b17c27 | 313 | } |
masahikofukasawa | 41:a3ea80c594ec | 314 | out->setArgument(0,(char)status); |
masahikofukasawa | 9:6fa3e7b17c27 | 315 | break; |
masahikofukasawa | 15:1238993fd75f | 316 | } |
masahikofukasawa | 15:1238993fd75f | 317 | case Message::CMD_REG_WRITE: |
masahikofukasawa | 9:6fa3e7b17c27 | 318 | case Message::CMD_REG_WRITEN: |
masahikofukasawa | 9:6fa3e7b17c27 | 319 | { |
masahikofukasawa | 9:6fa3e7b17c27 | 320 | char address = in->getArgument(0); |
masahikofukasawa | 9:6fa3e7b17c27 | 321 | int len = (int)in->getArgument(1); |
masahikofukasawa | 15:1238993fd75f | 322 | if(in->getArgNum() != len+2){ |
masahikofukasawa | 39:3821886c902e | 323 | MSG("#Error: argument num. Args=%d\r\n",in->getArgNum()); |
masahikofukasawa | 15:1238993fd75f | 324 | status = AkmSensor::ERROR; |
masahikofukasawa | 15:1238993fd75f | 325 | out->setArgument(0,(char)status); |
masahikofukasawa | 15:1238993fd75f | 326 | return status; |
masahikofukasawa | 15:1238993fd75f | 327 | } |
masahikofukasawa | 15:1238993fd75f | 328 | |
masahikofukasawa | 9:6fa3e7b17c27 | 329 | char data[len]; |
masahikofukasawa | 9:6fa3e7b17c27 | 330 | for(int i=0; i<len; i++){ |
masahikofukasawa | 15:1238993fd75f | 331 | data[i] = in->getArgument(i+2); |
masahikofukasawa | 9:6fa3e7b17c27 | 332 | } |
masahikofukasawa | 9:6fa3e7b17c27 | 333 | if( compass->write(address, data, len) != AkmECompass::SUCCESS) { |
masahikofukasawa | 9:6fa3e7b17c27 | 334 | status = AkmSensor::ERROR; |
masahikofukasawa | 39:3821886c902e | 335 | MSG("#Error: register write.\r\n"); |
masahikofukasawa | 9:6fa3e7b17c27 | 336 | } |
masahikofukasawa | 9:6fa3e7b17c27 | 337 | out->setArgument(0,(char)status); |
masahikofukasawa | 9:6fa3e7b17c27 | 338 | break; |
masahikofukasawa | 15:1238993fd75f | 339 | } |
masahikofukasawa | 9:6fa3e7b17c27 | 340 | case Message::CMD_REG_READ: |
masahikofukasawa | 9:6fa3e7b17c27 | 341 | case Message::CMD_REG_READN: |
masahikofukasawa | 9:6fa3e7b17c27 | 342 | { |
masahikofukasawa | 15:1238993fd75f | 343 | if(in->getArgNum() != 2){ |
masahikofukasawa | 39:3821886c902e | 344 | MSG("#Error: argument num. Args=%d\r\n",in->getArgNum()); |
masahikofukasawa | 15:1238993fd75f | 345 | status = AkmSensor::ERROR; |
masahikofukasawa | 15:1238993fd75f | 346 | return status; |
masahikofukasawa | 15:1238993fd75f | 347 | } |
masahikofukasawa | 15:1238993fd75f | 348 | |
masahikofukasawa | 9:6fa3e7b17c27 | 349 | char address = in->getArgument(0); |
masahikofukasawa | 9:6fa3e7b17c27 | 350 | int len = (int)in->getArgument(1); |
masahikofukasawa | 9:6fa3e7b17c27 | 351 | char data[len]; |
masahikofukasawa | 9:6fa3e7b17c27 | 352 | if( compass->read(address, data, len) != AkmECompass::SUCCESS) { |
masahikofukasawa | 9:6fa3e7b17c27 | 353 | status = AkmSensor::ERROR; |
masahikofukasawa | 39:3821886c902e | 354 | MSG("#Error: register read.\r\n"); |
masahikofukasawa | 9:6fa3e7b17c27 | 355 | } |
masahikofukasawa | 9:6fa3e7b17c27 | 356 | for(int i=0; i<len; i++){ |
masahikofukasawa | 9:6fa3e7b17c27 | 357 | out->setArgument(i, data[i]); |
masahikofukasawa | 9:6fa3e7b17c27 | 358 | } |
masahikofukasawa | 9:6fa3e7b17c27 | 359 | break; |
masahikofukasawa | 9:6fa3e7b17c27 | 360 | } |
masahikofukasawa | 9:6fa3e7b17c27 | 361 | default: |
masahikofukasawa | 9:6fa3e7b17c27 | 362 | { |
masahikofukasawa | 39:3821886c902e | 363 | MSG("#Error: no command.\r\n"); |
masahikofukasawa | 9:6fa3e7b17c27 | 364 | status = AkmSensor::ERROR; |
masahikofukasawa | 9:6fa3e7b17c27 | 365 | break; |
masahikofukasawa | 9:6fa3e7b17c27 | 366 | } |
masahikofukasawa | 9:6fa3e7b17c27 | 367 | } |
masahikofukasawa | 15:1238993fd75f | 368 | return status; |
masahikofukasawa | 0:7a00359e701e | 369 | } |