Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
DigitalRawInput.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++ DigitalRawInput.cpp -L../ -lhubo -lpthread -lrt -o DigitalRawInput.out
13 Run:
14  sudo ./DigitalRawInput.out
15 Purpose:
16  Sometimes it is necessary to keep track of a fast changing signal. Since the Linux scheduler cannot work efficient
17  at higher rates than 1ms further decreasing of the cycle time would not help. Too much time would be wasted due to
18  thread context changes...
19  However, from within any of the callbacks provided by the library it is save to call Get_DI_Channels_Raw() in order
20  to low level access the MCP23017 chip of the master module.
21  Make sure to not use Get_DI_Channels_Raw() from any other thread but the libraries background thread!
22  Maximum frequencies that can be achieved this way are around 2kHz. However, note that nearly no other application
23  will run during the "blocking" of a callback function due to the high priority it runs at!
24 */
25 
26 void CycleTickCallback (unsigned long* pADChannelValues, unsigned char* pDigitalInputValues, unsigned char* pDigitalOutputValues);
27 
28 int main(void)
29 {
30  // 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"
31  #ifdef BPI
32  g_I2CConfig.m_sI2CDevice = "/dev/i2c-0";
33  #endif
34 
35  // Initialize the library once in your program.
36  if (!Initialize())
37  {
38  printf ("Error: Initialize\n");
39  return 1;
40  }
41 
42  // Let's sample every 1s (1Hz).
43  Set_Cycle_Time (1000);
44 
45  // Register the callback.
47 
48  // Let the main thread run a while... in between use the cycle tick callback for latching digital inputs.
49  for (int i=0; i<10; i++)
50  {
51  printf ("Main thread waiting...\n");
52  usleep(1000000);
53  }
54 
55  // Once not needed anymore the callbacks can be unregistered.
57 
58  // Free library resources.
59  Uninitialize();
60 
61  return 0;
62 }
63 
64 void CycleTickCallback (unsigned long* pADChannelValues, unsigned char* pDigitalInputValues, unsigned char* pDigitalOutputValues)
65 {
66  // Usually we'd avoid lengthily operations in callback routines as this would lead the cycle tick to get broken.
67  // However, this time we'll demonstrate how fast we can latch 1000 digital inputs.
68 
69  printf ("CycleTickCallback called.\n");
70 
71  unsigned char value = 0;
72  unsigned long long startTime = GetTime_MicroSeconds();
73 
74  // Read the input 1000 times.
75  for (int i=0; i<1000; i++)
76  {
77  if (!Get_DI_Channels_Raw (value))
78  assert (false);
79  }
80  unsigned long long stopTime = GetTime_MicroSeconds();
81 
82  printf ("Reading digital inputs 1000 times lasts %lfms.\n", (stopTime-startTime)/1000.0);
83  // Reading digital inputs 1000 times lasts 473.426000ms.
84  // Note - this refers to the standard I2C clock speed of 100kHz. The speed can be changed by adding the following line
85  // to /boot/config.txt:
86  // dtparam=i2c1_baudrate=50000
87  // The above line will lower the clock speed to 50kHz. Double check the speed active in the system via:
88  // sudo cat /sys/module/i2c_bcm2708/parameters/baudrate
89 }
90 
const char * m_sI2CDevice
Definition: hubocfg.h:48
int main(void)
bool Initialize()
Initializes the library.
bool Get_DI_Channels_Raw(unsigned char &values)
Retrieves the unbuffered values of all digital inputs (of the Hubo master module).
bool Set_Cycle_Time(long cycleTime)
Sets the backgrounds threads polling interval in ms.
void CycleTickCallback(unsigned long *pADChannelValues, unsigned char *pDigitalInputValues, unsigned char *pDigitalOutputValues)
bool Unregister_CycleTickCallback(T_pfn_CycleTickCallback pFnCycleTickCallback)
Unregister a callback previouly registered by a call to Register_CycleTickCallback().
I2C_Config g_I2CConfig
void Uninitialize()
Releases any resources bound to the library.
unsigned long long GetTime_MicroSeconds()
Returns the number of micro seconds.
bool Register_CycleTickCallback(T_pfn_CycleTickCallback pFnCycleTickCallback)
Registers a callback function called on every cycle tick.