# 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

## 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.
