Skip to content

Instantly share code, notes, and snippets.

@natxopedreira
Last active September 27, 2017 19:33
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save natxopedreira/6bfdbb9045556f2760479f43c6cd4b99 to your computer and use it in GitHub Desktop.
clase para rpi watchdog
#ifndef rpiWatchdog_h
#define rpiWatchdog_h
#include <linux/watchdog.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
class piWatchdog
{
public:
int deviceHandle;
int timeFeed;
bool iniciado = false;
void setupWatchdog(){
if ((deviceHandle = open("/dev/watchdog", O_RDWR | O_NOCTTY)) < 0) {
printf("Error: Couldn't open watchdog device! %d\n", deviceHandle);
}else{
printf("watchdog OK.\n");
int timeout = 15;
ioctl(deviceHandle, WDIOC_SETTIMEOUT, &timeout);
ioctl(deviceHandle, WDIOC_GETTIMEOUT, &timeout);
printf("The watchdog timeout is %d seconds.\n\n", timeout);
iniciado = true;
}
timeFeed = ofGetElapsedTimeMillis();
}
void feedWatchdog(){
if(iniciado){
if(ofGetElapsedTimeMillis() - timeFeed > 8000){
printf("Feeding the dog with a heartbeat.\n");
ioctl(deviceHandle, WDIOC_KEEPALIVE, 0);
timeFeed = ofGetElapsedTimeMillis();
}
}
}
void exitWatchdog(){
printf("Disable watchdog.\n");
write(deviceHandle, "V", 1);
close(deviceHandle);
}
/* data */
};
#endif
@natxopedreira
Copy link
Author

natxopedreira commented Sep 27, 2017

ejemplo de uso

bool iniciaWatchdog;
piWatchdog watchdog;

setup(){
iniciaWatchdog = false;
}


update(){

    // si han pasado mas de 30 segs desde inicio comprueba color
    
    if (((ofGetElapsedTimeMillis() - tiempoCheckColorMedio) > 30000 ) & colorMedioComprobacion) {
        
        colorMedioComprobacion = false;
        ////////////////////////////////
        int resultado = colorMedio();
        
        if(resultado<30){
            
            ofLog(OF_LOG_NOTICE,"CIERRA LA APP Y REINICIALA");
            
            videoManager.activaelcover();
            mostrarCover = true;
            std::cout << "CIERRA LA APP Y REINICIALA" << std::endl;
        }

	   usable=true;
       iniciaWatchdog = true;
    }

    if(iniciaWatchdog){
        iniciaWatchdog = false;
        watchdog.setupWatchdog();
    }


    ////////
   if(usable){
    watchdog.feedWatchdog();
   } 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment