Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
DigitalInput6.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++ DigitalInput6.cpp -L../ -lhubo -lpthread -lrt -o DigitalInput6.out
13 Run:
14  sudo ./DigitalInput6.out
15 Purpose:
16  Read all digital inputs in one go.
17  Prior to reading wait until the buffers have valid values.
18  This will not only ensure that the input values are read from the hardware first
19  but also make sure the output latches are read back from the hardware!
20 */
21 
22 void BoostThreadPriority();
23 
24 int main(void)
25 {
26  // We need to boost the main threads priority in order to compete with the Hubo-libraries background thread.
27  // Otherwise we cannot demonstrate the issue as the background thread is likely to first finish the update
28  // of the buffers prior to us continuing reading the input channels!
30 
31  // 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"
32  #ifdef BPI
33  g_I2CConfig.m_sI2CDevice = "/dev/i2c-0";
34  #endif
35 
36  // Initialize the library once in your program.
37  if (!Initialize())
38  {
39  printf ("Error: Initialize\n");
40  return 1;
41  }
42 
43  // Wait in order to be sure the values read are already initialized from the hardware.
45 
46  while (1)
47  {
48  unsigned char allInputs = 0;
49 
50  // Read all 8 digital inputs (lets even wait and ensure that the background thread has finished initialization).
51  while (!Get_DI_Channels(allInputs))
52  {
53  printf ("Error: library not yet initialized.\n");
54  usleep(0);
55  }
56 
57  // Invert logic.
58  allInputs ^= 0xFF;
59 
60  printf ("Digital inputs (hex) = 0x%02X\n", allInputs);
61 
62  // Stop once we detect at least one input set.
63  if (allInputs != 0)
64  break;
65  // usleep (1000L * 10L);
66  }
67 
68  // Free library resources.
69  Uninitialize();
70 
71  return 0;
72 }
73 
74 #include <pthread.h>
76 {
77  // Scheduling params.
78  sched_param param;
79  int policy;
80  int ret;
81  pthread_t threadHandle = pthread_self();
82 
83  // Get current scheduling parameters of thread.
84  ret = pthread_getschedparam (threadHandle, &policy, &param);
85  assert (ret == 0);
86 
87  // Set scheduling parameters of thread to real time values (FIFO scheduling type and max prio).
88  policy = SCHED_FIFO; // SCHED_RR;
89  param.sched_priority = sched_get_priority_max(policy); // New max priority for new scheduling concept.
90  ret = pthread_setschedparam(threadHandle, policy, &param);
91  assert (ret == 0);
92 }
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_Channels(unsigned char &value)
Retrieves the value of all digital inputs.
bool Wait_For_MCP23017_Buffered_Values()
Waits until input and output buffers of the MCP23017 master are initialized from the hardware...
void BoostThreadPriority()