Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822 Nordic_UART_TEMPLATE_ROHM
Dependents: Nordic_UART_TEMPLATE_ROHM
Fork of UART_TEMPLATE by
Revision 3:c3ee9d663fb8, committed 2015-07-27
- Comitter:
- kbahar3
- Date:
- Mon Jul 27 20:32:00 2015 +0000
- Parent:
- 2:c7b9d588c80f
- Child:
- 4:eabae2996ecc
- Commit message:
- committing latest revision... with sensor TODO indication
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Jul 22 01:05:56 2015 +0000
+++ b/main.cpp Mon Jul 27 20:32:00 2015 +0000
@@ -36,9 +36,18 @@
//#define AnalogALS //BH1620 //Change 0: Remove this completely
#define AnalogTemp //BDE0600
#define AnalogUV //ML8511
-#define HallSensor //BU52011 //Change 1: Change to use GPIO for BU52014
+#define HallSensor //BU52011 //Change 1: Change to use GPIO for BU52014
#define RPR0521 //RPR0521 //Change 2: Remove This and add in the RPR-0521
- //Change 3: Add Code For BH1745, KX022, BM1383GLV, KMX62
+#define KMX62 //Change 3: Add Code For BH1745, KX022, BM1383GLV, KMX62
+
+//Devices To Add
+// PRessure Sensor
+// Accel Only - KX122
+// Check Functions for KMX62
+// Color Sensor
+
+// Gyro last...
+
#include "mbed.h"
#include "BLEDevice.h"
@@ -47,7 +56,7 @@
#include "I2C.h"
#define MAX_REPLY_LEN (UARTService::BLE_UART_SERVICE_MAX_DATA_LEN) //Actually equal to 20
-#define SENSOR_READ_INTERVAL_S (2.0F)
+#define SENSOR_READ_INTERVAL_S (10.0F)
#define ADV_INTERVAL_MS (1000UL)
#define UART_BAUD_RATE (19200UL)
#define DEVICE_NAME ("DEMO SENSOR") // This can be read AFTER connecting to the device.
@@ -109,6 +118,30 @@
float RPR0521_ALS_OUT = 0;
#endif
+#ifdef KMX62
+int KMX62_addr_w = 0x1C; //7bit addr = 0x38, with write bit 0
+int KMX62_addr_r = 0x1D; //7bit addr = 0x38, with read bit 1
+
+char KMX62_CNTL2[2] = {0x3A, 0x5F};
+char KMX62_Addr_Accel_ReadData = 0x0A;
+char KMX62_Content_Accel_ReadData[6];
+char KMX62_Addr_Mag_ReadData = 0x10;
+char KMX62_Content_Mag_ReadData[6];
+
+int MEMS_Accel_Xout = 0;
+int MEMS_Accel_Yout = 0;
+int MEMS_Accel_Zout = 0;
+float MEMS_Accel_Conv_Xout = 0;
+float MEMS_Accel_Conv_Yout = 0;
+float MEMS_Accel_Conv_Zout = 0;
+int MEMS_Mag_Xout = 0;
+int MEMS_Mag_Yout = 0;
+int MEMS_Mag_Zout = 0;
+float MEMS_Mag_Conv_Xout = 0;
+float MEMS_Mag_Conv_Yout = 0;
+float MEMS_Mag_Conv_Zout = 0;
+#endif
+
/**
* This callback is used whenever a disconnection occurs.
*/
@@ -165,7 +198,7 @@
else
{
len = snprintf((char*) buf, MAX_REPLY_LEN, "ERROR");
- }
+ ` }
m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
@@ -213,7 +246,6 @@
len = snprintf((char*) buf, MAX_REPLY_LEN, "Temp = %.2f C", BDE0600_output);
m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
-
}
#endif
@@ -281,8 +313,76 @@
}
#endif
+#ifdef KMX62
if (m_ble.getGapState().connected) {
- len = snprintf((char*) buf, MAX_REPLY_LEN, " "); //Print and Extra Line to show new data
+ //Read Accel Portion from the IC
+ i2c.write(KMX62_addr_w, &KMX62_Addr_Accel_ReadData, 1, RepStart);
+ i2c.read(KMX62_addr_r, &KMX62_Content_Accel_ReadData[0], 6, NoRepStart);
+
+ //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte.
+ // However, because we need the signed value, we will adjust the value when converting to "g"
+ MEMS_Accel_Xout = (KMX62_Content_Accel_ReadData[1]<<8) | (KMX62_Content_Accel_ReadData[0]);
+ MEMS_Accel_Yout = (KMX62_Content_Accel_ReadData[3]<<8) | (KMX62_Content_Accel_ReadData[2]);
+ MEMS_Accel_Zout = (KMX62_Content_Accel_ReadData[5]<<8) | (KMX62_Content_Accel_ReadData[4]);
+
+ //Note: Conversion to G is as follows:
+ // Axis_ValueInG = MEMS_Accel_axis / 1024
+ // However, since we did not remove the LSB previously, we need to divide by 4 again
+ // Thus, we will divide the output by 4095 (1024*4) to convert and cancel out the LSB
+ MEMS_Accel_Conv_Xout = (float)MEMS_Accel_Xout/4096/2;
+ MEMS_Accel_Conv_Yout = (float)MEMS_Accel_Yout/4096/2;
+ MEMS_Accel_Conv_Zout = (float)MEMS_Accel_Zout/4096/2;
+
+ //Read MAg portion from the IC
+ i2c.write(KMX62_addr_w, &KMX62_Addr_Mag_ReadData, 1, RepStart);
+ i2c.read(KMX62_addr_r, &KMX62_Content_Mag_ReadData[0], 6, NoRepStart);
+
+ //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte.
+ // However, because we need the signed value, we will adjust the value when converting to "g"
+ MEMS_Mag_Xout = (KMX62_Content_Mag_ReadData[1]<<8) | (KMX62_Content_Mag_ReadData[0]);
+ MEMS_Mag_Yout = (KMX62_Content_Mag_ReadData[3]<<8) | (KMX62_Content_Mag_ReadData[2]);
+ MEMS_Mag_Zout = (KMX62_Content_Mag_ReadData[5]<<8) | (KMX62_Content_Mag_ReadData[4]);
+
+ //Note: Conversion to G is as follows:
+ // Axis_ValueInG = MEMS_Accel_axis / 1024
+ // However, since we did not remove the LSB previously, we need to divide by 4 again
+ // Thus, we will divide the output by 4095 (1024*4) to convert and cancel out the LSB
+ MEMS_Mag_Conv_Xout = (float)MEMS_Mag_Xout*0.146;
+ MEMS_Mag_Conv_Yout = (float)MEMS_Mag_Yout*0.146;
+ MEMS_Mag_Conv_Zout = (float)MEMS_Mag_Zout*0.146;
+
+ len = snprintf((char*) buf, MAX_REPLY_LEN, "KMX61SensorData:");
+ m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
+ wait_ms(1000);
+
+ len = snprintf((char*) buf, MAX_REPLY_LEN, " AccX= %0.2f g", MEMS_Accel_Conv_Xout);
+ m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
+ wait_ms(1000);
+
+ len = snprintf((char*) buf, MAX_REPLY_LEN, " AccY= %0.2f g", MEMS_Accel_Conv_Yout);
+ m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
+ wait_ms(1000);
+
+ len = snprintf((char*) buf, MAX_REPLY_LEN, " AccZ= %0.2f g", MEMS_Accel_Conv_Zout);
+ m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
+ wait_ms(1000);
+
+ len = snprintf((char*) buf, MAX_REPLY_LEN, " MagX= %0.2f g", MEMS_Mag_Conv_Xout);
+ m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
+ wait_ms(1000);
+
+ len = snprintf((char*) buf, MAX_REPLY_LEN, " MagY= %0.2f g", MEMS_Mag_Conv_Yout);
+ m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
+ wait_ms(1000);
+
+ len = snprintf((char*) buf, MAX_REPLY_LEN, " MagZ= %0.2f g", MEMS_Mag_Conv_Zout);
+ m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
+ wait_ms(1000);
+ }
+#endif
+
+ if (m_ble.getGapState().connected) {
+ len = snprintf((char*) buf, MAX_REPLY_LEN, " "); //Print and Extra Line to show new data
m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
}
}
@@ -339,6 +439,11 @@
i2c.write(RPR0521_addr_w, &RPR0521_Persist[0], 2, false);
#endif
+#ifdef KMX62
+ // 1. CNTL2 (0x3A), write (0x5F): 4g, Max RES, EN temp mag and accel
+ i2c.write(KMX62_addr_w, &KMX62_CNTL2[0], 2, false);
+#endif
+
//Start BTLE Initialization Section
m_ble.init();
m_ble.onDisconnection(disconnectionCallback);
