File: ApogeeFilterWheel.cpp

package info (click to toggle)
libapogee3 3.2%2B20221221183454-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,284 kB
  • sloc: cpp: 26,737; sh: 8; makefile: 3
file content (230 lines) | stat: -rw-r--r-- 5,816 bytes parent folder | download | duplicates (2)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*! 
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright(c) 2011 Apogee Imaging Systems, Inc. 
* \class ApogeeFilterWheel 
* \brief class for apogee's usb filter wheel 
* 
*/ 

#include "ApogeeFilterWheel.h" 

#include "helpers.h" 

#include "FilterWheelIo.h" 
#include "apgHelper.h" 
#include "helpers.h" 
#include "CamHelpers.h" 
#include "ApgLogger.h" 

#include <map>
#include <sstream>

namespace
{
    

    struct FwInfo
    {
        ApogeeFilterWheel::Type type;
        std::string name;
        uint16_t maxPositions;
    };

    std::map<ApogeeFilterWheel::Type, FwInfo> GetInfoMap()
    {
        std::map<ApogeeFilterWheel::Type, FwInfo>  result;
        FwInfo i = { ApogeeFilterWheel::FW50_7S, "AI FW50 7S", 7 };
        result[ ApogeeFilterWheel::FW50_7S ] = i;

        FwInfo j = { ApogeeFilterWheel::FW50_9R, "AI FW50 9R", 9 };
        result[ ApogeeFilterWheel::FW50_9R ] = j;

        FwInfo k = { ApogeeFilterWheel::AFW50_10S, "AI AFW50 10S", 10 };
        result[ ApogeeFilterWheel::AFW50_10S ] = k;

        FwInfo l = { ApogeeFilterWheel::UNKNOWN_TYPE, "Unknown", 0 };
        result[ ApogeeFilterWheel::UNKNOWN_TYPE ] = l;

        FwInfo m = { ApogeeFilterWheel::AFW31_17R, "AI AFW31 17R", 17 };
        result[ ApogeeFilterWheel::AFW31_17R ] = m;

        return result;
    }

    FwInfo GetInfo( const ApogeeFilterWheel::Type type )
    {
        std::map<ApogeeFilterWheel::Type, FwInfo> filterMap = GetInfoMap();
        std::map<ApogeeFilterWheel::Type, FwInfo>::iterator iter =
                    filterMap.find( type);

        if( iter != filterMap.end() )
        {
                return (*iter).second;
        }

        return filterMap[ ApogeeFilterWheel::UNKNOWN_TYPE ];
    }

}

//////////////////////////// 
// CTOR 
ApogeeFilterWheel::ApogeeFilterWheel() : m_type( ApogeeFilterWheel::UNKNOWN_TYPE ),
                                           m_connected( false )
{ 

} 

//////////////////////////// 
// DTOR 
ApogeeFilterWheel::~ApogeeFilterWheel() 
{ 
    Close();
} 

//////////////////////////// 
//      INIT
void ApogeeFilterWheel::Init( const ApogeeFilterWheel::Type type, 
            const std::string & DeviceAddr )
{
    if( ApogeeFilterWheel::UNKNOWN_TYPE == type )
    {
        apgHelper::throwRuntimeException( __FILE__, 
            "Invalid input filter wheel type", __LINE__,
            Apg::ErrorType_InvalidUsage );
    }

    m_Usb =  std::shared_ptr<FilterWheelIo>(new FilterWheelIo( DeviceAddr ) );
    m_type = type;
    m_connected = true;

    std::stringstream msg;
    msg << "Successfully connected to filter wheel " << m_type << " at address " << DeviceAddr.c_str();

    ApgLogger::Instance().Write( ApgLogger::LEVEL_RELEASE,"info",msg.str() ); 
}

//////////////////////////// 
//    CLOSE
void ApogeeFilterWheel::Close()
{
    if( IsConnected() )
    {
        std::stringstream msg;
        msg << "Closing connection to filter wheel " << m_type;

        ApgLogger::Instance().Write( ApgLogger::LEVEL_RELEASE,"info",msg.str() ); 

        m_Usb.reset();
        m_connected = false;
        m_type = ApogeeFilterWheel::UNKNOWN_TYPE;
    }
}

//////////////////////////// 
//      GET     VENDOR         ID
uint16_t ApogeeFilterWheel::GetVendorId()
{
  return m_Usb->GetVendorId();
}
   
//////////////////////////// 
//      GET     PRODUCT         ID
uint16_t ApogeeFilterWheel::GetProductId()
{
    return m_Usb->GetProductId();
}

//////////////////////////// 
//      GET     DEVICE        ID
uint16_t ApogeeFilterWheel::GetDeviceId()
{
    return m_Usb->GetDeviceId();
}

//////////////////////////// 
//      GET        USB     FIRMWARE        REV
std::string ApogeeFilterWheel::GetUsbFirmwareRev()
{
    return m_Usb->GetUsbFirmwareRev();
}

//////////////////////////// 
//          GET    TYPE       NAME
std::string	ApogeeFilterWheel::GetName()
{
    FwInfo info = GetInfo( m_type );
    return info.name;
}

//////////////////////////// 
//          GET    MAX     POSITIONS
uint16_t ApogeeFilterWheel::GetMaxPositions()
{
    FwInfo info = GetInfo( m_type );
    return info.maxPositions;
}

//////////////////////////// 
//          GET     STATUS
ApogeeFilterWheel::Status ApogeeFilterWheel::GetStatus()
{
    if( !IsConnected() )
    {
        return ApogeeFilterWheel::NOT_CONNECTED;
    }

    uint8_t control = 0, pin = 0;
    m_Usb->ReadCtrlPort( control, pin );

    const uint8_t ACTIVE_MASK = 0x01;

    return(  (pin & ACTIVE_MASK) ? ApogeeFilterWheel::ACTIVE : ApogeeFilterWheel::READY );
}

//////////////////////////// 
//      SET     POSITION
void	ApogeeFilterWheel::SetPosition( const uint16_t Position )
{
    if( Position < 1 ) 
	{
		apgHelper::throwRuntimeException( __FILE__, 
            "Cannot set filter to position 0", __LINE__,
            Apg::ErrorType_InvalidUsage );
	}

    if( Position > GetMaxPositions() ) 
	{
		apgHelper::throwRuntimeException( __FILE__, 
            "Input filter position greater than max positions available", 
            __LINE__, Apg::ErrorType_InvalidUsage );
	}

    // hardware is zero-based
    // ui is one based that is why there is a minus 1
    const uint8_t control = help::GetLowByte( Position ) - 1;
    m_Usb->WriteCtrlPort( control, 0 );
}

//////////////////////////// 
//      GET     POSITION
uint16_t ApogeeFilterWheel::GetPosition()
{
     uint8_t control = 0, pin = 0;
    m_Usb->ReadCtrlPort( control, pin );

     // hardware is zero-based
     // ui is one based that is why there is a plus 1
    uint16_t result = (control & 0x0F) + 1;
    return result;
}

//////////////////////////// 
//  IS     CONNECTED
bool ApogeeFilterWheel::IsConnected()
{
    return m_connected;
}