Skip to content

Commit 65615d2

Browse files
authored
Fixed a stupid bug in the C++ application (#4)
* - Renamed `getChannel` to `getChannelState` because `getChannel` is a stupid name for this function. - Fixed a stupid bug where I mixed up GPIOs and channels - Reading channels, enabling channels and disabling channels now works as expected. - Fixed help text - Channel arguments now no longer have optional flags - Fixed small logic error in the output text. * Added new `all` switch * Added basic toolchain files for CMake * Bumped up version to next minor
1 parent 1f26421 commit 65615d2

File tree

4 files changed

+81
-34
lines changed

4 files changed

+81
-34
lines changed

cpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.20)
22

3-
project(waveshare_channel_select LANGUAGES CXX VERSION 1.0.0)
3+
project(waveshare_channel_select LANGUAGES CXX VERSION 1.1.0)
44
set(CMAKE_CXX_STANDARD 20)
55
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
66

cpp/channel_select.cpp

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ using optsm_t = optional<StateModifier>;
8484
optsm_t stateModifierFromString(const string& optArg); //!< Converts an optarg value to a boolean
8585
int32_t parseArgs(int32_t argc, char** argv); //!< Parses arguments passed to the application
8686

87-
bool getChannel(const int32_t channelId); //!< Gets the state of a given channel
87+
bool getChannelState(const int32_t channelId); //!< Gets the state of a given channel
8888
void listChannels(); //!< Lists all channels available with their state.
8989
void setChannel(const int32_t channelId, const StateModifier newState); //!< Sets the state of a given channel
9090

@@ -111,11 +111,11 @@ int32_t main(int32_t argc, char** argv) {
111111
setChannel(channel, *state);
112112
break;
113113
default:
114-
fmt::println("Channel {0:d} (GPIO pin {1:d}) set to {2:s}", channel, CHANNELS[channel], getChannel(CHANNELS[channel]) ? "OFF" : "ON");
114+
fmt::println("Channel {0:d} (GPIO pin {1:d}) set to {2:s}", channel + 1, CHANNELS[channel], getChannelState(CHANNELS[channel]) ? "OFF" : "ON");
115115
break;
116116
}
117117
} else if (g_stateModifier == StateModifier::READ) {
118-
fmt::println("Channel {0:d} (GPIO pin {1:d}) set to {2:s}", channel, CHANNELS[channel], getChannel(CHANNELS[channel]) ? "OFF" : "ON");
118+
fmt::println("Channel {0:d} (GPIO pin {1:d}) set to {2:s}", channel + 1, CHANNELS[channel], getChannelState(CHANNELS[channel]) ? "OFF" : "ON");
119119
continue;
120120
} else {
121121
setChannel(channel, g_stateModifier);
@@ -135,35 +135,36 @@ int32_t main(int32_t argc, char** argv) {
135135
*/
136136
int32_t parseArgs(int32_t argc, char** argv) {
137137
constexpr option APP_OPTIONS[] = {
138-
{ "help", no_argument, nullptr, 'h' },
139-
{ "version", no_argument, nullptr, 'v' },
138+
{ "help", no_argument, nullptr, 'h' },
139+
{ "version", no_argument, nullptr, 'v' },
140140

141141
// channels
142-
{ "channel1", optional_argument, nullptr, '1' },
143-
{ "channel2", optional_argument, nullptr, '2' },
144-
{ "channel3", optional_argument, nullptr, '3' },
145-
{ "channel4", optional_argument, nullptr, '4' },
146-
{ "channel5", optional_argument, nullptr, '5' },
147-
{ "channel6", optional_argument, nullptr, '6' },
148-
{ "channel7", optional_argument, nullptr, '7' },
149-
{ "channel8", optional_argument, nullptr, '8' },
142+
{ "channel1", no_argument, nullptr, '1' },
143+
{ "channel2", no_argument, nullptr, '2' },
144+
{ "channel3", no_argument, nullptr, '3' },
145+
{ "channel4", no_argument, nullptr, '4' },
146+
{ "channel5", no_argument, nullptr, '5' },
147+
{ "channel6", no_argument, nullptr, '6' },
148+
{ "channel7", no_argument, nullptr, '7' },
149+
{ "channel8", no_argument, nullptr, '8' },
150+
{ "all", no_argument, nullptr, 'a' },
150151

151152
// modifiers
152-
{ "enable", no_argument, nullptr, 'e' },
153-
{ "disable", no_argument, nullptr, 'd' },
154-
{ "read-state", no_argument, nullptr, 'r' },
153+
{ "enable", no_argument, nullptr, 'e' },
154+
{ "disable", no_argument, nullptr, 'd' },
155+
{ "read-state", no_argument, nullptr, 'r' },
155156

156157
// other operations
157-
{ "list-all", no_argument, nullptr, 'L' }
158+
{ "list-all", no_argument, nullptr, 'L' }
158159
};
159-
constexpr const char* APP_SHORTOPTS = R"(hv1::2::3::4::5::6::7::8::edrL)";
160+
constexpr const char* APP_SHORTOPTS = R"(hv12345678edrLa)";
160161

161162
int32_t optChar = -1;
162163

163164
while ((optChar = getopt_long(argc, argv, APP_SHORTOPTS, APP_OPTIONS, nullptr)) != -1) {
164165
if (optChar >= 49 && optChar <= 56) {
165166
// Handle channel inputs
166-
g_channelOptions.try_emplace(CHANNELS[optChar - 49], stateModifierFromString(optarg == nullptr ? string{} : optarg));
167+
g_channelOptions.try_emplace(optChar - 49, std::nullopt);
167168
} else {
168169
switch (optChar) {
169170
case 'h':
@@ -184,14 +185,19 @@ int32_t parseArgs(int32_t argc, char** argv) {
184185
case 'L':
185186
g_readAll = true;
186187
break;
188+
case 'a':
189+
for (int32_t i = 0; i < 8; i++) {
190+
g_channelOptions.try_emplace(i, std::nullopt);
191+
}
192+
break;
187193
}
188194
}
189195
}
190196

191197
return 0;
192198
}
193199

194-
bool getChannel(const int32_t channel) {
200+
bool getChannelState(const int32_t channel) {
195201
auto gpioChip = gpiod::chip(string{GPIO_CHIP});
196202
auto lineSettings = gpiod::line_settings{};
197203
auto line = gpioChip.prepare_request().add_line_settings(gpioChip.get_line_offset_from_name(fmt::format("GPIO{0:d}", channel)), lineSettings).do_request();
@@ -221,7 +227,7 @@ void listChannels() {
221227
size_t channelCounter = 1;
222228

223229
for (const auto channel : CHANNELS) {
224-
fmt::println("Channel {0:d} (GPIO pin {1:d}) set to {2:s}", channelCounter++, channel, getChannel(channel) ? "OFF" : "ON");
230+
fmt::println("Channel {0:d} (GPIO pin {1:d}) set to {2:s}", channelCounter++, channel, getChannelState(channel) ? "OFF" : "ON");
225231
}
226232
}
227233

@@ -232,7 +238,7 @@ void printHelp() {
232238
Usage:
233239
{0:s} -h
234240
{0:s} -e -123 # enable channels 1, 2, and 3
235-
{0:s} -d -528 -7=r # disable channels 5, 2, and 8 and read the state of channel 7
241+
{0:s} -d -528 # disable channels 5, 2, and 8 and read the state of channel 7
236242
{0:s} -L # read the states of all channels
237243
238244
Troubleshooting:
@@ -241,21 +247,22 @@ void printHelp() {
241247
242248
Options:
243249
Channel selection:
244-
--channel1, -1[=edr] Channel 1
245-
--channel2, -2[=edr] Look, it's the same up until -8
250+
--channel1, -1 Channel 1
251+
--channel2, -2 Look, it's the same up until -8
246252
...
247-
--channel8, -8[=edr] I'm sure it's self-explanatory at this point
253+
--channel8, -8 I'm sure it's self-explanatory at this point
254+
--all, -a All channels
248255
249256
Channel options:
250-
--enable, -e Enable channel(s)
251-
--disable, -d Disable channel(s)
252-
--read, -r Read the channel state
257+
--enable, -e Enable channel(s)
258+
--disable, -d Disable channel(s)
259+
--read, -r Read the channel state
253260
254261
General options:
255-
--list-all, -L List all channels and their current state
262+
--list-all, -L List all channels and their current state
256263
257-
--help, -h Displays this text and exits
258-
--version, -v Displays the version information and exits
264+
--help, -h Displays this text and exits
265+
--version, -v Displays the version information and exits
259266
)", APP_NAME, APP_VERS);
260267

261268
}
@@ -265,13 +272,13 @@ void printVersion() {
265272
}
266273

267274
void setChannel(const int32_t channel, const StateModifier newState) {
268-
const auto gpioLineName = fmt::format("GPIO{0:d}", channel);
275+
const auto gpioLineName = fmt::format("GPIO{0:d}", CHANNELS[channel]);
269276

270277
auto gpioChip = gpiod::chip(string{GPIO_CHIP});
271278
auto lineSettings = gpiod::line_settings{};
272279
lineSettings.set_direction(gpiod::line::direction::OUTPUT);
273280
auto line = gpioChip.prepare_request().add_line_settings(gpioChip.get_line_offset_from_name(gpioLineName), lineSettings).do_request();
274281

275-
fmt::println("Attempting to set GPIO{0:d} {1:s}", channel, newState == StateModifier::DISABLE ? "OFF" : "ON");
282+
fmt::println("Attempting to set GPIO{0:d} {1:s}", CHANNELS[channel], newState == StateModifier::DISABLE ? "ON" : "OFF");
276283
line.set_value(line.offsets()[0], newState == StateModifier::DISABLE ? gpiod::line::value::INACTIVE : gpiod::line::value::ACTIVE);
277284
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Set the CMake cross-compiling toolchain
2+
set(CMAKE_SYSTEM_NAME Linux)
3+
set(CMAKE_SYSTEM_PROCESSOR arm)
4+
5+
# Set the path to the aarch64-linux-gnu toolchain
6+
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
7+
set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)
8+
9+
# Set the target architecture
10+
set(CMAKE_LIBRARY_ARCHITECTURE aarch64-linux-gnu)
11+
12+
# Specify the linker
13+
set(CMAKE_LINKER aarch64-linux-gnu-ld)
14+
15+
# Specify the necessary libraries and include directories
16+
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
17+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
18+
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Set the CMake cross-compiling toolchain
2+
set(CMAKE_SYSTEM_NAME Linux)
3+
set(CMAKE_SYSTEM_PROCESSOR arm)
4+
5+
# Set the path to the arm-linux-gnueabihf toolchain
6+
set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
7+
set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
8+
9+
# Set the target architecture
10+
set(CMAKE_LIBRARY_ARCHITECTURE arm-linux-gnueabihf)
11+
12+
# Set other necessary flags and options
13+
set(CMAKE_C_FLAGS "-march=armv7-a -mfpu=neon -mfloat-abi=hard")
14+
set(CMAKE_CXX_FLAGS "-march=armv7-a -mfpu=neon -mfloat-abi=hard")
15+
16+
# Specify the linker
17+
set(CMAKE_LINKER arm-linux-gnueabihf-ld)
18+
19+
# Specify the necessary libraries and include directories
20+
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
21+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
22+
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

0 commit comments

Comments
 (0)