Instrument Neutral Distributed Interface INDI  2.0.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 
29  int num = 0;
30  for (int i = 0; i < 4; ++i)
31  {
32  num = num << 8 | buffer[position + i];
33  }
34  return num;
35 
36 }
37 
38 int ActiveFocuserUtils::Parser::Get16(const unsigned char *buffer, int position)
39 {
40 
41  return buffer[position] << 8 | buffer[position + 1];
42 
43 }
44 
46 {
47 
48  return ticks * SystemState::GetMmpp();
49 
50 }
51 
53 {
54 
55  return (int)(millimeters / SystemState::GetMmpp());
56 
57 }
58 
60 {
61 
62  std::stringstream outputStream;
63 
64  for (int i = 0; i < (int) sizeof(buffer); i++)
65  {
66  outputStream << std::hex << (int)buffer[i];
67  }
68 
69  IDLog("%s\r\n", outputStream.str().c_str());
70 
71 }
72 
74 {
75 
76  std::stringstream sst;
77 
78  sst << "Current device (v " <<
80  17) >> 8) << "." << std::to_string(ActiveFocuserUtils::Parser::Get16(buffer, 17) & 0xFF)
81  << ") state : (fan=" << std::to_string((buffer[7] & 32) != 0) <<
84  2))) << ")";
85 
86  IDLog("%s\r\n", sst.str().c_str());
87 
88 }
89 
90 // Poller method definitions
91 
92 ActiveFocuserUtils::Poller *ActiveFocuserUtils::Poller::pinstance_{nullptr};
93 std::mutex ActiveFocuserUtils::Poller::mutex_;
94 std::promise<void> exitSignal;
95 std::future<void> futureObj = exitSignal.get_future();
96 std::promise<void> exitSignalSender;
97 std::future<void> futureObjSender = exitSignalSender.get_future();
98 std::thread th;
99 std::thread thSender;
101 unsigned char poller_buffer[60];
103 
105 {
106 
107  std::lock_guard<std::mutex> lock(mutex_);
108  if (pinstance_ == nullptr)
109  {
110  device = &hid_handle;
111  pinstance_ = new ActiveFocuserUtils::Poller(hid_handle);
112  }
113  return pinstance_;
114 
115 }
116 
117 void threaded_sender(std::future<void> futureObjSender)
118 {
119 
120  while (futureObjSender.wait_for(std::chrono::milliseconds(1000)) == std::future_status::timeout)
121  {
122 
123  memset(poller_buffer, 0, sizeof(poller_buffer));
124 
125  const unsigned char data[3] = {0x01, ActiveFocuserUtils::CommandsMap.at(ActiveFocuserUtils::DUMMY)};
126  hid_write(device, data, sizeof(data));
127 
128  if (poller_res < 0)
129  IDLog("Unable to write \r\n");
130 
131  }
132 
133 }
134 
135 void threaded_poller(std::future<void> futureObj)
136 {
137 
138  while (futureObj.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout)
139  {
140 
141  unsigned char buf[256];
142  int res = hid_read(device, buf, sizeof(buf));
143 
144  if (res > 0)
145  {
146  if (buf[0] == 0x3C)
147  {
148 
152  std::stringstream ssVersion;
154  17) >> 8) + "." + std::to_string(ActiveFocuserUtils::Parser::Get16(buf, 17) & 0xFF);
155  ActiveFocuserUtils::SystemState::SetHardwareRevision(const_cast<char *>(ssVersion.str().c_str()));
166 
167  }
168  }
169  if (res < 0)
170  {
171  IDLog("Unable to read \r\n");
172  }
173 
174 
175  }
176 
177 }
178 
180 {
181 
182  th = std::thread(&threaded_poller, std::move(futureObj));
183  thSender = std::thread(&threaded_sender, std::move(futureObjSender));
184 
185  IsRunning = true;
186 
187  IDLog("Poller started\r\n");
188 
189  return true;
190 
191 }
192 
194 {
195 
196  exitSignal.set_value();
197  exitSignalSender.set_value();
198 
199  th.join();
200  thSender.join();
201 
202  IDLog("Poller stopped\r\n");
203 
204  IsRunning = false;
205 
206  return true;
207 
208 }
209 
210 // SystemState variable definitions
211 
212 static int CurrentPositionStep;
213 static double CurrentPosition;
214 static bool IsOrigin;
215 static bool IsFanOn;
216 static bool IsHold;
217 static bool IsMoving;
218 static char * HardwareRevision;
219 static int Immpp;
220 static int Span;
221 static double Mmpp;
222 static double AirTemperature;
223 static double TubeTemperature;
224 static double MirrorTemperature;
225 
226 // SystemState method definitions
227 
229 {
230  return CurrentPositionStep;
231 }
232 
234 {
235  CurrentPositionStep = currentPositionStep;
236 }
237 
239 {
240  return CurrentPosition;
241 }
242 
244 {
245  CurrentPosition = currentPosition;
246 }
247 
248 bool ActiveFocuserUtils::SystemState::GetIsOrigin()
249 {
250  return IsOrigin;
251 }
252 
254 {
255  IsOrigin = isOrigin;
256 }
257 
259 {
260  return IsMoving;
261 }
262 
264 {
265  IsMoving = isMoving;
266 }
267 
269 {
270  return IsFanOn;
271 }
272 
274 {
275  IsFanOn = isFanOn;
276 }
277 
279 {
280  return IsHold;
281 }
282 
284 {
285  IsHold = isHold;
286 }
287 
289 {
290  return HardwareRevision;
291 }
292 
294 {
295  HardwareRevision = hardwareRevision;
296 }
297 
299 {
300  return Immpp;
301 }
302 
304 {
305  Immpp = immpp;
306 }
307 
309 {
310  return Span;
311 }
312 
314 {
315  Span = span;
316 }
317 
319 {
320  return Mmpp;
321 }
322 
324 {
325  Mmpp = mmpp;
326 }
327 
329 {
330  return AirTemperature;
331 }
332 
334 {
335  AirTemperature = airTemperature;
336 }
337 
339 {
340  return TubeTemperature;
341 }
342 
344 {
345  TubeTemperature = tubeTemperature;
346 }
347 
349 {
350  return MirrorTemperature;
351 }
352 
354 {
355  MirrorTemperature = mirrorTemperature;
356 }
357 
358 // Commands enum map
359 
360 const std::map<ActiveFocuserUtils::Commands, unsigned char> ActiveFocuserUtils::CommandsMap =
361 {
362  {ActiveFocuserUtils::Commands::ZERO, 0x03},
363  {ActiveFocuserUtils::Commands::RELEASE, 0x04},
364  {ActiveFocuserUtils::Commands::FREE, 0x06},
365  {ActiveFocuserUtils::Commands::AUTO, 0x07},
366  {ActiveFocuserUtils::Commands::MOVE, 0x09},
367  {ActiveFocuserUtils::Commands::STOP, 0x0A},
368  {ActiveFocuserUtils::Commands::FAN_ON, 0x0B},
369  {ActiveFocuserUtils::Commands::FAN_OFF, 0x0C},
370  {ActiveFocuserUtils::Commands::RESET, 0x7E},
371  {ActiveFocuserUtils::Commands::DUMMY, 0xFF},
372 };
std::future< void > futureObjSender
std::thread thSender
std::thread th
void threaded_poller(std::future< void > futureObj)
std::future< void > futureObj
std::promise< void > exitSignal
hid_device * device
void threaded_sender(std::future< void > futureObjSender)
unsigned char poller_buffer[60]
std::promise< void > exitSignalSender
int poller_res
static void PrintBasicDeviceData(const unsigned char *buffer)
static int Get32(const unsigned char *buffer, int position)
static double TicksToMillimeters(int ticks)
static int MillimetersToTicks(double millimeters)
static void PrintFrame(const unsigned char *buffer)
static int Get16(const unsigned char *buffer, int position)
static Poller * GetInstance(hid_device &hid_handle)
static void SetAirTemperature(double airTemperature)
static void SetIsOrigin(bool isOrigin)
static void SetTubeTemperature(double tubeTemperature)
static void SetHardwareRevision(char *hardwareRevision)
static void SetMirrorTemperature(double mirrorTemperature)
static void SetIsFanOn(bool isFanOn)
static void SetCurrentPositionStep(int currentPositionStep)
static void SetCurrentPosition(double currentPosition)
static void SetIsMoving(bool isMoving)
static void SetIsHold(bool isHold)
static const std::map< Commands, unsigned char > CommandsMap
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
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
void IDLog(const char *fmt,...)
Definition: indicom.c:316
std::vector< uint8_t > buffer
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.h:23613
#define currentPosition
Definition: robofocus.cpp:37