Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
labrobotica
drivers
usb_i2c_adapter
Commits
3ab6b557
Commit
3ab6b557
authored
Jul 12, 2010
by
Sergi Hernandez
Browse files
added an example to test the basic features of the adapter
parent
af17df27
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/examples/test_usb_i2c.cpp
0 → 100644
View file @
3ab6b557
#include
"exceptions.h"
#include
"usb_i2c.h"
#include
<iostream>
const
std
::
string
usb_port
=
"/dev/ttyUSB0"
;
const
unsigned
char
dev_id
=
0x22
;
int
main
(
int
argc
,
char
*
argv
[])
{
CUSBI2C
adapter
(
"adapter"
);
int
i
=
0
;
try
{
adapter
.
open
(
usb_port
);
std
::
cout
<<
"Firmware revision: "
<<
(
int
)
adapter
.
get_revision
()
<<
std
::
endl
;
std
::
cout
<<
"Turning the led off ..."
<<
std
::
endl
;
adapter
.
turn_led_off
();
sleep
(
1
);
std
::
cout
<<
"Turning the led on ..."
<<
std
::
endl
;
adapter
.
turn_led_on
();
std
::
cout
<<
"Using the second general purose I/O as digital output ..."
<<
std
::
endl
;
adapter
.
config_gpio
(
gpio2
,
digital_output
);
std
::
cout
<<
" Outputing a high level ..."
<<
std
::
endl
;
adapter
.
set_gpio
(
gpio2
,
true
);
sleep
(
2
);
std
::
cout
<<
" Outputing a low level ..."
<<
std
::
endl
;
adapter
.
set_gpio
(
gpio2
,
false
);
sleep
(
2
);
std
::
cout
<<
"Using the second general purose I/O as digital input ..."
<<
std
::
endl
;
adapter
.
config_gpio
(
gpio2
,
digital_input
);
std
::
cout
<<
" Reading the current value ..."
<<
std
::
endl
;
for
(
i
=
0
;
i
<
10
;
i
++
)
{
if
(
adapter
.
get_gpio
(
gpio2
))
std
::
cout
<<
" high level."
<<
std
::
endl
;
else
std
::
cout
<<
" low level."
<<
std
::
endl
;
usleep
(
500000
);
}
std
::
cout
<<
"Using the second general purose I/O as analog input ..."
<<
std
::
endl
;
adapter
.
config_gpio
(
gpio2
,
analog_input
);
std
::
cout
<<
" Reading the current value ..."
<<
std
::
endl
;
for
(
i
=
0
;
i
<
10
;
i
++
)
{
std
::
cout
<<
" "
<<
adapter
.
get_analog
(
gpio2
)
<<
std
::
endl
;
usleep
(
500000
);
}
std
::
cout
<<
"Using the second general purose I/O as I2C ..."
<<
std
::
endl
;
adapter
.
config_gpio
(
gpio2
,
i2c
);
std
::
cout
<<
"Using the third general purose I/O as digital output ..."
<<
std
::
endl
;
adapter
.
config_gpio
(
gpio1
,
digital_output
);
std
::
cout
<<
" Outputing a high level ..."
<<
std
::
endl
;
adapter
.
set_gpio
(
gpio3
,
true
);
sleep
(
2
);
std
::
cout
<<
" Outputing a low level ..."
<<
std
::
endl
;
adapter
.
set_gpio
(
gpio3
,
false
);
sleep
(
2
);
std
::
cout
<<
"Using the third general purose I/O as digital input ..."
<<
std
::
endl
;
adapter
.
config_gpio
(
gpio3
,
digital_input
);
std
::
cout
<<
" Reading the current value ..."
<<
std
::
endl
;
for
(
i
=
0
;
i
<
10
;
i
++
)
{
if
(
adapter
.
get_gpio
(
gpio3
))
std
::
cout
<<
" high level."
<<
std
::
endl
;
else
std
::
cout
<<
" low level."
<<
std
::
endl
;
usleep
(
500000
);
}
std
::
cout
<<
"Using the third general purose I/O as analog input ..."
<<
std
::
endl
;
adapter
.
config_gpio
(
gpio3
,
analog_input
);
std
::
cout
<<
" Reading the current value ..."
<<
std
::
endl
;
for
(
i
=
0
;
i
<
10
;
i
++
)
{
std
::
cout
<<
" "
<<
adapter
.
get_analog
(
gpio3
)
<<
std
::
endl
;
usleep
(
500000
);
}
std
::
cout
<<
"Using the third general purose I/O as I2C ..."
<<
std
::
endl
;
adapter
.
config_gpio
(
gpio3
,
i2c
);
std
::
cout
<<
"Using the first general purose I/O as digital input ..."
<<
std
::
endl
;
adapter
.
config_gpio
(
gpio1
,
digital_input
);
for
(
i
=
0
;
i
<
10
;
i
++
)
{
if
(
adapter
.
get_gpio
(
gpio1
))
std
::
cout
<<
" high level."
<<
std
::
endl
;
else
std
::
cout
<<
" low level."
<<
std
::
endl
;
usleep
(
500000
);
}
}
catch
(
CException
&
e
)
{
std
::
cout
<<
e
.
what
()
<<
std
::
endl
;
}
}
src/usb_i2c.cpp
View file @
3ab6b557
...
...
@@ -180,6 +180,7 @@ short int CUSBI2C::get_analog(gpio_pins pin)
analog_value
=
answer
[
0
]
*
256
+
answer
[
1
];
else
analog_value
=
answer
[
2
]
*
256
+
answer
[
3
];
return
analog_value
;
}
else
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment