Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
DigitalInput3.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++ DigitalInput3.cpp -L../ -lhubo -lpthread -lrt -o DigitalInput3.out
12 Run:
13  sudo ./DigitalInput3.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  Check whether the calls to the library really succeded. If not stop.
19 */
20 
21 int main(void)
22 {
23  // 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"
24  #ifdef BPI
25  g_I2CConfig.m_sI2CDevice = "/dev/i2c-0";
26  #endif
27 
28  // Initialize the library once in your program.
29  if (!Initialize())
30  {
31  printf ("Error: Initialize\n");
32  return 1;
33  }
34 
35  while (1)
36  {
37  bool bState = false;
38 
39  // Read digital input number 0.
40  if (!Get_DI_Channel(0, bState))
41  {
42  printf ("Error: Get_DI_Channel\n");
43  break;
44  }
45  printf ("Digital input 0 = %s ", bState ? "ON " : "OFF");
46 
47  // Read digital input number 1.
48  if (!Get_DI_Channel(1, bState))
49  {
50  printf ("Error: Get_DI_Channel\n");
51  break;
52  }
53  printf ("Digital input 1 = %s ", bState ? "ON " : "OFF");
54 
55  printf ("\n");
56 
57  // Should we finish?
58  if (!Get_DI_Channel(2, bState))
59  {
60  printf ("Error: Get_DI_Channel\n");
61  break;
62  }
63 
64  if (bState == false)
65  break;
66 
67  usleep (1000L * 10L);
68  }
69 
70  // Free library resources.
71  Uninitialize();
72 
73  return 0;
74 }
const char * m_sI2CDevice
Definition: hubocfg.h:48
int main(void)
bool Initialize()
Initializes the library.
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.