Skip to content

Instantly share code, notes, and snippets.

@nliviu
Created November 7, 2019 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nliviu/85058d0703ff47b8d581264d8410810e to your computer and use it in GitHub Desktop.
Save nliviu/85058d0703ff47b8d581264d8410810e to your computer and use it in GitHub Desktop.
volatile uint64_t i = 0;
struct pin_state {
int64_t uptime;
bool state;
};
static void cb2(void *arg) {
const struct pin_state *state = (const struct pin_state *) (arg);
LOG(LL_INFO, ("uptime: %lld, state: %d", state->uptime, state->state));
}
static IRAM void cb(int pin, void *arg) {
struct pin_state *state = (struct pin_state *) (arg);
state->uptime = mgos_uptime_micros();
state->state = mgos_gpio_read(pin);
if (i++ % 5 == 0) {
mgos_invoke_cb(cb2, state, true);
}
}
static void system_restart_cb(int ev, void *ev_data, void *userdata) {
const int pin = (const int) (userdata);
LOG(LL_INFO, ("%s - mgos_gpio_disable_int(%d)", __FUNCTION__, pin));
mgos_gpio_disable_int(pin);
(void) ev;
(void) ev_data;
}
enum mgos_app_init_result mgos_app_init(void) {
const int pin = 2;
mgos_event_add_handler(MGOS_EVENT_REBOOT, system_restart_cb, (void *) pin);
struct pin_state *state = (struct pin_state *) calloc(1, sizeof(*state));
mgos_gpio_set_int_handler_isr(pin, MGOS_GPIO_INT_EDGE_ANY, cb, state);
mgos_gpio_enable_int(pin);
const int restart_after = 500;
mgos_system_restart_after(restart_after);
// report to mdash
mgos_set_timer(10000, MGOS_TIMER_REPEAT, report_timer_cb, NULL);
return MGOS_APP_INIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment