//
// Created by zx on 2019/8/19.
//

#ifndef CP16CARD_H
#define CP16CARD_H
extern "C"
{
    #include "dev_certify_energy.h"
};

#include <mutex>
#include <condition_variable>
class Semaphore {
public:
    explicit Semaphore() : count_(0) {
    }

    void Signal() {
        std::unique_lock<std::mutex> lock(mutex_);
        ++count_;
        cv_.notify_one();
    }

    void Wait() {
        std::unique_lock<std::mutex> lock(mutex_);
        cv_.wait(lock, [=] { return count_ > 0; });
        --count_;
    }

private:
    std::mutex mutex_;
    std::condition_variable cv_;
    int count_;
};

#include <thread>
class CP16Card
{
protected:
    static bool m_quit;
    static device_data_t m_write_data;
    static device_data_t m_read_data;
public:
    CP16Card();
    ~CP16Card();
    static bool Init();
    static bool UnInit();
    void WriteData(PNIO_UINT32 submodel,float data);
    float ReadData(PNIO_UINT32 submodel);

protected:
    static PNIO_IOXS PNIO_cbf_data_write(
        PNIO_UINT32    DevHndl,            /* [in] Handle for device */
        PNIO_DEV_ADDR* pAddr,              /* [in] geographical address def. in 'pniobase.h' */
        PNIO_UINT32    BufLen,             /* [in] length of the submodule input data */
        PNIO_UINT8*    pBuffer,            /* [out] Ptr to data buffer to write to */
        PNIO_IOXS      Iocs);

    static PNIO_IOXS PNIO_cbf_data_read(
        PNIO_UINT32    DevHndl,            /* [in] Handle for Multidevice */
        PNIO_DEV_ADDR* pAddr,              /* [in] geographical address */
        PNIO_UINT32    BufLen,             /* [in] length of the submodule input data */
        PNIO_UINT8*    pBuffer,            /* [in] Ptr to data buffer to read from */
        PNIO_IOXS      Iops);             /* [in] (io controller) provider status */
};


#endif //CP16CARD_H
