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