darker
2025-02-20 0b8c9e0701a300eea0b85c4bb775b64785178530
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "stdafx.h"
#include "Common.h"
#include "CMaster.h"
 
namespace SERVO {
    CMaster* g_pMaster = NULL;
    void CALLBACK MasterTimerProc(HWND hWnd, UINT nMsg, UINT nTimerid, DWORD dwTime)
    {
        if (g_pMaster != NULL) {
            g_pMaster->onTimer(nTimerid);
        }
    }
 
    CMaster::CMaster()
    {
        m_listener = {nullptr, nullptr};
    }
 
    CMaster::~CMaster()
    {
        for (auto item : m_listEquipment) {
            delete item;
        }
        m_listEquipment.clear();
    }
 
    void CMaster::setListener(MasterListener listener)
    {
        m_listener.onEqAlive = listener.onEqAlive;
        m_listener.onEqCimStateChanged = listener.onEqCimStateChanged;
    }
 
    int CMaster::init()
    {
        LOGI("<Master>ÕýÔÚ³õʼ»¯...");
 
 
        //     cclink
        if (m_cclink.Connect(CC_LINK_IE_CONTROL_CHANNEL(1)) != 0) {
            LOGE("Á¬½ÓCC-Linkʧ°Ü.");
        }
        else {
            LOGI("Á¬½ÓCC-Link³É¹¦.");
            BoardVersion version{};
            int nRet = m_cclink.GetBoardVersion(version);
            if (nRet == 0) {
                LOGI("°æ±¾ÐÅÏ¢£º%s.", version.toString().c_str());
            }
            else {
                LOGE("»ñÈ¡CC-Link°æ±¾ÐÅϢʧ°Ü.");
            }
 
            BoardStatus status;
            nRet = m_cclink.GetBoardStatus(status);
            if (nRet == 0) {
                LOGI("״̬£º%s.", status.toString().c_str());
            }
            else {
                LOGE("»ñÈ¡CC-Link״̬ʧ°Ü.");
            }
        }
 
 
        // ³õʼ»¯Ìí¼Ó¸÷×ÓÉ豸
        {
            CEFEM* pEquipment = new CEFEM();
            pEquipment->setID(EQ_ID_EFEM);
            pEquipment->setName("EFEM(ROBOT)");
            pEquipment->setDescription("EFEM(ROBOT).");
            pEquipment->setReadBitBlock(0x4000, 0x45ff);
            pEquipment->setStation(0, 255);
            addEquipment(pEquipment);
 
            pEquipment->init();
            LOGE("ÒÑÌí¼Ó¡°EFEM(ROBOT)¡±.");
        }
        /*
        {
            CBonder* pBonder = new CBonder();
            pBonder->setName("Bonder 1");
            pBonder->setDescription("Bonder 1.");
            pBonder->setReadBitBlock(0x4600, 0x4bff);
            pBonder->setStation(1, 3);
            addEquipment(pBonder);
            LOGE("ÒÑÌí¼Ó¡°Bonder 1¡±.");
        }
        */
 
        // ¶¨Ê±Æ÷
        g_pMaster = this;
        SetTimer(NULL, 1, 250, (TIMERPROC)MasterTimerProc);
 
 
        LOGI("<Master>³õʼ»¯Íê³É.");
        return 0;
    }
 
    int CMaster::term()
    {
        LOGI("<Master>ÕýÔÚ½áÊø³ÌÐò.");
        for (auto item : m_listEquipment) {
            item->term();
        }
 
        return 0;
    }
 
    int CMaster::addEquipment(CEquipment* pEquipment)
    {
        EquipmentListener listener;
        listener.onAlive = [&](void* pEquipment, BOOL bAlive) -> void {
            CEquipment* p = (CEquipment*)pEquipment;
            if (m_listener.onEqAlive != nullptr) {
                m_listener.onEqAlive(this, p, bAlive);
            }
        };
        listener.onCimStateChanged = [&](void* pEquipment, BOOL bOn) -> void {
            CEquipment* p = (CEquipment*)pEquipment;
            if (m_listener.onEqCimStateChanged != nullptr) {
                m_listener.onEqCimStateChanged(this, p, bOn);
            }
        };
        pEquipment->setListener(listener);
        pEquipment->setCcLink(&m_cclink);
        m_listEquipment.push_back(pEquipment);
 
        return 0;
    }
 
    CEquipment* CMaster::getEquipment(int id)
    {
        for (auto item : m_listEquipment) {
            if (item->getID() == id) return item;
        }
 
        return nullptr;
    }
 
    void CMaster::onTimer(UINT nTimerid)
    {
        for (auto item : m_listEquipment) {
            item->onTimer(nTimerid);
        }
 
 
        // °´Ò»¶¨ÆµÂÊɨÃèLBÊý¾Ý
        static int i = 0;
        i++;
        if (i % (4 * 1) == 0) {
 
            for (auto item : m_listEquipment) {
                const StationIdentifier& station = item->getStation();
                MemoryBlock& block = item->getReadBitBlock();
 
                int nRet = m_cclink.ReadData2(station, (DeviceType)block.type,
                    block.start, block.size, block.buffer);
                if (0 == nRet) {
                    item->onReceiveLBData(block.buffer, block.size);
                }
            }
        }
    }
}