Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
DigitalOutput2.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <assert.h>
4 
5 #include "../hubolib.h"
6 #include "../hubocfg.h" // Required for changing default I2C device.
7 
8 using namespace HuboLib;
9 
10 /*
11 Compile and link:
12  g++ DigitalOutput2.cpp -L../ -lhubo -lpthread -lrt -o DigitalOutput2.out
13 Run:
14  sudo ./DigitalOutput2.out
15 Purpose:
16  The setting of digital outputs is done with a slight delay.
17  Use Readback_DO_Channels() to check what the hardware outputs are really set to.
18  Thus double check the values as requested by calls to Set_DO_Channel() and Set_DO_Channels().
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  // Reset all channels.
36  printf ("Clearing all channels, then setting channels 1, 2 and 3.\n");
37  Set_DO_Channels(0x00);
38  usleep(1000000);
39  Set_DO_Channels(0x07);
40 
41  // Read back the digital output values as requested by the Set_DO_Channel(s) function.
42  // We'd expect a 0x07 as the three lower channels are set.
43  unsigned char digitalOutputs = 0;
44  Get_DO_Channels(digitalOutputs);
45  assert (digitalOutputs == 0x07);
46 
47  // But infact - the outputs might not yet be set to 0x07 in the hardware
48  // but still show 0x00 for a while.
49  unsigned char currentDigitalOutputs = 0;
50  do
51  {
52  Readback_DO_Channels(currentDigitalOutputs);
53  printf ("Hardware outputs are 0x%02X\n", currentDigitalOutputs);
54  } while (currentDigitalOutputs != digitalOutputs);
55 
56  // The answer to this is that setting digital outputs is an asynchronous operation.
57  // A value set by a call to Set_DO_Channel() or Set_DO_Channels() gets buffered and transmitted
58  // in the next cycle time slot. Until then the hardware might still be showing the former state
59  // of the outputs.
60  // So if you really need to stop your execution until the outputs are set then use
61  // Readback_DO_Channels() to double check until the hardware has switched to the required state.
62 
63  // Free library resources.
64  Uninitialize();
65 
66  return 0;
67 }
const char * m_sI2CDevice
Definition: hubocfg.h:48
bool Initialize()
Initializes the library.
bool Set_DO_Channels(unsigned char value)
Requests the background thread to update all 8 bits of the digital output to the value specified...
int main(void)
bool Get_DO_Channels(unsigned char &value)
Retrieves the requested digital output value from the output buffer.
I2C_Config g_I2CConfig
void Uninitialize()
Releases any resources bound to the library.
bool Readback_DO_Channels(unsigned char &value)
Reads back the digital output latchs of the IO expander.