Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
SetDigitalOutput.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++ SetDigitalOutput.cpp -L../ -lhubo -lpthread -lrt -o SetDigitalOutput
12 Run:
13  sudo ./SetDigitalOutput
14 Purpose:
15  Turns on/off a single output as specified in the command line.
16 */
17 
18 void BoostThreadPriority();
19 
20 int main(int argc, char* argv[])
21 {
22  // Lets have this program finish as soon as possible.
24 
25  if (argc != 3)
26  {
27  printf ("Usage: \n SetDigitalOutput <digital output to activate>\n example: SetDigitalOutput 7 0 // this will set channel 7 to 0\n");
28  return 0;
29  }
30 
31  int channel = 0;
32  if (sscanf (argv[1], "%d", &channel) != 1)
33  {
34  printf ("Digital output could not be determined!\n");
35  return 0;
36  }
37 
38  int outValue = 0;
39  if (sscanf (argv[2], "%d", &outValue) != 1)
40  {
41  printf ("Value for digital output could not be determined!\n");
42  return 0;
43  }
44 
45  if (outValue<0 || outValue>1)
46  {
47  printf ("Value for digital output must be 0 or 1!\n");
48  return 0;
49  }
50 
51  printf ("Setting digital output %d to %d.\n", channel, outValue);
52 
53  // 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"
54  #ifdef BPI
55  g_I2CConfig.m_sI2CDevice = "/dev/i2c-0";
56  #endif
57 
58  // Initialize the library once in your program.
59  if (!Initialize())
60  {
61  printf ("Error: Initialize\n");
62  return 0;
63  }
64 
65  // Set the cycle time to 2ms (50Hz).
66  Set_Cycle_Time(20);
67 
68  // Wait until the output latch buffer is filled.
70 
71  unsigned char read_out_value, channel_values;
72  Readback_DO_Channels (channel_values);
73 
74  printf ("Old channel values x%02X\n", channel_values);
75 
76  if (outValue)
77  channel_values |= (1 << channel);
78  else
79  channel_values &= ~(1 << channel);
80 
81  printf ("New channel values x%02X\n", channel_values);
82 
83  // Set the outputs.
84  Set_DO_Channels (channel_values);
85 
86  // For the given cycle time 20ms wait should be sufficient!
87  bool bResult = false;
88  for (int retry=0; retry<10; retry++)
89  {
90  usleep(2L*1000L);
91  Readback_DO_Channels (read_out_value);
92  if (read_out_value == channel_values)
93  {
94  bResult = true;
95  break;
96  }
97  }
98 
99  // Free library resources.
100  Uninitialize();
101 
102  return bResult ? 1 : 0;
103 }
104 
105 #include <pthread.h>
106 #include <assert.h>
108 {
109  // Scheduling params.
110  sched_param param;
111  int policy;
112  int ret;
113  pthread_t threadHandle = pthread_self();
114 
115  // Get current scheduling parameters of thread.
116  ret = pthread_getschedparam (threadHandle, &policy, &param);
117  assert (ret == 0);
118 
119  // Set scheduling parameters of thread to real time values (FIFO scheduling type and max prio).
120  policy = SCHED_FIFO; // SCHED_RR;
121  param.sched_priority = sched_get_priority_max(policy); // New max priority for new scheduling concept.
122  ret = pthread_setschedparam(threadHandle, policy, &param);
123  assert (ret == 0);
124 }
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...
void BoostThreadPriority()
bool Set_Cycle_Time(long cycleTime)
Sets the backgrounds threads polling interval in ms.
I2C_Config g_I2CConfig
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 Readback_DO_Channels(unsigned char &value)
Reads back the digital output latchs of the IO expander.
int main(int argc, char *argv[])