Sony's LANC camera control protocol project.

Dependencies:   aconno_LANC aconno_bsp aconno_SEGGER_RTT

Revision:
6:8cf24e946b0b
Parent:
5:ecb7712b0825
Child:
7:5a9d22d7fe00
Child:
8:3375dd304db9
--- a/main.cpp	Wed Nov 01 17:39:50 2017 +0000
+++ b/main.cpp	Wed Nov 01 18:05:20 2017 +0000
@@ -11,6 +11,9 @@
  
 #include "mbed.h"
 #include "acd52832_bsp.h"
+#include "ble/BLE.h"
+#include "GapAdvertisingData.h"
+#include "AckService.h"
 
 #define MY_BUF_SIZE     13*8
 #define LANC_H      0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF    /* 13 B */
@@ -25,8 +28,18 @@
 #define LANC_PIN            (p3)   // Lanc bus pin (to scan for START/STOP bits)        3/25
 #define LED_ON              (0)
 #define LED_OFF             (1)
+#define MSD_SIZE            (29)             /* Manufacturer Specific Data lenght (in B) */
 
-DigitalOut alive(p24);
+uint8_t MSD[MSD_SIZE] = {0x59, 0x00, 0xDD, 0x4E, 0xCD, 0xC5, 0x5E, 0xA3, 0x43, 0x67, 0x8B, 0x84, 0x94, 0xFF, 0xBA, 0xD9, 0x29, 0xC6};
+uint8_t myMacAddress[6] = {};
+ACKService<1> *ackServicePtr;
+BLE &ble = BLE::Instance();
+
+void sendCommand(uint32_t *commandType, uint32_t *command);
+GapAdvertisingData advData = GapAdvertisingData();
+
+DigitalOut alive(p23);
+DigitalOut connectedLED(p24);
 
 uint8_t normalCommand[MY_BUF_SIZE] __attribute__ ((aligned (32))) = {LANC_L,LANC_L,LANC_L,LANC_H, LANC_H,LANC_L,LANC_L,LANC_L};
 uint8_t zoomCommand[MY_BUF_SIZE] __attribute__ ((aligned (32))) = {LANC_L,LANC_L,LANC_L,LANC_H, LANC_L,LANC_H,LANC_L,LANC_L};
@@ -40,12 +53,81 @@
 uint32_t *zoomInAddr = (uint32_t*)zoomIn;
 uint32_t *zoomOutAddr = (uint32_t*)zoomOut;
 
-
 uint8_t volatile i2sFlag = 0;   
 uint8_t state = 0;  /* 1 -> Send command type, 0 -> send command */
+
 InterruptIn button(LANC_PIN);
 Timer frameTimer;
 
+void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){
+    connectedLED = LED_ON;
+    ble.gap().stopAdvertising();
+}
+
+void onDataWrittenCallback(const GattWriteCallbackParams *params) {
+    //newImage[cnt] = params->data[i];
+    switch(params->data[0]){
+        case 0x00:{
+            sendCommand(normalCmdAddr, startStopAddr);        
+            break;
+        }
+        case 0x01:{
+            sendCommand(zoomCmdAddr, zoomOutAddr);        
+            break;
+        }
+        case 0x02:{
+            sendCommand(zoomCmdAddr, zoomInAddr);        
+            break;
+        }
+        default: break;
+    }
+        
+    
+    return;
+}
+
+/**
+ * Restart Advertising on disconnection
+ */
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
+    connectedLED = LED_OFF;
+    BLE::Instance().gap().startAdvertising();
+}
+
+
+/**
+ * This function is called when the ble initialization process has failed
+ */
+void onBleInitError(BLE &ble, ble_error_t error){
+    /* Avoid compiler warnings */
+    (void) ble;
+    (void) error;
+    /* Initialization error handling should go here */
+}
+
+/**
+ * Callback triggered when the ble initialization process has finished
+ */
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
+    BLE&        ble   = params->ble;  
+    uint8_t init_values[1];
+    
+   
+    /* Get my MAC address */
+    BLEProtocol::AddressType_t temp_address_type;
+    ble.gap().getAddress(&temp_address_type, myMacAddress);
+    ackServicePtr = new ACKService<1>(ble, init_values);
+    
+    ble.gap().onDisconnection(disconnectionCallback);
+    ble.gap().onConnection(onConnectionCallback);         
+    ble.gattServer().onDataWritten(onDataWrittenCallback);
+    
+    /* setup advertising */
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE);
+    ble.gap().setAdvertisingInterval(500);  // --> Has to be at least 100ms!
+    ble.gap().startAdvertising();
+}
+
 void i2sReady(){
     /* Interrupt handler */
     i2sFlag = 1;
@@ -145,28 +227,16 @@
 int main(void){
     alive = 0;
     
-    i2sInit();
+    ble.init(bleInitComplete);
+    while (ble.hasInitialized()  == false) { /* spin loop */ }
+    ble.gap().startAdvertising();
     
+    i2sInit();
     button.fall(i2sReady);
     
+    sendCommand(zoomCmdAddr, zoomInAddr);
+    
     while(1){
-        sendCommand(zoomCmdAddr, zoomInAddr);
-        sendCommand(zoomCmdAddr, zoomInAddr);
-        sendCommand(zoomCmdAddr, zoomInAddr);
-        sendCommand(zoomCmdAddr, zoomInAddr);
-        sendCommand(zoomCmdAddr, zoomInAddr);
-        sendCommand(zoomCmdAddr, zoomInAddr);
-        sendCommand(zoomCmdAddr, zoomInAddr);
-        sendCommand(zoomCmdAddr, zoomInAddr);
-        wait_ms(350);
-        sendCommand(zoomCmdAddr, zoomOutAddr);
-        sendCommand(zoomCmdAddr, zoomOutAddr);
-        sendCommand(zoomCmdAddr, zoomOutAddr);
-        sendCommand(zoomCmdAddr, zoomOutAddr);
-        sendCommand(zoomCmdAddr, zoomOutAddr);
-        sendCommand(zoomCmdAddr, zoomOutAddr);
-        sendCommand(zoomCmdAddr, zoomOutAddr);
-        sendCommand(zoomCmdAddr, zoomOutAddr);
-        wait_ms(350);
+        ble.waitForEvent();
     }
 }