-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
270 lines (225 loc) · 6.44 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "time_help.hpp"
#include "lcd.hpp"
#include "history.hpp"
#include "mbed.h"
#include "freetronicsLCDShield.h"
#include "USBHID.h"
#include "TSISensor.h"
#include "RoundRobin.hpp"
#if defined SERIAL_DEBUG
Serial pc(USBTX, USBRX); // tx, rx
#endif
#define HID_SEND_REPORT_LEN 64U
#define HID_RECV_REPORT_LEN HID_SEND_REPORT_LEN
USBHID hid(HID_SEND_REPORT_LEN, HID_RECV_REPORT_LEN, 0x1987U, 0x1100U);
freetronicsLCDShield lx(D8, D9,
D4, D5, D6, D7,
D10, A0);
LCDIf lcdInterface( lx );
TSISensor tsi;
Ticker buttonWatcher;
freetronicsLCDShield::ShieldButton lastButton = freetronicsLCDShield::None;
uint32_t currentMessageId = UINT32_MAX;
void displayMessage( const DisplayMessage_t* const p_msg )
{
currentMessageId = p_msg->id;
for( unsigned i = 0; i < MAX_ROW_COUNT; i++ )
{
lcdInterface.setString(i ,p_msg->lines[i].c_str());
}
lcdInterface.setScrollEffect( p_msg->scrollEffect );
lcdInterface.setPulsing( !( p_msg->dismissed ) );
}
bool showMessageFromHistory( const Offset_t p_offset )
{
const DisplayMessage_t* const msg = getMessage( currentMessageId, p_offset );
bool retVal = false;
if( msg != NULL )
{
displayMessage( msg );
retVal = true;
}
return retVal;
}
void watchButtons( void )
{
freetronicsLCDShield::ShieldButton currentButton = lx.pressedButton();
if( tsi.readPercentage() )
{
DisplayMessage_t* msg = getMessage( currentMessageId, OFFSET_NONE );
if( msg != NULL )
{
msg->dismissed = true;
}
lcdInterface.setPulsing(false);
lcdInterface.resetSleepTimer();
}
if( lastButton == freetronicsLCDShield::None )
{
if( currentButton != freetronicsLCDShield::None )
{
lcdInterface.resetSleepTimer();
}
switch( currentButton ) {
case freetronicsLCDShield::Up:
lcdInterface.incrementBackLight();
break;
case freetronicsLCDShield::Down:
lcdInterface.decrementBackLight();
break;
case freetronicsLCDShield::Right:
showMessageFromHistory( OFFSET_AFTER );
break;
case freetronicsLCDShield::Left:
showMessageFromHistory( OFFSET_BEFORE );
break;
default:
break;
}
}
lastButton = currentButton;
}
void lcdRefresh( void )
{
lcdInterface.refresh();
}
void showBanner( void )
{
lcdInterface.setString( 0, "mbed LCD HID" );
lcdInterface.setString( 1, "github.com/bright-tools/mbed_lcd_hid" );
lcdInterface.setPulsing( false );
}
#define MESSAGE_ID_NEW_MESSAGE 0x10U
#define MESSAGE_ID_REMOVE_MESSAGE 0x20U
void handleRemoveMessage( const uint8_t* const p_data )
{
uint32_t id = (uint32_t)p_data[0] |
(uint32_t)p_data[1] |
(uint32_t)p_data[2] |
(uint32_t)p_data[3];
#if defined SERIAL_DEBUG
pc.printf("RemoveMessage, id is %04x\r\n",id);
#endif
if( id == UINT32_MAX )
{
removeAllMessages();
showBanner();
currentMessageId = UINT32_MAX;
}
else
{
/* Have we removed the message currently displayed? */
if( id == currentMessageId )
{
#if defined SERIAL_DEBUG
pc.printf("That's the current message!\r\n");
#endif
if( !showMessageFromHistory( OFFSET_BEFORE ) )
{
if( !showMessageFromHistory( OFFSET_AFTER ) )
{
showBanner();
}
}
}
removeMessage( id );
}
}
void handleNewMessage( const uint8_t* const p_data )
{
uint32_t id = (uint32_t)p_data[0] |
(uint32_t)p_data[1] |
(uint32_t)p_data[2] |
(uint32_t)p_data[3];
unsigned row = p_data[4];
char strBuffer[ MAX_ROW_LEN + 1 ];
DisplayMessage_t newMessage;
DisplayMessage_t* msgPtr;
#if defined SERIAL_DEBUG
pc.printf("NewMessage, id is %04x row is %d\r\n",id,row);
#if 0
for( unsigned i = 0; i < 60 ; i++ )
{
pc.printf("%02x ",p_data[i]);
}
pc.printf("\r\n");
#endif
#endif
if( row < MAX_ROW_COUNT )
{
if( msgPtr = getMessage( id, OFFSET_NONE ))
{
#if defined SERIAL_DEBUG
pc.printf("Found an existing message\r\n");
#endif
newMessage = *msgPtr;
}
strncpy( strBuffer, (const char*)(&(p_data[ 14 ])), MAX_ROW_LEN );
strBuffer[ MAX_ROW_LEN ] = 0;
newMessage.id = id;
newMessage.dismissed = false;
newMessage.lines[row] = strBuffer;
newMessage.scrollEffect = (LCDIf::ScrollEffect_t)(p_data[4]);
addMessage( &newMessage );
displayMessage( &newMessage );
lcdInterface.resetSleepTimer();
}
}
void handleReport( HID_REPORT* recv_report )
{
#if defined SERIAL_DEBUG
pc.printf("Message received with message type %02x\r\n",recv_report->data[0]);
#endif
switch( recv_report->data[0] )
{
case MESSAGE_ID_NEW_MESSAGE:
handleNewMessage( &(recv_report->data[1]) );
break;
case MESSAGE_ID_REMOVE_MESSAGE:
handleRemoveMessage( &(recv_report->data[1]) );
break;
}
}
int main() {
#if defined SERIAL_DEBUG
pc.printf("Hello");
#endif
RoundRobin::instance()->SetBaseRate( BASE_RATE );
RoundRobin::instance()->addTask( LCD_REFRESH_MULTIPLE, lcdRefresh );
RoundRobin::instance()->addTask( BUTTON_WATCH_MULTIPLE, watchButtons );
#if 0
DisplayMessage_t test;
test.id = 1;
test.lines[0] = "Hello";
test.lines[1] = "There";
addMessage( &test );
test.id = 2;
test.lines[0] = "This is a test";
test.lines[1] = "Of the interface history";
addMessage( &test );
#endif
showBanner();
while(true) {
HID_REPORT recv_report;
#if defined SERIAL_DEBUG
pc.printf("+");
#endif
//try to read a msg
while(hid.readNB(&recv_report)) {
handleReport( &recv_report );
}
HID_REPORT send_report;
send_report.length = 64;
//Send the report
hid.send(&send_report);
//Fill the report
for (unsigned i = 0; i < send_report.length; i++)
{
send_report.data[i] = rand() & 0xff;
}
#if defined SERIAL_DEBUG
pc.printf("Button State: %f %d\r\n",lx.readButton(),lx.pressedButton());
#endif
wait(1);
}
}