Skip to content

Commit

Permalink
New feature: calling scripts on left and right key long press
Browse files Browse the repository at this point in the history
  • Loading branch information
lanceliao committed Oct 20, 2019
1 parent dfa955b commit c6301c4
Show file tree
Hide file tree
Showing 21 changed files with 78 additions and 6 deletions.
Empty file modified lib/k3screenctrl-test/basic.sh 100644 → 100755
Empty file.
Empty file modified lib/k3screenctrl-test/host.sh 100644 → 100755
Empty file.
3 changes: 3 additions & 0 deletions lib/k3screenctrl-test/left_long.sh
@@ -0,0 +1,3 @@
#!/bin/sh

logger left key long pressed on page $1
Empty file modified lib/k3screenctrl-test/port.sh 100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions lib/k3screenctrl-test/right_long.sh
@@ -0,0 +1,4 @@
#!/bin/sh

# check the output with the command logread -f
logger right key long pressed on page $1
Empty file modified lib/k3screenctrl-test/wan.sh 100644 → 100755
Empty file.
Empty file modified lib/k3screenctrl-test/weather.sh 100644 → 100755
Empty file.
Empty file modified lib/k3screenctrl-test/wifi.sh 100644 → 100755
Empty file.
Empty file modified lib/k3screenctrl/basic.sh 100644 → 100755
Empty file.
Empty file modified lib/k3screenctrl/host.sh 100644 → 100755
Empty file.
3 changes: 3 additions & 0 deletions lib/k3screenctrl/left_long.sh
@@ -0,0 +1,3 @@
#!/bin/sh

logger left key long pressed on page $1
Empty file modified lib/k3screenctrl/port.sh 100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions lib/k3screenctrl/right_long.sh
@@ -0,0 +1,4 @@
#!/bin/sh

# check the output with the command logread -f
logger right key long pressed on page $1
Empty file modified lib/k3screenctrl/wan.sh 100644 → 100755
Empty file.
Empty file modified lib/k3screenctrl/weather.sh 100644 → 100755
Empty file.
Empty file modified lib/k3screenctrl/wifi.sh 100644 → 100755
Empty file.
24 changes: 20 additions & 4 deletions src/config.c 100644 → 100755
Expand Up @@ -22,7 +22,7 @@ static void config_show_help() {
"corresponding to current page and update content every SECS seconds\n"
"\t-m, --screen-timeout <SECS>\tTurn off screen after this period of "
"time if there isn't any user interaction\n"
"\t-o, --home-page <NUM>\tDefine as home page, the value is from %d to %d"
"\t-o, --home-page <NUM>\t\tDefine as home page, the value is from %d to %d"
"This page is switched to when middle button is pressed\n"
"\t-s, --host-script <PATH>\tUse this script to gather hosts "
"info\n"
Expand All @@ -33,10 +33,12 @@ static void config_show_help() {
"\t-n, --wan-script <PATH>\t\tUse this script to gather WAN speed "
"and internet connection info\n"
"\t-i, --basic-info-script <PATH>\tUse this script to gather "
"\t-e, --weather-script <PATH>\tUse this script to gather "
"weather info\n"
"\t-e, --weather-script <PATH>\tUse this script to gather "
"basic info\n"
"\nThe defaults are /lib/k3screenctrl/{host,wifi,port,wan,basic}.sh "
"\t-l, --left-long-script <PATH>\tThis script is called on long press of left key\n"
"\t-y, --right-long-script <PATH>\tThis script is called on long press of right key\n"
"\nThe defaults are /lib/k3screenctrl/{host,wifi,port,wan,basic,weather,left_long,right_long}.sh "
"with an interval of 2 seconds\n");
exit(1);
}
Expand All @@ -56,8 +58,10 @@ void config_parse_cmdline(int argc, char *argv[]) {
{"port-script", required_argument, NULL, 'p'},
{"wan-script", required_argument, NULL, 'n'},
{"basic-info-script", required_argument, NULL, 'i'},
{"left-long-script", required_argument, NULL, 'l' },
{"right-long-script", required_argument, NULL, 'y' },
{0, 0, 0, 0}};
static const char *short_opts = "hfrtd:m:s:e:w:p:n:i:u:o:";
static const char *short_opts = "hfrtd:m:s:e:w:p:n:i:u:o:l:y:";

int opt_index;
signed char result;
Expand Down Expand Up @@ -109,6 +113,14 @@ void config_parse_cmdline(int argc, char *argv[]) {
free(g_config.basic_info_script);
g_config.basic_info_script = strdup(optarg);
break;
case 'l':
free(g_config.left_long_script);
g_config.left_long_script = strdup(optarg);
break;
case 'y':
free(g_config.right_long_script);
g_config.right_long_script = strdup(optarg);
break;
}
}
}
Expand All @@ -126,6 +138,8 @@ void config_load_defaults() {
g_config.port_script = strdup(DEFAULT_PORT_SCRIPT);
g_config.wan_script = strdup(DEFAULT_WAN_SCRIPT);
g_config.basic_info_script = strdup(DEFAULT_BASIC_INFO_SCRIPT);
g_config.left_long_script = strdup(DEFAULT_LEFT_LONG_SCRIPT);
g_config.right_long_script = strdup(DEFAULT_RIGHT_LONG_SCRIPT);
}

void config_free() {
Expand All @@ -135,6 +149,8 @@ void config_free() {
free(g_config.wan_script);
free(g_config.basic_info_script);
free(g_config.weather_script);
free(g_config.left_long_script);
free(g_config.right_long_script);
}

CONFIG *config_get() { return &g_config; }
12 changes: 12 additions & 0 deletions src/config.h 100644 → 100755
Expand Up @@ -143,6 +143,18 @@ typedef struct _config {
char *weather_script;
#define DEFAULT_WEATHER_SCRIPT "/lib/k3screenctrl/weather.sh"

/**
* This script will be called when left key is long pressed
*/
char *left_long_script;
#define DEFAULT_LEFT_LONG_SCRIPT "/lib/k3screenctrl/left_long.sh"

/**
* This script will be called when right key is long pressed
*/
char *right_long_script;
#define DEFAULT_RIGHT_LONG_SCRIPT "/lib/k3screenctrl/right_long.sh"

/**
* Shall we skip GPIO setup (do not reset the microcontroller)?
* Useful when debugging.
Expand Down
31 changes: 29 additions & 2 deletions src/handlers.c 100644 → 100755
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>

#include "handlers.h"
#include "mcu_proto.h"
Expand All @@ -26,9 +27,33 @@ void handle_mcu_version(const unsigned char *payload, int len) {
g_mcu_version.major_ver, g_mcu_version.minor_ver,
g_mcu_version.patch_ver);
}
int handle_long_press(const char *script, int page_num)
{
int ret = -1;
int len = 0;
static char cmd_with_param[256];

memset(&cmd_with_param, 0, sizeof(cmd_with_param));
len = snprintf(cmd_with_param, sizeof(cmd_with_param), "%s %d", script, page_num);
if (len > sizeof(cmd_with_param)) {
syslog(LOG_WARNING, "script path is too long, make it less than 256 chars: %s\n", script);
return -1;
}

FILE *fp = popen(cmd_with_param, "r");
if (fp == NULL) {
syslog(LOG_ERR, "could not execute key long press script \"%s\": %s\n", script,
strerror(errno));
return -1;
}

return pclose(fp);
}

int g_is_screen_on = 1;
void handle_key_press(const unsigned char *payload, int len) {
int ret = -1;

if (len < 1) {
syslog(LOG_WARNING, "Got malformed key press response. Length is %d\n",
len);
Expand Down Expand Up @@ -64,10 +89,12 @@ void handle_key_press(const unsigned char *payload, int len) {
g_is_screen_on = 0;
return;
case KEY_LEFT_LONG:
printf("KEY_LEFT_LONG\n");
ret = handle_long_press(CFG->left_long_script, page_get_index());
printf("KEY_LEFT_LONG script %s with status code %d\n", CFG->left_long_script, ret);
return;
case KEY_RIGHT_LONG:
printf("KEY_RIGHT_LONG\n");
ret = handle_long_press(CFG->right_long_script, page_get_index());
printf("KEY_RIGHT_LONG script %s with status code %d\n", CFG->right_long_script, ret);
return;
default:
syslog(LOG_WARNING, "unknown key code: %hhx\n", payload[0]);
Expand Down
2 changes: 2 additions & 0 deletions src/pages.c 100644 → 100755
Expand Up @@ -16,6 +16,8 @@ struct _host_info_single *get_hosts() {

static int get_hosts_count() { return g_host_info_elements; }

int page_get_index() { return g_current_page; }

static void send_page_data(PAGE page) {
switch (page) {
case PAGE_BASIC_INFO:
Expand Down
1 change: 1 addition & 0 deletions src/pages.h 100644 → 100755
Expand Up @@ -3,6 +3,7 @@

#include "mcu_proto.h"

int page_get_index();
void page_send_initial_data();
void page_update();
void page_switch_next();
Expand Down

0 comments on commit c6301c4

Please sign in to comment.