Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
DigitalInput4.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++ DigitalInput4.cpp -L../ -lhubo -lpthread -lrt -o DigitalInput4.out
12 Run:
13  sudo ./DigitalInput4.out
14 Purpose:
15  Read all digital inputs in one go.
16  The program will terminate when digital input 2 is lowered to ground.
17 */
18 
19 int main(void)
20 {
21  // 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"
22  #ifdef BPI
23  g_I2CConfig.m_sI2CDevice = "/dev/i2c-0";
24  #endif
25 
26  // Initialize the library once in your program.
27  if (!Initialize())
28  {
29  printf ("Error: Initialize\n");
30  return 1;
31  }
32 
33  while (1)
34  {
35  unsigned char allInputs = 0;
36 
37  // Read all 8 digital inputs.
38  if (!Get_DI_Channels(allInputs))
39  {
40  printf ("Error: Get_DI_Channels\n");
41  return 1;
42  }
43  printf ("Digital inputs (hex) = 0x%02X\n", allInputs);
44 
45  // Break if input 2 is lowered.
46  if (~allInputs & 4)
47  break;
48 
49  usleep (1000L * 10L);
50  }
51 
52  // Free library resources.
53  Uninitialize();
54 
55  return 0;
56 }
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_Channels(unsigned char &value)
Retrieves the value of all digital inputs.