You are on page 1of 8

 The software is based on Windows XP/Vista/7 and developed under Microsoft Visual Studio C++.

 
 All the window forms created have to be resources and be graphically editable. 

A ‐ Abstract:
The goal of this software is to question an USB HID (Human Interface Device) device and to display the results received on a window. 
This USB HID device itself receives data from remote devices (out of scope) interprets them and keep them in a buffer ready to be 
delivered to the computer when required. The code source working with this USB HID device is provided and has to be integrated in 
the software. It is a simple example of a windows form receiving and displaying the data received via USB. 

  Computer with the 
USB HID Device Remote device
software 
 

The source code working with the USB HID provided works as follow: 

 Initialization of the USB  
 Initialization of the window form 
 Then a thread regularly read/write to the USB buffers (OUTBuffer, INBuffer) and update variables. 
 The window form is updated regularly reading the values of variables updated by the Thread (FormUpdateTimer_Tick) 

It  is  Important  to  understand  that  the  Read/Write  thread  is  sending  a  code  to  the  USB  Device  (Refer  to  the 
ReadWriteThread_DoWorks function)  

OUTBuffer[0] = 0  ////// Always 0 

OUTBuffer[1] = 0x37 //////   Code to tell the USB HID device what to do 

and the USB device is answering with the same code to confirm its acknowledgment of the order then with the following data: 

INBuffer[0] = 0  ////// We don’t care 

INBuffer[1] = 0x37////// First byte of the response with the same code we sent to confirm, else the software does nothing. 

INBuffer[2..] = 0xxx //// Random data 

The goal of the software to be developed is going further on the interpretation of the data received. 

B ‐ The new software:

B.1. ‐ Communication with the USB device:

The thread will regularly send a code ‘0x34’ to the USB device (like in the provided code) and this one will answer with 2 possibilities: 

a) Nothing available: 

 INBuffer[0] = 0  ///// We don’t care 
 INBuffer[1] = 0x34 ///// Same request code : it means I have understood (i.e. the USB device) 
 INBuffer[2] = 0x00 ///// CODE = 0x00 : It means nothing is available Do Nothing 
 
 The software has nothing to do 

b) Data available 
 INBuffer[0] = 0  ///// We don’t care 
 INBuffer[1] = 0x34 ///// Same request code : it means I have understood 
 INBuffer[2] = 0x03  ///// CODE different from 0x00 : It means I have something for you 
 INBuffer[3] = 0xE7  ///// ADDR_0 
 INBuffer[4] = 0xE7  ///// ADDR_1 
 INBuffer[5] = 0x17  ///// ADDR_2 
 INBuffer[6] = 0xC2  ///// ADDR_3 
 INBuffer[7] = 0x12  ///// ADDR_4 
 
 The software has something to do 

So what will do the software? 

It will update virtual databases that have been created from a file with the data provided by the usb and create a form to display 
them. 

B.2. ‐ The databases:

By database we refer to an array temporary created during the execution of the software. 

At the opening of the software the user will chose a profile available which will be connected to a file. It means for instance the user 
will have the choice between the profile 1 which will be connected to the file profile1.txt or the profile called mouse connected to 
key.txt. 

What contains the file? 

The file is a txt and is stored in a directory called profile. So when starting the software it looks inside the folder to see which file is 
inside, open them and read the headers to display them in the profile selection to help the user to chose. The header is completed 
with a line referring to a picture. This is for the form, detailed later on. The file includes the following sections: 

Profile1.txt 

[HEADER] 

Name_profile = “My first profile” 

Description_profile = “This is just a test to try” 

Picture = “try.bmp” 

A  section  for  CODE,  it  will  be  used  by  the  form  to  translate  the  hexadecimal  code  in  the  main  database  to  a  text  and  a  picture, 
maximum of 4 codes which are: 

[CODE] 

0x01 “It is cold” “cold.bmp”        The hex code is equivalent to 0b00000001 

0x02 “It is hot” “hot.bmp”           The hex code is equivalent to 0b00000010 

0x04 “It is raining” “rain.bmp”    The hex code is equivalent to 0b00000100 

0x08 “It is snowing” “snow.bmp”  The hex code is equivalent to 0b00001000 

A section for PRIORITY, once again it will be used by the form to translate the code to a human language (3 maximums) 
[PRIORITY] 

P1 “go fast” 

P2 “go slow” 

P3 “Take ur time” 

Then the file is followed by a data section (maximum 255), 5 hex code for ADDR, 2 coordinates X&Y, 1 priority and 1 string: 

[DATA] 

0xA0 0xE1 0xE7 0xC2 0x12 X45 Y10 P3 “Remote device 34” 

0xE7 0xE7 0x17 0xC2 0x12 X30 Y15 P2 “Remote device 21” 

0xC1 0xE7 0xE7 0xC2 0x11 X29 Y11 P1 “Remote device 18” 

0xFF 0x37 0x27 0x32 0x02 X5 Y2 P2 “Remote device 69” 

‐ DATABASE 1(Array) : The main database 

Once the user chooses the profile he wants, the software creates the virtual database (an array) with the following structure: 

a. Int ADDR_0; 
b. Int ADDR_1; 
c. Int ADDR_2; 
d. Int ADDR_3; 
e. Int ADDR_4; 
f. Int X; 
g. Int Y; 
h. String NAME; 
i. Int CODE; 
j. Int PRIORITY; 
k. Date D; (including day and hour, minute, second) 
l. String NAME2 

And fill it with the section [DATA] of the profile selected: 
ADDR_0  ADDR_1  ADDR_2  ADDR_3 ADDR_4  X  Y NAME CODE (default init) PRI  Date  NAME2
0xA0  0xE1  0xE7  0xC2  0x12  45  10  Remote device 34  0x00  3  NULL  Remote device 34 
0xE7  0xE7  0x17  0xC2  0x12  30  15  Remote device 21  0x00  2  NULL  Remote device 21 
0xC1  0xE7  0xE7  0xC2  0x11  29  11  Remote device 18  0x00  1  NULL  Remote device 18 
0xFF  0x37  0x27  0x32  0x02  5  2  Remote device 69  0x00  2  NULL  Remote device 69 

Important: ADDR_0 to ADDR_4 are coming as hexadecimal. 

‐ DATABASE 2 (Array) : The code database 

Another database is created containing 3 columns: 1 for the code, 1 for the string, 1 for the picture associated with. This database 
will be used by the form to show a text and a picture interpreting the CODE read from the main database. 

‐ DATABASE 3 (Array) : The priority database 

This one has the same role than the database 2 but no picture is required. 
B.3. – Communication between the USB Thread and the main database of devices

Now everything is initialized and the USB thread is doing its job, let’s see the case of the B.1.b. when a data is available: 

 INBuffer[0] = 0  ///// We don’t care 
 INBuffer[1] = 0x34 ///// Same request code : it means I have understood 
 INBuffer[2] = 0x03  ///// CODE different from 0x00 : It means I have something for you 
 INBuffer[3] = 0xE7  ///// ADDR_0 
 INBuffer[4] = 0xE7  ///// ADDR_1 
 INBuffer[5] = 0x17  ///// ADDR_2 
 INBuffer[6] = 0xC2  ///// ADDR_3 
 INBuffer[7] = 0x12  ///// ADDR_4 

The process has to search in the database the line with the ADDR_0 to ADDR_4 matching with the INBuffer[4] to INBuffer[8] and so 
update the CODE with INBuffer[2]. The process has also to input in the Date column the current date at the moment of this update. 

 
CODE (default 
ADDR_0  ADDR_1  ADDR_2  ADDR_3  ADDR_4  X  Y  NAME  PRI  Date  NAME2 
init)
0xA0  0xE1  0xE7  0xC2  0x12  45  10  Remote device  0x00  3  NULL  Remote device 
34  34 
0xE7  0xE7  0x17  0xC2  0x12  30  15  Remote device  0x03  2  Current  Remote device 
21  date  21 
0xC1  0xE7  0xE7  0xC2  0x11  29  11  Remote device  0x00  1  NULL  Remote device 
18  18 
0xFF  0x37  0x27  0x32  0x02  5  2  Remote device  0x00  2  NULL  Remote device 
69  69 

NOTE1: If the CODE received is 0xFF, the CODE written in the main database is 0x00 ‐> It is a reset command. 

NOTE2: 4 codes are possible (0x01,0x02,0x04,0x08) and are cumulative. It means that if the Thread receives a code 0x03, it is the 
combination of 0x01 and 0x02. The form will have to mask the CODE in the main database to determine if it is active or not. 

Example of the 0x03 here: 

 0x03 & 0x01 = 1 ‐> Code 0x01 ok, form will display this code 
 0x03 & 0x02 = 1 ‐> Code 0x02 ok, form will display this code 
 0x03 & 0x04 = 0 ‐> Code 0x04 not ok, form will not display this code 
 0x03 & 0x08 = 0 ‐> Code 0x08 not ok, form will not display this code 

B.3. – The window form

Finally  the  window  form  is  going  to  read  this  database  regularly  (Timer)  and  display  it  with  a  formatted  view  and  will  have  few 
possibilities to act on the database column ‘PRI’ and ‘NAME2’. 

The form is divided in 2 parts, the data area and the matrix/picture area. 

a) Data area 

This area will display in rectangular shapes the content of the main database where CODE is different from 0x00. It means that the 
form  will  update  itself  regularly  by  checking  the  main  database  where  the  CODE  is  different  from  0x00  and  sorting  by  first  the 
priority (from 1 highest to 3 lowest) secondly the date (the biggest from the actual time first).  

Consequences: 

‐ If a shape is created because the CODE is different from 0x00, when the CODE goes back to 0x00 the shape is destroyed 
automatically and the followings are lifted up. 
‐ The shape at the top o of the windoww is the one w with the highest priority (1  in the database) and the biggest time frrom the 
current date. 
‐ The proceess calculates and shows for how many time the shapee is displayed.
‐ our  codes  maay  be  displayeed  using  the  code  database  to  only  diisplay  the  strring  and  the  picture 
Inside  a  shape  the  fo
associated (pictures aree replacing the color boxes below) 
‐ The softwware is using tthe correspon ndence of cod de in the priority database  and the codee database to  show only a  human 
language. 

The ttexts and pictu
ures to print in a shape are: 

‐ NAME2 frrom the main database. 
‐ PRIORITYY text from thee PRIORITY dattabase relatedd to PRI in main database.
‐ CODE texxts and picturees from the COODE database related to thee CODE hex in main databasse (up to 4). 
‐ The time elapsed sincee this shape is created (calcu
ulated from th
he DATE in maain database). 

b) Matrix and pictu
ure area 

The p
picture referreed in the Head
der section of the profile is displayed herre.  

Then
n a virtual Mattrix is overlapp ping this pictu
ure of a size that could be ch hosen later mo odifying the co
ode. To start w
we will take 
100xx100.It is not p
pixels. The areea where the p picture is displlayed is divideed in 100 lines and 100 columns with coordinates X {0...99} 
and YY {0..99}. 

So w
when a shape iss created, thee process has tto use the coo
ordinates X and h this code (in our 
d Y of the maiin database asssociated with
exam
mple above X3
30 and Y15to ccreate a small blinking box aand draw a line between the box and thee shape. 
Of course when the shape is destroyed, the box and the line are also destroyed. 

c) Option window 

An option window will be open and will allow modifying the NAME2 and the PRI in the Main Database of a device showing only the 
NAME as a reference (NAME cannot be modified). 

 
C – Flow Chart of the USB Thread and databases processes

Starting the software

Check if USB device is present

If no quit the software with an  If yes, open the profile window 
error message to let the user select a profile

Load the profile in the 3 
databases : [DATA], [CODE], 
[PRIORITY]

Open the main window 
interface and start the USB 
THREAD (send 0x34 to 
OUTBuffer)

When INBuffer[2] is different 
from 0x00 update the main 
database with the new CODE 
at the ADDR matching with 
ADDR from INBuffer

If INBuffer[2] = 0xFF, Write  Main Database is updated with 
0x00 to the CODE column in  the CODE and the current 
the main database matching  DATE (day , hours, minutes, 
with the good ADDR  seconds)
 
D – Flow chart of the form window

Select the CODE that are 
different from 0x00 in the 
Main Database

Sort the result by priority 
and date (P1 first, oldest 
date) 

Create, Modify or Destroy 
shapes

Use the CODE database 
and PRIORITY database to 
print the  human language 
text in the shapes

Update the Matrix with 
the good coordinates 
 

You might also like