MP3(DFR0534)+BLE(TYBLE16)

Dependencies:   MP3_DFR0534 mbed-os_TYBLE16

see /users/kenjiArai/notebook/mp3--voice-module-dfr0534/

Revision:
10:5d2299daf831
Parent:
9:8c9e6e270b67
--- a/main.cpp	Sun Dec 22 06:33:18 2019 +0000
+++ b/main.cpp	Tue Feb 04 08:23:56 2020 +0000
@@ -1,191 +1,287 @@
 /*
- * Mbed Application program / TYBLE-16 Data logger
+ *      December 31st, 2015     Modified by Kenji Arai
+ *		January  19th, 2016
+ *		October  12th, 2017		!! Run on Mbed-os5
+ *                              http://www.page.sannet.ne.jp/kenjia/index.html
+ *                              https://os.mbed.com/users/kenjiArai/
+ *		April    14th, 2018		run on TYBLE16
+ *      January  23rd, 2020		TYBLE16(mbed-os5.1.5.0)
  *
- * Copyright (c) 2019 Kenji Arai / JH1PJL
- *  http://www.page.sannet.ne.jp/kenjia/index.html
- *  https://os.mbed.com/users/kenjiArai/
- *      Created:    December  14th, 2019
- *      Revised:    December  22nd, 2019
+ *  Original:
+ *   BLE_RCBController2_Motor
+ *   https://developer.mbed.org/users/robo8080/code/BLE_RCBController2_Motor/
+ *  Tested Controller Device:
+ *   iPhone6 RCBController (Ver1.4.0)
+ *   https://itunes.apple.com/jp/app/rcbcontroller/id689724127?mt=8
  */
 
-//  Include --------------------------------------------------------------------
 #include "mbed.h"
+#include "BLE.h"
+#include "RCBController.h"
 #include "TYBLE16_BASE.h"
-#include "dt_logger.h"
-#include "nRF51_Vdd.h"
-#include "RX8025NB.h"
-#include "BME280_SPI.h"
-#include "MB85RSxx_SPI.h"
+#include "drf0534.h"
+
+#define NEED_CONSOLE_OUTPUT	 0 // Keep 1
 
-//  Definition -----------------------------------------------------------------
-#define KEPP_SLEEP_TIME 1               // 0= continuos mode (every 10sec)           
-#define SLEEP_TIME      2               // Sleep 2 minutes
-#define FRAM_ID         (0x047f4803)    // Fijitsu 2Mbits
+#if NEED_CONSOLE_OUTPUT
+#define DEBUG(...) { pc.printf(__VA_ARGS__); }
+#else
+#define DEBUG(...)
+#endif
 
-//  Constructor ----------------------------------------------------------------
-DigitalOut  bme280_pwr(D9, 1);  // BME280 sensor power on
-DigitalOut  fram_pwr(D5, 1);    // FRAM power on
-DigitalInOut  x0(I2C_SDA, PIN_OUTPUT, PullUp, 1);    // I2C for dummy
-DigitalInOut  x1(I2C_SCL, PIN_OUTPUT, PullUp, 1);    // I2C for dummy
-InterruptIn rtc_irq(P0_2, PullUp);
+//BLE  		ble_rcb;
+BLE &ble_rcb = BLE::Instance();
+#if NEED_CONSOLE_OUTPUT
 Serial      pc(USBTX, USBRX);
-nRF51_Vdd   vdd(3.6f, 1.6f, ONLY4VDD);
+#else
+DFR0534 	mp3(USBTX, USBRX, A0);  // tx, rx, busy
+#endif
 
-//  RAM ------------------------------------------------------------------------
-uint8_t fram_id[4];
-bool fram_ready = false;
+uint8_t RCBControllerPayload[10] = {0,};
+RCBController 		controller;
 
-// Global data
-time_t log_sec;
-float vcc_voltage;
-float barometer;
-float temperature;
-float humidity;
+// RCBController Service
+static const uint16_t RCBController_service_uuid = 0xFFF0;
+static const uint16_t RCBController_Characteristic_uuid = 0xFFF1;
+const char *deviceName = "Mbed-BLE";
+const char *const opngmsg =
+    "\x1b[2J\x1b[H" __FILE__ "\r\n" __DATE__ " " __TIME__ " (UTC)\r\n""\r\n";
 
-//  ROM / Constant data --------------------------------------------------------
-const char *const opngmsg0 =
-    "\r\n---------\r\nCompiled on " __DATE__ " " __TIME__ " (UTC)\r\n";
-const char *const opngmsg1 =
-    "Project: TYBLE16_simple_data_logger  by Kenji Arai\r\n";
-
-//  Function prototypes --------------------------------------------------------
-static void rtc_interrupt(void);
-static void goto_standby(void);
-static void priparation_for_sleep(void);
-time_t w_check_rtc_time(RX8025 &ex_rtc);
+GattCharacteristic  ControllerChar (
+    RCBController_Characteristic_uuid,RCBControllerPayload,10, 10,
+    GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE |
+    GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE
+);
+GattCharacteristic *ControllerChars[] = {&ControllerChar};
+GattService         RCBControllerService(
+    RCBController_service_uuid,
+    ControllerChars,
+    sizeof(ControllerChars) / sizeof(GattCharacteristic *)
+);
 
 //------------------------------------------------------------------------------
 //  Control Program
 //------------------------------------------------------------------------------
-int main()
+void data_analysis(void)
 {
-    char buf[64];               // data buffer for text
-    time_t seconds;
+    static uint8_t key = 0;
+	static bool idle = true;
+	static bool previous_key = 0;
 
-    // open message
-    pc.puts(opngmsg0);
-    pc.puts(opngmsg1);
-    // Check TYBLE-16 configuration
-    cpu_sys();
-    compile_condition();
-    // Create constructor
-    // BM280
-    BME280_SPI bme280(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, P0_25);
-    // FRAM
-    MB85RSxx_SPI fram(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, P0_6);
-    fram.read_device_id(fram_id);
-    uint32_t id = 0, num = 24;
-    for (uint32_t i = 0; i < 4; i++) {
-        id += fram_id[i] << num;
-        num -= 8;
-    }
-    pc.printf("FRAM ID=0x%08x", id);
-    if (id == FRAM_ID) {
-        fram_ready = true;
-    }
-    if (fram_ready == true) {
-        pc.printf("(looks like Fujitsu MB85RS2M)\r\n");
-    } else {
-        pc.printf("(unknown device)\r\n");
+    uint16_t key_data = (controller.data[0] << 8) + controller.data[1];
+    switch(key_data) {
+        case    0:
+            key =  0;
+            idle = true;
+            break;
+        case    1:
+            key = 11;
+            if (idle == true) {
+            	idle = false;
+            	mp3.play_one(key);
+            }
+            break;
+        case    2:
+            key = 15;
+            break;
+        case    4:
+            key = 16;
+            break;
+        case    5:
+            key = 12;
+            if (idle == true) {
+            	idle = false;
+            	mp3.play_one(key);
+            }
+            break;
+        case    6:
+            key = 17;
+            break;
+        case    8:
+            key = 13;
+            if (idle == true) {
+            	idle = false;
+            	mp3.volume_increase();
+            }
+            break;
+        case    9:
+            key = 10;
+            if (idle == true) {
+            	idle = false;
+            	mp3.play_one(key);
+            }
+            break;
+        case   10:
+            key = 14;
+            if (idle == true) {
+            	idle = false;
+            	mp3.volume_decrease();
+            }
+            break;
+        case   16:
+            key =  2;
+            if (idle == true) {
+            	idle = false;
+            	mp3.play_one(key);
+            }
+            break;
+        case   32:
+            key =  8;
+            if (idle == true) {
+            	idle = false;
+            	mp3.play_one(key);
+            }
+            break;
+        case   64:
+            key =  6;
+            if (idle == true) {
+            	idle = false;
+            	mp3.play_one(key);
+            }
+            break;
+        case   80:
+            key =  3;
+            if (idle == true) {
+            	idle = false;
+            	mp3.play_one(key);
+            }
+            break;
+        case   96:
+            key =  9;
+            if (idle == true) {
+            	idle = false;
+            	mp3.play_one(key);
+            }
+            break;
+        case  128:
+            key =  5;
+            if (idle == true) {
+            	idle = false;
+            	mp3.play_one(key);
+            }
+            break;
+        case  256:
+            key =  4;
+            if (idle == true) {
+            	idle = false;
+            	mp3.play_one(key);
+            }
+            break;
+        case  272:
+            key =  1;
+            if (idle == true) {
+            	idle = false;
+            	mp3.play_one(key);
+            }
+            break;
+        case  288:
+            key =  7;
+            if (idle == true) {
+            	idle = false;
+            	mp3.play_one(key);
+            }
+            break;
+        case  512:
+            key = 18;
+            if (idle == true) {
+            	idle = false;
+            	mp3.command(Prev_Audio);
+            }
+            break;
+        case 1024:
+            key = 19;
+            if (idle == true) {
+            	idle = false;
+            	mp3.command(Play);
+            }
+            break;
+        case 2048:
+            key = 21;
+            if (idle == true) {
+            	idle = false;
+            	uint32_t num = mp3.total_num_of_audio();
+		        for (uint32_t i = 1; i < num + 1; i++) {
+		            mp3.play_one(i);
+		        }
+            }
+            break;
+        case 4096:
+            key = 20;
+            if (idle == true) {
+            	idle = false;
+            	mp3.command(Next_Audio);
+            }
+            break;
+        default:
+            key = 0;
     }
-    // RX8025
-    RX8025 ex_rtc(I2C_SDA, I2C_SCL);    // RTC(RX8025) (Fixed address)
-    ThisThread::sleep_for(20);
-    ex_rtc.clear_alarmD_INTA();
-    w_check_rtc_time(ex_rtc);
-    int32_t count = 3;
-    while(true) {
-        seconds = w_check_rtc_time(ex_rtc);
-        log_sec = seconds;
-        strftime(buf, 50, "%Y/%m/%d,%H:%M:%S, ", localtime(&seconds));
-        barometer = bme280.getPressure();
-        temperature = bme280.getTemperature();
-        humidity = bme280.getHumidity();
-        vcc_voltage = vdd.read_real_value();
-        if (count == 1) {
-            pc.printf("%s", buf);
-            pc.printf("%+2.2f,%2.2f,%04.2f,",
-                      temperature, humidity, barometer);
-            pc.printf("%3.2f\r\n", vcc_voltage);
-        }
-        ThisThread::sleep_for(200);
-        if (pc.readable() == 1) {
-            mon(ex_rtc, fram);
-        }
-        if (--count <= 0) {
-            break;
-        }
-    }
-    dtlog_data_pack();          // Data preparation for FRAM
-    dtlog_one_write(fram);      // Write data to FRAM
-#if KEPP_SLEEP_TIME
-    rtc_irq.fall(&rtc_interrupt);
-    uint16_t sleeping_time = SLEEP_TIME;
-    pc.printf("Enter sleep mode and wake up after %d minutes\r\n",
-              sleeping_time);
-    ex_rtc.set_next_alarmD_INTA(sleeping_time);
-    //----- SLEEP --------------------------------------------------------------
-    // FRAM & BME280 power off
-    bme280_pwr = 0;
-    fram_pwr = 0;
-    priparation_for_sleep();
-    // Enter sleep mode
-    ThisThread::sleep_for((sleeping_time) * 60 * 1000 + 10000);
-    system_reset();
-#else
-    // FRAM & BME280 power off
-    bme280_pwr = 0;
-    fram_pwr = 0;
-    priparation_for_sleep();
-    ThisThread::sleep_for(8000);
-    system_reset();
-#endif
+    DEBUG("DATA:%d\r\n", key);
 }
 
-void priparation_for_sleep(void)
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    DEBUG("Disconnected!\r\n");
+    DEBUG("Restarting the advertising process\r\n");
+    ble_rcb.gap().startAdvertising();
+}
+
+void onDataWritten(const GattWriteCallbackParams *params)
 {
-    // SPI output keeps low
-    DigitalOut p0(SPIS_PSELMOSI, 0);
-    DigitalOut p1(SPIS_PSELSCK, 0);
-    DigitalOut p2(P0_25, 0);
-    DigitalOut p3(P0_6, 0);
-    // SPI MISO sets pulldown
-    DigitalIn  p4(SPIS_PSELMISO, PullDown);
-    // Periperal power off
-    NRF_UART0->ENABLE = 0;
-    NRF_UART0->POWER  = 0;
-    NRF_TWI1->ENABLE  = 0;
-    NRF_TWI1->POWER   = 0;
-    NRF_SPI0->ENABLE  = 0;
-    NRF_SPI0->POWER   = 0;
-    NRF_SPIS1->ENABLE = 0;
-    NRF_SPIS1->POWER  = 0;
-    NRF_ADC->ENABLE   = 0;
-    NRF_ADC->POWER    = 0;
+    if (params->handle == ControllerChar.getValueAttribute().getHandle()) {
+        uint16_t bytesRead;
+        ble_rcb.gattServer().read(
+        //ble_rcb.readCharacteristicValue(
+            ControllerChar.getValueAttribute().getHandle(),
+            RCBControllerPayload, &bytesRead
+        );
+        memcpy( &controller.data[0], RCBControllerPayload, sizeof(controller));
+        DEBUG("DATA:0x%02x 0x%02x %d %d %d %d %d %d %d 0x%02x\r\n",
+              controller.data[0],controller.data[1],
+              controller.data[2],controller.data[3],
+              controller.data[4],controller.data[5],
+              controller.data[6],controller.data[7],
+              controller.data[8],controller.data[9]
+             );
+        uint16_t key_data = (controller.data[0] << 8) + controller.data[1];
+        DEBUG("DATA:%d\r\n", key_data);
+        data_analysis();
+    }
 }
 
-void rtc_interrupt(void)
+int main(void)
 {
-    system_reset();
-}
-
-time_t w_check_rtc_time(RX8025 &ex_rtc)
-{
-    time_t seconds_1st, seconds_2nd, diff;
-    struct tm t;
+    mp3.volume_setting(0);
+    DEBUG("%s", opngmsg);
+    DEBUG("\r\nInitialising the nRF51822\r\n");
+    // Check TYBLE-16 configuration
+    //cpu_sys();
+    //compile_condition();
 
-    for (uint32_t i = 0; i < 10; i++) {
-        ex_rtc.get_time_rtc(&t);   // read External RTC data
-        seconds_1st = mktime(&t);
-        ex_rtc.get_time_rtc(&t);   // read External RTC data again (make sure)
-        seconds_2nd = mktime(&t);
-        diff = seconds_2nd - seconds_1st;
-        if ((diff == 0) || (diff == 1)) {
-            if (seconds_2nd > DATE_COUNT_START) {
-                return seconds_2nd;
-            }
-        }
+    ble_rcb.init();
+    ble_rcb.gap().setDeviceName((const uint8_t *)deviceName);
+    ble_rcb.gap().onDisconnection(disconnectionCallback);
+    ble_rcb.gattServer().onDataWritten(onDataWritten);
+    DEBUG("Start RCB Controller\r\n");
+    /* setup advertising */
+    ble_rcb.gap().accumulateAdvertisingPayload(
+        GapAdvertisingData::BREDR_NOT_SUPPORTED
+    );
+    ble_rcb.gap().setAdvertisingType(
+        GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED
+    );
+    ble_rcb.gap().accumulateAdvertisingPayload(
+        GapAdvertisingData::SHORTENED_LOCAL_NAME,
+        (const uint8_t *)deviceName, strlen(deviceName)
+    );
+    ble_rcb.gap().accumulateAdvertisingPayload(
+        GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
+        (const uint8_t *)RCBController_service_uuid,
+        sizeof(RCBController_service_uuid)
+    );
+    ble_rcb.gap().setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
+    ble_rcb.gap().startAdvertising();
+    ble_rcb.gattServer().addService(RCBControllerService);
+    while (true) {
+        ble_rcb.waitForEvent();
     }
-    // Without success
-    system_reset();
 }