Repository navigation
Expand file tree
/
Copy pathSimObjectiveTurret.h
More file actions
129 lines (108 loc) · 3.58 KB
/
Copy pathSimObjectiveTurret.h
File metadata and controls
129 lines (108 loc) · 3.58 KB
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
// Copyright 2025-2026 Board of Regents of the University of Wisconsin System
// SPDX-License-Identifier: BSD-2-Clause
#pragma once
#include "DeviceBase.h"
#include "SimHub.h"
#include <array>
#include <atomic>
#include <cstddef>
#include <cstdio>
#include <string>
#include <utility>
class SimObjectiveTurret : public CStateDeviceBase<SimObjectiveTurret> {
struct Objective {
int magnification;
double na;
const char *medium;
};
static constexpr std::array<Objective, 6> objectives_ = {{
{10, 0.3, nullptr},
{20, 0.7, nullptr},
{40, 0.75, nullptr},
{40, 1.3, "Oil"},
{60, 1.4, "Oil"},
{100, 1.4, "Oil"},
}};
public:
SimObjectiveTurret(std::string name) : name_(std::move(name)) {
InitializeDefaultErrorMessages();
}
~SimObjectiveTurret() { Shutdown(); }
int Initialize() final {
hub_ = static_cast<SimHub *>(GetParentHub());
if (!hub_) {
return DEVICE_COMM_HUB_MISSING;
}
int ret{};
// create default positions and labels
for (std::size_t i = 0; i < objectives_.size(); ++i) {
const auto &obj = objectives_[i];
char label[32];
if (obj.medium) {
std::snprintf(label, sizeof(label), "%dx %.2fNA %s",
obj.magnification, obj.na, obj.medium);
} else {
std::snprintf(label, sizeof(label), "%dx %.2fNA",
obj.magnification, obj.na);
}
SetPositionLabel(static_cast<long>(i), label);
}
// State
// -----
CPropertyAction *pAct =
new CPropertyAction(this, &SimObjectiveTurret::OnState);
ret = CreateIntegerProperty(MM::g_Keyword_State, 0, false, pAct);
if (ret != DEVICE_OK)
return ret;
// Label
// -----
pAct = new CPropertyAction(this, &CStateBase::OnLabel);
ret = CreateStringProperty(MM::g_Keyword_Label, "", false, pAct);
if (ret != DEVICE_OK)
return ret;
hub_->SetGetMagnificationFunction([this] {
return objectives_[static_cast<std::size_t>(state_.load())]
.magnification;
});
hub_->SetGetNAFunction([this] {
return objectives_[static_cast<std::size_t>(state_.load())].na;
});
return DEVICE_OK;
}
int Shutdown() final {
if (hub_) {
hub_->SetGetMagnificationFunction([] { return 1.0; });
hub_->SetGetNAFunction([] { return 1.0; });
hub_ = nullptr;
}
return DEVICE_OK;
}
void GetName(char *name) const final {
CDeviceUtils::CopyLimitedString(name, name_.c_str());
}
bool Busy() { return false; };
unsigned long GetNumberOfPositions() const {
return static_cast<unsigned long>(objectives_.size());
}
int OnState(MM::PropertyBase *pProp, MM::ActionType eAct) {
if (eAct == MM::BeforeGet) {
pProp->Set(state_.load());
} else if (eAct == MM::AfterSet) {
long newState;
pProp->Get(newState);
if (newState >= 0 &&
newState < static_cast<long>(objectives_.size())) {
state_ = newState;
} else {
pProp->Set(state_.load());
return DEVICE_INVALID_PROPERTY_VALUE;
}
}
return DEVICE_OK;
}
private:
std::string name_;
SimHub *hub_ = nullptr;
// Read by the camera thread via the hub
std::atomic<long> state_{0};
};