Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
DigitalInput2.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <unistd.h>
3 
4 #include "../hubolib.h"
5 #include "../hubocfg.h" // Required for changing default I2C device.
6 
7 using namespace HuboLib;
8 
9 /*
10 Compile and link:
11  g++ DigitalInput2.cpp -L../ -lhubo -lpthread -lrt -o DigitalInput2.out
12 Run:
13  sudo ./DigitalInput2.out
14 Purpose:
15  Read digital inputs 0 and 1 and display them in the console.
16  The program will terminate when digital input 2 is lowered to ground.
17  The hubo library gets properly de-initialized prior to finishing.
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  // Initialize the library once in your program.
28  Initialize();
29 
30  while (1)
31  {
32  bool bState = false;
33 
34  // Read digital input number 0.
35  Get_DI_Channel(0, bState);
36  printf ("Digital input 0 = %s ", bState ? "ON " : "OFF");
37 
38  // Read digital input number 1.
39  Get_DI_Channel(1, bState);
40  printf ("Digital input 1 = %s \n", bState ? "ON " : "OFF");
41 
42  usleep (1000L * 10L);
43 
44  // Should we finish?
45  Get_DI_Channel(2, bState);
46  if (bState == false)
47  break;
48  }
49 
50  // Free library resources.
51  Uninitialize();
52 
53  return 0;
54 }
const char * m_sI2CDevice
Definition: hubocfg.h:48
bool Initialize()
Initializes the library.
int main(void)
I2C_Config g_I2CConfig
void Uninitialize()
Releases any resources bound to the library.
bool Get_DI_Channel(int channel, bool &bValue)
Retrieves the value of one digital input.