# Gate

Enables easy opening and closing a message stream in runtime.

This example requires the DepthAI v3 API, see [installation instructions](https://docs.luxonis.com/software-v3/depthai.md).

## Pipeline

### examples/gate.pipeline.json

```json
{
  "pipeline": {
    "bridges": [],
    "connections": [
      {
        "node1Id": 4,
        "node1Output": "out",
        "node1OutputGroup": "",
        "node2Id": 1,
        "node2Input": "inputControl",
        "node2InputGroup": ""
      },
      {
        "node1Id": 1,
        "node1Output": "output",
        "node1OutputGroup": "",
        "node2Id": 5,
        "node2Input": "in",
        "node2InputGroup": ""
      },
      {
        "node1Id": 0,
        "node1Output": "0",
        "node1OutputGroup": "dynamicOutputs",
        "node2Id": 1,
        "node2Input": "input",
        "node2InputGroup": ""
      }
    ],
    "globalProperties": {
      "calibData": null,
      "cameraSocketTuningBlobSize": [],
      "cameraSocketTuningBlobUri": [],
      "cameraTuningBlobSize": null,
      "cameraTuningBlobUri": "",
      "eepromId": 0,
      "leonCssFrequencyHz": 700000000.0,
      "leonMssFrequencyHz": 700000000.0,
      "pipelineName": null,
      "pipelineVersion": null,
      "sippBufferSize": 18432,
      "sippDmaBufferSize": 16384,
      "xlinkChunkSize": -1
    },
    "nodes": [
      [
        5,
        {
          "alias": "",
          "deviceId": "1936701492",
          "deviceNode": true,
          "id": 5,
          "ioInfo": [
            [
              [
                "",
                "pipelineEventOutput"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 17,
                "name": "pipelineEventOutput",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "in"
              ],
              {
                "blocking": true,
                "group": "",
                "id": 16,
                "name": "in",
                "queueSize": 3,
                "type": 3,
                "waitForMessage": false
              }
            ]
          ],
          "logLevel": 1,
          "name": "XLinkOut",
          "parentId": -1,
          "properties": [185, 5, 136, 0, 0, 128, 191, 189, 12, 95, 95, 120, 95, 49, 95, 111, 117, 116, 112, 117, 116, 0, 255, 255]
        }
      ],
      [
        4,
        {
          "alias": "",
          "deviceId": "1936701492",
          "deviceNode": true,
          "id": 4,
          "ioInfo": [
            [
              [
                "",
                "out"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 15,
                "name": "out",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "pipelineEventOutput"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 14,
                "name": "pipelineEventOutput",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ]
          ],
          "logLevel": 1,
          "name": "XLinkIn",
          "parentId": -1,
          "properties": [185, 3, 189, 19, 95, 95, 120, 95, 49, 95, 95, 105, 110, 112, 117, 116, 67, 111, 110, 116, 114, 111, 108, 130, 0, 6, 26, 1, 8]
        }
      ],
      [
        1,
        {
          "alias": "",
          "deviceId": "1936701492",
          "deviceNode": true,
          "id": 1,
          "ioInfo": [
            [
              [
                "",
                "output"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 8,
                "name": "output",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "pipelineEventOutput"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 7,
                "name": "pipelineEventOutput",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "inputControl"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 6,
                "name": "inputControl",
                "queueSize": 4,
                "type": 3,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "input"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 5,
                "name": "input",
                "queueSize": 1,
                "type": 3,
                "waitForMessage": false
              }
            ]
          ],
          "logLevel": 1,
          "name": "Gate",
          "parentId": -1,
          "properties": [185, 1, 185, 3, 1, 255, 255]
        }
      ],
      [
        0,
        {
          "alias": "",
          "deviceId": "1936701492",
          "deviceNode": true,
          "id": 0,
          "ioInfo": [
            [
              [
                "dynamicOutputs",
                "0"
              ],
              {
                "blocking": false,
                "group": "dynamicOutputs",
                "id": 4,
                "name": "0",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "pipelineEventOutput"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 2,
                "name": "pipelineEventOutput",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "raw"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 3,
                "name": "raw",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "mockIsp"
              ],
              {
                "blocking": true,
                "group": "",
                "id": 1,
                "name": "mockIsp",
                "queueSize": 8,
                "type": 3,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "inputControl"
              ],
              {
                "blocking": true,
                "group": "",
                "id": 0,
                "name": "inputControl",
                "queueSize": 3,
                "type": 3,
                "waitForMessage": false
              }
            ]
          ],
          "logLevel": 1,
          "name": "Camera",
          "parentId": -1,
          "properties": [185, 22, 185, 33, 0, 3, 0, 136, 0, 0, 0, 0, 128, 203, 128, 175, 185, 3, 130, 210, 60, 167, 206, 130, 27, 150, 152, 107, 130, 147, 182, 86, 184, 185, 5, 129, 144, 132, 129, 12, 171, 129, 31, 57, 129, 112, 19, 130, 149, 218, 232, 115, 185, 5, 129, 88, 123, 129, 60, 187, 129, 70, 49, 129, 114, 149, 130, 159, 68, 240, 46, 54, 128, 240, 128, 168, 0, 0, 128, 152, 128, 177, 128, 221, 128, 185, 185, 3, 44, 128, 139, 132, 139, 185, 3, 134, 92, 234, 227, 32, 134, 1, 55, 201, 148, 130, 10, 242, 221, 79, 130, 36, 65, 196, 239, 15, 82, 230, 209, 128, 228, 0, 0, 129, 156, 212, 128, 239, 24, 0, 186, 0, 0, 255, 189, 0, 255, 255, 255, 255, 255, 136, 0, 0, 128, 191, 136, 0, 0, 128, 191, 0, 3, 134, 0, 0, 160, 0, 3, 134, 0, 0, 160, 0, 4, 4, 4, 190, 190, 186, 1, 185, 6, 185, 1, 184, 0, 186, 2, 129, 128, 2, 129, 144, 1, 185, 1, 184, 0, 136, 0, 0, 240, 65, 190, 0, 190, 0]
        }
      ]
    ]
  }
}
```

## Source code

#### Python

```python
import time
import cv2 
import depthai as dai

device = dai.Device()

withGate = True 

with dai.Pipeline(device) as pipeline:
    camera = pipeline.create(dai.node.Camera).build()
    cameraOut = camera.requestOutput((640, 400), fps=30)

    gate  = pipeline.create(dai.node.Gate)
    cameraOut.link(gate.input)
    cameraQueue = gate.output.createOutputQueue()
    gateControlQueue = gate.inputControl.createInputQueue()

    gateControl = dai.GateControl()

    pipeline.start()

    ellapsed = 0
    startTime = time.monotonic()
    gateOpen = True

    while pipeline.isRunning():
        frame = cameraQueue.tryGet()
        if frame is not None:
            cv2.imshow("camera", frame.getCvFrame())

        currentTime = time.monotonic()
        elapsed = currentTime - startTime

        if elapsed > 5.0:  # 5 seconds
            # Toggle the value
            gateOpen = not gateOpen
            # Create and send the control message
            if (gateOpen):
                gateControlQueue.send(dai.GateControl.openGate())
            else:
                gateControlQueue.send(dai.GateControl.closeGate())
            
            print(f"Gate toggled to: {gateOpen}")
            
            # Reset the timer
            startTime = currentTime

        key = cv2.waitKey(1)

        if key == ord('q'):
            pipeline.stop()
            break
```

#### C++

```cpp
#include <atomic>
#include <chrono>
#include <csignal>
#include <iostream>

// DepthAI and OpenCV headers
#include <opencv2/opencv.hpp>

#include "depthai/depthai.hpp"
#include "depthai/pipeline/node/Gate.hpp"

std::atomic<bool> quitEvent(false);

void signalHandler(int) {
    quitEvent = true;
}

int main() {
    signal(SIGTERM, signalHandler);
    signal(SIGINT, signalHandler);

    dai::Pipeline pipeline;

    auto camera = pipeline.create<dai::node::Camera>()->build();
    auto cameraOut = camera->requestOutput(std::make_pair(640, 400), std::nullopt, dai::ImgResizeMode::CROP, 30);

    auto gate = pipeline.create<dai::node::Gate>();

    cameraOut->link(gate->input);

    auto cameraQueue = gate->output.createOutputQueue();

    auto gateControlQueue = gate->inputControl.createInputQueue();

    pipeline.start();

    auto startTime = std::chrono::steady_clock::now();
    bool openGate = true;

    while(pipeline.isRunning() && !quitEvent) {
        auto frame = cameraQueue->tryGet<dai::ImgFrame>();
        if(frame != nullptr) {
            cv::imshow("camera", frame->getCvFrame());
        }

        auto currentTime = std::chrono::steady_clock::now();
        std::chrono::duration<double> elapsed = currentTime - startTime;

        if(elapsed.count() > 5.0) {
            openGate = !openGate;

            if(openGate) {
                gateControlQueue->send(dai::GateControl::openGate());
            } else {
                gateControlQueue->send(dai::GateControl::closeGate());
            }

            std::cout << "Gate toggled to: " << (openGate ? "True" : "False") << std::endl;

            startTime = currentTime;
        }

        int key = cv::waitKey(1);
        if(key == 'q') {
            break;
        }
    }

    pipeline.stop();
    pipeline.wait();

    return 0;
}
```

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
