Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
DigitalSlaveOutput2.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++ DigitalSlaveOutput2.cpp -L../ -lhubo -lpthread -lrt -o DigitalSlaveOutput2.out
13 Run:
14  sudo ./DigitalSlaveOutput2.out
15 Purpose:
16  The setting of digital outputs (this includes I2C slaves too) is done with a slight delay.
17  Use Readback_Slave_DO_Channels() to check what the hardware outputs are really set to.
18  Thus double check the values as requested by calls to Set_Slave_DO_Channel() and Set_Slave_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  int slaveNo = 0; // ... resulting in i2c address 0x21.
36 
37  // Reset all channels.
38  printf ("Clearing all channels on slave %d, then setting channels 1, 2 and 3.\n", slaveNo);
39  Set_Slave_DO_Channels(slaveNo, 0x00);
40  usleep(1000000);
41  Set_Slave_DO_Channels(slaveNo, 0x07);
42 
43  // Read back the digital output values as requested by the Set_Slave_DO_Channel(s) function.
44  // We'd expect a 0x07 as the three lower channels are set.
45  unsigned char digitalOutputs = 0;
46  Get_Slave_DO_Channels(slaveNo, digitalOutputs);
47  assert (digitalOutputs == 0x07);
48 
49  // But infact - the outputs might not yet be set to 0x07 in the hardware
50  // but still show 0x00 for a while.
51  unsigned char currentDigitalOutputs = 0;
52  do
53  {
54  Readback_Slave_DO_Channels(slaveNo, currentDigitalOutputs);
55  printf ("Hardware outputs of slave %d are 0x%02X\n", slaveNo, currentDigitalOutputs);
56  } while (currentDigitalOutputs != digitalOutputs);
57 
58  // The answer to this is that setting digital outputs is an asynchronous operation.
59  // A value set by a call to Set_Slave_DO_Channel() or Set_Slave_DO_Channels() gets buffered and transmitted
60  // in the next cycle time slot. Until then the hardware might still be showing the former state
61  // of the outputs.
62  // So if you really need to stop your execution until the outputs are set then use
63  // Readback_Slave_DO_Channels() to double check until the hardware has switched to the required state.
64 
65  // Free library resources.
66  Uninitialize();
67 
68  return 0;
69 }
const char * m_sI2CDevice
Definition: hubocfg.h:48
bool Get_Slave_DO_Channels(int slaveNo, unsigned char &value)
Retrieves the requested digital output value from the output buffer for a given slave.
bool Initialize()
Initializes the library.
int main(void)
I2C_Config g_I2CConfig
bool Readback_Slave_DO_Channels(int slaveNo, unsigned char &value)
Reads back the digital output latchs of the IO expander for a given slave.
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 ...