Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
DigitalSlaveOutput1.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++ DigitalSlaveOutput1.cpp -L../ -lhubo -lpthread -lrt -o DigitalSlaveOutput1.out
12 Run:
13  sudo ./DigitalSlaveOutput1.out
14 Purpose:
15  Set all digital outputs of the i2c slave 0 (0x21) in one go or one by one.
16 */
17 
18 int main(void)
19 {
20  // 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"
21  #ifdef BPI
22  g_I2CConfig.m_sI2CDevice = "/dev/i2c-0";
23  #endif
24 
25  // Initialize the library once in your program.
26  if (!Initialize())
27  {
28  printf ("Error: Initialize\n");
29  return 1;
30  }
31 
32  int slaveNo = 0;
33 
34  // Reset all channels.
35  printf ("Clearing all channels\n");
36  Set_Slave_DO_Channels(slaveNo, 0x00);
37  usleep (1000000);
38 
39  // Set one channel after the other.
40  for (int i=0; i<8; i++)
41  {
42  // Turn on channel i.
43  printf ("Setting channel %d\n", i);
44  Set_Slave_DO_Channel(slaveNo, i, 1);
45  usleep (1000000);
46  }
47 
48  // Clear one channel after the other.
49  for (int i=0; i<8; i++)
50  {
51  // Turn off channel i.
52  printf ("Clearing channel %d\n", i);
53  Set_Slave_DO_Channel(slaveNo, i, 0);
54  usleep (1000000);
55  }
56 
57  // Set all channels.
58  printf ("Setting all channels\n");
59  Set_Slave_DO_Channels(slaveNo, 0xFF);
60 
61  // If you omit this "wait" then it might happen that the last call to Set_Slave_DO_Channels(0xFF) is not
62  // sent to the hardware anymore since the Uninitialize() call below has terminated before!
63  usleep (1000000);
64 
65  // Free library resources.
66  Uninitialize();
67 
68  return 0;
69 }
const char * m_sI2CDevice
Definition: hubocfg.h:48
bool Initialize()
Initializes the library.
bool Set_Slave_DO_Channel(int slaveNo, int channel, bool bValue)
Requests the background thread to set one of the digital outputs to the value specified for a given s...
int main(void)
I2C_Config g_I2CConfig
void Uninitialize()
Releases any resources bound to the library.
bool Set_Slave_DO_Channels(int slaveNo, unsigned char value)
Requests the background thread to update all 8 bits of the digital output to the value specified for ...