Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
Diff: akmsensormanager.cpp
- Revision:
- 34:1ea3357c8d9a
- Parent:
- 30:5a241d9b3262
- Parent:
- 33:d3e1e9eb2ef9
- Child:
- 37:c76d2edf3426
--- a/akmsensormanager.cpp Fri Apr 28 20:32:31 2017 +0000 +++ b/akmsensormanager.cpp Wed May 03 18:00:45 2017 +0000 @@ -8,6 +8,7 @@ #include "ak9752ctrl.h" #include "ak7451ctrl.h" #include "ak7401ctrl.h" +#include "ap1017ctrl.h" #include "akmakd.h" #include "debug.h" #include "Message.h" @@ -193,7 +194,17 @@ } break; } - + case AkmSensor::AKM_PRIMARY_ID_MOTOR_DRIVER: + { // TODO: Other motor driver cases + if(subId == Ap1017Ctrl::SUB_ID_AP1017){ + Ap1017Ctrl* ap1017ctrl = new Ap1017Ctrl(); + sensor[0] = ap1017ctrl; + sensorNum = 1; + }else{ + return NULL; + } + break; + } default: { MSG("#Can't find ID=%d SubID=%d %s\r\n", primaryId, subId, AKM_PRIMARY_ID_STR[primaryId]); @@ -223,6 +234,8 @@ void AkmSensorManager::dummyCallbackForCommandReceived(){} + +// For commands received via BLE AkmSensorManager::Status AkmSensorManager::commandReceived(char* buf){ // Construct message Status status = SUCCESS; @@ -230,15 +243,16 @@ if ((Message::parse(&msg, buf)) != Message::SUCCESS) { MSG("#Failed to parse message. %s\r\n", buf); status = ERROR; - eventCommandReceived = false; + eventCommandReceived = false; // Reset flag }else{ - eventCommandReceived = true; + eventCommandReceived = true; // Set flag // MSG("#Parsed message. %s\r\n", buf); } t.attach(callback(this, &AkmSensorManager::dummyCallbackForCommandReceived),0); // wake-up from ble.waitForEvent return status; } + int16_t AkmSensorManager::getAdcData(MCP342X *mcp3428, MCP342X::AdcChannel ch, MCP342X::SampleSetting s) { const int WAIT_ADC_MS = 1; @@ -257,32 +271,39 @@ return data.value; } + uint8_t AkmSensorManager::getId(PinName pin, uint8_t bits) { MSG("#GetID\r\n"); - I2C i2c(I2C_SDA, I2C_SCL); + I2C i2c(I2C_SDA, I2C_SCL); // establish I2C to read ID + // ADC - MCP342X mcp342x(&i2c, MCP342X::SLAVE_ADDRESS_6EH); - mcp342x.setConversionMode(MCP342X::ONE_SHOT); + MCP342X mcp342x(&i2c, MCP342X::SLAVE_ADDRESS_6EH); // ADC to convert voltage + mcp342x.setConversionMode(MCP342X::ONE_SHOT); // Set to single sample MCP342X::AdcChannel ch; - if (pin == ANALOG_SENSOR_ID) { + + if (pin == ANALOG_SENSOR_ID) { // Primary ID ch = MCP342X::ADC_CH1; - } else { // pin == ANALOG_SENSOR_ID_SUB - ch = MCP342X::ADC_CH2; + } else { // pin == ANALOG_SENSOR_ID_SUB + ch = MCP342X::ADC_CH2; // Secondary ID } + int16_t val = getAdcData(&mcp342x, ch, MCP342X::SAMPLE_240HZ_12BIT); MSG("#12bit ADC Val = %d.\r\n", val); + // Voltage boundaries for ID voltage divider system const int16_t VAL_MAX = 3000-2048; // Corresponds to 3V const int16_t VAL_MIN = -2048; // Corresponds to 0V + // Convert voltage to ID value uint8_t value = (uint8_t)((val - VAL_MIN)/(float)(VAL_MAX - VAL_MIN) * (1 << bits) + 0.5); MSG("#ID = %d.\r\n", value); return value; } + bool AkmSensorManager::isEvent() { // check sensor related event @@ -301,13 +322,13 @@ void AkmSensorManager::processCommand() { - // Gets command in the message + // Extracts command contained in the message Message::Command cmd = msg.getCommand(); - // Creates an message object to return + // Creates a message object to return Message resMsg; - // Return message has the same command as input + // Return message contains the extracted command resMsg.setCommand(cmd); switch(cmd) @@ -435,6 +456,10 @@ } break; } + case Message::CMD_MOTOR_START_MOTOR: + case Message::CMD_MOTOR_STOP_MOTOR: + case Message::CMD_MOTOR_SET_DIRECTION: + case Message::CMD_MOTOR_SET_DUTY_CYCLE: case Message::CMD_PROGSW_GET_THRESHOLD: case Message::CMD_PROGSW_SET_THRESHOLD: case Message::CMD_PROGSW_GET_READ_COFIGURATION: @@ -488,13 +513,13 @@ AkmSensorManager::Status AkmSensorManager::processEvent() { AkmSensorManager::Status status = AkmSensorManager::SUCCESS; - - // command received from the host + + // If event is a command received from BLE if(eventCommandReceived) { // MSG("#Command received.\r\n"); processCommand(); - eventCommandReceived = false; + eventCommandReceived = false; // clear the flag } // check sensor event @@ -502,22 +527,24 @@ if( sensor[i]->isEvent() ){ Message msg; if( sensor[i]->readSensorData(&msg) != AkmSensor::SUCCESS) status = AkmSensorManager::ERROR; - throwMessage(&msg); + throwMessage(&msg); // Process and output message to USB/BLE } } - if(eventConnected) // BLE connected. Start sensor. + if(eventConnected) // If BLE connected, clear flag and start sensor. { - eventConnected = false; + eventConnected = false; // clear the flag MSG("#BLE connected.\r\n"); } - if(eventDisconnected) // BLE dis-connected. Stop sensor. + + // If event is the BLE being disconnected, stop the sensor + if(eventDisconnected) { MSG("#BLE dis-connected.\r\n"); for(int i=0; i<sensorNum; i++){ if( sensor[i]->stopSensor() != AkmSensor::SUCCESS) status = AkmSensorManager::ERROR; } - eventDisconnected = false; + eventDisconnected = false; // clear the flag } return status; } @@ -526,13 +553,13 @@ int len = Message::getMaxMessageLength(); char buf[len]; - buf[0] = '$'; + buf[0] = '$'; // Output message prefix - // Processes command + // Processes command in msg to ASCII char cmd = (char)msg->getCommand(); Message::charToAscii(&buf[1], &cmd); - // Processes arguments + // Processes arguments in msg to ASCII for (int i=0; i < msg->getArgNum(); i++) { char arg = msg->getArgument(i); Message::charToAscii(&buf[3+2*i], &arg); @@ -548,14 +575,16 @@ buf[tIdx++] = CR; // '\r' buf[tIdx++] = LF; // '\n' buf[tIdx] = '\0'; - if(isEnabledBle) uartService->writeString(buf); + + // If BLE is enabled, send to AKDP app + if(isEnabledBle) uartService->writeString(buf); + + // If USB is enabled, send to serial terminal if(isEnabledUsb) serial->printf(buf); return SUCCESS; } - - char* AkmSensorManager::my_strcat(char* str1, char* str2) { int num1;