Basic output of force sensor data to serial, in form "value1/n"
Dependencies: mbed BLE_API nRF51822
main.cpp
00001 #include "mbed.h" 00002 #include "BLEDevice.h" 00003 #include <string> 00004 #include "UARTService.h" 00005 00006 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; 00007 * it will have an impact on code-size and power consumption. */ 00008 Serial pc(USBTX, USBRX); 00009 00010 #if NEED_CONSOLE_OUTPUT 00011 #define DEBUG(...) { pc.printf(__VA_ARGS__); } 00012 #else 00013 #define DEBUG(...) /* nothing */ 00014 #endif /* #if NEED_CONSOLE_OUTPUT */ 00015 00016 BLEDevice ble; 00017 DigitalOut led1(LED1); 00018 DigitalOut led2(LED2); 00019 00020 AnalogIn strut1Force(P0_1); 00021 AnalogIn strut2Force(P0_2); 00022 AnalogIn strut3Force(P0_3); 00023 AnalogIn strut4Force(P0_4); 00024 AnalogIn seatForce(P0_5); 00025 AnalogIn cupBottomForce(P0_6); 00026 AnalogIn sensors[6] = {strut1Force, strut2Force, strut3Force, strut4Force, seatForce, cupBottomForce}; 00027 00028 static volatile bool triggerSensorPolling = false; 00029 00030 float sensorValues[6]; 00031 00032 char buffer[6]; 00033 00034 UARTService *uartServicePtr; 00035 00036 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00037 { 00038 DEBUG("Disconnected!\n\r"); 00039 DEBUG("Restarting the advertising process\n\r"); 00040 ble.startAdvertising(); 00041 } 00042 00043 00044 void onDataWritten(const GattCharacteristicWriteCBParams *params) 00045 { 00046 //doSomething 00047 /* 00048 if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { 00049 uint16_t bytesRead = params->len; 00050 DEBUG("received %u bytes\n\r", bytesRead); 00051 DEBUG("first byte received: %u\n\r", params->data[0]); 00052 if (params->data[0] == '1') { 00053 led2 = 1; 00054 powerSwitch = 1; 00055 } else if (params->data[0] == '0') { 00056 led2 = 0; 00057 powerSwitch = 0; 00058 } 00059 char buffer[4]; 00060 DEBUG("%1.2f,%1.2f,%1.2f,%1.2f,%1.2f,%1.2f\n", strut1Force.read(), strut2Force.read(), strut3Force.read(), strut4Force.read(), seatForce.read(), cupBottomForce.read()); 00061 // sprintf(buffer, "%1.2f,%1.2f,%1.2f,%1.2f\n", strut1Force.read(), strut2Force.read(), strut3Force.read(), strut4Force.read()); 00062 //ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t *)buffer, sizeof(&buffer)); 00063 for(int i=0; i <= 5; i++) 00064 { 00065 sprintf(buffer, "%1.2f\n", sensors[i].read()); 00066 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t *)buffer, sizeof(&buffer)); 00067 } 00068 } 00069 */ 00070 } 00071 00072 void periodicCallback(void) 00073 { 00074 led1 = !led1; 00075 triggerSensorPolling = true; 00076 } 00077 00078 int main(void) 00079 { 00080 00081 led1 = 1; 00082 led2 = 0; 00083 Ticker ticker; 00084 ticker.attach(periodicCallback, 1); 00085 00086 DEBUG("Initialising the nRF51822\n\r"); 00087 ble.init(); 00088 ble.onDisconnection(disconnectionCallback); 00089 ble.onDataWritten(onDataWritten); // called when someone writes to me 00090 00091 /* setup advertising */ 00092 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00093 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00094 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00095 (const uint8_t *)"pgao", sizeof("pgao") - 1); 00096 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00097 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); 00098 00099 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ 00100 ble.startAdvertising(); 00101 00102 UARTService uartService(ble); 00103 uartServicePtr = &uartService; 00104 00105 while (true) { 00106 if(triggerSensorPolling /*&& ble.getGapState().connected*/ && uartServicePtr != NULL) { 00107 triggerSensorPolling = false; 00108 00109 for(int i = 0; i < 6; i++) { 00110 sensorValues[i] = sensors[i].read()*100; 00111 if(sensorValues[i] == 100) sensorValues[i] = 99; 00112 00113 sprintf(buffer, "%d%2.0f", i,sensorValues[i]); 00114 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t *)buffer, sizeof(&buffer)); 00115 } 00116 DEBUG("trying to write values:\n%1.2f,%1.2f,%1.2f,%1.2f,%1.2f,%1.2f\n", strut1Force.read(), strut2Force.read(), strut3Force.read(), strut4Force.read(), seatForce.read(), cupBottomForce.read()); 00117 00118 // do something 00119 } 00120 ble.waitForEvent(); 00121 } 00122 } 00123 00124
Generated on Tue Jul 26 2022 07:50:46 by 1.7.2