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