Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
usb_i2c_adapter
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
labrobotica
drivers
usb_i2c_adapter
Commits
3ab6b557
Commit
3ab6b557
authored
14 years ago
by
Sergi Hernandez
Browse files
Options
Downloads
Patches
Plain Diff
added an example to test the basic features of the adapter
parent
af17df27
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/examples/test_usb_i2c.cpp
+93
-0
93 additions, 0 deletions
src/examples/test_usb_i2c.cpp
src/usb_i2c.cpp
+1
-0
1 addition, 0 deletions
src/usb_i2c.cpp
with
94 additions
and
0 deletions
src/examples/test_usb_i2c.cpp
0 → 100644
+
93
−
0
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
;
}
}
This diff is collapsed.
Click to expand it.
src/usb_i2c.cpp
+
1
−
0
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
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment