Current version

Fork of mbed-os-example-ble-LED by mbed-os-examples

Revision:
19:41b1396c50d2
Parent:
18:d089fb6ff789
Child:
20:f9f18052df0c
--- a/source/main.cpp	Fri Feb 10 13:41:29 2017 +0000
+++ b/source/main.cpp	Thu Feb 23 13:07:54 2017 +0000
@@ -19,20 +19,45 @@
 #include "ble/BLE.h"
 #include "LEDService.h"
 
-#define NO_COMMAND      (0)
-#define ZOOM_IN         (1)
-#define ZOOM_OUT        (2)
-#define FOCUS_NEAR      (3)
-#define FOCUS_FAR       (4)
-#define RECORD          (5)
+#define NO_COMMAND          (0)
+#define ZOOM_IN             (1)
+#define ZOOM_OUT            (2)
+#define FOCUS_NEAR          (3)
+#define FOCUS_FAR           (4)
+#define RECORD              (5)
+
+#define LANC_COMMAND_PIN    (p2)   // Pin connected to Tr to pull lanc bus down/up
+#define LANC_PIN            (p3)   // Lanc bus pin (to scan for START/STOP bits)
+
 
+#define LANC_BIT_DURATION   (104)   // One LANC bit duration in ms
+#define LANC_MIN_DELAY      (5000)
+#define NUMBER_OF_REPETITIONS (1)
+
+#define REC_CMD_1           (0x18)
+#define REC_CMD_2           (0x33)
+#define ZOOM_IN_CMD_4_1     (0x28)
+#define ZOOM_IN_CMD_4_2     (0x08)
+#define ZOOM_OUT_CMD_4_1    (0x28)
+#define ZOOM_OUT_CMD_4_2    (0x18)
+#define FOCUS_NEAR_1        (0x28)
+#define FOCUS_NEAR_2        (0x47)
+#define FOCUS_FAR_1         (0x28)
+#define FOCUS_FAR_2         (0x45)
 
 DigitalOut alivenessLED(p26, 0);
 DigitalOut led_red(p22, 0);
 DigitalOut led_blue(p23, 0);
 DigitalOut led_green(p24, 0);
 
-const static char     DEVICE_NAME[] = "LED";
+void send_command(uint8_t command_1, uint8_t command_2);
+
+DigitalOut command_pin     LANC_COMMAND_PIN;
+DigitalIn  lanc_pin        LANC_PIN;
+
+Timer timer;
+
+const static char     DEVICE_NAME[] = "Lanc";
 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
 
 static EventQueue eventQueue(
@@ -64,20 +89,25 @@
         led_green = 1;
         if (*(params->data)==ZOOM_IN){
                 led_red = 0;
+                send_command(ZOOM_IN_CMD_4_1,ZOOM_IN_CMD_4_2);
             }
         else if (*(params->data)==ZOOM_OUT){
                 led_blue = 0;
+                send_command(ZOOM_OUT_CMD_4_1,ZOOM_OUT_CMD_4_2);
             }
         else if (*(params->data)==FOCUS_NEAR){
                 led_green = 0;
+                send_command(FOCUS_NEAR_1, FOCUS_NEAR_2);
             }
         else if (*(params->data)==FOCUS_FAR){
                 led_green = 0;
                 led_red = 0;
+                send_command(FOCUS_FAR_1,FOCUS_FAR_2);
             }
         else if (*(params->data)==RECORD){
                 led_blue = 0;
                 led_red = 0;
+                send_command(REC_CMD_1, REC_CMD_2);
         }
         else if (*(params->data)==NO_COMMAND){
                 // No command
@@ -88,16 +118,14 @@
 /**
  * This function is called when the ble initialization process has failled
  */
-void onBleInitError(BLE &ble, ble_error_t error)
-{
+void onBleInitError(BLE &ble, ble_error_t error){
     /* Initialization error handling should go here */
 }
 
 /**
  * Callback triggered when the ble initialization process has finished
  */
-void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
-{
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
     BLE&        ble   = params->ble;
     ble_error_t error = params->error;
 
@@ -132,12 +160,75 @@
     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
 }
 
+void init_lanc_camera(void){
+    // Function will set bus to HIGH and hold it for 5ms
+    
+}
+
+void init_lanc_bus(){
+    // Function initialize lanc bus by pulling it up
+    // Bus goes HIGH by setting command pin to LOW
+    command_pin = 0;
+    wait_ms(LANC_MIN_DELAY);
+}
+
+void send_command(uint8_t command_1, uint8_t command_2){
+    uint8_t command_counter = 0;
+        
+    
+    while(command_counter < NUMBER_OF_REPETITIONS){
+        //Arduino's PulseIn function (inline implemetation)
+        while (lanc_pin == 0);      // Waiting for HIGH
+        timer.start();
+        while(lanc_pin == 1);        // Waiting for LOW 
+        timer.stop();
+        if(timer.read_ms()>LANC_MIN_DELAY){
+            break;
+        }
+    }
+    
+    // Program is out of the while loop -> bus is LOW -> START bit of BYTE_0 is here
+    
+    // Write Byte 0 to the bus
+    for(int i=0; i<8; i++){
+        command_pin = (command_1 & (1<<i));
+        wait_ms(LANC_BIT_DURATION);
+    }
+    // Put LANC bus back to HIGH
+    command_pin = 0;
+    // Make sure to be in the stop bit before the Byte 1
+    wait_ms(10); 
+    
+    // Look while lanc bus is HIGH for STOP bit
+    while(lanc_pin == 1);
+    
+    wait_ms(LANC_BIT_DURATION);
+    //Write Byte 1 to the bus
+    for(int i=0; i<8; i++){
+        command_pin = (command_2 & (1<<i));
+        wait_ms(LANC_BIT_DURATION);
+    }
+    // Put LANC bus back to HIGH
+    command_pin = 0;
+    command_counter++;
+    
+    /*
+     *  Control Bytes 0 and 1 are written. Program does not care what happend in
+     *  Bytes 2-7 and just waits for next start bit after a long pause to send 
+     *  command again
+     */  
+}
+
+
 int main(){
+    // Clear RGB led
     led_red = 1;
     led_green = 1;
     led_blue = 1;
+    
+    init_lanc_bus();
+    
     eventQueue.call_every(50, blinkCallback);
-
     BLE &ble = BLE::Instance();
     ble.onEventsToProcess(scheduleBleEventsProcessing);
     ble.init(bleInitComplete);