add channel_set_duration() for timer based polls

This commit is contained in:
Pim van Pelt
2018-11-01 20:29:51 +01:00
parent 3140cc72b8
commit 8eb6bcb112
2 changed files with 23 additions and 0 deletions

View File

@ -24,6 +24,7 @@ bool channel_init(const char *fn);
uint8_t channel_gpio_by_idx(int idx); 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);
void channel_set_duration(int idx, bool state, uint16_t seconds);
bool channel_get(int idx); bool channel_get(int idx);
int channel_get_total(); int channel_get_total();
void channel_handler(int gpio, void *arg); void channel_handler(int gpio, void *arg);

View File

@ -144,6 +144,28 @@ uint8_t channel_idx_by_gpio(int gpio) {
return GPIO_INVALID; return GPIO_INVALID;
} }
// Slightly tricky callback (installed on a timer by channel_set and
// channel_toggle carries information in the arg pointer -- top 16 bits are
// the channel idx, LSB is boolean value to set the channel to.
static void channel_set_cb(void *arg) {
uint32_t input=(uint32_t) arg;
int idx = input>>16;
int value = input & 0x1;
LOG(LL_INFO, ("Channel callback for channel %d value %d", idx, value));
channel_set(idx, (bool)value);
}
void channel_set_duration(int idx, bool state, uint16_t seconds) {
uint32_t val;
channel_set(idx, state);
// Pass an argument to the channel_set_cb callback, top 16 bits is channel,
// bottom bit is what to set it to
val = idx<<16+(!state);
LOG(LL_INFO, ("Setting timer for %d seconds on channel %d with future value %d", seconds, idx, !state));
mgos_set_timer (1000 * seconds, false /* oneshot */, channel_set_cb, (void *)val);
}
void channel_set(int idx, bool state) { void channel_set(int idx, bool state) {
double now = mg_time(); double now = mg_time();