Skip to content

Instantly share code, notes, and snippets.

@natxopedreira

natxopedreira/piWatchdog.h

Last active Sep 27, 2017
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@natxopedreira 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
You can’t perform that action at this time.