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
DigitalOutput1.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++ DigitalOutput1.cpp -L../ -lhubo -lpthread -lrt -o DigitalOutput1.out
12 Run:
13 sudo ./DigitalOutput1.out
14 Purpose:
15 Set all digital outputs in one go or one by one.
16 */
17
18 int main(void)
19 {
20 // 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"
21 #ifdef BPI
22 g_I2CConfig.m_sI2CDevice = "/dev/i2c-0";
23 #endif
24
25 // Initialize the library once in your program.
26 if (!Initialize())
27 {
28 printf ("Error: Initialize\n");
29 return 1;
30 }
31
32 // Reset all channels.
33 printf ("Clearing all channels\n");
34 Set_DO_Channels(0x00);
35 usleep (1000000);
36
37 // Set one channel after the other.
38 for (int i=0; i<8; i++)
39 {
40 // Turn on channel i.
41 printf ("Setting channel %d\n", i);
42 Set_DO_Channel(i, 1);
43 usleep (1000000);
44 }
45
46 // Clear one channel after the other.
47 for (int i=0; i<8; i++)
48 {
49 // Turn off channel i.
50 printf ("Clearing channel %d\n", i);
51 Set_DO_Channel(i, 0);
52 usleep (1000000);
53 }
54
55 // Set all channels.
56 printf ("Setting all channels\n");
57 Set_DO_Channels(0xFF);
58
59 // If you omit this "wait" then it might happen that the last call to Set_DO_Channels(0xFF) is not
60 // sent to the hardware anymore since the Uninitialize() call below has terminated before!
61 usleep (1000000);
62
63 // Free library resources.
64 Uninitialize();
65
66 return 0;
67 }
HuboLib::I2C_Config::m_sI2CDevice
const char * m_sI2CDevice
Definition: hubocfg.h:48
HuboLib::Set_DO_Channel
bool Set_DO_Channel(int channel, bool bValue)
Requests the background thread to set one of the digital outputs to the value specified.
HuboLib::Initialize
bool Initialize()
Initializes the library.
main
int main(void)
Definition: DigitalOutput1.cpp:18
HuboLib::Set_DO_Channels
bool Set_DO_Channels(unsigned char value)
Requests the background thread to update all 8 bits of the digital output to the value specified...
HuboLib::g_I2CConfig
I2C_Config g_I2CConfig
HuboLib::Uninitialize
void Uninitialize()
Releases any resources bound to the library.
HuboLib
Definition: hubocfg.h:10