Instrument Neutral Distributed Interface INDI  1.9.2
activefocuser_utils.cpp
Go to the documentation of this file.
1 /*
2  ActiveFocuser driver for Takahashi CCA-250 and Mewlon-250/300CRS
3 
4  Driver written by Alvin FREY <https://afrey.fr> for Optique Unterlinden and Takahashi Europe
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 
20 */
21 
22 #include "activefocuser_utils.h"
23 
24 // Parser method definitions
25 
26 int ActiveFocuserUtils::Parser::Get32(const unsigned char *buffer, int position) {
27 
28  int num = 0;
29  for (int i = 0; i < 4; ++i) {
30  num = num << 8 | buffer[position + i];
31  }
32  return num;
33 
34 }
35 
36 int ActiveFocuserUtils::Parser::Get16(const unsigned char *buffer, int position) {
37 
38  return buffer[position] << 8 | buffer[position + 1];
39 
40 }
41 
43 
44  return ticks * SystemState::GetMmpp();
45 
46 }
47 
49 
50  return (int)(millimeters / SystemState::GetMmpp());
51 
52 }
53 
54 void ActiveFocuserUtils::Parser::PrintFrame(const unsigned char *buffer) {
55 
56  std::stringstream outputStream;
57 
58  for (int i = 0; i < (int) sizeof(buffer); i++) {
59  outputStream << std::hex << (int)buffer[i];
60  }
61 
62  IDLog("%s\r\n", outputStream.str().c_str());
63 
64 }
65 
67 
68  std::stringstream sst;
69 
70  sst << "Current device (v " <<
71  std::to_string(ActiveFocuserUtils::Parser::Get16(buffer, 17) >> 8) << "." << std::to_string(ActiveFocuserUtils::Parser::Get16(buffer, 17) & 0xFF)
72  << ") state : (fan=" << std::to_string((buffer[7] & 32) != 0) <<
73  ", position=" << std::to_string(ActiveFocuserUtils::Parser::Get32(buffer, 2)) <<
74  ", position_mm=" << std::to_string(ActiveFocuserUtils::Parser::TicksToMillimeters(ActiveFocuserUtils::Parser::Get32(buffer, 2))) << ")";
75 
76  IDLog("%s\r\n", sst.str().c_str());
77 
78 }
79 
80 // Poller method definitions
81 
82 ActiveFocuserUtils::Poller *ActiveFocuserUtils::Poller::pinstance_{nullptr};
83 std::mutex ActiveFocuserUtils::Poller::mutex_;
84 std::promise<void> exitSignal;
85 std::future<void> futureObj = exitSignal.get_future();
86 std::promise<void> exitSignalSender;
87 std::future<void> futureObjSender = exitSignalSender.get_future();
88 std::thread th;
89 std::thread thSender;
91 unsigned char poller_buffer[60];
93 
95 
96  std::lock_guard<std::mutex> lock(mutex_);
97  if (pinstance_ == nullptr) {
98  device = &hid_handle;
99  pinstance_ = new ActiveFocuserUtils::Poller(hid_handle);
100  }
101  return pinstance_;
102 
103 }
104 
105 void threaded_sender(std::future<void> futureObjSender) {
106 
107  while (futureObjSender.wait_for(std::chrono::milliseconds(1000)) == std::future_status::timeout) {
108 
109  memset(poller_buffer, 0, sizeof(poller_buffer));
110 
111  const unsigned char data[3] = {0x01, ActiveFocuserUtils::CommandsMap.at(ActiveFocuserUtils::DUMMY)};
112  hid_write(device, data, sizeof(data));
113 
114  if (poller_res < 0)
115  IDLog("Unable to write \r\n");
116 
117  }
118 
119 }
120 
121 void threaded_poller(std::future<void> futureObj) {
122 
123  while (futureObj.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout) {
124 
125  unsigned char buf[256];
126  int res = hid_read(device, buf, sizeof(buf));
127 
128  if (res > 0) {
129  if (buf[0] == 0x3C) {
130 
134  std::stringstream ssVersion;
135  ssVersion << std::to_string(ActiveFocuserUtils::Parser::Get16(buf, 17) >> 8) + "." + std::to_string(ActiveFocuserUtils::Parser::Get16(buf, 17) & 0xFF);
136  ActiveFocuserUtils::SystemState::SetHardwareRevision(const_cast<char *>(ssVersion.str().c_str()));
146 
147  }
148  }
149  if (res < 0) {
150  IDLog("Unable to read \r\n");
151  }
152 
153 
154  }
155 
156 }
157 
159 
160  th = std::thread(&threaded_poller, std::move(futureObj));
161  thSender = std::thread(&threaded_sender, std::move(futureObjSender));
162 
163  IsRunning = true;
164 
165  IDLog("Poller started\r\n");
166 
167  return true;
168 
169 }
170 
172 
173  exitSignal.set_value();
174  exitSignalSender.set_value();
175 
176  th.join();
177  thSender.join();
178 
179  IDLog("Poller stopped\r\n");
180 
181  IsRunning = false;
182 
183  return true;
184 
185 }
186 
187 // SystemState variable definitions
188 
189 static int CurrentPositionStep;
190 static double CurrentPosition;
191 static bool IsOrigin;
192 static bool IsFanOn;
193 static bool IsHold;
194 static bool IsMoving;
195 static char * HardwareRevision;
196 static int Immpp;
197 static int Span;
198 static double Mmpp;
199 static double AirTemperature;
200 static double TubeTemperature;
201 static double MirrorTemperature;
202 
203 // SystemState method definitions
204 
206  return CurrentPositionStep;
207 }
208 
210  CurrentPositionStep = currentPositionStep;
211 }
212 
214  return CurrentPosition;
215 }
216 
218  CurrentPosition = currentPosition;
219 }
220 
221 bool ActiveFocuserUtils::SystemState::GetIsOrigin() {
222  return IsOrigin;
223 }
224 
226  IsOrigin = isOrigin;
227 }
228 
230  return IsMoving;
231 }
232 
234  IsMoving = isMoving;
235 }
236 
238  return IsFanOn;
239 }
240 
242  IsFanOn= isFanOn;
243 }
244 
246  return IsHold;
247 }
248 
250  IsHold = isHold;
251 }
252 
254  return HardwareRevision;
255 }
256 
258  HardwareRevision = hardwareRevision;
259 }
260 
262  return Immpp;
263 }
264 
266  Immpp = immpp;
267 }
268 
270  return Span;
271 }
272 
274  Span = span;
275 }
276 
278  return Mmpp;
279 }
280 
282  Mmpp = mmpp;
283 }
284 
286  return AirTemperature;
287 }
288 
290  AirTemperature = airTemperature;
291 }
292 
294  return TubeTemperature;
295 }
296 
298  TubeTemperature = tubeTemperature;
299 }
300 
302  return MirrorTemperature;
303 }
304 
306  MirrorTemperature = mirrorTemperature;
307 }
308 
309 // Commands enum map
310 
311 const std::map<ActiveFocuserUtils::Commands, unsigned char> ActiveFocuserUtils::CommandsMap =
312  {
313  {ActiveFocuserUtils::Commands::ZERO, 0x03},
314  {ActiveFocuserUtils::Commands::RELEASE, 0x04},
315  {ActiveFocuserUtils::Commands::FREE, 0x06},
316  {ActiveFocuserUtils::Commands::AUTO, 0x07},
317  {ActiveFocuserUtils::Commands::MOVE, 0x09},
318  {ActiveFocuserUtils::Commands::STOP, 0x0A},
319  {ActiveFocuserUtils::Commands::FAN_ON, 0x0B},
320  {ActiveFocuserUtils::Commands::FAN_OFF, 0x0C},
321  {ActiveFocuserUtils::Commands::RESET, 0x7E},
322  {ActiveFocuserUtils::Commands::DUMMY, 0xFF},
323  };
ActiveFocuserUtils::SystemState::GetIsMoving
static bool GetIsMoving()
Definition: activefocuser_utils.cpp:229
th
std::thread th
Definition: activefocuser_utils.cpp:88
thSender
std::thread thSender
Definition: activefocuser_utils.cpp:89
ActiveFocuserUtils::SystemState::SetIsMoving
static void SetIsMoving(bool isMoving)
Definition: activefocuser_utils.cpp:233
ActiveFocuserUtils::SystemState::SetIsFanOn
static void SetIsFanOn(bool isFanOn)
Definition: activefocuser_utils.cpp:241
ActiveFocuserUtils::Parser::PrintBasicDeviceData
static void PrintBasicDeviceData(const unsigned char *buffer)
Definition: activefocuser_utils.cpp:66
hid_write
int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
Write an Output report to a HID device.
Definition: hid_libusb.c:929
activefocuser_utils.h
ActiveFocuserUtils::SystemState::GetCurrentPosition
static double GetCurrentPosition()
Definition: activefocuser_utils.cpp:213
ActiveFocuserUtils::Poller
Definition: activefocuser_utils.h:70
exitSignal
std::promise< void > exitSignal
Definition: activefocuser_utils.cpp:84
ActiveFocuserUtils::SystemState::GetMirrorTemperature
static double GetMirrorTemperature()
Definition: activefocuser_utils.cpp:301
ActiveFocuserUtils::SystemState::GetIsFanOn
static bool GetIsFanOn()
Definition: activefocuser_utils.cpp:237
futureObjSender
std::future< void > futureObjSender
Definition: activefocuser_utils.cpp:87
hid_read
int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
Read an Input report from a HID device.
Definition: hid_libusb.c:1095
ActiveFocuserUtils::Poller::GetInstance
static Poller * GetInstance(hid_device &hid_handle)
Definition: activefocuser_utils.cpp:94
ActiveFocuserUtils::Parser::TicksToMillimeters
static double TicksToMillimeters(int ticks)
Definition: activefocuser_utils.cpp:42
futureObj
std::future< void > futureObj
Definition: activefocuser_utils.cpp:85
poller_buffer
unsigned char poller_buffer[60]
Definition: activefocuser_utils.cpp:91
threaded_sender
void threaded_sender(std::future< void > futureObjSender)
Definition: activefocuser_utils.cpp:105
ActiveFocuserUtils::Parser::Get32
static int Get32(const unsigned char *buffer, int position)
Definition: activefocuser_utils.cpp:26
currentPosition
#define currentPosition
Definition: robofocus.cpp:37
IDLog
void void void void void IDLog(const char *msg,...) ATTRIBUTE_FORMAT_PRINTF(1
Function Drivers call to log a message locally.
ActiveFocuserUtils::SystemState::SetIsHold
static void SetIsHold(bool isHold)
Definition: activefocuser_utils.cpp:249
ActiveFocuserUtils::SystemState::GetSpan
static int GetSpan()
Definition: activefocuser_utils.cpp:269
threaded_poller
void threaded_poller(std::future< void > futureObj)
Definition: activefocuser_utils.cpp:121
hid_device_
Definition: hid_libusb.c:93
device
hid_device * device
Definition: activefocuser_utils.cpp:92
ActiveFocuserUtils::SystemState::GetTubeTemperature
static double GetTubeTemperature()
Definition: activefocuser_utils.cpp:293
ActiveFocuserUtils::CommandsMap
static const std::map< Commands, unsigned char > CommandsMap
Definition: activefocuser_utils.h:53
ActiveFocuserUtils::Parser::Get16
static int Get16(const unsigned char *buffer, int position)
Definition: activefocuser_utils.cpp:36
ActiveFocuserUtils::Poller::Stop
bool Stop()
Definition: activefocuser_utils.cpp:171
ActiveFocuserUtils::SystemState::SetCurrentPositionStep
static void SetCurrentPositionStep(int currentPositionStep)
Definition: activefocuser_utils.cpp:209
ActiveFocuserUtils::SystemState::GetCurrentPositionStep
static int GetCurrentPositionStep()
Definition: activefocuser_utils.cpp:205
ActiveFocuserUtils::SystemState::GetAirTemperature
static double GetAirTemperature()
Definition: activefocuser_utils.cpp:285
ActiveFocuserUtils::SystemState::SetSpan
static void SetSpan(int span)
Definition: activefocuser_utils.cpp:273
exitSignalSender
std::promise< void > exitSignalSender
Definition: activefocuser_utils.cpp:86
ActiveFocuserUtils::SystemState::SetAirTemperature
static void SetAirTemperature(double airTemperature)
Definition: activefocuser_utils.cpp:289
ActiveFocuserUtils::SystemState::SetCurrentPosition
static void SetCurrentPosition(double currentPosition)
Definition: activefocuser_utils.cpp:217
ActiveFocuserUtils::SystemState::SetImmpp
static void SetImmpp(int immpp)
Definition: activefocuser_utils.cpp:265
ActiveFocuserUtils::SystemState::SetIsOrigin
static void SetIsOrigin(bool isOrigin)
Definition: activefocuser_utils.cpp:225
ActiveFocuserUtils::DUMMY
@ DUMMY
Definition: activefocuser_utils.h:50
ActiveFocuserUtils::SystemState::SetMmpp
static void SetMmpp(double mmpp)
Definition: activefocuser_utils.cpp:281
ActiveFocuserUtils::SystemState::GetIsHold
static bool GetIsHold()
Definition: activefocuser_utils.cpp:245
ActiveFocuserUtils::Parser::MillimetersToTicks
static int MillimetersToTicks(double millimeters)
Definition: activefocuser_utils.cpp:48
ActiveFocuserUtils::SystemState::GetMmpp
static double GetMmpp()
Definition: activefocuser_utils.cpp:277
poller_res
int poller_res
Definition: activefocuser_utils.cpp:90
ActiveFocuserUtils::Poller::Start
bool Start()
Definition: activefocuser_utils.cpp:158
ActiveFocuserUtils::SystemState::GetHardwareRevision
static char * GetHardwareRevision()
Definition: activefocuser_utils.cpp:253
ActiveFocuserUtils::SystemState::SetMirrorTemperature
static void SetMirrorTemperature(double mirrorTemperature)
Definition: activefocuser_utils.cpp:305
ActiveFocuserUtils::SystemState::SetHardwareRevision
static void SetHardwareRevision(char *hardwareRevision)
Definition: activefocuser_utils.cpp:257
ActiveFocuserUtils::SystemState::SetTubeTemperature
static void SetTubeTemperature(double tubeTemperature)
Definition: activefocuser_utils.cpp:297
ActiveFocuserUtils::SystemState::GetImmpp
static int GetImmpp()
Definition: activefocuser_utils.cpp:261
ActiveFocuserUtils::Parser::PrintFrame
static void PrintFrame(const unsigned char *buffer)
Definition: activefocuser_utils.cpp:54
Aux::buffer
std::vector< uint8_t > buffer
Definition: celestronauxpacket.h:38