Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
GetDigitalInput.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <pthread.h>
4 #include <assert.h>
5 
6 #include "../hubolib.h"
7 #include "../hubocfg.h" // Required for changing default I2C device.
8 
9 using namespace HuboLib;
10 
11 /*
12 Compile and link:
13  g++ GetDigitalInput.cpp -L../ -lhubo -lpthread -lrt -o GetDigitalInput
14 Run:
15  sudo ./GetDigitalInput
16 Purpose:
17  Reads the digital input as specified in the command line and returns the result.
18 */
19 
20 void BoostThreadPriority();
21 
22 int main(int argc, char* argv[])
23 {
24  // Lets have this program finish as soon as possible.
26 
27  if (argc != 2)
28  {
29  printf ("Usage: \n GetDigitalInput <digital input to read>\n example: GetDigitalInput 7 // this will read digital input 7 and return the result\n");
30  return 0;
31  }
32 
33  int channel = 0;
34  if (sscanf (argv[1], "%d", &channel) != 1)
35  {
36  printf ("Digital input could not be determined!\n");
37  return 0;
38  }
39 
40  // 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"
41  #ifdef BPI
42  g_I2CConfig.m_sI2CDevice = "/dev/i2c-0";
43  #endif
44 
45  // Initialize the library once in your program.
46  if (!Initialize())
47  {
48  printf ("Error: Initialize\n");
49  return 0;
50  }
51 
52  // Set the cycle time to 2ms (50Hz).
53  Set_Cycle_Time(20);
54 
55  // Wait for input buffers to be filled.
57 
58  // Finally get the output.
59  bool bOutValue = false;
60  bool bResult = Get_DI_Channel (channel, bOutValue);
61 
62  // Free library resources.
63  Uninitialize();
64 
65  printf ("%s\n", bOutValue ? "1" : "0");
66 
67  return bResult;
68 }
69 
71 {
72  // Scheduling params.
73  sched_param param;
74  int policy;
75  int ret;
76  pthread_t threadHandle = pthread_self();
77 
78  // Get current scheduling parameters of thread.
79  ret = pthread_getschedparam (threadHandle, &policy, &param);
80  assert (ret == 0);
81 
82  // Set scheduling parameters of thread to real time values (FIFO scheduling type and max prio).
83  policy = SCHED_FIFO; // SCHED_RR;
84  param.sched_priority = sched_get_priority_max(policy); // New max priority for new scheduling concept.
85  ret = pthread_setschedparam(threadHandle, policy, &param);
86  assert (ret == 0);
87 }
const char * m_sI2CDevice
Definition: hubocfg.h:48
bool Initialize()
Initializes the library.
int main(int argc, char *argv[])
bool Set_Cycle_Time(long cycleTime)
Sets the backgrounds threads polling interval in ms.
I2C_Config g_I2CConfig
void BoostThreadPriority()
void Uninitialize()
Releases any resources bound to the library.
bool Wait_For_MCP23017_Buffered_Values()
Waits until input and output buffers of the MCP23017 master are initialized from the hardware...
bool Get_DI_Channel(int channel, bool &bValue)
Retrieves the value of one digital input.