Waveshare 8ch Relay Board Example
A simple repository containing (functioning) examples of how to use the Waveshare 8ch relay board.
Functions | Variables
channel_select.cpp File Reference
#include <algorithm>
#include <iostream>
#include <fstream>
#include <regex>
#include <string>
#include <stdint.h>
#include <sys/stat.h>
#include <unistd.h>
Include dependency graph for channel_select.cpp:

Go to the source code of this file.

Functions

template<typename... Args>
string formatString (const string &format, Args... args)
 Simple string formatter template function. More...
 
bool directoryExists (string dirPath)
 Whether or not a given directory exists or not. More...
 
bool parseArgs (int32_t argC, char **argV)
 Parses command-line arguments passed to the application. More...
 
void enableGpio ()
 Enables a given GPIO pin. More...
 
void setGpio ()
 Sets a GPIO pin on/off, depending on the desired state. More...
 
void showHelp ()
 Prints the help text. More...
 
int main (int32_t argC, char **argV)
 

Variables

static uint32_t g_channel = 0
 The selected channel to enable and/or toggle. More...
 
static bool g_state = false
 The desired state for the channel (on/off) More...
 
static const uint32_t CHANNELS [8]
 A simple array representing a map to the channels on the relay board, corresponding to the GPIO pings. More...
 

Function Documentation

◆ directoryExists()

bool directoryExists ( string  dirPath)

Whether or not a given directory exists or not.

Determines whether or not a given directory exists.

Parameters
dirPathThe path to check.
Returns
true If the directory exists.
false Otherwise.

Definition at line 99 of file channel_select.cpp.

99  {
100  struct stat _stat;
101  if (stat(dirPath.c_str(), &_stat) == 0 && S_ISDIR(_stat.st_mode)) {
102  return true;
103  }
104  return false;
105 }

◆ enableGpio()

void enableGpio ( )

Enables a given GPIO pin.

Enables use of the desired GPIO pin.

Definition at line 142 of file channel_select.cpp.

142  {
143  static const string GPIO_DIR_PATH = "/sys/class/gpio/gpio";
144 
145  const auto gpioPath = formatString("%s%d", GPIO_DIR_PATH.c_str(), g_channel);
146 
147  if (directoryExists(gpioPath)) return;
148 
149  // Make sure Linux exports the pin
150  {
151  std::ofstream fileStream("/sys/class/gpio/export", std::ios::trunc);
152  fileStream << g_channel << endl;
153  }
154 
155  // Set the GPIO direction
156  {
157  std::ofstream fileStream(formatString("%s/direction", gpioPath.c_str()), std::ios::trunc);
158  fileStream << "out" << endl;
159  }
160 }
static uint32_t g_channel
The selected channel to enable and/or toggle.
string formatString(const string &format, Args... args)
Simple string formatter template function.
bool directoryExists(string dirPath)
Whether or not a given directory exists or not.

References g_channel.

◆ formatString()

template<typename... Args>
string formatString ( const string &  format,
Args...  args 
)

Simple string formatter template function.

Template Parameters
ArgsThe arguments to be formatted into the string
Parameters
formatThe C-style format string
argsVarargs list of arguments
Returns
string The formatted string.

Definition at line 60 of file channel_select.cpp.

60  {
61  auto stringSize = snprintf(NULL, 0, format.c_str(), args...) + 1; // +1 for \0
62  std::unique_ptr<char[]> buffer(new char[stringSize]);
63 
64  snprintf(buffer.get(), stringSize, format.c_str(), args...);
65 
66  return string(buffer.get(), buffer.get() + stringSize - 1); // std::string handles termination for us.
67 }

◆ main()

int main ( int32_t  argC,
char **  argV 
)

Definition at line 79 of file channel_select.cpp.

79  {
80 
81  if (argC <= 2) {
82  showHelp();
83  return -1;
84  }
85 
86  if (!parseArgs(argC, argV)) return -1;
87 
88  return 0;
89 }
bool parseArgs(int32_t argC, char **argV)
Parses command-line arguments passed to the application.
void showHelp()
Prints the help text.

References parseArgs(), and showHelp().

◆ parseArgs()

bool parseArgs ( int32_t  argC,
char **  argV 
)

Parses command-line arguments passed to the application.

Parses arguments passed to the application and sets the required parameters accordingly.

Parameters
argCThe total arg count
argVA pointer-pointer to the arg list
Returns
true If the application should stay alive
false Otherwise

Definition at line 116 of file channel_select.cpp.

116  {
117  const static regex CHANNEL_PATTERN(R"((ch)?[0-8])");
118  const static regex STATE_PATTERN(R"(on|off|true|false)");
119 
120  for (int32_t i = 0; i < argC; i++) {
121  string arg = argV[i];
122 
123  // Convert arg to lower
124  transform(arg.begin(), arg.end(), arg.begin(), [&](const auto c) { return tolower(c); });
125 
126  if (regex_search(arg, CHANNEL_PATTERN)) {
127  g_channel = std::stoi(arg.substr(arg.size() - 1, 1));
128  } else if (regex_search(arg, STATE_PATTERN)) {
129  g_state = (arg == "on" || arg == "true");
130  } else {
131  showHelp();
132  return false;
133  }
134  }
135 
136  return true;
137 }
static bool g_state
The desired state for the channel (on/off)

References g_channel, g_state, and showHelp().

Referenced by main().

◆ setGpio()

void setGpio ( )

Sets a GPIO pin on/off, depending on the desired state.

Sets the desired GPIO pin either on or off, depending on cmd-args.

Definition at line 165 of file channel_select.cpp.

165  {
166  static const string GPIO_DIR_PATH = "/sys/class/gpio/gpio";
167 
168  const auto gpioPath = formatString("%s%d/value", GPIO_DIR_PATH.c_str(), g_channel);
169 
170  std::ofstream fileStream(gpioPath, std::ios::trunc);
171  fileStream << static_cast<int32_t>(g_state) << endl;
172 }

References g_channel, and g_state.

◆ showHelp()

void showHelp ( )

Prints the help text.

Prints the help text to the console.

Definition at line 177 of file channel_select.cpp.

177  {
178  cout << R"(
179 Usage:

Referenced by main(), and parseArgs().

Variable Documentation

◆ CHANNELS

const uint32_t CHANNELS[8]
static
Initial value:
{
5, 6, 13, 16, 19, 20, 21, 26
}

A simple array representing a map to the channels on the relay board, corresponding to the GPIO pings.

E.g.: CHANNELS[0] = channel 1 = GPIO5

Definition at line 45 of file channel_select.cpp.

◆ g_channel

uint32_t g_channel = 0
static

The selected channel to enable and/or toggle.

Definition at line 36 of file channel_select.cpp.

Referenced by enableGpio(), parseArgs(), and setGpio().

◆ g_state

bool g_state = false
static

The desired state for the channel (on/off)

Definition at line 37 of file channel_select.cpp.

Referenced by parseArgs(), and setGpio().