RelianceCore  1.4.0
Reliance Thermal Printer API
RelianceHID.hpp
Go to the documentation of this file.
1 /**
2  * @copyright Pyramid Technologies Inc. 2017
3  * @defgroup API RelianceCore API
4  * @author Cory Todd
5  * @date 9/19/2017
6  * @brief Required header for integration into your project
7  *
8  * @mainpage
9  * RelianceCore API is a native C++ library for monitoring your Reliance Thermal Printer.
10  * This library support status checks and a number of configuration options.
11  *
12  * Getting started requires 3 files:
13  *
14  * - RelianceHID.dll
15  * - RelianceHID.lib
16  * - RelianceHID.hpp
17  *
18  * 1. Place the DLL where your app can find it at run time
19  * 2. Link the library during compilation time
20  * 3. Include the header in your project
21  *
22  * Sample usage:
23  * @code
24  * ...
25  * #include <iostream>
26  * #include <RelianceHID.hpp>
27  *
28  * // Use namespace for brevity
29  * using namespace reliancecore;
30  * ...
31  *
32  * // Create an initialize your printer
33  * RelianceHID printer;
34  * auto result = printer.initialize();
35  *
36  * // result holds the initialization success/error messages
37  * std::cout << result.message.c_str() << std::endl;
38  *
39  * // Check the status
40  * RelianceHID::RelianceStatus status;
41  * result = printer.getStatus(&status);
42  *
43  * // result holds the command success/error messages
44  * std::cout << result.message.c_str() << std::endl;
45  *
46  * if(status.ret == RelianceHID::ReturnCode::Okay)
47  * {
48  * // explore the status struct, this contains complete status
49  * ...
50  * }
51  *
52  * @endcode
53  *
54  * Please contact <mailto:support@pyramidacceptors.com> to request the right DLL version
55  * for your system.
56  *
57  */
58 
59 #pragma once
60 #include <vector>
61 #include <string>
62 #include <memory>
63 #include <cstdint>
64 
65 #if defined (_WIN32)
66 #if defined(RelianceCore_EXPORTS)
67 /// DLL Export macro
68 #define RELIANCECORE_EXPORT __declspec(dllexport)
69 #else
70 /// DLL Export macro
71 #define RELIANCECORE_EXPORT __declspec(dllimport)
72 #endif /* RelianceCore_EXPORTS */
73 #else /* defined (_WIN32) */
74 /// DLL Export macro
75 #define RELIANCECORE_EXPORT
76 #endif
77 
78 /**
79  * @brief API Revision Level
80  */
81 #define RELIANCE_HID_VERSION ("1.4.0")
82 
83 #if !defined(NAMESPACE_BEGIN) || defined(DOXYGEN_DOCUMENTATION_BUILD)
84 /**
85 * @brief Convenience macro for namespace declarations
86 *
87 * The macro ``NAMESPACE_BEGIN(reliancecore)`` will expand to ``namespace
88 * reliancecore {``. This is done to hide the namespace scope from editors and
89 * C++ code formatting tools that may otherwise indent the entire file.
90 * The corresponding ``NAMESPACE_END`` macro also lists the namespace
91 * name for improved readability.
92 *
93 * @param name The name of the namespace scope to open
94 */
95 #define NAMESPACE_BEGIN(name) namespace name {
96 #endif
97 #if !defined(NAMESPACE_END) || defined(DOXYGEN_DOCUMENTATION_BUILD)
98 /**
99 * @brief Convenience macro for namespace declarations
100 *
101 * Closes a namespace (counterpart to ``NAMESPACE_BEGIN``)
102 * ``NAMESPACE_END(reliancecore)`` will expand to only ``}``.
103 *
104 * @param name The name of the namespace scope to close
105 */
106 #define NAMESPACE_END(name) }
107 #endif
108 
109 NAMESPACE_BEGIN(reliancecore)
110 
111 /**
112  *@class RelianceHID
113  *
114 * @brief Primary interface for interacting with Reliance Thermal Printer
115 *
116 * @ingroup API
117 */
118 class RelianceHID {
119 
120  RelianceHID(RelianceHID &) = delete;
121  RelianceHID(const RelianceHID &&) = delete;
122 
123 public:
124  /**
125  * @brief List of string arguments
126  *
127  * @ingroup API
128  */
129  using ArgList = std::vector<std::string>;
130 
131  /**
132  * @struct RelianceStatus
133  *
134  * @ingroup API
135  *
136  * @brief Status response from Reliance printer
137  */
138  struct RelianceStatus {
139 
140  ///! Constructor
141  RelianceStatus() = default;
142  ///! Destructor
143  ~RelianceStatus() = default;
144 
145  /**
146  * @enum SensorStatus
147  *
148  * @ingroup API
149  *
150  * @brief Bit enumeration describes the state of each sensor
151  */
152  enum class SensorStatus : uint8_t {
153  platenOn = 1 << 0, ///< - 0: Platen off, - 1: Platen on
154  cutterHome = 1 << 1, ///< - 0: Cuter not home, - 1: Cutter home
155  tachOk = 1 << 2, ///< - 0: Tach okay, - 1: Tach error (ALWAYS 0)
156  presenterPaperPres = 1 << 3, ///< - 0: Paper not present - 1: Paper present
157  pathPaperPres = 1 << 4, ///< - 0: Paper not present - 1: Paper present
158  paperPaperPres = 1 << 5, ///< - 0: Paper not present - 1: Paper present
159  notchPres = 1 << 6, ///< - 0: Notch not present - 1: Notch present/detected
160  armPaperPres = 1 << 7, ///< - 0: Paper arm not installed - 1: Paper arm installed
161  };
162 
163  /**
164  * @enum ErrorStatus
165  *
166  * @ingroup API
167  *
168  * @brief Bit enumberation describes each error state that can be reported by Reliance
169  */
170  enum class ErrorStatus : uint8_t {
171  isJammed = 1 << 0, ///< - 0: No jam, - 1: Jam has disabled printer
172  isOverheated = 1 << 1, ///< - 0: Normal system temp, - 1: Printer is overheated and disabled
173  isCutterErr = 1 << 2, ///< - 0: Cutter is okay, - 1: Cutter is in an error staes (e.g. stuck up)
174  isOverVoltage = 1 << 3, ///< - 0: System voltage okay, - 1: System over voltage
175  isUnderVoltage = 1 << 4, ///< - 0: System voltage okay, - 1: System under voltage
176  isPlatenOpen = 1 << 5, ///< - 0: Platen (head) is closed, - 1: Platen (head) open
177  _reserved = 1 << 6, ///< Reserved
178  isUnknownErr = 1 << 7, ///< - 0: No unknown error, -1: Has an error, we don't know what
179 
180  };
181 
182  /**
183  * @enum TicketState
184  *
185  * @ingroup API
186  *
187  * @brief Reliance will always be in one of these states
188  */
189  enum class TicketState : uint8_t {
190  Idle = 0, ///< Printer is not moving paper, no ticket in bezel
191  Printing, ///< Printer is actively printing on paper
192  Presenting, ///< Printing is complete and paper is in transit
193  Presented ///< Printing is complete, transit is complete, paper is at bezel
194  };
195 
196  uint8_t headVoltage[6]; ///< Print head voltage
197  uint8_t headTempC; ///< Temperature of head, degrees C
198 
199 
200  SensorStatus sensorStatus; ///< Sensor status flags
201 
202 
203  ErrorStatus errorStatus; ///< Error status flags
204 
205 
206  TicketState ticketState; ///< Ticket status flag
207 
208  /// Raw voltages - ADC 12-bit
209  uint16_t presenterADC; ///< Presenter sensor 12-bit ADC value
210  uint16_t pathRawADC; ///< Path sensor 12-bit ADC value
211  uint16_t paperADC; ///< Paper sensor 12-bit ADC value
212  uint16_t notchADC; ///< Notch sensor 12-bit ADC value
213  uint16_t armADC; ///< ARM sensor 12-bit ADC value
214 
215 
216  /**
217  * @brief Helper function to check if specified flag is set
218  *
219  * @ingroup API
220  *
221  * @param flag to check
222  *
223  * @return true if current SensorStatus contains flag
224  */
225  bool hasSensorFlag(SensorStatus flag);
226 
227 
228  /**
229  * @brief Helper function to check if specified flag is set
230  *
231  * @ingroup API
232  *
233  * @param flag to check
234  *
235  * @return true if current ErrorStatus contains flag
236  */
237  bool hasErrorFlag(ErrorStatus flag);
238 
239 
240  /**
241  * @brief Const function method to check if specified flag is set
242  *
243  * @ingroup API
244  *
245  * @param flag to check
246  *
247  * @return true if current SensorStatus contains flag
248  */
249  bool hasSensorFlag(SensorStatus flag) const;
250 
251 
252  /**
253  * @brief Const function method to check if specified flag is set
254  *
255  * @ingroup API
256  *
257  * @param flag to check
258  *
259  * @return true if current ErrorStatus contains flag
260  */
261  bool hasErrorFlag(ErrorStatus flag) const;
262 
263  /**
264  * @brief Helper function returns true if status report indicates paper low
265  *
266  * @ingroup API
267  *
268  * @warning If no arm is attached to Reliance, the response will always be true
269  * regardless of paper level.
270  *
271  * @return true if paper low. If false, no paper low warnings are present
272  */
273  bool isPaperLow();
274 
275  /**
276  * @brief Helper function returns true if status report indicates paper is out
277  *
278  * @ingroup API
279  *
280  * @return true if there is no paper. If false, paper is present
281  */
282  bool isPaperOut();
283 
284  /**
285  * @brief Returns the voltage given a raw ADC reading. This
286  * assumes a 12-bit ADC
287  *
288  * @ingroup API
289  */
290  static float adc2Voltage(uint16_t adc);
291  };
292 
293  /**
294  * @enum ReturnCode
295  *
296  * @ingroup API
297  *
298  * @brief Quick return code
299  */
300  enum class ReturnCode {
301  Okay, ///< Successful command
302  Timeout, ///< No response within timeout period
303  BadResp, ///< Response received but it was corrupt
304  BadArg, ///< Invalid argument provided to command builder
305  DeviceNXE, ///< No device found
306  NoInit, ///< Device is not initialized
307  };
308 
309  /**
310  * @struct Result
311  *
312  * @ingroup API
313  *
314  * @brief Describes response from all RelianceHID printer commands
315  */
316  struct Result {
317  ReturnCode ret; ///< For command execution
318  ArgList args; ///< List of parameters passed with command, if any
319  std::string command;///< Name of command
320  std::string message;///< Parsed message may contain more info about errors or results
321  int value = 0; ///< Used for commands that return integer values
322  };
323 
324  /**
325  * @struct Revision
326  *
327  * @ingroup API
328  *
329  * @brief Describes a firmware revision levels using semver syntax
330  */
331  struct Revision {
332  int major; ///< Major revision, e.g. 1.x.x
333  int minor; ///< Minor revision, e.g. x.1.x
334  int build; ///< Build revision, e.g. x.x.1
335  };
336 
337  /**
338  * @brief Create instance of Reliance printer and bind to specified serial number
339  *
340  * @param sn serial number to attach to. If not provided, this connects to first available
341  * reliance.
342  */
343  RELIANCECORE_EXPORT explicit RelianceHID(const std::string &sn = "");
344  /**
345  * @brief Destructor
346  */
347  RELIANCECORE_EXPORT ~RelianceHID();
348 
349  /**
350  * @brief Attempt to connect to device
351  *
352  * @ingroup API
353  *
354  * @warning This must be called before executing any commands
355  *
356  * @return ret and message fields will be set with additional information
357  */
358  RELIANCECORE_EXPORT Result initialize();
359 
360  /**
361  * @brief Read firmware revision from target
362  *
363  * @ingroup API
364  *
365  * @param rev out parameter
366  *
367  * @return ret and message fields will be set with additional information
368  */
369  RELIANCECORE_EXPORT Result getRevision(Revision *rev);
370 
371  /**
372  * @brief Side effect free command to test for presence of printer
373  *
374  * @ingroup API
375  *
376  * @return ret and message fields will be set with additional information
377  */
379 
380  /**
381  * @brief Immediately perform a hard reboot of the printer
382  *
383  * @ingroup API
384  *
385  * @warning You must call initialize after a reboot
386  *
387  * @return ret and message fields will be set with additional information
388  */
389  RELIANCECORE_EXPORT Result reboot();
390 
391  /**
392  * @brief Get the current status of Reliance printer
393  *
394  * @ingroup API
395  *
396  * @param status will be written with current status
397  *
398  * @return ret and message fields will be set with additional information
399  */
400  RELIANCECORE_EXPORT Result getStatus(RelianceStatus *status);
401 
402  /**
403  * @brief Gets the state of the USB virtual comm port feature
404  *
405  * @ingroup API
406  *
407  * @details When enabled, Reliance will enumerate an additional USB
408  * interface as a CDC comm port. Check device manager or /dev/tty*
409  * to find the OS assigned port name.
410  *
411  * @return Result.value -
412  * 0 - Off (disabled)
413  * 1 - On (enabled)
414  */
415  RELIANCECORE_EXPORT Result getVirtualCommsEnabled();
416 
417  /**
418  * @brief Enable or disable the USB virtual comm port feature
419  *
420  * @ingroup API
421  *
422  * @details When enabled, Reliance will enumerate an additional USB
423  * interface as a CDC comm port. Check device manager or /dev/tty*
424  * to find the OS assigned port name.
425  *
426  * @note A reboot of the printer is required in order for this to take effect
427  *
428  * @see RelianceHID#reboot
429  *
430  * @param enabled true or false
431  *
432  * @return ret and message fields will be set with additional information
433  */
434  RELIANCECORE_EXPORT Result setVirtualCommsEnabled(bool enabled);
435 
436  /**
437  * @brief Gets the state of the startup ticket feature
438  *
439  * @ingroup API
440  *
441  * @details When enabled, Reliance will print its configuration to a ticket
442  * on every power up. When disabled, this behavior is disabled.
443  *
444  * @note This only controls the startup ticket. The ticket that is printed when
445  * you feed in paper cannot be disabled.
446  *
447  * @return ret and message fields will be set with additional information
448  */
449  RELIANCECORE_EXPORT Result getStartupTicketEnabled();
450 
451  /**
452  * @brief Sets the state of the startup ticket feature
453  *
454  * @ingroup API
455  *
456  * @note When enabled, Reliance will print its configuration to a ticket
457  * on every power up. When disabled, this behavior is disabled.
458  *
459  * @note This only controls the startup ticket. The ticket that is printed when
460  *
461  * @param enabled true or false
462  *
463  * @return ret and message fields will be set with additional information
464  */
465  RELIANCECORE_EXPORT Result setStartupTicketEnabled(bool enabled);
466 
467  /**
468  * @brief Returns a list of ESC/POS commands the Reliance does not understand
469  *
470  * @ingroup API
471  *
472  * @details The list will be written to dst in the order cmd:arg where cmd is
473  * a known ESC/POS prefix such as ESC, FS, or GS (1B, 1C, 1D) and the arg is the first
474  * given parmeter. This is useful for legacy applications that send uncommon or customer
475  * ESC/POS commands to their printers.
476  *
477  * @param dst vector to write log to
478  *
479  * @return ret and message fields will be set with additional information
480  */
481  RELIANCECORE_EXPORT Result getUnknownCommands(std::vector<int> &dst);
482 
483 
484  /**
485  * @brief Returns the current configured paper roll width
486  *
487  * @ingroup API
488  *
489  * @details Reliance supports paper rolls width from 58mm though 80mm. This
490  * setting enables proper print margins for your chosen roll width.
491  *
492  * @return ret and message fields will be set with additional information.
493  * Result#value set to paper width in mm
494  */
495  RELIANCECORE_EXPORT Result getPaperWidth();
496 
497  /**
498  * @brief Change the paper roll width configuration
499  *
500  * @ingroup API
501  *
502  * @param width value between 58 and 80, inclusive
503  *
504  * @return ret and message fields will be set with additional information
505  */
506  RELIANCECORE_EXPORT Result setPaperWidth(int width);
507 
508  /**
509  * @brief Returns the current motor configuration
510  *
511  * @ingroup API
512  *
513  * @details There are currently two motors, gen1 and gen2. The returned result
514  * will populate the value field with 0, 1, etc. which represent these motor gens.
515  *
516  * @return ret and message fields will be set with additional information.
517  * Result#value set to motor generation code
518  */
519  RELIANCECORE_EXPORT Result getMotorGen();
520 
521  /**
522  * @brief Change the motor configuration to another gen
523  *
524  * @ingroup API
525  *
526  * @warning This function should not be called under normal conditions. This
527  * is for service technicians that may be replacing motors in older units.
528  *
529  * @param gen 0 for gen1, 1 for gen 2
530  *
531  * @return ret and message fields will be set with additional information
532  */
533  RELIANCECORE_EXPORT Result setMotorGen(int gen);
534 
535  /**
536  * @brief Returns the current motor current in milliamps
537  *
538  * @ingroup API
539  *
540  * @details This is the current that is applied to printer motor.
541  *
542  * @return ret and message fields will be set with additional information.
543  * Result#value set to motor current value
544  */
545  RELIANCECORE_EXPORT Result getMotorCurrent();
546 
547  /**
548  * @brief Change the motor current
549  *
550  * @ingroup API
551  *
552  * @warning This function should not be called under normal conditions. This
553  * is for service technicians that may be replacing motors in older units.
554  *
555  * @param current milliamps to apply to printer motor (min: 50, max: 1500)
556  *
557  * @return ret and message fields will be set with additional information
558  */
559  RELIANCECORE_EXPORT Result setMotorCurrent(int current);
560 
561 private:
562 
563  struct Impl; ///< Internal implementation
564  std::unique_ptr<Impl> impl;
565 };
566 NAMESPACE_END(reliancecore)
SensorStatus sensorStatus
Sensor status flags.
Definition: RelianceHID.hpp:200
uint16_t pathRawADC
Path sensor 12-bit ADC value.
Definition: RelianceHID.hpp:210
int major
Major revision, e.g. 1.x.x.
Definition: RelianceHID.hpp:332
std::string message
Parsed message may contain more info about errors or results.
Definition: RelianceHID.hpp:320
Status response from Reliance printer.
Definition: RelianceHID.hpp:138
Describes a firmware revision levels using semver syntax.
Definition: RelianceHID.hpp:331
ErrorStatus errorStatus
Error status flags.
Definition: RelianceHID.hpp:203
ReturnCode ret
For command execution.
Definition: RelianceHID.hpp:317
int build
Build revision, e.g. x.x.1.
Definition: RelianceHID.hpp:334
SensorStatus
Bit enumeration describes the state of each sensor.
Definition: RelianceHID.hpp:152
uint16_t armADC
ARM sensor 12-bit ADC value.
Definition: RelianceHID.hpp:213
uint16_t paperADC
Paper sensor 12-bit ADC value.
Definition: RelianceHID.hpp:211
Describes response from all RelianceHID printer commands.
Definition: RelianceHID.hpp:316
#define RELIANCECORE_EXPORT
DLL Export macro.
Definition: RelianceHID.hpp:75
#define NAMESPACE_BEGIN(name)
Convenience macro for namespace declarations.
Definition: RelianceHID.hpp:95
#define NAMESPACE_END(name)
Convenience macro for namespace declarations.
Definition: RelianceHID.hpp:106
std::string command
Name of command.
Definition: RelianceHID.hpp:319
int minor
Minor revision, e.g. x.1.x.
Definition: RelianceHID.hpp:333
std::vector< std::string > ArgList
List of string arguments.
Definition: RelianceHID.hpp:129
uint16_t presenterADC
Raw voltages - ADC 12-bit.
Definition: RelianceHID.hpp:209
Primary interface for interacting with Reliance Thermal Printer.
Definition: RelianceHID.hpp:118
TicketState
Reliance will always be in one of these states.
Definition: RelianceHID.hpp:189
ReturnCode
Quick return code.
Definition: RelianceHID.hpp:300
ErrorStatus
Bit enumberation describes each error state that can be reported by Reliance.
Definition: RelianceHID.hpp:170
ArgList args
List of parameters passed with command, if any.
Definition: RelianceHID.hpp:318
uint8_t headTempC
Temperature of head, degrees C.
Definition: RelianceHID.hpp:197
TicketState ticketState
Ticket status flag.
Definition: RelianceHID.hpp:206
uint16_t notchADC
Notch sensor 12-bit ADC value.
Definition: RelianceHID.hpp:212