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.
Fork of LinkNode_LIS3DH by
main.cpp
00001 #include<mbed.h> 00002 #include<string.h> 00003 #include<math.h> 00004 #include<stdlib.h> 00005 #include "mbed.h" 00006 #include "ble/BLE.h" 00007 #include "Servo.h" 00008 #include "GattCallbackParamTypes.h" 00009 00010 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ 00011 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ 00012 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ 00013 00014 #define TXRX_BUF_LEN 20 00015 00016 #define DIGITAL_OUT_PIN P0_20 //D7 00017 #define DIGITAL_IN_PIN P0_29 //A4 00018 #define PWM_PIN P0_19 //D6 00019 #define SERVO_PIN P0_22 //D10 00020 #define ANALOG_IN_PIN P0_1 //A5 00021 00022 BLE ble; 00023 DigitalOut LED(DIGITAL_OUT_PIN); 00024 DigitalIn BUTTON(DIGITAL_IN_PIN); 00025 //PwmOut PWM(PWM_PIN); 00026 AnalogIn ANALOG(ANALOG_IN_PIN); 00027 Servo MYSERVO(SERVO_PIN); 00028 00029 SPI spi_master(P0_6,P0_5,P0_7); //mosi miso sclk 00030 DigitalOut cs(P0_4); 00031 00032 Serial pc(P0_23,P0_25); 00033 00034 static uint8_t analog_enabled = 0; 00035 static uint8_t old_state = 0; 00036 00037 uint16_t x_a,y_a,z_a; 00038 bool flag = 0; 00039 00040 00041 // The Nordic UART Service 00042 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00043 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00044 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00045 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; 00046 00047 00048 uint8_t txPayload[TXRX_BUF_LEN] = {0,}; 00049 uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; 00050 00051 //static uint8_t rx_buf[TXRX_BUF_LEN]; 00052 //static uint8_t rx_len=0; 00053 00054 00055 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); 00056 00057 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00058 00059 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; 00060 00061 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); 00062 00063 00064 00065 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00066 { 00067 pc.printf("Disconnected \r\n"); 00068 pc.printf("Restart advertising \r\n"); 00069 ble.startAdvertising(); 00070 } 00071 00072 void WrittenHandler(const GattWriteCallbackParams *Handler) 00073 { 00074 uint8_t buf[TXRX_BUF_LEN]; 00075 uint16_t bytesRead, index; 00076 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 00077 { 00078 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); 00079 memset(txPayload, 0, TXRX_BUF_LEN); 00080 memcpy(txPayload, buf, TXRX_BUF_LEN); 00081 00082 for(index=0; index<bytesRead; index++) 00083 pc.putc(buf[index]); 00084 00085 if(buf[0]==0xA0){ 00086 if(buf[1]==0x01) 00087 { 00088 analog_enabled=1; 00089 } 00090 else 00091 analog_enabled=0; 00092 } 00093 00094 } 00095 } 00096 00097 00098 00099 uint8_t LIS3DH_SPI_RD(uint8_t addr) 00100 { 00101 uint8_t temp; 00102 cs = 0; 00103 wait_us(10); 00104 spi_master.write(addr); 00105 temp=spi_master.write(0xff); 00106 wait_us(10); 00107 cs = 1; 00108 return temp; 00109 } 00110 00111 void LIS3DH_SPI_WR(uint8_t addr,uint8_t wrdata) 00112 { 00113 cs = 0; 00114 wait_us(10); 00115 spi_master.write(addr); 00116 spi_master.write(wrdata); 00117 wait_us(10); 00118 cs = 1; 00119 } 00120 00121 void SPI_LIS3DH_Init() 00122 { 00123 spi_master.format(8,3); 00124 spi_master.frequency(100000); 00125 wait_ms(5); 00126 LIS3DH_SPI_WR(0x24,0x80); 00127 wait_ms(5); 00128 LIS3DH_SPI_WR(0x20,0x17); 00129 LIS3DH_SPI_WR(0x23,0x80); 00130 } 00131 00132 void get_val(void) 00133 { 00134 uint8_t Dx_L=1,Dy_L=1,Dz_L=1; 00135 uint8_t Dx_H=1,Dy_H=1,Dz_H=1; 00136 if(LIS3DH_SPI_RD(0x0f|0x80)==0x33) 00137 { 00138 printf("check device ok!\r\n"); 00139 flag=1; 00140 Dx_H=LIS3DH_SPI_RD(0x29|0x80); 00141 Dx_L=LIS3DH_SPI_RD(0x28|0x80); 00142 Dy_H=LIS3DH_SPI_RD(0x2b|0x80); 00143 Dy_L=LIS3DH_SPI_RD(0x2A|0x80); 00144 Dz_H=LIS3DH_SPI_RD(0x2d|0x80); 00145 Dz_L=LIS3DH_SPI_RD(0x2C|0x80); 00146 } 00147 else 00148 { 00149 printf("check device err!\r\n"); 00150 wait(1); 00151 } 00152 x_a=Dx_H<<8|Dx_L/16; 00153 y_a=Dy_H<<8|Dy_L/16; 00154 z_a=Dz_H<<8|Dz_L/16; 00155 } 00156 00157 void m_status_check_handle(void) 00158 { 00159 uint8_t buf[3]; 00160 LED=1; 00161 if (analog_enabled) // if analog reading enabled 00162 { 00163 // Read and send out 00164 get_val(); 00165 if (flag) 00166 { 00167 if (abs(x_a)>1000) 00168 LED=0; 00169 buf[0] = (0x0A); 00170 buf[1] = (x_a >> 8); 00171 buf[2] = (x_a); 00172 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 00173 wait_us(100); 00174 00175 buf[0] = (0x0B); 00176 buf[1] = (y_a >> 8); 00177 buf[2] = (y_a); 00178 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 00179 wait_us(100); 00180 00181 buf[0] = (0x0C); 00182 buf[1] = (z_a >> 8); 00183 buf[2] = (z_a); 00184 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 00185 wait_us(100); 00186 00187 flag=0; 00188 wait_us(100); 00189 } 00190 00191 } 00192 } 00193 00194 00195 00196 int main(void) 00197 { 00198 uint8_t buf[3]; 00199 00200 SPI_LIS3DH_Init(); 00201 00202 Ticker ticker; 00203 ticker.attach_us(m_status_check_handle, 200000); 00204 00205 ble.init(); 00206 ble.onDisconnection(disconnectionCallback); 00207 ble.onDataWritten(WrittenHandler); 00208 00209 pc.baud(9600); 00210 pc.printf("SimpleChat Init \r\n"); 00211 00212 // setup advertising 00213 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00214 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00215 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00216 (const uint8_t *)"MolCom", sizeof("MolCom") - 1); 00217 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00218 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); 00219 // 100ms; in multiples of 0.625ms. 00220 ble.setAdvertisingInterval(160); 00221 00222 ble.addService(uartService); 00223 00224 ble.startAdvertising(); 00225 00226 pc.printf("Advertising Start \r\n"); 00227 00228 while(1) 00229 { 00230 ble.waitForEvent(); 00231 } 00232 }
Generated on Sat Jul 16 2022 12:30:49 by
1.7.2
