Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
FanSpeed.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++ FanSpeed.cpp -L../ -lhubo -lpthread -lrt -o FanSpeed.out
12 Run:
13  sudo ./FanSpeed.out
14 Purpose:
15  Simple demo doing sending ON-OFF patterns to a digital output e.g. to control fan speed.
16 */
17 
18 #define FAN_CHANNEL 0
19 #define WAIT_TIME 10000
20 
21 
22 int main(void)
23 {
24  // 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"
25  #ifdef BPI
26  g_I2CConfig.m_sI2CDevice = "/dev/i2c-0";
27  #endif
28 
29  // Initialize the library once in your program.
30  if (!Initialize())
31  {
32  printf ("Error: Initialize\n");
33  return 1;
34  }
35 
36  // Set background cycle time to 5ms in order to have the digital IO's being updated every 5ms.
37  Set_Cycle_Time(5);
38 
39  // The pattern to use to update the FAN-CHANNEL IO pin.
40 // int channelPattern [10] = {0, 0, 1, -1}; // 33% duty time
41 // int channelPattern [10] = {0, 1, -1}; // 50% duty time
42 // int channelPattern [10] = {0, 1, 1, -1}; // 66% duty time
43  int channelPattern [10] = {0, 1, 1, 1, -1}; // 75% duty time
44 // int channelPattern [10] = {1, -1}; // 100% duty time
45 
46  // This rover goes round and round.
47  int patternRover = 0;
48 
49  // Cyclic update of the FAN_CHANNEL IO pin.
50  while (1)
51  {
52  int value = channelPattern[patternRover++];
53  if (value == -1)
54  {
55  patternRover = 0;
56  continue;
57  }
58 
59  // Set output to new value.
61 
62  // Wait a while.
63  usleep (WAIT_TIME);
64  }
65 
66  // Free library resources (despite the fact we'll never get here).
67  Uninitialize();
68 
69  return 0;
70 }
const char * m_sI2CDevice
Definition: hubocfg.h:48
bool Set_DO_Channel(int channel, bool bValue)
Requests the background thread to set one of the digital outputs to the value specified.
bool Initialize()
Initializes the library.
bool Set_Cycle_Time(long cycleTime)
Sets the backgrounds threads polling interval in ms.
#define FAN_CHANNEL
Definition: FanSpeed.cpp:18
#define WAIT_TIME
Definition: FanSpeed.cpp:19
I2C_Config g_I2CConfig
int main(void)
Definition: FanSpeed.cpp:22
void Uninitialize()
Releases any resources bound to the library.