Hubo Library

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
DigitalInput5.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++ DigitalInput5.cpp -L../ -lhubo -lpthread -lrt -o DigitalInput5.out
12 Run:
13 sudo ./DigitalInput5.out
14 Purpose:
15 Let's get notified whenever a digital input changes.
16 The program will terminate when digital input 2 is lowered to ground.
17 */
18
19 // This function is called whenever a digital input changes.
20 void DIChangedCallback (unsigned char changedInputs, unsigned char allInputs);
21
22 bool bTerminate = false;
23
24 int main(void)
25 {
26 // 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"
27 #ifdef BPI
28 g_I2CConfig.m_sI2CDevice = "/dev/i2c-0";
29 #endif
30
31 // Initialize the library once in your program.
32 if (!Initialize())
33 {
34 printf ("Error: Initialize\n");
35 return 1;
36 }
37
38 // After initialization the callback can be registered.
39 Register_DIChangedCallback (DIChangedCallback);
40
41 while (!bTerminate)
42 {
43 usleep (1000);
44 }
45
46 // It's good programming style to de-register the callback.
47 Unregister_DIChangedCallback (DIChangedCallback);
48
49 // Free library resources.
50 Uninitialize();
51
52 return 0;
53 }
54
55 void DIChangedCallback (unsigned char changedInputs, unsigned char allInputs)
56 {
57 // This function is called from the background thread running at high priority.
58 // Thus lengthy operations (such as prinf) should not be performed as they
59 // could exceed the time slice for the cycle tick.
60 // Note that the synchronisation with the main thread is missing for the bool
61 // variable bTerminate and should be done in a proper application.
62 printf ("DIChangedCallback changed=0x%02X new=0x%02X\n", changedInputs, allInputs);
63
64 // Has the input 2 changed?
65 if (changedInputs & 4)
66 bTerminate = true;
67 }
HuboLib::I2C_Config::m_sI2CDevice
const char * m_sI2CDevice
Definition: hubocfg.h:48
HuboLib::Initialize
bool Initialize()
Initializes the library.
main
int main(void)
Definition: DigitalInput5.cpp:24
DIChangedCallback
void DIChangedCallback(unsigned char changedInputs, unsigned char allInputs)
Definition: DigitalInput5.cpp:55
HuboLib::Unregister_DIChangedCallback
bool Unregister_DIChangedCallback(T_pfn_DIChangedCallback pFnDIChangedCallback)
Unregister a callback previouly registered by a call to Register_DIChangedCallback().
bTerminate
bool bTerminate
Definition: DigitalInput5.cpp:22
HuboLib::g_I2CConfig
I2C_Config g_I2CConfig
HuboLib::Register_DIChangedCallback
bool Register_DIChangedCallback(T_pfn_DIChangedCallback pFnDIChangedCallback)
Register a callback to be called whenever one or more of the digital inputs have changed.
HuboLib::Uninitialize
void Uninitialize()
Releases any resources bound to the library.
HuboLib
Definition: hubocfg.h:10