Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
DigitalSlaveList.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <vector>
3 
4 #include "../hubolib.h"
5 #include "../hubocfg.h" // Required for changing default I2C device.
6 
7 using namespace std;
8 
9 using namespace HuboLib;
10 
11 /*
12 Compile and link:
13  g++ DigitalSlaveList.cpp -L../ -lhubo -lpthread -lrt -o DigitalSlaveList.out
14 Run:
15  sudo ./DigitalSlaveList.out
16 Purpose:
17  Print out a list of all digital slaves found on the I2C bus.
18 */
19 
20 int main(void)
21 {
22  // If required - set the I2C device to work with. The Raspberry Pi uses "/dev/i2c-1" which is default, the Banana Pi uses "/dev/i2c-0"
23  #ifdef BPI
24  g_I2CConfig.m_sI2CDevice = "/dev/i2c-0";
25  #endif
26 
27  // We need to initialize the library prior to a call to GetSlaveDeviceList().
28  vector<int> slaveAddressList;
29  if (!GetSlaveDeviceList (slaveAddressList))
30  printf ("No slaves found - call Initialize() first.\n");
31 
32  // Initialize the library once in your program.
33  Initialize();
34 
35  if (GetSlaveDeviceList (slaveAddressList))
36  {
37  for (unsigned int i=0; i<slaveAddressList.size(); i++)
38  printf ("Found slave address 0x%02X.\n", slaveAddressList[i]);
39  }
40  else
41  printf ("No slaves found - call Initialize() first.\n");
42 
43  // Free library resources.
44  Uninitialize();
45 
46  return 0;
47 }
const char * m_sI2CDevice
Definition: hubocfg.h:48
bool Initialize()
Initializes the library.
I2C_Config g_I2CConfig
void Uninitialize()
Releases any resources bound to the library.
bool GetSlaveDeviceList(std::vector< int > &slaveAddressList)
Returns the list of I2C addresses of the MCP23017 slaves.
int main(void)