AKM Development Platform. This is the D7.014 version.
Dependencies: AK09970 AK099XX AK7401 AK7451 AK8963X AK9750 AK9752 AkmSensor BLE_API I2CNano MCP342x SerialNano SpiNano TCA9554A mbed nRF51822
Fork of AKDP by
main.cpp
- Committer:
- masahikofukasawa
- Date:
- 2016-05-09
- Revision:
- 4:749a21fd9c2d
- Parent:
- 2:11a5873f8ec0
- Child:
- 7:f1f544deaaa3
File content as of revision 4:749a21fd9c2d:
#include <stdio.h> #include <stdlib.h> #include "mbed.h" #include "ble/BLE.h" #include "ble/services/UARTService.h" #include "SerialNano.h" #include "akmsensormanager.h" #include "debug.h" #include "tca9554a.h" #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ //#define TXRX_LEN UARTService::BLE_UART_SERVICE_MAX_DATA_LEN+1 #define TXRX_LEN 50 // Temp #define CR '\r' #define LF '\n' BLE ble; UARTService* uartService; #ifndef REV_D SerialNano serial(P0_28, P0_29); // Rev.C pin configuration #else //SerialNano serial(P0_4, P0_5); // Rev.D pin configuration // for test SerialNano serial(P0_28, P0_29); // Rev.C pin configuration DigitalOut dummy_Tx(P0_4); DigitalIn dummy_Rx(P0_5); #endif AkmSensorManager* manager; static char commandStr[TXRX_LEN]; static uint16_t rx_len=0; /* void WrittenHandler(const GattWriteCallbackParams *Handler) { uint8_t buf[TXRX_LEN]; uint16_t bytesRead; if (Handler->handle == uartService->getTXCharacteristicHandle()) { ble.gattServer().read(uartService->getTXCharacteristicHandle(), buf, &bytesRead); int len = 0; for(int i=0; i<bytesRead; i++){ commandStr[len++] = buf[i]; if(i>=TXRX_LEN || buf[i]==LF ) { manager->commandReceived(commandStr); return; } } } } */ void WrittenHandler(const GattWriteCallbackParams *Handler) { uint8_t buf[TXRX_LEN]; uint16_t bytesRead; if (Handler->handle == uartService->getTXCharacteristicHandle()) { ble.gattServer().read(uartService->getTXCharacteristicHandle(), buf, &bytesRead); char command[TXRX_LEN]; int len = 0; for(int i=0; i<bytesRead; i++){ if(i>=TXRX_LEN || buf[i]==CR ) { break; } else { if(buf[i] != LF) { command[len++] = (char)buf[i]; } } } manager->commandReceived(command); } } /* void usbUartCallback(void) { if(serial.readable()) { uint8_t c = serial.getc(); if(rx_len>=TXRX_LEN || c == CR ) { manager->commandReceived(commandStr); rx_len = 0; } else{ if(c != LF) { commandStr[rx_len++] = c; } } } } */ void usbUartCallback(void) { if(serial.readable()) { uint8_t c = serial.getc(); // ignore CR if(c==CR) return; commandStr[rx_len++] = c; if(rx_len>=TXRX_LEN || c == LF) { manager->commandReceived(commandStr); rx_len = 0; } } } void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { manager->setEventConnected(); MSG("#Connected\r\n"); } void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { manager->setEventDisconnected(); MSG("#Disconnected \r\n"); ble.gap().startAdvertising(); } int main(void) { // USB serial serial.baud(115200); #ifdef DEBUG Debug::setSerial(&serial); MSG("#Debug Mode.\n"); #endif ble.init(); // setup advertising ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)"AKDP RevD 001", sizeof("AKDP RevD 001") - 1); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); ble.gap().onConnection(connectionCallback); ble.gap().onDisconnection(disconnectionCallback); ble.gattServer().onDataWritten(WrittenHandler); // 100ms; in multiples of 0.625ms. ble.gap().setAdvertisingInterval(80); ble.gap().startAdvertising(); // BLE UART service uartService = new UARTService(ble); // serial port RX event serial.attach(&usbUartCallback); // create sensor manager manager = new AkmSensorManager(&serial, uartService); if( manager->init() == AkmSensorManager::ERROR){ MSG("#Error: sensor is NULL\n"); } #ifdef REV_D /* Rev.D */ { const int TIME_FOR_OE_MS = 100; const TCA9554A::Port PORT_OE_LVS1 = TCA9554A::PORT_7; const TCA9554A::Port PORT_OE_LVS2 = TCA9554A::PORT_6; I2C i2c(I2C_SDA0, I2C_SCL0); TCA9554A tca9554a(&i2c, TCA9554A::SLAVE_ADDRESS_38H); // Initializes TCA9554A (I2C GPIO Expander) tca9554a.configurePort(PORT_OE_LVS1, TCA9554A::DIR_OUTPUT); tca9554a.configurePort(PORT_OE_LVS2, TCA9554A::DIR_OUTPUT); // Makes sure that the OE is low first. tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::LOW); tca9554a.setPortLevel(PORT_OE_LVS2, TCA9554A::LOW); wait_ms(TIME_FOR_OE_MS); // Sets the OE pins high. tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::HIGH); tca9554a.setPortLevel(PORT_OE_LVS2, TCA9554A::HIGH); } #endif // main loop while(1) { if(manager->isEvent()){ manager->processEvent(); }else{ ble.waitForEvent(); } } }