Skip to content

Commit

Permalink
🔩Code: Minor fixes & -q argument for suppressing the output
Browse files Browse the repository at this point in the history
  • Loading branch information
vookimedlo committed Nov 8, 2020
1 parent f2c8b53 commit 5981075
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ The resulting binary is called `cooling_hat`.

## Usage
```
./cooling_hat [-d] [-r fan_range:...] | [-h] | [-f fan_settings] | [-l led_settings] | [-e effect_settings]
./cooling_hat [-d] [-q] [-r fan_range:...] | [-h] | [-f fan_settings] | [-l led_settings] | [-e effect_settings]
The following options are available:
-h Shows usage.
-q Suppress logging.
-d Runs as a daemon.
-f fan_settings Sets the fan speed to the specified value and quits.
fan_settings:
Expand Down Expand Up @@ -90,7 +91,7 @@ UNIT LOAD ACTIVE S
cooling_hat.service loaded active running Yahboom's Cooling Hat control service
```

All logging is redirected to the `/var/log/syslog` which might be useful for debugging purposes.
All logging is redirected to the `/var/log/syslog` which might be useful for debugging purposes. The output can be suppressed by the `-q` argument, which is specified by default in the `systemd` service.

Many thanks to [@abecko][3] who provided his yahboom HW and got the idea for implementing this!

Expand Down
5 changes: 3 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ SET(SOURCES
cooling_hat_information.c
cooling_hat_oled.c
cooling_hat_rgb.c
cooling_hat_rgb_effect.c
cooling_hat_utils.c
3rd_party_code/ssd1306_i2c.c cooling_hat_rgb_effect.c cooling_hat_rgb_effect.h)
3rd_party_code/ssd1306_i2c.c)

ADD_EXECUTABLE(cooling_hat
${SOURCES} cooling_hat_utils.c)
${SOURCES})

TARGET_LINK_LIBRARIES(cooling_hat wiringPi)
20 changes: 14 additions & 6 deletions src/cooling_hat_arguments.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ static void parse_fan_ranges(const char *ranges_argument) {
}

// Create the own copy to prevent the modification of the input string during the tokenization
strncpy(ranges_argument_tmp_buffer, ranges_argument, sizeof(ranges_argument_tmp_buffer));
strncpy(ranges_argument_tmp_buffer, ranges_argument, sizeof(ranges_argument_tmp_buffer) - 1);
ranges_argument_tmp_buffer[sizeof(ranges_argument_tmp_buffer) - 1]= '\0';

// Tokenize the first range
ranges_argument_token = strtok(ranges_argument_tmp_buffer, ":");
Expand All @@ -103,7 +104,8 @@ static void parse_fan_ranges(const char *ranges_argument) {
break;

// Create the own copy to prevent the modification of the input range during the tokenization
strncpy(ranges_tmp_buffer, ranges_argument_token, sizeof(ranges_tmp_buffer));
strncpy(ranges_tmp_buffer, ranges_argument_token, sizeof(ranges_tmp_buffer) - 1);
ranges_tmp_buffer[sizeof(ranges_tmp_buffer) - 1]= '\0';

// Tokenize the temperature from the given range
token = strtok(ranges_tmp_buffer, ",");
Expand Down Expand Up @@ -154,9 +156,10 @@ static void parse_fan_ranges(const char *ranges_argument) {
}

static void show_usage(const char *name) {
PRINT("%s [-d] [-r fan_range:...] | [-h] | [-f fan_settings] | [-l led_settings] | [-e effect_settings]", name);
PRINT("%s [-d] [-q] [-r fan_range:...] | [-h] | [-f fan_settings] | [-l led_settings] | [-e effect_settings]", name);
PRINT("\n\tThe following options are available:");
PRINT("\t\t-h Shows usage.");
PRINT("\t\t-q Suppress logging.");
PRINT("\t\t-d Runs as a daemon.");
PRINT("\t\t-f fan_settings Sets the fan speed to the specified value and quits.");
PRINT("\t\t fan_settings:");
Expand Down Expand Up @@ -213,16 +216,20 @@ void handle_arguments(int argc, char *argv[]) {
char tmp_buffer[255];
char *token;

while ((opt = getopt(argc, argv, ":l:r:f:hd")) != -1) {
while ((opt = getopt(argc, argv, ":l:r:f:hdq")) != -1) {
switch (opt) {
case 'q':
suppress_logs = true;
break;
case 'd':
is_daemon = true;
PRINT("[APP] Will run as a daemon");
break;
case 'l':
is_rgb_only = true;
PRINT("[APP] LED will be set to %s", optarg);
strncpy(tmp_buffer, optarg, sizeof(tmp_buffer));
strncpy(tmp_buffer, optarg, sizeof(tmp_buffer) - 1);
tmp_buffer[sizeof(tmp_buffer) - 1]= '\0';
token = strtok(tmp_buffer, ",");
if (!atoi_ex(token, &rgb_number) || (rgb_number > 3)) {
PRINT("[APP] Wrong format of the LED number: %s\n", optarg);
Expand Down Expand Up @@ -251,7 +258,8 @@ void handle_arguments(int argc, char *argv[]) {
case 'e':
is_effect_only = true;
PRINT("[APP] LED effect will be set to %s", optarg);
strncpy(tmp_buffer, optarg, sizeof(tmp_buffer));
strncpy(tmp_buffer, optarg, sizeof(tmp_buffer) - 1);
tmp_buffer[sizeof(tmp_buffer) - 1]= '\0';
token = strtok(tmp_buffer, ",");
if (!atoi_ex(token, &effect) || (effect > 4)) {
PRINT("[APP] Wrong format of the effect: %s\n", optarg);
Expand Down
1 change: 1 addition & 0 deletions src/cooling_hat_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "cooling_hat_utils.h"

bool has_tty = true;
bool suppress_logs = false;
37 changes: 20 additions & 17 deletions src/cooling_hat_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,35 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define COOLING_UTILS_H

#include <stdbool.h>
#include <stdio.h>
#include <syslog.h>

extern bool has_tty;
extern bool suppress_logs;

#if defined(NDEBUG)
#define DEBUG_PRINT(...)
#else

#include <stdio.h>
#else // !NDEBUG

#define DEBUG_PRINT(FORMAT, ...) \
do { \
if (has_tty) \
fprintf(stderr, FORMAT "\n" __VA_OPT__(,) __VA_ARGS__); \
else \
syslog(LOG_DEBUG, FORMAT __VA_OPT__(,) __VA_ARGS__); \
#define DEBUG_PRINT(FORMAT, ...) \
do { \
if (!suppress_logs) { \
if (has_tty) \
fprintf(stderr, FORMAT "\n" __VA_OPT__(,) __VA_ARGS__); \
else \
syslog(LOG_DEBUG, FORMAT __VA_OPT__(,) __VA_ARGS__); \
} \
} while(0)
#endif // DEBUG
#endif // !NDEBUG

#define PRINT(FORMAT, ...) \
do { \
if (has_tty) \
fprintf(stderr, FORMAT "\n" __VA_OPT__(,) __VA_ARGS__); \
else \
syslog(LOG_INFO, FORMAT __VA_OPT__(,) __VA_ARGS__); \
#define PRINT(FORMAT, ...) \
do { \
if (!suppress_logs) { \
if (has_tty) \
fprintf(stderr, FORMAT "\n" __VA_OPT__(,) __VA_ARGS__); \
else \
syslog(LOG_INFO, FORMAT __VA_OPT__(,) __VA_ARGS__); \
} \
} while(0)


#endif //COOLING_UTILS_H
2 changes: 1 addition & 1 deletion systemd/cooling_hat.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ After=multi-user.target
[Service]
Type=forking
ExecStart=/opt/bin/cooling_hat -r 30,0:40,3:60,5:70,9 -d
ExecStart=/opt/bin/cooling_hat -q -r 30,0:40,3:60,5:70,9 -d
WorkingDirectory=/tmp
StandardOutput=inherit
StandardError=inherit
Expand Down

0 comments on commit 5981075

Please sign in to comment.