Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
   int i, rest, g, wave_id, mode;
   gpioPulse_t pulse[2];
   int count[MAX_GPIOS];

   /* command line parameters */

   rest = initOpts(argc, argv);

   /* get the gpios to monitor */

   g_num_gpios = 0;

   for (i=rest; i<argc; i++)
   {
      g = atoi(argv[i]);
      if ((g>=0) && (g<32))
      {
         g_gpio[g_num_gpios++] = g;
         g_mask |= (1<<g);
      }
      else fatal(1, "%d is not a valid g_gpio number\n", g);
   }

   if (!g_num_gpios) fatal(1, "At least one gpio must be specified");

   printf("Monitoring gpios");
   for (i=0; i<g_num_gpios; i++) printf(" %d", g_gpio[i]);
   printf("\nSample rate %d micros, refresh rate %d deciseconds\n",
      g_opt_s, g_opt_r);

   gpioCfgClock(g_opt_s, 1, 1);

   if (gpioInitialise()<0) return 1;

   gpioWaveClear();

   pulse[0].gpioOn  = g_mask;
   pulse[0].gpioOff = 0;
   pulse[0].usDelay = g_opt_p;

   pulse[1].gpioOn  = 0;
   pulse[1].gpioOff = g_mask;
   pulse[1].usDelay = g_opt_p;

   gpioWaveAddGeneric(2, pulse);

   wave_id = gpioWaveCreate();

   /* monitor g_gpio level changes */

   gpioSetGetSamplesFunc(samples, g_mask);

   mode = PI_INPUT;

   if (g_opt_t)
   {
      gpioWaveTxSend(wave_id, PI_WAVE_MODE_REPEAT);
      mode = PI_OUTPUT;
   }

   for (i=0; i<g_num_gpios; i++) gpioSetMode(g_gpio[i], mode);

   while (1)
   {
      for (i=0; i<g_num_gpios; i++) count[i] = g_pulse_count[i];

      g_reset_counts = 1;

      for (i=0; i<g_num_gpios; i++)
      {
         printf(" %d=%d", g_gpio[i], count[i]);
      }

      printf("\n");

      gpioDelay(g_opt_r * 100000);
   }

   gpioTerminate();
}
Ejemplo n.º 2
0
int main(int argc, char **argv)
{
   pid_t pid;
   int flags;

   /* check command line parameters */

   initOpts(argc, argv);

   if (!foreground) {
      /* Fork off the parent process */

      pid = fork();

      if (pid < 0) { exit(EXIT_FAILURE); }

      /* If we got a good PID, then we can exit the parent process. */

      if (pid > 0) { exit(EXIT_SUCCESS); }

      /* Change the file mode mask */

      umask(0);

      /* Open any logs here */

      /* NONE */

      /* Create a new SID for the child process */

      if (setsid() < 0) fatal("setsid failed (%m)");

      /* Change the current working directory */

      if ((chdir("/")) < 0) fatal("chdir failed (%m)");

      /* Close out the standard file descriptors */

      fclose(stdin);
      fclose(stdout);
   }

   /* configure library */

   gpioCfgBufferSize(bufferSizeMilliseconds);

   gpioCfgClock(clockMicros, clockPeripheral, 0);

   gpioCfgInterfaces(ifFlags);

   gpioCfgDMAchannels(DMAprimaryChannel, DMAsecondaryChannel);

   gpioCfgSocketPort(socketPort);

   gpioCfgMemAlloc(memAllocMode);

   if (updateMaskSet) gpioCfgPermissions(updateMask);

   gpioCfgNetAddr(numSockNetAddr, sockNetAddr);

   gpioCfgSetInternals(cfgInternals);

   /* start library */

   if (gpioInitialise()< 0) fatal("Can't initialise pigpio library");

   /* create pipe for error reporting */

   unlink(PI_ERRFIFO);

   mkfifo(PI_ERRFIFO, 0664);

   if (chmod(PI_ERRFIFO, 0664) < 0)
      fatal("chmod %s failed (%m)", PI_ERRFIFO);

   errFifo = freopen(PI_ERRFIFO, "w+", stderr);

   if (errFifo)
   {
      /* set stderr non-blocking */

      flags = fcntl(fileno(errFifo), F_GETFL, 0);
      fcntl(fileno(errFifo), F_SETFL, flags | O_NONBLOCK);

      /* request SIGHUP/SIGTERM from libarary for termination */

      gpioSetSignalFunc(SIGHUP, terminate);
      gpioSetSignalFunc(SIGTERM, terminate);

      /* sleep forever */

      while (1)
      {
         /* cat /dev/pigerr to view daemon errors */

         sleep(5);

         fflush(errFifo);
      }
   }
   else
   {
      fprintf(stderr, "freopen failed (%m)");

      gpioTerminate();
   }

   return 0;
}
Ejemplo n.º 3
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;
}