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
DigitalSlaveOutput3.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++ DigitalSlaveOutput3.cpp -L../ -lhubo -lpthread -lrt -o DigitalSlaveOutput3.out
13 Run:
14 sudo ./DigitalSlaveOutput3.out
15 Purpose:
16 As long as the buffers are not filled the read-back of the DO channels will return initialization values - hence 0.
17 Thus the read-back value might not be the one the hardware is currently set to. In order to test this sample you first
18 need to set one or more of the slave outputs (e.g. use DigitalSlaveOutput1.cpp) and then run this program.
19 */
20
21 void BoostThreadPriority();
22
23 int main(void)
24 {
25 printf("Prior to running this sample ensure that at least on digital output is set (e.g. use DigitalSlaveOutput1.cpp)!\n");
26 int slaveNo = 0; // ... resulting in i2c address 0x21.
27 unsigned char currentDigitalOutputs = 0;
28
29 // We need to boost the main threads priority in order to compete with the Hubo-libraries background thread.
30 // Otherwise we cannot demonstrate the issue as the background thread is likely to first finish the update prior
31 // to us continuing reading back the output channels!
32 BoostThreadPriority();
33
34 // 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"
35 #ifdef BPI
36 g_I2CConfig.m_sI2CDevice = "/dev/i2c-0";
37 #endif
38
39 // Initialize the library once in your program.
40 if (!Initialize())
41 {
42 printf("Error: Initialize\n");
43 return 1;
44 }
45
46 // Without this line below you might read initialization values of the buffer rather than
47 // the real values of the output latches.
48 Wait_For_MCP23017_Slaves_Buffered_Values();
49
50 while (!currentDigitalOutputs)
51 {
52 Readback_Slave_DO_Channels(slaveNo, currentDigitalOutputs);
53 printf("Outputs read back are: 0x%02X\n", currentDigitalOutputs);
54 }
55
56 // Free library resources.
57 Uninitialize();
58
59 return 0;
60 }
61
62 #include <pthread.h>
63 void BoostThreadPriority()
64 {
65 // Scheduling params.
66 sched_param param;
67 int policy;
68 int ret;
69 pthread_t threadHandle = pthread_self();
70
71 // Get current scheduling parameters of thread.
72 ret = pthread_getschedparam (threadHandle, &policy, &param);
73 assert (ret == 0);
74
75 // Set scheduling parameters of thread to real time values (FIFO scheduling type and max prio).
76 policy = SCHED_FIFO; // SCHED_RR;
77 param.sched_priority = sched_get_priority_max(policy); // New max priority for new scheduling concept.
78 ret = pthread_setschedparam(threadHandle, policy, &param);
79 assert (ret == 0);
80 }
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: DigitalSlaveOutput3.cpp:23
HuboLib::Wait_For_MCP23017_Slaves_Buffered_Values
bool Wait_For_MCP23017_Slaves_Buffered_Values()
Waits until input and output buffers of the MCP23017 slaves are initialized from the hardware...
HuboLib::g_I2CConfig
I2C_Config g_I2CConfig
HuboLib::Readback_Slave_DO_Channels
bool Readback_Slave_DO_Channels(int slaveNo, unsigned char &value)
Reads back the digital output latchs of the IO expander for a given slave.
HuboLib::Uninitialize
void Uninitialize()
Releases any resources bound to the library.
BoostThreadPriority
void BoostThreadPriority()
Definition: DigitalSlaveOutput3.cpp:63
HuboLib
Definition: hubocfg.h:10