Portál AbcLinuxu, 28. března 2024 10:25
#include#include #include #include FILE *f; char buf[1024]; const char *pwm = "/sys/bus/i2c/devices/0-0290/pwm2"; const char *tmp = "/sys/bus/i2c/devices/0-0290/temp2_input"; void obsluha_signalu(int sig) { #ifdef DEBUG printf("Signal %d\n",sig); #endif FILE *f = fopen(pwm,"w+"); if (!f) { #ifdef DEBUG printf("Can't open %s\n",tmp); #endif exit(1); } fprintf(f,"240"); fclose(f); exit(0); } int main() { signal(SIGINT,obsluha_signalu); signal(SIGTERM,obsluha_signalu); signal(SIGQUIT,obsluha_signalu); while(1) { f = fopen(tmp,"r"); if (!f) { #ifdef DEBUG printf("Can't open /%s\n",tmp); #endif return 1; } fread(buf,1,1024,f); int temp = atoi(buf)/1000; #ifdef DEBUG printf("Readed %s temperature: %dC\n",buf,temp); #endif fclose(f); FILE *f = fopen(pwm,"w+"); if (!f) { #ifdef DEBUG printf("Can't open %s\n",pwm); #endif return 1; } if (temp<40) { fprintf(f,"100"); #ifdef DEBUG printf("PWM set to 100\n"); #endif } else if (temp<50) { fprintf(f,"150"); #ifdef DEBUG printf("PWM set to 150\n"); #endif } else if (temp<60) { fprintf(f,"200"); #ifdef DEBUG printf("PWM set to 200\n"); #endif } else { fprintf(f,"240"); #ifdef DEBUG printf("PWM set to 240\n"); #endif } fclose(f); sleep(1); } return 0; }
Tiskni Sdílej:
if (temp<40) { fprintf(f,"100"); #ifdef DEBUG printf("PWM set to 100\n"); #endif } else if (temp<50) { fprintf(f,"150"); #ifdef DEBUG printf("PWM set to 150\n"); #endif } else if (temp<60) { fprintf(f,"200"); #ifdef DEBUG printf("PWM set to 200\n"); #endif } else { fprintf(f,"240"); #ifdef DEBUG printf("PWM set to 240\n"); #endif }napsat nejak takhle:
if (temp<40) { hodnota=100; } else if (temp<50) { hodnota=150; } else if (temp<60) { hodnota=200; } else { hodnota=240; } fprintf(f,hodnota); #ifdef DEBUG printf("PWM set to %d\n", hodnota); #endif
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.