void Pi_Wieg_cancel(Pi_Wieg_t *wieg) { /* Cancel the Wiegand decoder. */ if (wieg) { gpioSetAlertFunc(wieg->mygpio_0, 0); gpioSetAlertFunc(wieg->mygpio_1, 0); free(wieg); } }
int main( void ) { gpioInitialise(); gpioSetAlertFunc( 4, callback ); sleep( 10 ); gpioTerminate(); return 0; }
int main(int argc, char *argv[]) { int secs=60; if (argc>1) secs = atoi(argv[1]); /* program run seconds */ if ((secs<1) || (secs>3600)) secs = 3600; if (gpioInitialise()<0) return 1; gpioSetMode(HALL, PI_INPUT); gpioSetPullUpDown(HALL, PI_PUD_UP); gpioSetAlertFunc(HALL, alert); sleep(secs); printf("%d\n", count); gpioTerminate(); }
int main(int argc, char *argv[]) { int i, n; char buff[128], ch; if (gpioInitialise()<0) return 1; gpioSetMode(ECHO, PI_INPUT); // register callback on change of sonar pin gpioSetAlertFunc(ECHO, range); sleep(2); while (1) { ping(); // prime the last_range variable sleep(1); printf("%d \n", last_range); } sleep(1); puts("Bye now!"); gpioTerminate(); return 0; }
void Pi_Hasher_cancel(Pi_Hasher_t *hasher) { if (hasher) { gpioSetAlertFunc(hasher->gpio, 0); free(hasher); } }
void *tickerThread(void *arg) { struct _gpio_param* pParam; if( (pParam = (struct _gpio_param*) arg) != NULL ) { gpioSetAlertFunc( pParam->bcm_gpio, pParam->callback ); // set event handler "ticker" while(1) { gpioSleep(PI_TIME_RELATIVE, 0, 10000); } } return( NULL ); }
int main() { if(gpioInitialise() < 0) return 1; // init pigpio lib if(gpioSetMode(DUST_PIN, PI_INPUT) < 0) return 1; // configure DUST_PIN as input if(gpioSetAlertFunc(DUST_PIN, edge_detected) < 0) return 1; // configure interrupt routine for DUST_PIN if(gpioDelay(SAMPLETIME_MS * 1000) < 0) return 1; // wait SAMPLETIME_MS (gpioDelay wants us) gpioTerminate(); // try to terminate pigpio lib float ratio = (lowOccupancy / SAMPLETIME_MS) * 100; // calculate low occupancy in percent /* calculate particle concentration -> amount of particles per 283ml | particle size > 1um eqation for calculation from http://www.howmuchsnow.com/arduino/airquality/grovedust/ */ float concentration = 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62; printf("%d %f %f", lowOccupancy, ratio, concentration); return 0; }
int main (int argc, char **argv) { struct sigaction act; char configFilePath[MAXPATHLEN]; char hostName[1024]; int c; // for getopt int socket_fd_cms; config_t cfg; struct config config; struct sockaddr_in info_cms; struct hostent *he_cms; // struct data data; struct timeval time_last, time_now; char tcp_buffer[1024]; int num; // print help if no arguments given if (argc==1) { printHelp(); return 1; } // init library if (gpioInitialise()<0) return 1; // load and parse config file opterr = 0; while ((c=getopt(argc, argv, "c:")) != -1) { switch (c) { case 'c': strcpy(configFilePath, optarg); break; case '?': if (optopt=='d' || optopt=='h') fprintf(stderr, "Option -%c requires an argument.\n", optopt); else if (isprint (optopt)) fprintf(stderr, "Unknown option '-%c'.\n", optopt); else fprintf(stderr, "Unknown option character '\\x%x'.\n", optopt); gpioTerminate(); return 1; default: gpioTerminate(); abort(); } } config_init(&cfg); // Read the file. If there is an error, report it and exit. if(! config_read_file(&cfg, configFilePath)) { fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); gpioTerminate(); return(EXIT_FAILURE); } // node, host, apikey if (!(config_lookup_string(&cfg, "node", &(config.pNodeName)))) { fprintf(stderr, "No 'node' setting in configuration file.\n"); config_destroy(&cfg); gpioTerminate(); return(EXIT_FAILURE); } if (!(config_lookup_string(&cfg, "host", &(config.pHostName)))) { fprintf(stderr, "No 'host' setting in configuration file.\n"); config_destroy(&cfg); gpioTerminate(); return(EXIT_FAILURE); } if (!(config_lookup_string(&cfg, "apikey", &(config.pApiKey)))) { fprintf(stderr, "No 'apikey' setting in configuration file.\n"); config_destroy(&cfg); gpioTerminate(); return(EXIT_FAILURE); } if (!config.pHostName[0]) { printf("No host name found. Did you specify the '-h hostname' option or mention it in the config file?\n"); config_destroy(&cfg); gpioTerminate(); return EX_UNAVAILABLE; } if (!(he_cms = gethostbyname(config.pHostName))) { fprintf(stderr, "Could not resolve host name '%s', err %d.\n", config.pHostName, h_errno); config_destroy(&cfg); gpioTerminate(); exit(1); } info_cms.sin_family = AF_INET; info_cms.sin_port = htons(80); info_cms.sin_addr = *((struct in_addr *)he_cms->h_addr); /* if ((socket_fd_cms = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { fprintf(stderr, "Could not allocate socket, err %d.\n", errno); config_destroy(&cfg); gpioTerminate(); exit(1); } if (connect(socket_fd_cms, (struct sockaddr *)&info_cms, sizeof(struct sockaddr)) < 0) { fprintf(stderr, "Could not connect to server, err%d.\n", errno); close (socket_fd_cms); config_destroy(&cfg); gpioTerminate(); exit(1); } */ // config GPIO gpioSetMode (GPIO, PI_INPUT); gpioSetPullUpDown (GPIO, PI_PUD_UP); gpioSetAlertFunc (GPIO, myAlert); act.sa_handler = intHandler; sigaction(SIGINT, &act, NULL); // catch Ctrl-C sigaction(SIGTERM, &act, NULL); // catch kill gettimeofday(&time_last, NULL); gettimeofday(&time_now, NULL); unsigned long elapsedtime_us; // [us] float elapsedtime_s; // [s] float power; // [W] syslog(LOG_INFO, "started. waiting for pulse..."); while (!clean_up) { pause(); if (!clean_up) // to avoid running even once if SIGINT arrives during pause() { // printf ("back.\n"); fflush(stdout); gettimeofday(&time_now, NULL); elapsedtime_us = (time_now.tv_sec - time_last.tv_sec)*1000000 - time_last.tv_usec + time_now.tv_usec; printf("elapsed microseconds: %lu\n", elapsedtime_us); elapsedtime_s = elapsedtime_us / 1000000.0; printf("elapsed seconds: %f\n", elapsedtime_s); power = 1.0 * 3600.0 / elapsedtime_s; // 1.0Wh per pulse * 3600 s/h / seconds = Whs/hs = W printf("current power: %f\n", power); syslog(LOG_INFO, "elapsed seconds: %f, current power: %f", elapsedtime_s, power); if (power > 10000.0) { syslog(LOG_INFO, "error: power >10kW."); } else { time_last.tv_sec = time_now.tv_sec; time_last.tv_usec = time_now.tv_usec; // generate json string for emonCMS sprintf (tcp_buffer, "GET /input/post.json?node=\"%s\"&json={pulseBoiler:1,powerBoiler:%5.3f}&apikey=%s HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s %s\r\nConnection: keep-alive\r\n\r\n", config.pNodeName, power, config.pApiKey, config.pHostName, TOOLNAME, RASPI_PULSECOUNT_VERSION); printf ("-----\nbuflen: %ld\n%s\n", strlen(tcp_buffer), tcp_buffer); if ((socket_fd_cms = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { fprintf(stderr, "Could not allocate socket, err %d.\n", errno); } else { if (connect(socket_fd_cms, (struct sockaddr *)&info_cms, sizeof(struct sockaddr)) < 0) // re-connect each time { fprintf(stderr, "Could not connect to server, err %d.\n", errno); syslog(LOG_WARNING,"Could not connect to server, err %d (%s).", errno, strerror(errno)); } else { num=send(socket_fd_cms, tcp_buffer, strlen(tcp_buffer), 0); if (num <0) printf ("send error: %d\n", errno); else printf ("sent: %ld\n", num); } close (socket_fd_cms); } } } } // clean up (not usually executed until Ctrl-C) printf ("clean up.\n"); syslog(LOG_INFO, "clean up."); close (socket_fd_cms); config_destroy(&cfg); gpioTerminate(); return (EX_OK); }
int main(int argc, char *argv[]) { char fname[120]; FILE *fp; // for output log file struct tm *tm; struct tm * timeinfo; struct timeval t1, deltaT; char tod[80]; // time of day string char tbuf[80]; int millisec; float deltaSec = 0; int retval = 0; if (gpioInitialise()<0) { printf("Cannot initialize GPIO, quitting.\n"); return 1; } fresh = FALSE; // start out with no GPIO level changes yet seen gettimeofday(&t1, NULL); // start time if ((tm = localtime(&t1.tv_sec)) == NULL) return -1; strftime(tod, sizeof tod, "%Y%m%d_%H%M%S.csv",tm); strcpy(fname,BASENAME); strncat(fname,tod,strlen(tod)); printf("Opening %s\n",fname); fp = fopen(fname,"w"); if (fp == NULL) return -1; gpioSetMode(SIGIN, PI_INPUT); // gpioSetPullUpDown(SIGIN, PI_PUD_UP); gpioSetPullUpDown(SIGIN, PI_PUD_DOWN); gpioSetAlertFunc(SIGIN, alert); printf("# Motion log start...\n"); do { if (fresh == TRUE) { fresh = FALSE; timeval_subtract (&deltaT, &t2, &t1); if (g_level == 1) { // is this rising edge (start of new motion event?) t1.tv_sec = t2.tv_sec; // save time structure values for next time t1.tv_usec = t2.tv_usec; } millisec = lrint(t2.tv_usec/1000.0); // round to nearest millisec if (millisec>=1000) { // Allow for rounding up to nearest second millisec -=1000; t2.tv_sec++; } timeinfo = localtime(&t2.tv_sec); strftime(tbuf, 80, "%Y-%m-%d %H:%M:%S",timeinfo); fprintf(fp,"%s.%03d, ",tbuf,millisec); // write to file including milliseconds if (g_level == 0) { // returned to zero; end of motion event deltaSec = (g_tick-lastTick) / 1E6; // ticks in microseconds fprintf(fp,"%d, %4.1f\n", g_level, deltaSec); } else { // start of new motion event deltaSec = deltaT.tv_sec + (deltaT.tv_usec/1.0E6); fprintf(fp,"%d, %4.1f\n", g_level, deltaSec); retval = system(STILLCAP); // command to trigger local capture of still image retval |= system(SENDALARM); // notify other device of motion } lastTick = g_tick; fflush(fp); // make sure it's written to the file /* if (g_level == 1) { // capture still image // raspistill -t 1000 -rot 180 -o test.jpg tm = localtime(&t2.tv_sec); strftime(tod, sizeof tod, "m_%Y%m%d_%H%M%S.jpg",tm); strcpy(fname, "raspistill -t 1000 -rot 180 -o "); // command to record image strcat(fname, tod); printf("Saving %s\n", tod); retval = system(fname); } */ } // if fresh sleep(1); } while (1); gpioTerminate(); // never reached fclose(fp); return retval; }
int gpio_init() { fprintf(stderr,"encoder_init\n"); #ifdef odroid VFO_ENCODER_A=88; VFO_ENCODER_B=87; #endif gpio_restore_state(); #ifdef raspberrypi fprintf(stderr,"encoder_init: VFO_ENCODER_A=%d VFO_ENCODER_B=%d\n",VFO_ENCODER_A,VFO_ENCODER_B); fprintf(stderr,"gpioInitialize\n"); if(gpioInitialise()<0) { fprintf(stderr,"Cannot initialize GPIO\n"); return -1; } if(ENABLE_FUNCTION_BUTTON) { gpioSetMode(FUNCTION_BUTTON, PI_INPUT); gpioSetPullUpDown(FUNCTION_BUTTON,PI_PUD_UP); gpioSetAlertFunc(FUNCTION_BUTTON, functionAlert); } if(ENABLE_VFO_ENCODER) { if(gpioSetMode(VFO_ENCODER_A, PI_INPUT)!=0) { fprintf(stderr,"gpioSetMode for VFO_ENCODER_A failed\n"); } if(gpioSetMode(VFO_ENCODER_B, PI_INPUT)!=0) { fprintf(stderr,"gpioSetMode for VFO_ENCODER_B failed\n"); } if(ENABLE_VFO_PULLUP) { gpioSetPullUpDown(VFO_ENCODER_A, PI_PUD_UP); gpioSetPullUpDown(VFO_ENCODER_B, PI_PUD_UP); } else { gpioSetPullUpDown(VFO_ENCODER_A, PI_PUD_OFF); gpioSetPullUpDown(VFO_ENCODER_B, PI_PUD_OFF); } if(gpioSetAlertFunc(VFO_ENCODER_A, vfoEncoderPulse)!=0) { fprintf(stderr,"gpioSetAlertFunc for VFO_ENCODER_A failed\n"); } if(gpioSetAlertFunc(VFO_ENCODER_B, vfoEncoderPulse)!=0) { fprintf(stderr,"gpioSetAlertFunc for VFO_ENCODER_B failed\n"); } vfoEncoderPos=0; } gpioSetMode(AF_FUNCTION, PI_INPUT); gpioSetPullUpDown(AF_FUNCTION,PI_PUD_UP); gpioSetAlertFunc(AF_FUNCTION, afFunctionAlert); afFunction=0; if(ENABLE_AF_ENCODER) { gpioSetMode(AF_ENCODER_A, PI_INPUT); gpioSetMode(AF_ENCODER_B, PI_INPUT); if(ENABLE_AF_PULLUP) { gpioSetPullUpDown(AF_ENCODER_A, PI_PUD_UP); gpioSetPullUpDown(AF_ENCODER_B, PI_PUD_UP); } else { gpioSetPullUpDown(AF_ENCODER_A, PI_PUD_OFF); gpioSetPullUpDown(AF_ENCODER_B, PI_PUD_OFF); } gpioSetAlertFunc(AF_ENCODER_A, afEncoderPulse); gpioSetAlertFunc(AF_ENCODER_B, afEncoderPulse); afEncoderPos=0; } gpioSetMode(RF_FUNCTION, PI_INPUT); gpioSetPullUpDown(RF_FUNCTION,PI_PUD_UP); gpioSetAlertFunc(RF_FUNCTION, rfFunctionAlert); rfFunction=0; if(ENABLE_RF_ENCODER) { gpioSetMode(RF_ENCODER_A, PI_INPUT); gpioSetMode(RF_ENCODER_B, PI_INPUT); if(ENABLE_AF_PULLUP) { gpioSetPullUpDown(RF_ENCODER_A, PI_PUD_UP); gpioSetPullUpDown(RF_ENCODER_B, PI_PUD_UP); } else { gpioSetPullUpDown(RF_ENCODER_A, PI_PUD_OFF); gpioSetPullUpDown(RF_ENCODER_B, PI_PUD_OFF); } gpioSetAlertFunc(RF_ENCODER_A, rfEncoderPulse); gpioSetAlertFunc(RF_ENCODER_B, rfEncoderPulse); rfEncoderPos=0; } gpioSetMode(AGC_FUNCTION, PI_INPUT); gpioSetPullUpDown(AGC_FUNCTION,PI_PUD_UP); gpioSetAlertFunc(AGC_FUNCTION, agcFunctionAlert); agcFunction=0; if(ENABLE_AGC_ENCODER) { gpioSetMode(AGC_ENCODER_A, PI_INPUT); gpioSetMode(AGC_ENCODER_B, PI_INPUT); if(ENABLE_AF_PULLUP) { gpioSetPullUpDown(AGC_ENCODER_A, PI_PUD_UP); gpioSetPullUpDown(AGC_ENCODER_B, PI_PUD_UP); } else { gpioSetPullUpDown(AGC_ENCODER_A, PI_PUD_OFF); gpioSetPullUpDown(AGC_ENCODER_B, PI_PUD_OFF); } gpioSetAlertFunc(AGC_ENCODER_A, agcEncoderPulse); gpioSetAlertFunc(AGC_ENCODER_B, agcEncoderPulse); agcEncoderPos=0; } if(ENABLE_BAND_BUTTON) { gpioSetMode(BAND_BUTTON, PI_INPUT); gpioSetPullUpDown(BAND_BUTTON,PI_PUD_UP); gpioSetAlertFunc(BAND_BUTTON, bandAlert); } if(ENABLE_BANDSTACK_BUTTON) { gpioSetMode(BANDSTACK_BUTTON, PI_INPUT); gpioSetPullUpDown(BANDSTACK_BUTTON,PI_PUD_UP); gpioSetAlertFunc(BANDSTACK_BUTTON, bandstackAlert); } if(ENABLE_MODE_BUTTON) { gpioSetMode(MODE_BUTTON, PI_INPUT); gpioSetPullUpDown(MODE_BUTTON,PI_PUD_UP); gpioSetAlertFunc(MODE_BUTTON, modeAlert); } if(ENABLE_FILTER_BUTTON) { gpioSetMode(FILTER_BUTTON, PI_INPUT); gpioSetPullUpDown(FILTER_BUTTON,PI_PUD_UP); gpioSetAlertFunc(FILTER_BUTTON, filterAlert); } if(ENABLE_NOISE_BUTTON) { gpioSetMode(NOISE_BUTTON, PI_INPUT); gpioSetPullUpDown(NOISE_BUTTON,PI_PUD_UP); gpioSetAlertFunc(NOISE_BUTTON, noiseAlert); } if(ENABLE_AGC_BUTTON) { gpioSetMode(AGC_BUTTON, PI_INPUT); gpioSetPullUpDown(AGC_BUTTON,PI_PUD_UP); gpioSetAlertFunc(AGC_BUTTON, agcAlert); } if(ENABLE_MOX_BUTTON) { gpioSetMode(MOX_BUTTON, PI_INPUT); gpioSetPullUpDown(MOX_BUTTON,PI_PUD_UP); gpioSetAlertFunc(MOX_BUTTON, moxAlert); } if(ENABLE_LOCK_BUTTON) { gpioSetMode(LOCK_BUTTON, PI_INPUT); gpioSetPullUpDown(LOCK_BUTTON,PI_PUD_UP); gpioSetAlertFunc(LOCK_BUTTON, lockAlert); } #endif #ifdef odroid //VFO_ENCODER_A=ODROID_VFO_ENCODER_A; //VFO_ENCODER_B=ODROID_VFO_ENCODER_B; //VFO_ENCODER_A_PIN=ODROID_VFO_ENCODER_A_PIN; //VFO_ENCODER_B_PIN=ODROID_VFO_ENCODER_B_PIN; fprintf(stderr,"encoder_init: VFO_ENCODER_A=%d VFO_ENCODER_B=%d\n",VFO_ENCODER_A,VFO_ENCODER_B); fprintf(stderr,"wiringPiSetup\n"); if (wiringPiSetup () < 0) { printf ("Unable to setup wiringPi: %s\n", strerror (errno)); return -1; } FILE *fp; fp = popen("echo 88 > /sys/class/gpio/export\n", "r"); pclose(fp); fp = popen("echo \"in\" > /sys/class/gpio/gpio88/direction\n", "r"); pclose(fp); fp = popen("chmod 0666 /sys/class/gpio/gpio88/value\n", "r"); pclose(fp); fp = popen("echo 87 > /sys/class/gpio/export\n", "r"); pclose(fp); fp = popen("echo \"in\" > /sys/class/gpio/gpio87/direction\n", "r"); pclose(fp); fp = popen("chmod 0666 /sys/class/gpio/gpio87/value\n", "r"); pclose(fp); if ( wiringPiISR (0, INT_EDGE_BOTH, &interruptB) < 0 ) { printf ("Unable to setup ISR: %s\n", strerror (errno)); return -1; } if ( wiringPiISR (1, INT_EDGE_BOTH, &interruptA) < 0 ) { printf ("Unable to setup ISR: %s\n", strerror (errno)); return -1; } #endif int rc=pthread_create(&rotary_encoder_thread_id, NULL, rotary_encoder_thread, NULL); if(rc<0) { fprintf(stderr,"pthread_create for rotary_encoder_thread failed %d\n",rc); } return 0; }
int main(int argc, char *argv[]) { char str[256]; if (argc > 1) micros = atoi(argv[1]); if (argc > 2) millis = atoi(argv[2]); putTTY("\033c"); /* clear console */ gpioCfgBufferSize(millis); gpioCfgClock(micros, PI_CLOCK_PCM, PI_CLOCK_PLLD); /* before using the library you must call gpioInitialise */ version = gpioInitialise(); if (version >= 0) { /* initialise pins, only gpio numbers are supported */ gpioSetMode(SERVO1, PI_OUTPUT); gpioSetMode(SERVO2, PI_OUTPUT); gpioSetMode(SERVO3, PI_OUTPUT); gpioSetMode(LASER, PI_OUTPUT); gpioSetMode(LED1, PI_OUTPUT); gpioSetMode(LED2, PI_OUTPUT); gpioSetMode(LED3, PI_OUTPUT); gpioSetMode(MOTOR_A_IN1, PI_OUTPUT); gpioSetMode(MOTOR_A_IN2, PI_OUTPUT); gpioSetMode(MOTOR_B_IN1, PI_OUTPUT); gpioSetMode(MOTOR_B_IN2, PI_OUTPUT); gpioSetMode(SONAR_TRIGGER, PI_OUTPUT); gpioWrite (SONAR_TRIGGER, PI_OFF); gpioSetMode(SONAR_ECHO, PI_INPUT); gpioSetMode(LAUNCHPAD, PI_INPUT); gpioSetMode(LDR, PI_INPUT); /* update i2c fifty times a second, timer #0 */ gpioSetTimerFunc(0, 20, i2cTick); //gpioSetTimerFunc(0, 1000, servoTick); /* update LEDs and laser once a second, timer #1 */ gpioSetTimerFunc(1, 1000, LEDlaserTick); /* update motors every three seconds, timer #2 */ gpioSetTimerFunc(2, 3000, motorTick); /* update sonar/LDR 10 times a second, timer #3 */ gpioSetTimerFunc(3, 100, sonarLDRtick); /* an attachecd TI launchpad is transmitting high pulses of 15, 35, 55, 75, ..., 975, 995 microseconds repeating with 50 microseconds off between each pulse */ gpioSetAlertFunc(LAUNCHPAD, launchpadAlert); /* monitor sonar echos */ gpioSetAlertFunc(SONAR_ECHO, sonarAlert); /* monitor LDR level changes */ gpioSetAlertFunc(LDR, LDRalert); while (1) { sleep(1); sprintf(str, "TI pulses %8ld", launchpadPulses); putTTYstr(9, 1, str); sprintf(str, "+/-5 %8ld", launchpad5); putTTYstr(10, 6, str); sprintf(str, "+/-10 %8ld", launchpad10); putTTYstr(11, 5, str); sprintf(str, "+/-15 %8ld", launchpad15); putTTYstr(12, 5, str); sprintf(str, "Others %8ld (last %d) ", launchpadOutRange, launchpadErr); putTTYstr(13, 4, str); } } gpioTerminate(); return 0; }