-
Notifications
You must be signed in to change notification settings - Fork 18
/
USBCheckStatus.ino
84 lines (64 loc) · 2.25 KB
/
USBCheckStatus.ino
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
/*
USBCheckStatus.ino - Check the USB Host port status on the Galileo board
This example shows how to check the USB Host port connection status.
This example has been tested on a Galileo Gen2.
This example code is in the public domain.
Revision History
----------------------------------------------------------
Author Date Description
----------------------------------------------------------
Diego Villalobos 07-28-2015 Example created
*/
FILE *usb_info_file;
int c, new_usb_status, current_usb_status;
String usb_info;
int checkStatus(){
/*
Create a file in Linux side with the recent activity in the USB bus. Then open that file from the
Arduino sketch, read and return the current USB port status. The file created is deleted before
returning the value read.
*/
String line_output;
int new_status = 0;
system("dmesg | grep 'USB' | tail -5 > /home/usb_info.txt");
usb_info_file = fopen("/home/usb_info.txt","r");
if (usb_info_file)
{
while ((c = getc(usb_info_file)) != EOF) // Keep reading until the end of the file
{
if(c != 10) // Detect if it is an end of line
{
line_output += (char)c;
}
else
{
if (line_output.substring(24,38) == "USB disconnect") { new_status = 0; }
if (line_output.substring(24,49) == "new high-speed USB device") { new_status = 1; }
line_output = "";
}
}
if (line_output.substring(24,38) == "USB disconnect") { new_status = 0; }
if (line_output.substring(24,49) == "new high-speed USB device") { new_status = 1; }
fclose(usb_info_file);
}
// Delete the file created in the Linux side
system("rm /home/usb_info.txt");
// Return the status
return new_status;
}
void setup() {
// Initialize the Serial Monitor
Serial.begin(9600);
new_usb_status = 0;
current_usb_status = 0;
}
void loop() {
new_usb_status = checkStatus(); // Return the status of the USB port
// Checks if the status has changed
if (new_usb_status != current_usb_status){
current_usb_status = new_usb_status;
if (current_usb_status == 0) { Serial.println("USB device disconnected"); }
if (current_usb_status == 1) { Serial.println("USB device connected"); }
}
delay(1000);
}