399 lines
9.3 KiB
C++
399 lines
9.3 KiB
C++
#include "MGMHandlerLIS3MDL.h"
|
|
|
|
MGMHandlerLIS3MDL::MGMHandlerLIS3MDL(object_id_t objectId,
|
|
object_id_t deviceCommunication, CookieIF* comCookie):
|
|
DeviceHandlerBase(objectId, deviceCommunication, comCookie) {
|
|
registers[0] = 0x00;
|
|
registers[1] = 0x00;
|
|
registers[2] = 0x00;
|
|
registers[3] = 0x00;
|
|
registers[4] = 0x00;
|
|
|
|
}
|
|
|
|
MGMHandlerLIS3MDL::~MGMHandlerLIS3MDL() {
|
|
}
|
|
|
|
uint8_t MGMHandlerLIS3MDL::readCommand(uint8_t command, bool continuousCom) {
|
|
command |= (1 << RW_BIT);
|
|
if (continuousCom == true) {
|
|
command |= (1 << MS_BIT);
|
|
}
|
|
return command;
|
|
}
|
|
|
|
uint8_t MGMHandlerLIS3MDL::writeCommand(uint8_t command, bool continuousCom) {
|
|
command &= ~(1 << RW_BIT);
|
|
if (continuousCom == true) {
|
|
command |= (1 << MS_BIT);
|
|
}
|
|
return command;
|
|
}
|
|
|
|
ReturnValue_t MGMHandlerLIS3MDL::setupMGM() {
|
|
|
|
registers[0] = (1 << TEMP_EN) | (1 << OM1) | (1 << DO0) | (1 << DO1)
|
|
| (1 << DO2);
|
|
registers[1] = 0;
|
|
registers[2] = 0;
|
|
registers[3] = (1 << OMZ1);
|
|
registers[4] = 0;
|
|
|
|
return prepareRegisterWrite();
|
|
|
|
}
|
|
|
|
ReturnValue_t MGMHandlerLIS3MDL::buildNormalDeviceCommand(
|
|
DeviceCommandId_t *id) { //defines CommandID of MGM in normal operation and build command from command
|
|
*id = READALL_MGM;
|
|
|
|
return buildCommandFromCommand(*id, NULL, 0);
|
|
|
|
}
|
|
|
|
ReturnValue_t MGMHandlerLIS3MDL::buildCommandFromCommand(
|
|
DeviceCommandId_t deviceCommand, const uint8_t *commandData,
|
|
size_t commandDataLen) {
|
|
lastSentCommand = deviceCommand;
|
|
switch(deviceCommand) {
|
|
case(READALL_MGM): {
|
|
if (commandDataLen == 0) {
|
|
for (size_t i = 0; i < sizeof(commandBuffer); i++) {
|
|
commandBuffer[i] = 0;
|
|
}
|
|
commandBuffer[0] = readCommand(0, true);
|
|
|
|
rawPacket = commandBuffer;
|
|
rawPacketLen = sizeof(commandBuffer);
|
|
return RETURN_OK;
|
|
}
|
|
case(IDENTIFY_DEVICE): {
|
|
return identifyDevice();
|
|
}
|
|
case(TEMP_SENSOR_ENABLE): {
|
|
return enableTemperatureSensor(commandData, commandDataLen);
|
|
}
|
|
case(SETUP_MGM): {
|
|
return setupMGM();
|
|
}
|
|
case(ACCURACY_OP_MODE_SET): {
|
|
return setOperatingMode(commandData, commandDataLen);
|
|
}
|
|
default:
|
|
lastSentCommand = NO_COMMAND;
|
|
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
|
|
}
|
|
}
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
}
|
|
|
|
ReturnValue_t MGMHandlerLIS3MDL::buildTransitionDeviceCommand(
|
|
DeviceCommandId_t *id) {
|
|
switch (internalState) {
|
|
case STATE_FIRST_CONTACT:
|
|
*id = IDENTIFY_DEVICE;
|
|
break;
|
|
|
|
case STATE_SETUP:
|
|
*id = SETUP_MGM;
|
|
break;
|
|
|
|
case STATE_CHECK_REGISTERS:
|
|
*id = READALL_MGM;
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
return buildCommandFromCommand(*id, NULL, 0);
|
|
|
|
}
|
|
|
|
ReturnValue_t MGMHandlerLIS3MDL::scanForReply(const uint8_t *start,
|
|
uint32_t len, DeviceCommandId_t *foundId, uint32_t *foundLen) {
|
|
*foundLen = len;
|
|
if (len == TOTAL_NR_OF_ADRESSES + 1) {
|
|
*foundLen = len;
|
|
*foundId = READALL_MGM;
|
|
//WHO AM I test
|
|
if (*(start + 16) != DEVICEID) {
|
|
return DeviceHandlerIF::INVALID_DATA;
|
|
}
|
|
|
|
} else if (len == SETUP_REPLY) {
|
|
*foundLen = len;
|
|
*foundId = SETUP_MGM;
|
|
} else if (len == SINGLE_COMMAND_ANSWER_LEN) {
|
|
*foundLen = len;
|
|
*foundId = lastSentCommand;
|
|
} else {
|
|
|
|
return DeviceHandlerIF::INVALID_DATA;
|
|
}
|
|
|
|
if (start[0] == 0b11111111) { //Data with SPI Interface has always this answer
|
|
return RETURN_OK;
|
|
} else {
|
|
return DeviceHandlerIF::INVALID_DATA;
|
|
}
|
|
|
|
}
|
|
ReturnValue_t MGMHandlerLIS3MDL::interpretDeviceReply(DeviceCommandId_t id,
|
|
const uint8_t *packet) {
|
|
|
|
switch (id) {
|
|
case IDENTIFY_DEVICE: {
|
|
break;
|
|
}
|
|
case SETUP_MGM: {
|
|
break;
|
|
}
|
|
case READALL_MGM: {
|
|
//DataSet mySet;
|
|
|
|
registers[0] = *(packet + 33);
|
|
registers[1] = *(packet + 34);
|
|
registers[2] = *(packet + 35);
|
|
registers[3] = *(packet + 36);
|
|
registers[4] = *(packet + 37);
|
|
|
|
uint8_t reg2_value = *(packet + 34);
|
|
uint8_t scale = getFullScale(®2_value);
|
|
float sensitivity = getSensitivity(scale);
|
|
|
|
int16_t x_value_raw;
|
|
int16_t y_value_raw;
|
|
int16_t z_value_raw;
|
|
int16_t temp_value_raw;
|
|
//size_t size = 2;
|
|
uint8_t *accessBuffer;
|
|
accessBuffer = (uint8_t*) (packet + 41);
|
|
|
|
x_value_raw = *(accessBuffer + 1) << 8 | *(accessBuffer);
|
|
accessBuffer += 2;
|
|
y_value_raw = *(accessBuffer + 1) << 8 | *(accessBuffer);
|
|
accessBuffer += 2;
|
|
z_value_raw = *(accessBuffer + 1) << 8 | *(accessBuffer);
|
|
accessBuffer += 2;
|
|
|
|
temp_value_raw = *(accessBuffer + 1) << 8 | *(accessBuffer);
|
|
|
|
float x_value = (float) x_value_raw * sensitivity;
|
|
float y_value = (float) y_value_raw * sensitivity;
|
|
float z_value = (float) z_value_raw * sensitivity;
|
|
float temp_value = 25.0 + (((float) temp_value_raw) / 8.0);
|
|
|
|
// PoolVector<float, 3> mgm_measurement(mgmMagneticPoolId, &mySet,
|
|
// PoolVariableIF::VAR_WRITE);
|
|
// PoolVariable<float> mgm_temperature(mgmTempPoolId, &mySet,
|
|
// PoolVariableIF::VAR_WRITE);
|
|
|
|
// mgm_measurement[0] = x_value;
|
|
// mgm_measurement[1] = y_value;
|
|
// mgm_measurement[2] = z_value;
|
|
//
|
|
// mgm_temperature = temp_value;
|
|
// handleDeviceTM(&mySet, id);
|
|
|
|
break;
|
|
}
|
|
|
|
default: {
|
|
return DeviceHandlerIF::UNKNOW_DEVICE_REPLY;
|
|
}
|
|
|
|
}
|
|
return RETURN_OK;
|
|
}
|
|
|
|
uint8_t MGMHandlerLIS3MDL::getFullScale(uint8_t *reg2) {
|
|
bool FS0 = false;
|
|
bool FS1 = false;
|
|
if ((*reg2 >> 5) == 1)
|
|
FS0 = true;
|
|
if ((*reg2 >> 6) == 1)
|
|
FS1 = true;
|
|
if ((FS0 == true) && (FS1 == true))
|
|
return 16;
|
|
else if ((FS0 == false) && (FS1 == true))
|
|
return 12;
|
|
else if ((FS0 == true) && (FS1 == false))
|
|
return 8;
|
|
else
|
|
return 4;
|
|
|
|
}
|
|
|
|
float MGMHandlerLIS3MDL::getSensitivity(uint8_t scale) {
|
|
|
|
return (float) scale / (INT16_MAX);
|
|
|
|
}
|
|
ReturnValue_t MGMHandlerLIS3MDL::identifyDevice() {
|
|
uint32_t size = 2;
|
|
commandBuffer[0] = readCommand(IDENTIFYDEVICE);
|
|
commandBuffer[1] = 0x00;
|
|
|
|
rawPacket = commandBuffer;
|
|
rawPacketLen = size;
|
|
|
|
return RETURN_OK;
|
|
}
|
|
|
|
ReturnValue_t MGMHandlerLIS3MDL::enableTemperatureSensor(
|
|
const uint8_t *commandData, size_t commandDataLen) {
|
|
triggerEvent(CHANGE_OF_SETUP_PARAMETER);
|
|
uint32_t size = 2;
|
|
commandBuffer[0] = writeCommand(CTRL_REG1);
|
|
if (commandDataLen > 1) {
|
|
return INVALID_NUMBER_OR_LENGTH_OF_PARAMETERS;
|
|
}
|
|
switch (*commandData) {
|
|
case (ON):
|
|
commandBuffer[1] = registers[0] | (1 << 7);
|
|
break;
|
|
|
|
case (OFF):
|
|
commandBuffer[1] = registers[0] & ~(1 << 7);
|
|
break;
|
|
|
|
default:
|
|
return INVALID_COMMAND_PARAMETER;
|
|
break;
|
|
}
|
|
registers[0] = commandBuffer[1];
|
|
|
|
rawPacket = commandBuffer;
|
|
rawPacketLen = size;
|
|
|
|
return RETURN_OK;
|
|
}
|
|
|
|
void MGMHandlerLIS3MDL::doStartUp() {
|
|
switch (internalState) {
|
|
case STATE_NONE:
|
|
internalState = STATE_FIRST_CONTACT;
|
|
break;
|
|
|
|
case STATE_FIRST_CONTACT:
|
|
internalState = STATE_SETUP;
|
|
break;
|
|
|
|
case STATE_SETUP:
|
|
internalState = STATE_CHECK_REGISTERS;
|
|
break;
|
|
|
|
case STATE_CHECK_REGISTERS:
|
|
if (setupMGM() == RETURN_OK) {
|
|
for (size_t i = 1; i <= NR_OF_CTRL_REGISTERS; i++) {
|
|
if (registers[i - 1] != commandBuffer[i]) {
|
|
break;
|
|
}
|
|
}
|
|
setMode(_MODE_TO_ON);
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
void MGMHandlerLIS3MDL::doShutDown() {
|
|
setMode(_MODE_POWER_DOWN);
|
|
}
|
|
|
|
ReturnValue_t MGMHandlerLIS3MDL::setOperatingMode(const uint8_t *commandData,
|
|
size_t commandDataLen) {
|
|
triggerEvent(CHANGE_OF_SETUP_PARAMETER);
|
|
if (commandDataLen != 1) {
|
|
return INVALID_NUMBER_OR_LENGTH_OF_PARAMETERS;
|
|
}
|
|
|
|
switch (commandData[0]) {
|
|
case LOW:
|
|
registers[0] = (registers[0] & (~(1 << OM1))) & (~(1 << OM0));
|
|
registers[3] = (registers[3] & (~(1 << OMZ1))) & (~(1 << OMZ0));
|
|
break;
|
|
case MEDIUM:
|
|
registers[0] = (registers[0] & (~(1 << OM1))) | (1 << OM0);
|
|
registers[3] = (registers[3] & (~(1 << OMZ1))) | (1 << OMZ0);
|
|
break;
|
|
|
|
case HIGH:
|
|
registers[0] = (registers[0] | (1 << OM1)) & (~(1 << OM0));
|
|
registers[3] = (registers[3] | (1 << OM1)) & (~(1 << OM0));
|
|
break;
|
|
|
|
case ULTRA:
|
|
registers[0] = (registers[0] | (1 << OM1)) | (1 << OM0);
|
|
registers[3] = (registers[3] | (1 << OM1)) | (1 << OM0);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return prepareRegisterWrite();
|
|
}
|
|
|
|
void MGMHandlerLIS3MDL::fillCommandAndReplyMap() {
|
|
/*Actually SPI answers directly, but as commanding ArduinoBoard the communication could be delayed
|
|
* SPI always has to be triggered, so there could be no periodic answer of the device, the device has
|
|
* to asked with a command, so periodic is zero.
|
|
*
|
|
* We dont read single registers, we just expect special
|
|
* reply from he Readall_MGM
|
|
*/
|
|
|
|
insertInCommandAndReplyMap(READALL_MGM, 1, 0);
|
|
insertInCommandAndReplyMap(SETUP_MGM, 1, 0);
|
|
insertInCommandAndReplyMap(IDENTIFY_DEVICE, 1, 0);
|
|
insertInCommandAndReplyMap(TEMP_SENSOR_ENABLE, 1, 0);
|
|
insertInCommandAndReplyMap(ACCURACY_OP_MODE_SET, 1, 0);
|
|
}
|
|
|
|
ReturnValue_t MGMHandlerLIS3MDL::prepareRegisterWrite() {
|
|
|
|
commandBuffer[0] = writeCommand(CTRL_REG1, true);
|
|
|
|
for (size_t i = 1; i <= NR_OF_CTRL_REGISTERS; i++) {
|
|
commandBuffer[i] = registers[i];
|
|
}
|
|
rawPacket = commandBuffer;
|
|
rawPacketLen = NR_OF_CTRL_REGISTERS;
|
|
|
|
// We dont have to check if this is working because we just did it
|
|
return RETURN_OK;
|
|
}
|
|
|
|
void MGMHandlerLIS3MDL::setNormalDatapoolEntriesInvalid() {
|
|
// DataSet mySet;
|
|
// PoolVector<float, 3> mgmMeasurements(mgmMagneticPoolId, &mySet,
|
|
// PoolVariableIF::VAR_WRITE);
|
|
// PoolVariable<float> mgmTemperature(mgmTempPoolId, &mySet,
|
|
// PoolVariableIF::VAR_WRITE);
|
|
//
|
|
// mgmMeasurements[0] = 0;
|
|
// mgmMeasurements[1] = 0;
|
|
// mgmMeasurements[2] = 0;
|
|
//
|
|
// mgmTemperature = 999;
|
|
|
|
// mySet.commit(PoolVariableIF::INVALID);
|
|
|
|
}
|
|
|
|
void MGMHandlerLIS3MDL::doTransition(Mode_t modeFrom, Submode_t subModeFrom) {
|
|
|
|
}
|
|
|
|
uint32_t MGMHandlerLIS3MDL::getTransitionDelayMs(Mode_t from, Mode_t to) {
|
|
return 5000;
|
|
}
|
|
|
|
void MGMHandlerLIS3MDL::modeChanged(void) {
|
|
internalState = STATE_NONE;
|
|
}
|