my fork
Diff: main.cpp
- Revision:
- 4:987b201ec4b1
- Parent:
- 2:b60cb847489c
--- a/main.cpp Thu Nov 06 10:13:20 2014 +0000 +++ b/main.cpp Wed Apr 08 14:17:20 2015 +0000 @@ -1,12 +1,136 @@ -#include "mbed.h" +#include "mbed.h" +#include "cube_hal.h" + +#include "osal.h" +#include "sensor_service.h" +#include "debug.h" +#include "stm32_bluenrg_ble.h" + -DigitalOut myled(LED1); +Serial pc(USBTX, USBRX); // tx, rx +/* Private macro -------------------------------------------------------------*/ +#define BDADDR_SIZE 6 +/* Private variables ---------------------------------------------------------*/ +extern volatile uint8_t set_connectable; +extern volatile int connected; +extern AxesRaw_t axes_data; + +/* Private function prototypes -----------------------------------------------*/ +void User_Process(AxesRaw_t* p_axes); + + +float tri; int main() { - while(1) { - myled = 1; // LED is ON - wait(0.2); // 200 ms - myled = 0; // LED is OFF - wait(1.0); // 1 sec + const char *name = "BlueNRG"; + uint8_t SERVER_BDADDR[] = {0x12, 0x34, 0x00, 0xE1, 0x80, 0x02}; + //uint8_t bdaddr[BDADDR_SIZE]; + uint8_t bdaddr[BDADDR_SIZE] = {0x12, 0x34, 0x00, 0xE1, 0x80, 0x02} ; + uint16_t service_handle, dev_name_char_handle, appearance_char_handle; + int ret; + + HAL_Init(); + /* Configure the User Button in GPIO Mode */ + BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); + /* Configure the system clock */ +// SystemClock_Config(); + /* Initialize the BlueNRG SPI driver */ + BNRG_SPI_Init(); + /* Initialize the BlueNRG HCI */ + HCI_Init(); + /* Reset BlueNRG hardware */ + BlueNRG_RST(); + /* The Nucleo board must be configured as SERVER */ + //Osal_MemCpy(bdaddr, SERVER_BDADDR, sizeof(SERVER_BDADDR)); + + ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, + CONFIG_DATA_PUBADDR_LEN, + bdaddr); + + pc.printf("Hello World!\n"); + + if(ret){ + PRINTF("Setting BD_ADDR failed.\n"); + } + + ret = aci_gatt_init(); + if(ret){ + PRINTF("GATT_Init failed.\n"); + } + + ret = aci_gap_init(GAP_PERIPHERAL_ROLE, &service_handle, + &dev_name_char_handle, &appearance_char_handle); + if(ret != BLE_STATUS_SUCCESS){ + PRINTF("GAP_Init failed.\n"); + } + + ret = aci_gatt_update_char_value(service_handle, dev_name_char_handle, 0, + strlen(name), (uint8_t *)name); + + if(ret){ + PRINTF("aci_gatt_update_char_value failed.\n"); + while(1); + } + + ret = aci_gap_set_auth_requirement(MITM_PROTECTION_REQUIRED, + OOB_AUTH_DATA_ABSENT, + NULL, + 7, + 16, + USE_FIXED_PIN_FOR_PAIRING, + 123456, + BONDING); + if (ret == BLE_STATUS_SUCCESS) { + PRINTF("BLE Stack Initialized.\n"); + } + + PRINTF("SERVER: BLE Stack Initialized\n"); + + ret = Add_Acc_Service(); + + if(ret == BLE_STATUS_SUCCESS) + PRINTF("Acc service added successfully.\n"); + else + PRINTF("Error while adding Acc service.\n"); + + ret = Add_Environmental_Sensor_Service(); + + if(ret == BLE_STATUS_SUCCESS) + PRINTF("Environmental Sensor service added successfully.\n"); + else + PRINTF("Error while adding Environmental Sensor service.\n"); + + /* Set output power level */ + ret = aci_hal_set_tx_power_level(1,4); + + while(1) + { + HCI_Process(); + User_Process(&axes_data); + } +} + +void User_Process(AxesRaw_t* p_axes) +{ + if(set_connectable){ + setConnectable(); + set_connectable = FALSE; + } + + /* Check if the user has pushed the button */ + if(BSP_PB_GetState(BUTTON_KEY) == RESET) + { + while (BSP_PB_GetState(BUTTON_KEY) == RESET); + + //BSP_LED_Toggle(LED2); //used for debugging + + if(connected){ + /* Update acceleration data */ + p_axes->AXIS_X += 100; + p_axes->AXIS_Y += 100; + p_axes->AXIS_Z += 100; + //PRINTF("ACC: X=%6d Y=%6d Z=%6d\r\n", p_axes->AXIS_X, p_axes->AXIS_Y, p_axes->AXIS_Z); + Acc_Update(p_axes); } -} + } +} \ No newline at end of file