Commit 21fb1fd8 authored by Martin Cífka's avatar Martin Cífka
Browse files

added timelapse & support for shooting timelapse and bracketing at the same time

parent e6d6c251
Loading
Loading
Loading
Loading
Loading
+91 −14
Original line number Diff line number Diff line
@@ -17,9 +17,15 @@ Commander::Commander() :
        
    _iso((Codes::ISO)-1),
    _speed((Codes::Speed)-1),
    _bracketingStep(1),
    _bracketingHalfCount(1),
    _bracketingBuiltIn((Codes::ParamBracketingBuiltIn)-1)    
        
    _bracketingStep(0),
    _bracketingHalfCount(0),
    _bracketingBuiltIn((Codes::ParamBracketingBuiltIn)-1),
    
    _timelapseInterval(0),
    _timelapseCount(0),
        
    _stopShooting(false)
{};


@@ -217,6 +223,11 @@ bool Commander::setBracketing(size_t evStep, size_t halfCount)
}


void Commander::setTimelapse(size_t interval, size_t count)
{
    _timelapseInterval = interval;
    _timelapseCount = count;
}


bool Commander::shootNormal()
@@ -246,6 +257,7 @@ bool Commander::shootNormal()
bool Commander::shootBracketing()
{
    auto s = _speed;
    
    // shoot bracketing
    for (auto&& s : _bracketingVector)
    {   
@@ -255,6 +267,15 @@ bool Commander::shootBracketing()
        if (!shootNormal())
            return false;
        
        {
        lock_guard lock(_stopShootingMutex);
        if (_stopShooting)
        {
            _stopShooting = false;
            return false;
        }
        }
            
        this_thread::sleep_for(chrono::milliseconds(300));
    }
    
@@ -302,9 +323,65 @@ bool Commander::shootBracketingBuiltIn()
}


bool Commander::shootTimelapse(bool bracketing)
{
    auto interval = chrono::seconds(_timelapseInterval);
    auto next = chrono::steady_clock::now();
    
    for (size_t i = 0; i < _timelapseCount; ++i)
    {
        next += interval;
        
        if (bracketing)
            shootBracketing();
        else
            shootNormal();
        
        unique_lock lock(_stopShootingMutex);
        if (_stopShootingCV.wait_until(lock, next, [this](){return _stopShooting;}))
        {
            cout << "shooting stopped" << endl;
            _stopShooting = false;
            return false; // if thread is notified before timeout: stop shooting
        }
        // else (timeout): continue shooting
    }
    return true;
}

void Commander::stopShooting()
{
    lock_guard lock(_stopShootingMutex);
    _stopShooting = true;
    _stopShootingCV.notify_all();
}

bool Commander::shoot()
{
    if (_bracketingStep == 0 || _bracketingHalfCount == 0)
    {
        if (_timelapseInterval == 0 || _timelapseCount == 0)
            return shootNormal();
        else
            return shootTimelapse(false);
    }
    else
    {
        if (_timelapseInterval == 0 || _timelapseCount == 0)
            return shootBracketing();
        else
        {
            //TODO: check if timelapse interval isn't too short for current bracketing settings
            return shootTimelapse(true);
        }
    }
}



bool Commander::start()
{
    receiverThr = thread(&Commander::runReceiver, this);
    _receiverThr = thread(&Commander::runReceiver, this);
    this_thread::sleep_for(chrono::milliseconds(50));
    
    if (!isReceiverRunning() || !getToken() || !getCameraSettings())
@@ -315,7 +392,7 @@ bool Commander::start()
void Commander::stop()
{
    _sock.stopReceiver();
    receiverThr.join();
    _receiverThr.join();
}

bool Commander::connect()
@@ -336,8 +413,8 @@ void Commander::disconnect()

bool Commander::isReceiverRunning()
{
    lock_guard lock(receiverRunningMutex);
    return receiverRunning;
    lock_guard lock(_receiverRunningMutex);
    return _receiverRunning;
}

bool Commander::runReceiver()
@@ -348,16 +425,16 @@ bool Commander::runReceiver()
            return false;
        
        {
        lock_guard lock(receiverRunningMutex);
        receiverRunning = true;
        lock_guard lock(_receiverRunningMutex);
        _receiverRunning = true;
        }
        
        if (_sock.runReceiver())
            break;
            
        {
        lock_guard lock(receiverRunningMutex);
        receiverRunning = false;
        lock_guard lock(_receiverRunningMutex);
        _receiverRunning = false;
        }
        
        //if TcpSocket::runReceiver() returns false, reconnect
@@ -366,8 +443,8 @@ bool Commander::runReceiver()
    }
    
    {
    lock_guard lock(receiverRunningMutex);
    receiverRunning = false;
    lock_guard lock(_receiverRunningMutex);
    _receiverRunning = false;
    }
    
    return true;
+19 −7
Original line number Diff line number Diff line
@@ -40,10 +40,10 @@ public:
    * @return true if setting was successful, otherwise false
    */
    bool setBracketing(size_t evStep, size_t halfCount);
    void setTimelapse(size_t interval, size_t count);
    
    bool shootNormal();
    bool shootBracketing();
    bool shootBracketingBuiltIn();
    void stopShooting();//TODO
    bool shoot();
    
    bool getToken();
    bool getCameraSettings();
@@ -57,9 +57,9 @@ public:
    void stop();

private:
    std::thread receiverThr;
    bool receiverRunning;
    std::mutex receiverRunningMutex;
    std::thread _receiverThr;
    bool _receiverRunning;
    std::mutex _receiverRunningMutex;
    
    CustomReader _reader;
    TcpSocket _sock;
@@ -67,19 +67,31 @@ private:
    
    Codes::ISO _iso;
    Codes::Speed _speed;
    
    size_t _bracketingStep;
    size_t _bracketingHalfCount;
    std::vector<Codes::Speed> _bracketingVector;
    
    Codes::ParamBracketingBuiltIn _bracketingBuiltIn;
    
    size_t _timelapseInterval;
    size_t _timelapseCount;
    
    bool _stopShooting; //TODO: reset variable to false before reading new command
    std::mutex _stopShootingMutex;
    std::condition_variable _stopShootingCV;
    
    
    bool connect();
    void disconnect();
    
    void handleNotification(DynamicJsonDocument& json);
    
    bool shootNormal();
    bool shootBracketing();
    bool shootBracketing(const std::vector<Codes::Speed>& speeds);
    bool shootBracketingBuiltIn();
    bool shootTimelapse(bool bracketing);
    
    template <typename T>
    std::string generateJSON(Codes::MsgId msg, T param);
+8 −5
Original line number Diff line number Diff line
@@ -13,11 +13,14 @@ int main(int argc , char *argv[])
    comm.setISO(Codes::ISO::Auto)
            && cout << "====== ISO set ======\n\n" &&
    comm.setSpeed(Codes::Speed::S1_120)
            && cout << "===== Speed set =====\n\n" &&
            && cout << "===== Speed set =====\n\n";
    
    comm.setBracketing(1, 2)
        && cout << "====== Bracketing set =====\n\n" &&
    comm.shootBracketing()
    comm.setBracketing(1, 1)
        && cout << "====== Bracketing set =====\n\n";
    comm.setTimelapse(10, 3);
    cout << "====== Timelapse set =====\n\n";
    
    comm.shoot()
        && cout << "====== Bracketing OK =====\n\n";
    
    /*comm.setBracketingBuiltIn(Codes::ParamBracketingBuiltIn::EV2)
+4 −4
Original line number Diff line number Diff line
@@ -104,7 +104,7 @@ size_t attempts
            continue;
        
        unique_lock ulock(recvQueueMutex);
        if (!recvQueueCondVar.wait_for(ulock, wait, [this](){return !recvQueue.empty();} ))
        if (!recvQueueCV.wait_for(ulock, wait, [this](){return !recvQueue.empty();} ))
            continue; // timeout
        
        while (!recvQueue.empty())
@@ -200,7 +200,7 @@ size_t attempts
            
            
            unique_lock ulock(recvQueueMutex);
            if (!recvQueueCondVar.wait_for(ulock, wait, [this](){return !recvQueue.empty();} ))
            if (!recvQueueCV.wait_for(ulock, wait, [this](){return !recvQueue.empty();} ))
                continue; // timeout

            while (!recvQueue.empty())
@@ -411,7 +411,7 @@ bool TcpSocket::runReceiver()
                    recvQueue.push(jsonPtr);
                    recvExpected.pop();
                    
                    recvQueueCondVar.notify_all();
                    recvQueueCV.notify_all();
                }
            }
        
@@ -423,7 +423,7 @@ bool TcpSocket::runReceiver()
                lock_guard lock2(recvQueueMutex);
                recvQueue.push(jsonPtr);
                
                recvQueueCondVar.notify_all();
                recvQueueCV.notify_all();
            }
        }//unlock revExpectedMutex
    }//while
+1 −1
Original line number Diff line number Diff line
@@ -61,7 +61,7 @@ private:
    std::queue<std::pair<Codes::MsgId,Codes::Rval>> recvExpected;
    std::mutex recvExpectedMutex;
    std::mutex recvQueueMutex;
    std::condition_variable recvQueueCondVar;
    std::condition_variable recvQueueCV;
    
    void handleMsgId(DynamicJsonDocument& json);
    bool sendString(const std::string& msg);