You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

scale_rptparser.cpp 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Parser for standard HID scale (usage page 0x8d) data input report (ID 3) */
  2. #include "scale_rptparser.h"
  3. const char* UNITS[13] = {
  4. "units", // unknown unit
  5. "mg", // milligram
  6. "g", // gram
  7. "kg", // kilogram
  8. "cd", // carat
  9. "taels", // lian
  10. "gr", // grain
  11. "dwt", // pennyweight
  12. "tonnes", // metric tons
  13. "tons", // avoir ton
  14. "ozt", // troy ounce
  15. "oz", // ounce
  16. "lbs" // pound
  17. };
  18. ScaleReportParser::ScaleReportParser(ScaleEvents *evt) :
  19. scaleEvents(evt)
  20. {}
  21. void ScaleReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
  22. {
  23. bool match = true;
  24. // Checking if there are changes in report since the method was last called
  25. for (uint8_t i=0; i<RPT_SCALE_LEN; i++) {
  26. if( buf[i] != oldScale[i] ) {
  27. match = false;
  28. break;
  29. }
  30. }
  31. // Calling Game Pad event handler
  32. if (!match && scaleEvents) {
  33. scaleEvents->OnScaleChanged((const ScaleEventData*)buf);
  34. for (uint8_t i=0; i<RPT_SCALE_LEN; i++) oldScale[i] = buf[i];
  35. }
  36. }
  37. ScaleEvents::ScaleEvents( Max_LCD* pLCD ) :
  38. pLcd( pLCD )
  39. {}
  40. void ScaleEvents::LcdPrint( const char* str )
  41. {
  42. while( *str ) {
  43. pLcd->write( *str++ );
  44. }
  45. }
  46. void ScaleEvents::OnScaleChanged(const ScaleEventData *evt)
  47. {
  48. pLcd->clear();
  49. pLcd->home();
  50. pLcd->setCursor(0,0);
  51. if( evt->reportID != 3 ) {
  52. const char inv_report[]="Invalid report!";
  53. Serial.println(inv_report);
  54. LcdPrint(inv_report);
  55. return;
  56. }//if( evt->reportID != 3...
  57. switch( evt->status ) {
  58. case REPORT_FAULT:
  59. Serial.println(F("Report fault"));
  60. break;
  61. case ZEROED:
  62. Serial.println(F("Scale zero set"));
  63. break;
  64. case WEIGHING: {
  65. const char progress[] = "Weighing...";
  66. Serial.println(progress);
  67. LcdPrint(progress);
  68. break;
  69. }
  70. case WEIGHT_VALID: {
  71. char buf[10];
  72. double weight = evt->weight * pow( 10, evt->exp );
  73. Serial.print(F("Weight: "));
  74. Serial.print( weight );
  75. Serial.print(F(" "));
  76. Serial.println( UNITS[ evt->unit ]);
  77. LcdPrint("Weight: ");
  78. dtostrf( weight, 4, 2, buf );
  79. LcdPrint( buf );
  80. LcdPrint( UNITS[ evt->unit ]);
  81. break;
  82. }//case WEIGHT_VALID...
  83. case WEIGHT_NEGATIVE: {
  84. const char negweight[] = "Negative weight";
  85. Serial.println(negweight);
  86. LcdPrint(negweight);
  87. break;
  88. }
  89. case OVERWEIGHT: {
  90. const char overweight[] = "Max.weight reached";
  91. Serial.println(overweight);
  92. LcdPrint( overweight );
  93. break;
  94. }
  95. case CALIBRATE_ME:
  96. Serial.println(F("Scale calibration required"));
  97. break;
  98. case ZERO_ME:
  99. Serial.println(F("Scale zeroing required"));
  100. break;
  101. default:
  102. Serial.print(F("Undefined status code: "));
  103. Serial.println( evt->status );
  104. break;
  105. }//switch( evt->status...
  106. }