CAC_smartcushion / Mbed OS ADISense1000_Smartcushion_v2_ble_standard_stream

Fork of ADISense1000_Smartcushion_v1_ble_reply_over_usb2ser by CAC_smartcushion

Files at this revision

API Documentation at this revision

Comitter:
nfathurr
Date:
Mon Sep 10 09:59:13 2018 +0000
Parent:
1:25a2bf666957
Commit message:
Latest version Smartcushion BLE ADISENSE with firmware 1.0

Changed in this revision

ADISense1000_MBED.c Show annotated file Show diff for this revision Revisions of this file
inc/ble_interface/bl652.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
src/ble_interface/bl652.cpp Show annotated file Show diff for this revision Revisions of this file
src/ble_interface/ble_interface.cpp Show annotated file Show diff for this revision Revisions of this file
src/myproswift_periph.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/ADISense1000_MBED.c	Fri Aug 24 08:58:48 2018 +0000
+++ b/ADISense1000_MBED.c	Mon Sep 10 09:59:13 2018 +0000
@@ -113,33 +113,6 @@
                         .enableVbias = true,
                     },
                 },
-                [ADI_SENSE_1000_CHANNEL_ID_VOLTAGE_0] = {
-                    .enableChannel = true,
-                    .disablePublishing = false,
-                    .compensationChannel = ADI_SENSE_1000_CHANNEL_ID_NONE,
-                    .measurementsPerCycle = 1,
-                    .extraSettlingTime = 0.0000,
-                    .measurementUnit = ADI_SENSE_1000_MEASUREMENT_UNIT_DEFAULT,
-                    .adcChannelConfig = {
-                        .sensor = ADI_SENSE_1000_ADC_SENSOR_VOLTAGE_PRESSURE_AMPHENOL_NPA300X,
-                        .gain = ADI_SENSE_1000_ADC_GAIN_2X,
-                        .current = {
-                            .outputLevel = ADI_SENSE_1000_ADC_EXC_CURRENT_NONE,
-                            .swapOption = ADI_SENSE_1000_ADC_EXC_CURRENT_SWAP_NONE,
-                        },
-                        .filter = {
-                            .type = ADI_SENSE_1000_ADC_FILTER_FIR_25SPS,
-                            .fs = 0,
-                            //.type = ADI_SENSE_1000_ADC_FILTER_SINC4,
-                            //.fs = 1920,
-                        },
-                        .reference = {
-                            .type = ADI_SENSE_1000_ADC_REFERENCE_VOLTAGE_INTERNAL,
-                            .disableBuffer = false,
-                        },
-                        .enableVbias = false,
-                    },
-                },
                 [ADI_SENSE_1000_CHANNEL_ID_I2C_0] = {
                     .enableChannel = true,
                     .disablePublishing = false,
@@ -171,21 +144,27 @@
                   },
                 },
                 [ADI_SENSE_1000_CHANNEL_ID_SPI_1] = {
-                /* Accelerometer Y-Axis (virtual channel) */
-                .enableChannel = true,
-                .offsetAdjustment = -0.15,
-                /* Optional threshold limits applied to the processed measurement results */
-                .lowThreshold = -2.0,
-                .highThreshold = 2.0,
+                    /* Accelerometer Y-Axis (virtual channel) */
+                    .enableChannel = true,
+                    .offsetAdjustment = -0.15,
+                    /* Optional threshold limits applied to the processed measurement results */
+                    .lowThreshold = -2.0,
+                    .highThreshold = 2.0,
+                    .spiChannelConfig = {
+                        .sensor = ADI_SENSE_1000_SPI_SENSOR_ACCELEROMETER_ADI_ADXL362,
+                    },
                 },
                 [ADI_SENSE_1000_CHANNEL_ID_SPI_2] = {
-                /* Accelerometer Z-Axis (virtual channel) */
-                .enableChannel = true,
-                /* Optional adjustment applied to the PROCESSED OUTPUT value from the sensor */
-                .offsetAdjustment = -0.4,
-                /* Optional threshold limits applied to the processed measurement results */
-                .lowThreshold = -2,
-                .highThreshold = 2,
+                    /* Accelerometer Z-Axis (virtual channel) */
+                    .enableChannel = true,
+                    /* Optional adjustment applied to the PROCESSED OUTPUT value from the sensor */
+                    .offsetAdjustment = -0.4,
+                    /* Optional threshold limits applied to the processed measurement results */
+                    .lowThreshold = -2,
+                    .highThreshold = 2,
+                    .spiChannelConfig = {
+                        .sensor = ADI_SENSE_1000_SPI_SENSOR_ACCELEROMETER_ADI_ADXL362,
+                    },
                 },
             },
     },
--- a/inc/ble_interface/bl652.h	Fri Aug 24 08:58:48 2018 +0000
+++ b/inc/ble_interface/bl652.h	Mon Sep 10 09:59:13 2018 +0000
@@ -47,4 +47,6 @@
 //wait until BLE is awake
 uint32_t Bl652_WaitUntilAvailable( void );
 
+uint32_t Bl652_Printf(const char *format, ...);
+
 #endif
--- a/main.cpp	Fri Aug 24 08:58:48 2018 +0000
+++ b/main.cpp	Mon Sep 10 09:59:13 2018 +0000
@@ -22,6 +22,13 @@
 #include "ble_interface/bl652.h"
 #endif
 
+/** BLE COMMAND **
+    REQ_REG_PACKET_COUNT    = 0x21, //'!'
+    REQ_REG_PACKETS         = 0x40, //'@'
+    REQ_FIELD_NAMES         = 0x23, //'#'
+    START_STREAM            = 0x24, //'$'
+    STOP_STREAM             = 0x25, //'%'
+**/
 
 int main( void )
 {
@@ -66,7 +73,13 @@
     // Check adisense has booted correctly
     if( adiSense1000Active ) {
         Pc_ResetSuccess( bBootLoader );
-        ADI_SENSE_LOG_INFO("ADIsense boot successful...");
+        ADI_SENSE_LOG_INFO("ADIsense boot successful 03/09/2018");
+        ADI_SENSE_LOG_INFO("Available BLE COMMAND");
+        ADI_SENSE_LOG_INFO("REQ_REG_PACKET_COUNT = 0x21, //'!'");
+        ADI_SENSE_LOG_INFO("REQ_REG_PACKETS      = 0x40, //'@'");
+        ADI_SENSE_LOG_INFO("REQ_FIELD_NAMES      = 0x23, //'#'");
+        ADI_SENSE_LOG_INFO("START_STREAM         = 0x24, //'$'");
+        ADI_SENSE_LOG_INFO("STOP_STREAM          = 0x25, //'%'");
     }
     else {
         // Cannot continue without adi_sense module
--- a/src/ble_interface/bl652.cpp	Fri Aug 24 08:58:48 2018 +0000
+++ b/src/ble_interface/bl652.cpp	Mon Sep 10 09:59:13 2018 +0000
@@ -5,6 +5,7 @@
  */
 
 #include "bl652.h"
+#include <SerialBase.h>
 
 #include "mbed.h"
 #include "myproswift_periph.h"
@@ -78,6 +79,9 @@
 	wait_ms( RESET_LENGTH );
 	ble_rst = 1;
 	
+	//set_flow_control(TYPE=RTSCTS, RTS pin, CTS pin)
+	//bleSerialDevice.set_flow_control(Serial::RTSCTS, BLE_CTS_PIN, BLE_RTS_PIN );
+	
 	//start sending timer for minimum packet delay
 	timer.start();
 	
@@ -232,3 +236,21 @@
     return Uart_ClearCb( bleSerialDevice );
 }
 
+uint32_t Bl652_Printf(const char *format, ...)
+{
+	uint32_t ret = 0;
+	
+	char buffer[256];
+    va_list va_args;
+
+    if (!bleSerialDevice){
+        return 1;
+	}
+    va_start(va_args, format);
+    vsnprintf(buffer, sizeof(buffer), format, va_args);
+    va_end(va_args);
+
+    bleSerialDevice.printf("%s\r\n", buffer);
+    
+    return ret;
+}
--- a/src/ble_interface/ble_interface.cpp	Fri Aug 24 08:58:48 2018 +0000
+++ b/src/ble_interface/ble_interface.cpp	Mon Sep 10 09:59:13 2018 +0000
@@ -39,11 +39,11 @@
 
 //command ID to be handled
 enum {
-	REQ_REG_PACKET_COUNT 	= 0x30, //'0'
-	REQ_REG_PACKETS 		= 0x31, //'1'
-	REQ_FIELD_NAMES 		= 0x32, //'2'
-	START_STREAM 		= 0x33, //'3'
-	STOP_STREAM 		= 0x34, //'4'
+	REQ_REG_PACKET_COUNT 	= 0x21, //'!'
+	REQ_REG_PACKETS 		= 0x40, //'@'
+	REQ_FIELD_NAMES 		= 0x23, //'#'
+	START_STREAM 			= 0x24, //'$'
+	STOP_STREAM 			= 0x25, //'%'
 } MSG_TYPE;
 
 /*
@@ -58,9 +58,10 @@
 //possible measurement types
 enum {
 	TEMPERATURE = 0x00,
+	WEIGHT,
 	PRESSURE,
-	HUMIDITY,
-	ACCELEROMETER,
+	HUMIDITY = 3,
+	ACCELEROMETER = 4,
 	UNDEFINED
 } MEASUREMENT_TYPES;
 
@@ -132,6 +133,11 @@
 				channel_info.measurementName = "Temperature";
 			}
 			
+			else if( channel.spiChannelConfig.sensor == ADI_SENSE_1000_SPI_SENSOR_ACCELEROMETER_ADI_ADXL362  ) {
+				channel_info.measurementTypeId = ACCELEROMETER;
+				channel_info.measurementName = "Accelerometer";
+			}
+			
 			else if( channel.adcChannelConfig.sensor >= ADI_SENSE_1000_I2C_SENSOR_HUMIDITY_HONEYWELL_HUMIDICON && channel.adcChannelConfig.sensor <= ADI_SENSE_1000_I2C_SENSOR_HUMIDITY_SENSIRION_SHT3X ) {
 				channel_info.measurementTypeId = HUMIDITY;
 				channel_info.measurementName = "Humidity";
@@ -171,7 +177,7 @@
 	buffPos++;
 	
 	//amount of data samples
-	bleTxBuffer[buffPos] = activeChannelCount;
+	bleTxBuffer[buffPos] = (48 + activeChannelCount); //adding 48 decimal to convert to ascii
 	buffPos++;
 	
 	//respond to app
@@ -207,11 +213,11 @@
 		buffPos++;
 		
 		//add regPackets ID
-		bleTxBuffer[buffPos] = measurementInfo[i].measurementId;
+		bleTxBuffer[buffPos] = (48 + measurementInfo[i].measurementId);
 		buffPos++;
 		
 		//add type of measurement
-		bleTxBuffer[buffPos] = measurementInfo[i].measurementTypeId;
+		bleTxBuffer[buffPos] = (48 + measurementInfo[i].measurementTypeId);
 		buffPos++;
 		
 		//adding measurements name to packet
@@ -229,10 +235,10 @@
 		}
 		
 		//add number of plots on graph. ( 1 for now )
-		bleTxBuffer[buffPos] = measurementInfo[i].numberOfFields;
+		bleTxBuffer[buffPos] = (48 + measurementInfo[i].numberOfFields);
 		buffPos++;
 		//add data format ( always float32_t for now )
-		bleTxBuffer[buffPos] = measurementInfo[i].dataType;
+		bleTxBuffer[buffPos] = (48 + measurementInfo[i].dataType);
 		buffPos++;
 	
 		//transmit packet
@@ -277,7 +283,7 @@
 	switch( measurementInfo[bleRxBuffer[1]].measurementTypeId ) {
 		
 	  case TEMPERATURE:
-		strcpy( unitDescriptor, "�C" );
+		strcpy( unitDescriptor, "°C" );
 		break;
 		
 	  case PRESSURE:
@@ -541,14 +547,14 @@
 				buffPos++;
 				
 				//Tag with ID of measurement
-				bleTxBuffer[buffPos] = measurementInfo[activeChannelNumber].measurementId;
+				bleTxBuffer[buffPos] = (48 + measurementInfo[activeChannelNumber].measurementId);
 				buffPos++;
 					
 				//transmute sample data into bytes for transmission
 				for( uint32_t j = 0; j < sizeof( measurementInfo[activeChannelNumber].dataType ); j++ ) {
 					
 					//store bytes to buffer
-					bleTxBuffer[buffPos] = sample[j];
+					bleTxBuffer[buffPos] = (48 + sample[j]);
 					buffPos++;
 				}
 				
@@ -586,6 +592,199 @@
 }
 
 /***************************************************************
+* Function Name: Ble_HandleStartStream
+* Description  : handle the START_STREAM command
+****************************************************************/
+static uint32_t Ble_HandleStartStream_v2( void )
+{
+	//buffer for sending BLE data
+	char bleTxBuffer[BLE_PACKET_SIZE] = {0x00};
+	//stores poition in bleTxBuffer
+	uint8_t buffPos = 0;
+
+	//part of the adisense1000 sampling
+	ADI_SENSE_DATA_SAMPLE *pSampleBuffer;
+	ADI_SENSE_1000_OPERATING_MODE eOperatingMode;
+	ADI_SENSE_1000_DATAREADY_MODE eDataPublishMode;
+	ADI_SENSE_MEASUREMENT_MODE eMeasurementMode = ADI_SENSE_MEASUREMENT_MODE_NORMAL;
+	uint32_t nSamplesPerDataready, nSamplesPerCycle;
+	volatile bool_t bDataReady = false;
+	
+	//if ReqRegPacketCount has not been called, or had an error
+	if( activeChannelCount == 0 ) {
+		return Ble_HandleError( ERROR_UNDEFINED );
+	}
+	
+	//upload new configuration and apply. 
+	//This has been used to calculate all previous steps, so ensure that this is the active configuration
+	adi_sense_SetConfig( hDevice, &adi_sense_config );
+	adi_sense_ApplyConfigUpdates( hDevice );
+	
+	//get information about the adisense data ready mode
+	adi_sense_1000_GetDataReadyModeInfo(hDevice,
+							eMeasurementMode,
+							&eOperatingMode,
+							&eDataPublishMode,
+							&nSamplesPerDataready,
+							&nSamplesPerCycle );
+	//allocate a buffer to store the samples retreived on each DATAREADY pulse
+	pSampleBuffer = new ADI_SENSE_DATA_SAMPLE[nSamplesPerDataready];
+	if( pSampleBuffer == NULL ) {
+		return 1;
+	}
+	
+	//interrupt-triggered DATAREADY notifications in this mode
+	adi_sense_RegisterGpioCallback( hDevice, ADI_SENSE_GPIO_PIN_DATAREADY,
+						ble_data_ready_callback,
+						( void * )&bDataReady );
+	
+	//allow ble to send data for stream cancel command
+	ble_Cts = 0;
+	
+	//handle the single cycle device mode
+	if ( eOperatingMode == ADI_SENSE_1000_OPERATING_MODE_SINGLECYCLE ) {
+		
+		//loop single cycle until stream stop command is received
+		do {
+			//begin measurement cycles
+			adi_sense_StartMeasurement( hDevice, eMeasurementMode );
+			
+			//read in each measurement cycle
+			for ( uint32_t i = 0; i < nSamplesPerCycle / nSamplesPerDataready; i++ ) {
+				uint32_t nReturned;
+				
+				//Interrupt method: wait for DATAREADY interrupt callback
+				while ( !bDataReady );
+				
+				//reset bDataReady flag to detect the next callback
+				bDataReady = false;
+				
+				//read data into structure
+				adi_sense_GetData( hDevice, eMeasurementMode, pSampleBuffer, nSamplesPerDataready, &nReturned );
+				
+				//for all returned samples
+				for( uint32_t i = 0; i < nReturned; i++ ) {
+					
+					//to store adisense1000 channel index 
+					//and the channel number according to the BLE device
+					uint8_t channelIndex, activeChannelNumber;
+					
+					//obtain sample data in byte array form
+					uint8_t *sample = ( uint8_t* )&pSampleBuffer[i].processedValue;
+					
+					//match sample ID to active channel
+					for( uint32_t j = 0; j < activeChannelCount; j++ ) {
+						
+						//get the ID of the current active channel
+						channelIndex = measurementInfo[j].associatedChannel;
+						
+						//if the sample matches the current active channel
+						if( pSampleBuffer[i].channelId == channelIndex ) {
+							activeChannelNumber = j;
+							break;
+						}
+					}
+					
+					//clear buffPos
+					buffPos = 0;
+					
+					//tag samples with command value
+					bleTxBuffer[buffPos] = START_STREAM;
+					buffPos++;
+					
+					//Tag with ID of measurement
+					bleTxBuffer[buffPos] = measurementInfo[activeChannelNumber].measurementId;
+					buffPos++;
+						
+					//transmute sample data into bytes for transmission
+					for( uint32_t j = 0; j < sizeof( measurementInfo[activeChannelNumber].dataType ); j++ ) {
+						
+						//store bytes to buffer
+						bleTxBuffer[buffPos] = sample[j];
+						buffPos++;
+					}
+					
+					//stream measurement
+					Bl652_Write( bleTxBuffer, buffPos );
+				}
+			}
+			
+			if( bleSerialDevice.readable() ) {
+				ble_Cts = 1;
+				if( bleSerialDevice.getc() == STOP_STREAM ) {
+					//stop stream measurement
+					bleTxBuffer[0] = STOP_STREAM;
+					Bl652_Write( bleTxBuffer, 1 );
+					break;
+				}
+				else {
+					return Ble_HandleError( ERROR_UNDEFINED );
+				}
+			}			
+		} while ( 1 );
+	}
+
+	//handle continuous measurement mode
+	else {
+
+		//begin measurement cycles
+		adi_sense_StartMeasurement( hDevice, eMeasurementMode );
+	
+		do {
+			uint32_t nReturned;
+		
+			//Interrupt method: wait for DATAREADY interrupt callback
+			while ( !bDataReady );
+		
+			//reset bDataReady flag to detect the next callback
+			bDataReady = false;
+		
+			//read data into structure
+			adi_sense_GetData( hDevice, eMeasurementMode, pSampleBuffer, nSamplesPerDataready, &nReturned );
+		
+			//for all returned samples //TODO: Might need modified. Test with App
+			for( uint32_t i = 0; i < nReturned; i++ ) {
+				
+				Bl652_Printf("Sample # %2d Channel # %2d :: Raw %8d  :: Processed %.7f :: flags: %s %s",
+                           i+1,
+                           pSampleBuffer[i].channelId,
+                           pSampleBuffer[i].rawValue,
+                           pSampleBuffer[i].processedValue,
+                           pSampleBuffer[i].status & ADI_SENSE_DEVICE_STATUS_ERROR ? "ERROR" : "",
+                           pSampleBuffer[i].status & ADI_SENSE_DEVICE_STATUS_ALERT ? "ALERT" : "");
+				
+			}
+		
+			if( bleSerialDevice.readable() ) {
+				
+				//signal ble to stop sending bytes
+				ble_Cts = 1;
+				
+				//read byte buffer and determine if 
+				//the STOP_STREAM byte has been received
+				if( bleSerialDevice.getc() == STOP_STREAM ) {
+					//stop stream measurement
+					bleTxBuffer[0] = STOP_STREAM;
+					Bl652_Write( bleTxBuffer, 1 );
+					break;
+				}
+				else {
+					return Ble_HandleError( ERROR_UNDEFINED );
+				}
+			}
+		} while ( 1 );
+	
+		//stop measurement
+		adi_sense_StopMeasurement( hDevice );
+	}
+
+	//free buffer
+	delete [] pSampleBuffer;
+
+	return 0;
+}
+
+/***************************************************************
 * Function Name: Ble_HandleStopStream
 * Description  : handle the STOP_STREAM command
 ****************************************************************/
@@ -638,6 +837,7 @@
 		ret = Ble_HandleReqRegPacketCount();
 		char bleSend1[] = "PacketCount\r\n";
 		Bl652_Write(bleSend1 , 15);
+		ADI_SENSE_LOG_INFO("Result: ReqPacketCount");
 		break;
 		}
 	  case REQ_REG_PACKETS: {//app is requesting for all reg packets
@@ -645,6 +845,7 @@
 		ret = Ble_HandleReqRegPackets();
 		char bleSend2[] = "ReqRegister\r\n";
 		Bl652_Write(bleSend2 , 15);
+		ADI_SENSE_LOG_INFO("Result: ReqRegister");
 		break;
 		}
 	  case REQ_FIELD_NAMES: {//app is requesting field names
@@ -652,18 +853,23 @@
 		ret = Ble_HandleReqFieldNames( bleRxBuffer );
 		char bleSend3[] = "FieldNames\r\n";
 		Bl652_Write(bleSend3 , 15);
+		ADI_SENSE_LOG_INFO("Result: ReqFieldNames");
 		break;
 		}
 	  case START_STREAM: {//app is requesting the start of the data stream
 		//call handler function
-		ret = Ble_HandleStartStream();
+		ret = Ble_HandleStartStream_v2();
 		char bleSend4[] = "StartStream\r\n";
 		Bl652_Write(bleSend4 , 15);
+		ADI_SENSE_LOG_INFO("Result: StartStream");
 		break;
 		}
 	  case STOP_STREAM: {//app is requesting to stop the data stream
 		//call handler function
 		ret = Ble_HandleStopStream();
+		char bleSend5[] = "StopStream\r\n";
+		Bl652_Write(bleSend5 , 15);
+		ADI_SENSE_LOG_INFO("Result: StopStream");
 		break;
 		}
 		//Template
@@ -674,6 +880,9 @@
 		//handle an unrecognised command/error
 	  default: {//command not recognised
 		ret = Ble_HandleError( ERROR_UNDEFINED );
+		char bleSend6[] = "CmdUndifined\r\n";
+		Bl652_Write(bleSend6 , 15);
+		ADI_SENSE_LOG_INFO("Command not recognized");
 		break;
 		}
 	}
--- a/src/myproswift_periph.cpp	Fri Aug 24 08:58:48 2018 +0000
+++ b/src/myproswift_periph.cpp	Mon Sep 10 09:59:13 2018 +0000
@@ -36,10 +36,14 @@
  */
 //reset pin of BLE module
 DigitalOut ble_rst(BLE_RST_PIN, 0);
+
 //flow control
 DigitalOut 	ble_Cts(BLE_CTS_PIN, 1);
 DigitalIn  	ble_Rts(BLE_RTS_PIN);
 
+//DigitalOut 	ble_Cts(BLE_CTS_PIN, 0);
+//DigitalIn  	ble_Rts(BLE_RTS_PIN, PullDown);
+
 Serial bleSerialDevice(BLE_SERIAL_TX, BLE_SERIAL_RX, BLE_BAUD_RATE);
 
 #ifdef BL652