Add channel_get_total()

This commit is contained in:
Pim van Pelt
2018-01-22 22:23:48 +01:00
parent b78e79223a
commit 3327a561f9
3 changed files with 13 additions and 8 deletions

View File

@ -26,6 +26,7 @@ uint8_t channel_gpio_by_idx(int idx);
uint8_t channel_idx_by_gpio(int gpio); uint8_t channel_idx_by_gpio(int gpio);
void channel_set(int idx, bool state); void channel_set(int idx, bool state);
bool channel_get(int idx); bool channel_get(int idx);
int channel_get_total();
void channel_report(int idx, char *msg, int msg_len); void channel_report(int idx, char *msg, int msg_len);
void channel_handler(int gpio, void *arg); void channel_handler(int gpio, void *arg);

View File

@ -13,6 +13,10 @@ static bool valid_gpio(const int gpio) {
return true; return true;
} }
int channel_get_total() {
return s_num_channels;
}
bool channel_init(const char *fn) { bool channel_init(const char *fn) {
char *json; char *json;
void *h = NULL; void *h = NULL;
@ -120,7 +124,7 @@ uint8_t channel_gpio_by_idx(int idx) {
uint8_t channel_idx_by_gpio(int gpio) { uint8_t channel_idx_by_gpio(int gpio) {
uint8_t i; uint8_t i;
for(i=0; i<3; i++) { for(i=0; i<channel_get_total(); i++) {
if (gpio == s_channels[i].button_gpio) if (gpio == s_channels[i].button_gpio)
return i; return i;
} }
@ -131,7 +135,7 @@ void channel_report(int idx, char *msg, int msg_len) {
if (!msg || msg_len==0) if (!msg || msg_len==0)
return; return;
if (idx<0 || idx>=s_num_channels) if (idx<0 || idx>=channel_get_total())
return; return;
snprintf(msg, msg_len-1, "{\"idx\": %d, \"button_gpio\": %d, \"led_gpio\": %d, \"relay_gpio\": %d, \"relay_state\": %d}", snprintf(msg, msg_len-1, "{\"idx\": %d, \"button_gpio\": %d, \"led_gpio\": %d, \"relay_gpio\": %d, \"relay_state\": %d}",
@ -141,7 +145,7 @@ void channel_report(int idx, char *msg, int msg_len) {
void channel_set(int idx, bool state) { void channel_set(int idx, bool state) {
double now = mg_time(); double now = mg_time();
if (idx<0 || idx>=s_num_channels) if (idx<0 || idx>=channel_get_total())
return; return;
s_channels[idx].button_last_change = now; s_channels[idx].button_last_change = now;
@ -153,7 +157,7 @@ void channel_set(int idx, bool state) {
} }
bool channel_get(int idx) { bool channel_get(int idx) {
if (idx<0 || idx>=s_num_channels) if (idx<0 || idx>=channel_get_total())
return false; return false;
return s_channels[idx].relay_state == 1; return s_channels[idx].relay_state == 1;

View File

@ -21,8 +21,8 @@ static bool rpc_args_to_idx_and_gpio(struct mg_rpc_request_info *ri, struct mg_s
return false; return false;
} }
if (idx<0 || idx>2) { if (idx<0 || idx>=channel_get_total()) {
mg_rpc_send_errorf(ri, 400, "idx must be 0, 1, 2"); mg_rpc_send_errorf(ri, 400, "idx must be between 0 and %d", channel_get_total()-1);
ri = NULL; ri = NULL;
return false; return false;
} }
@ -95,8 +95,8 @@ static void rpc_channel_set_handler(struct mg_rpc_request_info *ri, void *cb_arg
return; return;
} }
if (idx<0 || idx>2) { if (idx<0 || idx>=channel_get_total()) {
mg_rpc_send_errorf(ri, 400, "idx must be 0, 1, 2"); mg_rpc_send_errorf(ri, 400, "idx must be between 0 and %d", channel_get_total()-1);
ri = NULL; ri = NULL;
return; return;
} }