#include <stdio.h>
#include <rc/button.h>
#include <rc/led.h>
#include <rc/start_stop.h>
#include <rc/time.h>
#define QUIT_TIMEOUT_US 1500000
#define QUIT_CHECK_US 100000
static const int us_delay[] = {400000, 170000, 100000};
static int mode;
static int toggle;
static void __on_pause_release(void)
{
if(rc_get_state()==RUNNING){
rc_set_state(PAUSED);
printf("PAUSED\n");
}
else if(rc_get_state()==PAUSED){
rc_set_state(RUNNING);
printf("RUNNING\n");
}
fflush(stdout);
return;
}
static void __on_pause_press(void)
{
int i=0;
const int samples = QUIT_TIMEOUT_US/QUIT_CHECK_US;
for(i=0;i<samples;i++){
rc_usleep(QUIT_CHECK_US);
if(rc_button_get_state(RC_BTN_PIN_PAUSE)==RC_BTN_STATE_RELEASED){
return;
}
}
printf("long press detected, shutting down\n");
rc_set_state(EXITING);
return;
}
static void __on_mode_release(void)
{
if(mode<2) mode++;
else mode=0;
printf("setting mode: %d\n", mode);
return;
}
int main()
{
if(rc_kill_existing_process(2.0)<-2) return -1;
if(rc_enable_signal_handler()<0){
fprintf(stderr,"ERROR: failed to complete rc_enable_signal_handler\n");
return -1;
}
if(rc_button_init(RC_BTN_PIN_PAUSE, RC_BTN_POLARITY_NORM_HIGH,
RC_BTN_DEBOUNCE_DEFAULT_US)){
fprintf(stderr,"ERROR: failed to init buttons\n");
return -1;
}
if(rc_button_init(RC_BTN_PIN_MODE, RC_BTN_POLARITY_NORM_HIGH,
RC_BTN_DEBOUNCE_DEFAULT_US)){
fprintf(stderr,"ERROR: failed to init buttons\n");
return -1;
}
rc_button_set_callbacks(RC_BTN_PIN_PAUSE, __on_pause_press, __on_pause_release);
rc_button_set_callbacks(RC_BTN_PIN_MODE, NULL, __on_mode_release);
if(rc_led_set(RC_LED_GREEN, 0)==-1){
fprintf(stderr, "ERROR in rc_blink, failed to set RC_LED_GREEN\n");
return -1;
}
if(rc_led_set(RC_LED_RED, 0)==-1){
fprintf(stderr, "ERROR in rc_blink, failed to set RC_LED_RED\n");
return -1;
}
rc_make_pid_file();
rc_set_state(RUNNING);
mode = 0;
printf("\nPress mode to change blink rate\n");
printf("hold pause button to exit\n");
while(rc_get_state()!=EXITING){
if(rc_get_state()==RUNNING){
if(toggle){
rc_led_set(RC_LED_GREEN,0);
rc_led_set(RC_LED_RED,1);
toggle = 0;
}
else{
rc_led_set(RC_LED_GREEN,1);
rc_led_set(RC_LED_RED,0);
toggle=1;
}
}
rc_usleep(us_delay[mode]);
}
rc_led_set(RC_LED_GREEN, 0);
rc_led_set(RC_LED_RED, 0);
rc_led_cleanup();
rc_button_cleanup();
rc_remove_pid_file();
return 0;
}