Skip to content

Commit

Permalink
Add tm1637_say options for GPIO lib & verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
neildavis committed Mar 9, 2023
1 parent 8531b98 commit ed20244
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
23 changes: 12 additions & 11 deletions say/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,35 @@ A simple utility using [lib_tm1637_rpi](../README.md) to scroll some text on a [

Ensure you have [built & installed libTM1637RPi](../README.md#building--installing) before building `tm1637_say`.

Use the [build.sh](./build.sh) script to build the utility. By default, the utility will use the `GPIOD` library. This can be overridden by setting the `GPIO_LIB` environment variable to one of the `GPIOLib` enum values defined in [tm1637.h](../inc/tm1637.h#L34)

e.g. to build using `wiringPi` (with BCM pin numbering):
Use the [build.sh](./build.sh) script to build the utility.

```sh
GPIO_LIB="GpioWiringPiBCM" ./build.sh
./build.sh
```

## Running

Running `tm1637_say` without any arguments, or with `--help` will display help & usage information:

```sh
```none
$ tm1637_say
tm1637_say utility - © 2023 Neil Davis
See LICENSE at https://github.com/neildavis/lib_tm1637_rpi/blob/main/LICENSE
Usage: tm1637_say [OPTIONS] <message>
Available OPTIONS:
--help Show usage information
-c [ --scl ] arg (=3) GPIO pin to use for clock
-d [ --sda ] arg (=2) GPIO pin to use for data
-t [ --delay-time ] arg (=250) Delay time between characters (ms)
-h [ --help ] Show usage information
-c [ --scl ] arg (=3) GPIO pin to use for clock
-d [ --sda ] arg (=2) GPIO pin to use for data
-g [ --gpio-lib ] arg (=GpioGPIOD) GPIO library
-t [ --delay-time ] arg (=250) Delay time between characters (ms)
-n [ --count ] arg (=1) Repeat the message <arg> number of times
-v [ --verbose ] [=arg(=1)] (=0) Enable/Disable verbose output
```

e.g. the following invocation uses pins 23 & 24 for `SCL` & `SDA` respectively, and displays a message faster than the default:
e.g. the following invocation uses the wiringPi library with BCM pin numbering, pins 23 & 24 for `SCL` & `SDA` respectively, and displays a message faster than the default three times over :

```sh
tm1637_say -c 23 -d 24 -t 100 "HELLO WORLD"
tm1637_say -c 23 -d 24 -g GpioWiringPiBCM -t 100 -n 3 "HELLO WORLD"
```
6 changes: 1 addition & 5 deletions say/build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#!/usr/bin/env bash

if [[ -z "${GPIO_LIB}" ]]; then
export GPIO_LIB="GpioGPIOD"
fi
echo "Building using GPIO library ${GPIO_LIB} - override by setting GPIO_LIB"
g++ -DGPIO_LIB="${GPIO_LIB}" -Wall -std=c++11 tm1637_say.cpp -o tm1637_say -lboost_program_options $(pkg-config --libs --cflags libTM1637Pi)
g++ -Wall -std=c++11 tm1637_say.cpp -o tm1637_say -lboost_program_options $(pkg-config --libs --cflags libTM1637Pi)
30 changes: 23 additions & 7 deletions say/tm1637_say.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@

namespace po = boost::program_options;

// If no GPIO lib is declared on command line, use GPIOD by default
#if !defined(GPIO_LIB)
#define GPIO_LIB GpioGPIOD
#endif
tm1637::GPIOLib gpioLibrary(std::string &str) {
if (str == "GpioWiringPi") return tm1637::GpioWiringPi;
if (str == "GpioWiringPiBCM") return tm1637::GpioWiringPiBCM;
if (str == "GpioPigpioInterface") return tm1637::GpioPigpioInterface;
if (str == "GpioPigpioDaemon") return tm1637::GpioPigpioDaemon;
str = "GpioGPIOD";
return tm1637::GpioGPIOD;
}

int main(int argc, char**argv) {
po::options_description user_options(
Expand All @@ -24,8 +28,10 @@ int main(int argc, char**argv) {
("help,h", "Show usage information")
("scl,c", po::value<int>()->default_value(3), "GPIO pin to use for clock")
("sda,d", po::value<int>()->default_value(2), "GPIO pin to use for data")
("gpio-lib,g", po::value<std::string>()->default_value("GpioGPIOD"), "GPIO library")
("delay-time,t", po::value<int>()->default_value(250), "Delay time between characters (ms)")
("count,n", po::value<int>()->default_value(1), "Repeat the message <arg> number of times")
("verbose,v", po::value<bool>()->default_value(false)->implicit_value(true), "Enable/Disable verbose output")
;
po::options_description hidden_options("Hidden options");
hidden_options.add_options()
Expand All @@ -52,10 +58,20 @@ int main(int argc, char**argv) {
int pinSCL = vm["scl"].as<int>();
int delay_ms = vm["delay-time"].as<int>();
int count = vm["count"].as<int>();

auto tm1637 = std::unique_ptr<tm1637::Device>(new tm1637::Device(pinSCL, pinSDA, tm1637::GPIO_LIB));
bool verbose = vm["verbose"].as<bool>();
auto message = vm["message"].as<std::string>();
auto gpioLibStr = vm["gpio-lib"].as<std::string>();
auto gpioLib = gpioLibrary(gpioLibStr);

if (verbose) {
std::cout << "Using GPIO library '" << gpioLibStr << "' with pins " << pinSCL << " (SCL) and " << pinSDA << " (SDA)" << std::endl;
std::cout << "Displaying message '" << message << "'" << std::endl;
std::cout << "Repeating " << count << " time(s) with delay time " << delay_ms << "ms" << std::endl;
}

auto tm1637 = std::unique_ptr<tm1637::Device>(new tm1637::Device(pinSCL, pinSDA, gpioLib));
tm1637::Sayer sayer(tm1637);
sayer.begin(vm["message"].as<std::string>());
sayer.begin(message);
for (int i = 0; i < count; i++) {
while (sayer.next()) {
std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
Expand Down

0 comments on commit ed20244

Please sign in to comment.