Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
ak9750ctrl.cpp@49:c8f8946129b6, 2018-05-01 (annotated)
- Committer:
- tkstreet
- Date:
- Tue May 01 21:31:15 2018 +0000
- Revision:
- 49:c8f8946129b6
- Parent:
- 47:221ec4b404ec
Modified for Rev.E. compatibility.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
masahikofukasawa | 10:5c69b067d88a | 1 | #include "ak9750ctrl.h" |
masahikofukasawa | 10:5c69b067d88a | 2 | |
masahikofukasawa | 10:5c69b067d88a | 3 | #define CONV16I(high,low) ((int16_t)(((high) << 8) | (low))) |
masahikofukasawa | 10:5c69b067d88a | 4 | |
masahikofukasawa | 10:5c69b067d88a | 5 | /** |
masahikofukasawa | 10:5c69b067d88a | 6 | * Constructor. |
masahikofukasawa | 10:5c69b067d88a | 7 | * |
masahikofukasawa | 10:5c69b067d88a | 8 | */ |
masahikofukasawa | 27:41aa9fb23a2f | 9 | Ak9750Ctrl::Ak9750Ctrl() : AkmSensor(){ |
masahikofukasawa | 10:5c69b067d88a | 10 | ak9750 = NULL; |
masahikofukasawa | 10:5c69b067d88a | 11 | } |
masahikofukasawa | 10:5c69b067d88a | 12 | |
masahikofukasawa | 10:5c69b067d88a | 13 | /** |
masahikofukasawa | 10:5c69b067d88a | 14 | * Destructor. |
masahikofukasawa | 10:5c69b067d88a | 15 | * |
masahikofukasawa | 10:5c69b067d88a | 16 | */ |
masahikofukasawa | 10:5c69b067d88a | 17 | Ak9750Ctrl::~Ak9750Ctrl(){ |
masahikofukasawa | 10:5c69b067d88a | 18 | if (ak9750) delete ak9750; |
masahikofukasawa | 10:5c69b067d88a | 19 | } |
masahikofukasawa | 10:5c69b067d88a | 20 | |
masahikofukasawa | 10:5c69b067d88a | 21 | AkmSensor::Status Ak9750Ctrl::init(const uint8_t id, const uint8_t subid){ |
masahikofukasawa | 10:5c69b067d88a | 22 | primaryId = id; |
masahikofukasawa | 10:5c69b067d88a | 23 | subId = subid; |
masahikofukasawa | 10:5c69b067d88a | 24 | |
masahikofukasawa | 10:5c69b067d88a | 25 | I2C* i2c = new I2C(I2C_SDA,I2C_SCL); |
masahikofukasawa | 40:42e48427e4b7 | 26 | i2c->frequency(I2C_SPEED); |
masahikofukasawa | 10:5c69b067d88a | 27 | |
masahikofukasawa | 10:5c69b067d88a | 28 | if(subId == SUB_ID_AK9750){ |
masahikofukasawa | 10:5c69b067d88a | 29 | ak9750 = new AK9750(); |
masahikofukasawa | 13:d008249f0359 | 30 | sensorName = "AK9750"; |
masahikofukasawa | 13:d008249f0359 | 31 | } |
masahikofukasawa | 13:d008249f0359 | 32 | else if(subId == SUB_ID_AK9753){ |
masahikofukasawa | 13:d008249f0359 | 33 | ak9750 = new AK9750(); |
masahikofukasawa | 13:d008249f0359 | 34 | sensorName = "AK9753"; |
masahikofukasawa | 10:5c69b067d88a | 35 | } |
masahikofukasawa | 10:5c69b067d88a | 36 | else{ |
masahikofukasawa | 10:5c69b067d88a | 37 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 38 | } |
masahikofukasawa | 10:5c69b067d88a | 39 | |
masahikofukasawa | 10:5c69b067d88a | 40 | bool foundSensor = false; |
masahikofukasawa | 10:5c69b067d88a | 41 | |
masahikofukasawa | 10:5c69b067d88a | 42 | AK9750::SlaveAddress slaveAddr[] |
masahikofukasawa | 10:5c69b067d88a | 43 | = { AK9750::SLAVE_ADDR_1, |
masahikofukasawa | 10:5c69b067d88a | 44 | AK9750::SLAVE_ADDR_2, |
masahikofukasawa | 10:5c69b067d88a | 45 | AK9750::SLAVE_ADDR_3}; |
masahikofukasawa | 10:5c69b067d88a | 46 | |
masahikofukasawa | 47:221ec4b404ec | 47 | for(unsigned int i=0; i<sizeof(slaveAddr); i++) |
masahikofukasawa | 10:5c69b067d88a | 48 | { |
masahikofukasawa | 10:5c69b067d88a | 49 | ak9750->init(i2c, slaveAddr[i]); |
masahikofukasawa | 10:5c69b067d88a | 50 | // Checks connectivity |
masahikofukasawa | 47:221ec4b404ec | 51 | if(ak9750->checkConnection() == AK9750::SUCCESS) { |
masahikofukasawa | 10:5c69b067d88a | 52 | // found |
masahikofukasawa | 10:5c69b067d88a | 53 | foundSensor = true; |
masahikofukasawa | 10:5c69b067d88a | 54 | break; |
masahikofukasawa | 10:5c69b067d88a | 55 | } |
masahikofukasawa | 10:5c69b067d88a | 56 | } |
masahikofukasawa | 10:5c69b067d88a | 57 | if(foundSensor != true){ |
masahikofukasawa | 39:3821886c902e | 58 | MSG("#Error: Failed to checkConnection AK9750.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 59 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 60 | } |
masahikofukasawa | 10:5c69b067d88a | 61 | |
masahikofukasawa | 10:5c69b067d88a | 62 | // reset |
masahikofukasawa | 10:5c69b067d88a | 63 | if (ak9750->reset() != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 64 | MSG("#Error: Failed to reset AK9750.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 65 | } |
masahikofukasawa | 15:1238993fd75f | 66 | /* |
masahikofukasawa | 10:5c69b067d88a | 67 | // Set to EEPROM mode to EEPROM access |
masahikofukasawa | 10:5c69b067d88a | 68 | if(ak9750->setOperationMode(AK9750::MODE_EEPROM_ACCESS, AK9750::DF_0P3HZ) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 69 | MSG("#Error: setOperationMode to EEPROM mode. AK9750.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 70 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 71 | } |
masahikofukasawa | 15:1238993fd75f | 72 | */ |
masahikofukasawa | 10:5c69b067d88a | 73 | // Gets threshold from EEPROM |
masahikofukasawa | 10:5c69b067d88a | 74 | AK9750::Threshold th; |
masahikofukasawa | 10:5c69b067d88a | 75 | if (ak9750->getThresholdFromEEPROM(&th) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 76 | MSG("#Error: Failed to get threshold from EEPROM AK9750.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 77 | } |
masahikofukasawa | 15:1238993fd75f | 78 | MSG("#Threshold:(0x%02X,0x%02X,0x%02X,0x%02X)\r\n",th.eth13h,th.eth13l,th.eth24h,th.eth24l); |
masahikofukasawa | 15:1238993fd75f | 79 | |
masahikofukasawa | 10:5c69b067d88a | 80 | // Gets hysteresis from EEPROM |
masahikofukasawa | 10:5c69b067d88a | 81 | AK9750::Hysteresis hys; |
masahikofukasawa | 10:5c69b067d88a | 82 | if (ak9750->getHysteresisFromEEPROM(&hys) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 83 | MSG("#Error: Failed to get hysteresis from EEPROM AK9750.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 84 | } |
masahikofukasawa | 15:1238993fd75f | 85 | MSG("#Hysteresis:(0x%02X,0x%02X)\r\n",hys.ehys13,hys.ehys24); |
masahikofukasawa | 15:1238993fd75f | 86 | |
masahikofukasawa | 10:5c69b067d88a | 87 | // Gets interrupt status from EEPROM |
masahikofukasawa | 10:5c69b067d88a | 88 | AK9750::InterruptStatus intStatus; |
masahikofukasawa | 10:5c69b067d88a | 89 | if ((ak9750->getInterruptEnableFromEEPROM(&intStatus)) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 90 | MSG("#Error: Failed to get interrupts of AK9750 from EEPROM.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 91 | } |
masahikofukasawa | 15:1238993fd75f | 92 | MSG("#Interrupt:(0x%02X,0x%02X,0x%02X,0x%02X,0x%02X)\r\n",intStatus.ir13h,intStatus.ir13l,intStatus.ir24h,intStatus.ir24l,intStatus.drdy); |
masahikofukasawa | 10:5c69b067d88a | 93 | |
masahikofukasawa | 10:5c69b067d88a | 94 | // Gets operation mode from EEPROM |
masahikofukasawa | 10:5c69b067d88a | 95 | if ((ak9750->getOperationModeFromEEPROM(&mode,&filter)) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 96 | MSG("#Error: Failed to get operation mode of AK9750 from EEPROM.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 97 | } |
masahikofukasawa | 11:cef8dc1cf010 | 98 | MSG("#Operation Mode:(0x%02X,0x%02X)\r\n",mode, filter); |
masahikofukasawa | 15:1238993fd75f | 99 | /* |
masahikofukasawa | 10:5c69b067d88a | 100 | // Back to Stand-by Mode for register access |
masahikofukasawa | 10:5c69b067d88a | 101 | if(ak9750->setOperationMode(AK9750::MODE_STANDBY, filter) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 102 | MSG("#Error: setOperationMode to stand-by mode of AK9750.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 103 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 104 | } |
masahikofukasawa | 15:1238993fd75f | 105 | */ |
masahikofukasawa | 10:5c69b067d88a | 106 | // Sets threshold from the red EEPROM values |
masahikofukasawa | 10:5c69b067d88a | 107 | if (ak9750->setThreshold(&th) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 108 | MSG("#Error: Failed to set threshold to AK9750.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 109 | } |
masahikofukasawa | 10:5c69b067d88a | 110 | |
masahikofukasawa | 10:5c69b067d88a | 111 | // Sets hysteresis from the red EEPROM values |
masahikofukasawa | 10:5c69b067d88a | 112 | if (ak9750->setHysteresis(&hys) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 113 | MSG("#Error: Failed to set hysteresis to AK9750.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 114 | } |
masahikofukasawa | 10:5c69b067d88a | 115 | |
masahikofukasawa | 10:5c69b067d88a | 116 | // Sets interruput status from the red EEPROM values |
masahikofukasawa | 10:5c69b067d88a | 117 | AK9750::Status status = ak9750->setInterruptEnable(&intStatus); |
masahikofukasawa | 10:5c69b067d88a | 118 | if (status != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 119 | MSG("#Error: Failed to set interrupts of AK9750. Status = 0x%02X\r\n", status); |
masahikofukasawa | 10:5c69b067d88a | 120 | } |
masahikofukasawa | 10:5c69b067d88a | 121 | |
masahikofukasawa | 15:1238993fd75f | 122 | if(ak9750->setOperationMode(mode, filter) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 123 | MSG("#Error: setOperationMode of AK9750.\r\n"); |
masahikofukasawa | 15:1238993fd75f | 124 | return AkmSensor::ERROR; |
masahikofukasawa | 15:1238993fd75f | 125 | } |
masahikofukasawa | 15:1238993fd75f | 126 | |
masahikofukasawa | 11:cef8dc1cf010 | 127 | MSG("#Init success AK9750.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 128 | return AkmSensor::SUCCESS; |
masahikofukasawa | 10:5c69b067d88a | 129 | } |
masahikofukasawa | 10:5c69b067d88a | 130 | |
masahikofukasawa | 29:b488d2c89fba | 131 | void Ak9750Ctrl::setEvent(){ |
masahikofukasawa | 29:b488d2c89fba | 132 | AK9750::Status status = ak9750->isDataReady(); |
masahikofukasawa | 29:b488d2c89fba | 133 | if( status == AK9750::DATA_READY ) base::setEvent(); |
masahikofukasawa | 10:5c69b067d88a | 134 | } |
masahikofukasawa | 10:5c69b067d88a | 135 | |
masahikofukasawa | 10:5c69b067d88a | 136 | AkmSensor::Status Ak9750Ctrl::startSensor(){ |
masahikofukasawa | 11:cef8dc1cf010 | 137 | // read one data to clear INT pin |
masahikofukasawa | 11:cef8dc1cf010 | 138 | AK9750::SensorData data; |
masahikofukasawa | 11:cef8dc1cf010 | 139 | ak9750->getSensorData(&data); |
masahikofukasawa | 11:cef8dc1cf010 | 140 | |
masahikofukasawa | 11:cef8dc1cf010 | 141 | // set operation mode |
masahikofukasawa | 10:5c69b067d88a | 142 | if(ak9750->setOperationMode(mode,filter) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 143 | MSG("#Error: Start sensor failed %s\r\n", sensorName); |
masahikofukasawa | 10:5c69b067d88a | 144 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 145 | } |
masahikofukasawa | 11:cef8dc1cf010 | 146 | |
masahikofukasawa | 29:b488d2c89fba | 147 | MSG("#Start sensor %s.\r\n",sensorName); |
masahikofukasawa | 10:5c69b067d88a | 148 | return AkmSensor::SUCCESS; |
masahikofukasawa | 10:5c69b067d88a | 149 | } |
masahikofukasawa | 10:5c69b067d88a | 150 | |
masahikofukasawa | 10:5c69b067d88a | 151 | AkmSensor::Status Ak9750Ctrl::startSensor(const float sec){ |
masahikofukasawa | 10:5c69b067d88a | 152 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 153 | } |
masahikofukasawa | 10:5c69b067d88a | 154 | |
masahikofukasawa | 10:5c69b067d88a | 155 | AkmSensor::Status Ak9750Ctrl::stopSensor(){ |
masahikofukasawa | 29:b488d2c89fba | 156 | AkmSensor::clearEvent(); |
masahikofukasawa | 29:b488d2c89fba | 157 | |
masahikofukasawa | 10:5c69b067d88a | 158 | if(ak9750->setOperationMode(AK9750::MODE_STANDBY, filter) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 159 | MSG("#Error: setOperationMode. AK9750.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 160 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 161 | } |
masahikofukasawa | 10:5c69b067d88a | 162 | |
masahikofukasawa | 10:5c69b067d88a | 163 | // read one data to clear INT pin |
masahikofukasawa | 11:cef8dc1cf010 | 164 | AK9750::SensorData data; |
masahikofukasawa | 11:cef8dc1cf010 | 165 | ak9750->getSensorData(&data); |
masahikofukasawa | 10:5c69b067d88a | 166 | |
masahikofukasawa | 10:5c69b067d88a | 167 | return AkmSensor::SUCCESS; |
masahikofukasawa | 10:5c69b067d88a | 168 | } |
masahikofukasawa | 10:5c69b067d88a | 169 | |
masahikofukasawa | 10:5c69b067d88a | 170 | AkmSensor::Status Ak9750Ctrl::readSensorData(Message* msg){ |
masahikofukasawa | 29:b488d2c89fba | 171 | AkmSensor::clearEvent(); |
masahikofukasawa | 10:5c69b067d88a | 172 | |
masahikofukasawa | 10:5c69b067d88a | 173 | AK9750::SensorData data; |
masahikofukasawa | 10:5c69b067d88a | 174 | AK9750::Status status = ak9750->getSensorData(&data); |
masahikofukasawa | 10:5c69b067d88a | 175 | if( status != AK9750::SUCCESS){ |
masahikofukasawa | 39:3821886c902e | 176 | MSG("#Error: getSensorData. AK9750.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 177 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 178 | } |
masahikofukasawa | 10:5c69b067d88a | 179 | msg->setCommand(Message::CMD_START_MEASUREMENT); |
masahikofukasawa | 10:5c69b067d88a | 180 | msg->setArgument( 0, data.intStatus.ir13h); |
masahikofukasawa | 10:5c69b067d88a | 181 | msg->setArgument( 1, data.intStatus.ir13l); |
masahikofukasawa | 10:5c69b067d88a | 182 | msg->setArgument( 2, data.intStatus.ir24h); |
masahikofukasawa | 10:5c69b067d88a | 183 | msg->setArgument( 3, data.intStatus.ir24l); |
masahikofukasawa | 10:5c69b067d88a | 184 | msg->setArgument( 4, data.intStatus.drdy); |
masahikofukasawa | 10:5c69b067d88a | 185 | msg->setArgument( 5,(char)((int32_t)(data.ir1) >> 8)); |
masahikofukasawa | 10:5c69b067d88a | 186 | msg->setArgument( 6, (char)((int32_t)(data.ir1) & 0x00FF) ); |
masahikofukasawa | 10:5c69b067d88a | 187 | msg->setArgument( 7,(char)((int32_t)(data.ir2) >> 8)); |
masahikofukasawa | 10:5c69b067d88a | 188 | msg->setArgument( 8, (char)((int32_t)(data.ir2) & 0x00FF) ); |
masahikofukasawa | 10:5c69b067d88a | 189 | msg->setArgument( 9,(char)((int32_t)(data.ir3) >> 8)); |
masahikofukasawa | 10:5c69b067d88a | 190 | msg->setArgument( 10, (char)((int32_t)(data.ir3) & 0x00FF) ); |
masahikofukasawa | 10:5c69b067d88a | 191 | msg->setArgument( 11,(char)((int32_t)(data.ir4) >> 8)); |
masahikofukasawa | 10:5c69b067d88a | 192 | msg->setArgument( 12, (char)((int32_t)(data.ir4) & 0x00FF) ); |
masahikofukasawa | 10:5c69b067d88a | 193 | msg->setArgument( 13,(char)((int32_t)(data.temperature) >> 8)); |
masahikofukasawa | 10:5c69b067d88a | 194 | msg->setArgument( 14, (char)((int32_t)(data.temperature) & 0x00FF) ); |
masahikofukasawa | 10:5c69b067d88a | 195 | msg->setArgument( 15, data.dor); |
masahikofukasawa | 10:5c69b067d88a | 196 | |
masahikofukasawa | 10:5c69b067d88a | 197 | return AkmSensor::SUCCESS; |
masahikofukasawa | 10:5c69b067d88a | 198 | } |
masahikofukasawa | 10:5c69b067d88a | 199 | |
masahikofukasawa | 10:5c69b067d88a | 200 | AkmSensor::Status Ak9750Ctrl::requestCommand(Message* in, Message* out){ |
masahikofukasawa | 10:5c69b067d88a | 201 | AkmSensor::Status status = AkmSensor::SUCCESS; |
masahikofukasawa | 10:5c69b067d88a | 202 | Message::Command cmd = in->getCommand(); |
masahikofukasawa | 10:5c69b067d88a | 203 | AK9750::Threshold th; |
masahikofukasawa | 10:5c69b067d88a | 204 | AK9750::Hysteresis hys; |
masahikofukasawa | 10:5c69b067d88a | 205 | AK9750::InterruptStatus interrupt; |
masahikofukasawa | 10:5c69b067d88a | 206 | |
masahikofukasawa | 10:5c69b067d88a | 207 | out->setCommand(cmd); |
masahikofukasawa | 10:5c69b067d88a | 208 | |
masahikofukasawa | 10:5c69b067d88a | 209 | switch(cmd){ |
masahikofukasawa | 10:5c69b067d88a | 210 | case Message::CMD_IR_GET_THRESHOLD: |
masahikofukasawa | 10:5c69b067d88a | 211 | { |
masahikofukasawa | 10:5c69b067d88a | 212 | if (ak9750->getThreshold(&th) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 213 | MSG("#Error: Failed to get threshold of AK9750.\r\n"); |
masahikofukasawa | 15:1238993fd75f | 214 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 215 | } |
masahikofukasawa | 10:5c69b067d88a | 216 | out->setArgument(0,(char)((int32_t)(th.eth13h) >> 8)); |
masahikofukasawa | 10:5c69b067d88a | 217 | out->setArgument(1,(char)((int32_t)(th.eth13h) & 0x00FF)); |
masahikofukasawa | 10:5c69b067d88a | 218 | out->setArgument(2,(char)((int32_t)(th.eth13l) >> 8)); |
masahikofukasawa | 10:5c69b067d88a | 219 | out->setArgument(3,(char)((int32_t)(th.eth13l) & 0x00FF)); |
masahikofukasawa | 10:5c69b067d88a | 220 | out->setArgument(4,(char)((int32_t)(th.eth24h) >> 8)); |
masahikofukasawa | 10:5c69b067d88a | 221 | out->setArgument(5,(char)((int32_t)(th.eth24h) & 0x00FF)); |
masahikofukasawa | 10:5c69b067d88a | 222 | out->setArgument(6,(char)((int32_t)(th.eth24l) >> 8)); |
masahikofukasawa | 10:5c69b067d88a | 223 | out->setArgument(7,(char)((int32_t)(th.eth24l) & 0x00FF)); |
masahikofukasawa | 10:5c69b067d88a | 224 | break; |
masahikofukasawa | 10:5c69b067d88a | 225 | } |
masahikofukasawa | 10:5c69b067d88a | 226 | case Message::CMD_IR_SET_THRESHOLD: |
masahikofukasawa | 10:5c69b067d88a | 227 | { |
masahikofukasawa | 10:5c69b067d88a | 228 | th.eth13h = CONV16I(in->getArgument(0), in->getArgument(1)); |
masahikofukasawa | 10:5c69b067d88a | 229 | th.eth13l = CONV16I(in->getArgument(2), in->getArgument(3)); |
masahikofukasawa | 10:5c69b067d88a | 230 | th.eth24h = CONV16I(in->getArgument(4), in->getArgument(5)); |
masahikofukasawa | 10:5c69b067d88a | 231 | th.eth24l = CONV16I(in->getArgument(6), in->getArgument(7)); |
masahikofukasawa | 10:5c69b067d88a | 232 | if (ak9750->setThreshold(&th) != AK9750::SUCCESS) { |
masahikofukasawa | 10:5c69b067d88a | 233 | status = AkmSensor::ERROR; |
masahikofukasawa | 39:3821886c902e | 234 | MSG("#Error: Failed to set threshold to AK9750.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 235 | } |
masahikofukasawa | 10:5c69b067d88a | 236 | out->setArgument(0,(char)status); |
masahikofukasawa | 10:5c69b067d88a | 237 | break; |
masahikofukasawa | 10:5c69b067d88a | 238 | } |
masahikofukasawa | 10:5c69b067d88a | 239 | case Message::CMD_IR_GET_HYSTERESIS: |
masahikofukasawa | 10:5c69b067d88a | 240 | { |
masahikofukasawa | 10:5c69b067d88a | 241 | if (ak9750->getHysteresis(&hys) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 242 | MSG("#Error: Failed to get hysteresis of AK9750.\r\n"); |
masahikofukasawa | 15:1238993fd75f | 243 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 244 | } |
masahikofukasawa | 10:5c69b067d88a | 245 | out->setArgument(0,hys.ehys13); |
masahikofukasawa | 10:5c69b067d88a | 246 | out->setArgument(1,hys.ehys24); |
masahikofukasawa | 10:5c69b067d88a | 247 | break; |
masahikofukasawa | 10:5c69b067d88a | 248 | } |
masahikofukasawa | 10:5c69b067d88a | 249 | case Message::CMD_IR_SET_HYSTERESIS: |
masahikofukasawa | 10:5c69b067d88a | 250 | { |
masahikofukasawa | 10:5c69b067d88a | 251 | hys.ehys13 = in->getArgument(0); |
masahikofukasawa | 10:5c69b067d88a | 252 | hys.ehys24 = in->getArgument(1); |
masahikofukasawa | 10:5c69b067d88a | 253 | if (ak9750->setHysteresis(&hys) != AK9750::SUCCESS) { |
masahikofukasawa | 10:5c69b067d88a | 254 | status = AkmSensor::ERROR; |
masahikofukasawa | 39:3821886c902e | 255 | MSG("#Error: Failed to set hysteresis to AK9750.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 256 | } |
masahikofukasawa | 10:5c69b067d88a | 257 | out->setArgument(0,(char)status); |
masahikofukasawa | 10:5c69b067d88a | 258 | break; |
masahikofukasawa | 10:5c69b067d88a | 259 | } |
masahikofukasawa | 10:5c69b067d88a | 260 | case Message::CMD_IR_GET_INTERRUPT: |
masahikofukasawa | 10:5c69b067d88a | 261 | { |
masahikofukasawa | 10:5c69b067d88a | 262 | if (ak9750->getInterruptEnable(&interrupt) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 263 | MSG("#Error: Failed to get interrupt of AK9750.\r\n"); |
masahikofukasawa | 15:1238993fd75f | 264 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 265 | } |
masahikofukasawa | 10:5c69b067d88a | 266 | out->setArgument(0, interrupt.ir13h); |
masahikofukasawa | 10:5c69b067d88a | 267 | out->setArgument(1, interrupt.ir13l); |
masahikofukasawa | 10:5c69b067d88a | 268 | out->setArgument(2, interrupt.ir24h); |
masahikofukasawa | 10:5c69b067d88a | 269 | out->setArgument(3, interrupt.ir24l); |
masahikofukasawa | 10:5c69b067d88a | 270 | out->setArgument(4, interrupt.drdy); |
masahikofukasawa | 10:5c69b067d88a | 271 | break; |
masahikofukasawa | 10:5c69b067d88a | 272 | } |
masahikofukasawa | 10:5c69b067d88a | 273 | case Message::CMD_IR_SET_INTERRUPT: |
masahikofukasawa | 10:5c69b067d88a | 274 | { |
masahikofukasawa | 10:5c69b067d88a | 275 | interrupt.ir13h = in->getArgument(0); |
masahikofukasawa | 10:5c69b067d88a | 276 | interrupt.ir13l = in->getArgument(1); |
masahikofukasawa | 10:5c69b067d88a | 277 | interrupt.ir24h = in->getArgument(2); |
masahikofukasawa | 10:5c69b067d88a | 278 | interrupt.ir24l = in->getArgument(3); |
masahikofukasawa | 10:5c69b067d88a | 279 | interrupt.drdy = in->getArgument(4); |
masahikofukasawa | 10:5c69b067d88a | 280 | if (ak9750->setInterruptEnable(&interrupt) != AK9750::SUCCESS) { |
masahikofukasawa | 10:5c69b067d88a | 281 | status = AkmSensor::ERROR; |
masahikofukasawa | 39:3821886c902e | 282 | MSG("#Error: Failed to set interrupt to AK9750.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 283 | } |
masahikofukasawa | 10:5c69b067d88a | 284 | out->setArgument(0,(char)status); |
masahikofukasawa | 10:5c69b067d88a | 285 | break; |
masahikofukasawa | 10:5c69b067d88a | 286 | } |
masahikofukasawa | 10:5c69b067d88a | 287 | case Message::CMD_IR_GET_OPERATION_MODE: |
masahikofukasawa | 10:5c69b067d88a | 288 | { |
masahikofukasawa | 10:5c69b067d88a | 289 | if(ak9750->getOperationMode(&mode, &filter) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 290 | MSG("#Error: getOperationMode. AK9750.\r\n"); |
masahikofukasawa | 15:1238993fd75f | 291 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 292 | } |
masahikofukasawa | 10:5c69b067d88a | 293 | out->setArgument(0,(char)mode); |
masahikofukasawa | 10:5c69b067d88a | 294 | out->setArgument(1,(char)filter); |
masahikofukasawa | 10:5c69b067d88a | 295 | break; |
masahikofukasawa | 10:5c69b067d88a | 296 | } |
masahikofukasawa | 10:5c69b067d88a | 297 | case Message::CMD_IR_SET_OPERATION_MODE: |
masahikofukasawa | 10:5c69b067d88a | 298 | { |
masahikofukasawa | 10:5c69b067d88a | 299 | mode = (AK9750::OperationMode)in->getArgument(0); |
masahikofukasawa | 10:5c69b067d88a | 300 | filter = (AK9750::DigitalFilter)in->getArgument(1); |
masahikofukasawa | 10:5c69b067d88a | 301 | if(ak9750->setOperationMode(mode, filter) != AK9750::SUCCESS) { |
masahikofukasawa | 10:5c69b067d88a | 302 | status = AkmSensor::ERROR; |
masahikofukasawa | 39:3821886c902e | 303 | MSG("#Error: setOperationMode. AK9750.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 304 | } |
masahikofukasawa | 10:5c69b067d88a | 305 | out->setArgument(0,(char)status); |
masahikofukasawa | 10:5c69b067d88a | 306 | break; |
masahikofukasawa | 10:5c69b067d88a | 307 | } |
masahikofukasawa | 10:5c69b067d88a | 308 | case Message::CMD_IR_GET_THRESHOLD_EEPROM: |
masahikofukasawa | 10:5c69b067d88a | 309 | { |
masahikofukasawa | 10:5c69b067d88a | 310 | if (ak9750->getThresholdFromEEPROM(&th) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 311 | MSG("#Error: Failed to get threshold of AK9750(EEPROM).\r\n"); |
masahikofukasawa | 15:1238993fd75f | 312 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 313 | } |
masahikofukasawa | 10:5c69b067d88a | 314 | out->setArgument(0,(char)((int32_t)(th.eth13h) >> 8)); |
masahikofukasawa | 10:5c69b067d88a | 315 | out->setArgument(1,(char)((int32_t)(th.eth13h) & 0x00FF)); |
masahikofukasawa | 10:5c69b067d88a | 316 | out->setArgument(2,(char)((int32_t)(th.eth13l) >> 8)); |
masahikofukasawa | 10:5c69b067d88a | 317 | out->setArgument(3,(char)((int32_t)(th.eth13l) & 0x00FF)); |
masahikofukasawa | 10:5c69b067d88a | 318 | out->setArgument(4,(char)((int32_t)(th.eth24h) >> 8)); |
masahikofukasawa | 10:5c69b067d88a | 319 | out->setArgument(5,(char)((int32_t)(th.eth24h) & 0x00FF)); |
masahikofukasawa | 10:5c69b067d88a | 320 | out->setArgument(6,(char)((int32_t)(th.eth24l) >> 8)); |
masahikofukasawa | 10:5c69b067d88a | 321 | out->setArgument(7,(char)((int32_t)(th.eth24l) & 0x00FF)); |
masahikofukasawa | 10:5c69b067d88a | 322 | break; |
masahikofukasawa | 10:5c69b067d88a | 323 | } |
masahikofukasawa | 10:5c69b067d88a | 324 | case Message::CMD_IR_SET_THRESHOLD_EEPROM: |
masahikofukasawa | 10:5c69b067d88a | 325 | { |
masahikofukasawa | 10:5c69b067d88a | 326 | th.eth13h = CONV16I(in->getArgument(0), in->getArgument(1)); |
masahikofukasawa | 10:5c69b067d88a | 327 | th.eth13l = CONV16I(in->getArgument(2), in->getArgument(3)); |
masahikofukasawa | 10:5c69b067d88a | 328 | th.eth24h = CONV16I(in->getArgument(4), in->getArgument(5)); |
masahikofukasawa | 10:5c69b067d88a | 329 | th.eth24l = CONV16I(in->getArgument(6), in->getArgument(7)); |
masahikofukasawa | 10:5c69b067d88a | 330 | if (ak9750->setThresholdToEEPROM(&th) != AK9750::SUCCESS) { |
masahikofukasawa | 10:5c69b067d88a | 331 | status = AkmSensor::ERROR; |
masahikofukasawa | 39:3821886c902e | 332 | MSG("#Error: Failed to set threshold to AK9750(EEPROM).\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 333 | } |
masahikofukasawa | 10:5c69b067d88a | 334 | out->setArgument(0,(char)status); |
masahikofukasawa | 10:5c69b067d88a | 335 | break; |
masahikofukasawa | 10:5c69b067d88a | 336 | } |
masahikofukasawa | 10:5c69b067d88a | 337 | case Message::CMD_IR_GET_HYSTERESIS_EEPROM: |
masahikofukasawa | 10:5c69b067d88a | 338 | { |
masahikofukasawa | 10:5c69b067d88a | 339 | if (ak9750->getHysteresisFromEEPROM(&hys) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 340 | MSG("#Error: Failed to get hysteresis of AK9750(EEPROM).\r\n"); |
masahikofukasawa | 15:1238993fd75f | 341 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 342 | } |
masahikofukasawa | 10:5c69b067d88a | 343 | out->setArgument(0,hys.ehys13); |
masahikofukasawa | 10:5c69b067d88a | 344 | out->setArgument(1,hys.ehys24); |
masahikofukasawa | 10:5c69b067d88a | 345 | break; |
masahikofukasawa | 10:5c69b067d88a | 346 | } |
masahikofukasawa | 10:5c69b067d88a | 347 | case Message::CMD_IR_SET_HYSTERESIS_EEPROM: |
masahikofukasawa | 10:5c69b067d88a | 348 | { |
masahikofukasawa | 10:5c69b067d88a | 349 | hys.ehys13 = in->getArgument(0); |
masahikofukasawa | 10:5c69b067d88a | 350 | hys.ehys24 = in->getArgument(1); |
masahikofukasawa | 10:5c69b067d88a | 351 | if (ak9750->setHysteresisToEEPROM(&hys) != AK9750::SUCCESS) { |
masahikofukasawa | 10:5c69b067d88a | 352 | status = AkmSensor::ERROR; |
masahikofukasawa | 39:3821886c902e | 353 | MSG("#Error: Failed to set hysteresis to AK9750(EEPROM).\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 354 | } |
masahikofukasawa | 10:5c69b067d88a | 355 | out->setArgument(0,(char)status); |
masahikofukasawa | 10:5c69b067d88a | 356 | break; |
masahikofukasawa | 10:5c69b067d88a | 357 | } |
masahikofukasawa | 10:5c69b067d88a | 358 | case Message::CMD_IR_GET_INTERRUPT_EEPROM: |
masahikofukasawa | 10:5c69b067d88a | 359 | { |
masahikofukasawa | 10:5c69b067d88a | 360 | if (ak9750->getInterruptEnableFromEEPROM(&interrupt) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 361 | MSG("#Error: Failed to get interrupt of AK9750(EEPROM).\r\n"); |
masahikofukasawa | 15:1238993fd75f | 362 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 363 | } |
masahikofukasawa | 10:5c69b067d88a | 364 | out->setArgument(0, interrupt.ir13h); |
masahikofukasawa | 10:5c69b067d88a | 365 | out->setArgument(1, interrupt.ir13l); |
masahikofukasawa | 10:5c69b067d88a | 366 | out->setArgument(2, interrupt.ir24h); |
masahikofukasawa | 10:5c69b067d88a | 367 | out->setArgument(3, interrupt.ir24l); |
masahikofukasawa | 10:5c69b067d88a | 368 | out->setArgument(4, interrupt.drdy); |
masahikofukasawa | 10:5c69b067d88a | 369 | break; |
masahikofukasawa | 10:5c69b067d88a | 370 | } |
masahikofukasawa | 10:5c69b067d88a | 371 | case Message::CMD_IR_SET_INTERRUPT_EEPROM: |
masahikofukasawa | 10:5c69b067d88a | 372 | { |
masahikofukasawa | 10:5c69b067d88a | 373 | interrupt.ir13h = in->getArgument(0); |
masahikofukasawa | 10:5c69b067d88a | 374 | interrupt.ir13l = in->getArgument(1); |
masahikofukasawa | 10:5c69b067d88a | 375 | interrupt.ir24h = in->getArgument(2); |
masahikofukasawa | 10:5c69b067d88a | 376 | interrupt.ir24l = in->getArgument(2); |
masahikofukasawa | 10:5c69b067d88a | 377 | interrupt.drdy = in->getArgument(4); |
masahikofukasawa | 10:5c69b067d88a | 378 | if (ak9750->setInterruptEnableToEEPROM(&interrupt) != AK9750::SUCCESS) { |
masahikofukasawa | 10:5c69b067d88a | 379 | status = AkmSensor::ERROR; |
masahikofukasawa | 39:3821886c902e | 380 | MSG("#Error: Failed to set interrupt to AK9750(EEPROM).\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 381 | } |
masahikofukasawa | 10:5c69b067d88a | 382 | out->setArgument(0,(char)status); |
masahikofukasawa | 10:5c69b067d88a | 383 | break; |
masahikofukasawa | 10:5c69b067d88a | 384 | } |
masahikofukasawa | 10:5c69b067d88a | 385 | case Message::CMD_IR_GET_OPERATION_MODE_EEPROM: |
masahikofukasawa | 10:5c69b067d88a | 386 | { |
masahikofukasawa | 10:5c69b067d88a | 387 | if(ak9750->getOperationModeFromEEPROM(&mode, &filter) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 388 | MSG("#Error: getOperationMode. AK9750(EEPROM).\r\n"); |
masahikofukasawa | 15:1238993fd75f | 389 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 390 | } |
masahikofukasawa | 10:5c69b067d88a | 391 | out->setArgument(0,(char)mode); |
masahikofukasawa | 10:5c69b067d88a | 392 | out->setArgument(1,(char)filter); |
masahikofukasawa | 10:5c69b067d88a | 393 | break; |
masahikofukasawa | 10:5c69b067d88a | 394 | } |
masahikofukasawa | 10:5c69b067d88a | 395 | case Message::CMD_IR_SET_OPERATION_MODE_EEPROM: |
masahikofukasawa | 10:5c69b067d88a | 396 | { |
masahikofukasawa | 10:5c69b067d88a | 397 | mode = (AK9750::OperationMode)in->getArgument(0); |
masahikofukasawa | 10:5c69b067d88a | 398 | filter = (AK9750::DigitalFilter)in->getArgument(1); |
masahikofukasawa | 10:5c69b067d88a | 399 | if(ak9750->setOperationModeToEEPROM(mode, filter) != AK9750::SUCCESS) { |
masahikofukasawa | 10:5c69b067d88a | 400 | status = AkmSensor::ERROR; |
masahikofukasawa | 39:3821886c902e | 401 | MSG("#Error: setOperationMode. AK9750(EEPROM).\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 402 | } |
masahikofukasawa | 10:5c69b067d88a | 403 | out->setArgument(0,(char)status); |
masahikofukasawa | 10:5c69b067d88a | 404 | break; |
masahikofukasawa | 10:5c69b067d88a | 405 | } |
masahikofukasawa | 10:5c69b067d88a | 406 | case Message::CMD_REG_WRITE: |
masahikofukasawa | 10:5c69b067d88a | 407 | case Message::CMD_REG_WRITEN: |
masahikofukasawa | 10:5c69b067d88a | 408 | { |
masahikofukasawa | 10:5c69b067d88a | 409 | char address = in->getArgument(0); |
masahikofukasawa | 46:5938ad2039b0 | 410 | const int len = (int)in->getArgument(1); |
masahikofukasawa | 15:1238993fd75f | 411 | if(in->getArgNum() != len+2){ |
masahikofukasawa | 39:3821886c902e | 412 | MSG("#Error: argument num. Args=%d\r\n",in->getArgNum()); |
masahikofukasawa | 15:1238993fd75f | 413 | status = AkmSensor::ERROR; |
masahikofukasawa | 15:1238993fd75f | 414 | out->setArgument(0,(char)status); |
masahikofukasawa | 15:1238993fd75f | 415 | return status; |
masahikofukasawa | 15:1238993fd75f | 416 | } |
masahikofukasawa | 15:1238993fd75f | 417 | |
masahikofukasawa | 10:5c69b067d88a | 418 | char data[len]; |
masahikofukasawa | 10:5c69b067d88a | 419 | for(int i=0; i<len; i++){ |
masahikofukasawa | 15:1238993fd75f | 420 | data[i] = in->getArgument(i+2); |
masahikofukasawa | 10:5c69b067d88a | 421 | } |
masahikofukasawa | 10:5c69b067d88a | 422 | if( ak9750->write(address, data, len) != AK9750::SUCCESS) { |
masahikofukasawa | 10:5c69b067d88a | 423 | status = AkmSensor::ERROR; |
masahikofukasawa | 39:3821886c902e | 424 | MSG("#Error: register write.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 425 | } |
masahikofukasawa | 10:5c69b067d88a | 426 | out->setArgument(0,(char)status); |
masahikofukasawa | 10:5c69b067d88a | 427 | break; |
masahikofukasawa | 10:5c69b067d88a | 428 | } |
masahikofukasawa | 10:5c69b067d88a | 429 | case Message::CMD_REG_READ: |
masahikofukasawa | 10:5c69b067d88a | 430 | case Message::CMD_REG_READN: |
masahikofukasawa | 10:5c69b067d88a | 431 | { |
masahikofukasawa | 15:1238993fd75f | 432 | if(in->getArgNum() != 2){ |
masahikofukasawa | 39:3821886c902e | 433 | MSG("#Error: argument num. Args=%d\r\n",in->getArgNum()); |
masahikofukasawa | 15:1238993fd75f | 434 | return AkmSensor::ERROR; |
masahikofukasawa | 15:1238993fd75f | 435 | } |
masahikofukasawa | 15:1238993fd75f | 436 | |
masahikofukasawa | 10:5c69b067d88a | 437 | char address = in->getArgument(0); |
masahikofukasawa | 46:5938ad2039b0 | 438 | const int len = (int)in->getArgument(1); |
masahikofukasawa | 10:5c69b067d88a | 439 | char data[len]; |
masahikofukasawa | 10:5c69b067d88a | 440 | if( ak9750->read(address, data, len) != AK9750::SUCCESS) { |
masahikofukasawa | 39:3821886c902e | 441 | MSG("#Error: register read.\r\n"); |
masahikofukasawa | 15:1238993fd75f | 442 | return AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 443 | } |
masahikofukasawa | 10:5c69b067d88a | 444 | for(int i=0; i<len; i++){ |
masahikofukasawa | 10:5c69b067d88a | 445 | out->setArgument(i, data[i]); |
masahikofukasawa | 10:5c69b067d88a | 446 | } |
masahikofukasawa | 10:5c69b067d88a | 447 | break; |
masahikofukasawa | 10:5c69b067d88a | 448 | } |
masahikofukasawa | 10:5c69b067d88a | 449 | default: |
masahikofukasawa | 10:5c69b067d88a | 450 | { |
masahikofukasawa | 39:3821886c902e | 451 | MSG("#Error: no command.\r\n"); |
masahikofukasawa | 10:5c69b067d88a | 452 | status = AkmSensor::ERROR; |
masahikofukasawa | 10:5c69b067d88a | 453 | break; |
masahikofukasawa | 10:5c69b067d88a | 454 | } |
masahikofukasawa | 10:5c69b067d88a | 455 | } |
masahikofukasawa | 10:5c69b067d88a | 456 | return status; |
masahikofukasawa | 10:5c69b067d88a | 457 | } |