Example #1
0
int main(int argc, char **argv)
{
	int msgid;
	msgbuf_st msgbuf;

	if (-1 == (msgid = msgget(KEY_VALUE, IPC_CREAT | 0666)))
		ERROR_EXIT("msgget");

	gpio_setup();

	msgbuf.mtype = MSG_TYPE;
	while(1) {
		if (-1 == msgrcv(msgid, &msgbuf, sizeof(msgbuf_st) - sizeof(long), msgbuf.mtype, 0))
			ERROR_EXIT("msgrcv");

		printf("Recv message:%s\n", msgbuf.minfo);
		process_msg(msgbuf.minfo);
	}
	gpio_cleanup();

	return 0;
}
Example #2
0
int main(void) {
    int cycle = 0;
    int value = 0;
    int ret;

    struct timespec sleepValue = {0, INTERVAL_MS};

    printf("LED usage example \n");

    // enable access to GPIOs
    ret = gpio_setup(LED_GPIO, GPIO_DIRECTION_OUT, value);
    if (ret)
	    return -1;

    do {
        value ^= 1;
	// toggle gpio_getValue
	gpio_setValue(LED_GPIO, value);
        nanosleep(&sleepValue, NULL);
    } while (cycle++ <= 10);

    gpio_cleanup(LED_GPIO);
    return 0;
}
Example #3
0
PyMODINIT_FUNC initGPIO(void)
#endif
{
   PyObject *module = NULL;

#if PY_MAJOR_VERSION > 2
   if ((module = PyModule_Create(&rpigpiomodule)) == NULL)
      goto exit;
#else
   if ((module = Py_InitModule("RPi.GPIO", rpi_gpio_methods)) == NULL)
      goto exit;
#endif

   WrongDirectionException = PyErr_NewException("RPi.GPIO.WrongDirectionException", NULL, NULL);
   PyModule_AddObject(module, "WrongDirectionException", WrongDirectionException);

   InvalidModeException = PyErr_NewException("RPi.GPIO.InvalidModeException", NULL, NULL);
   PyModule_AddObject(module, "InvalidModeException", InvalidModeException);

   InvalidDirectionException = PyErr_NewException("RPi.GPIO.InvalidDirectionException", NULL, NULL);
   PyModule_AddObject(module, "InvalidDirectionException", InvalidDirectionException);

   InvalidChannelException = PyErr_NewException("RPi.GPIO.InvalidChannelException", NULL, NULL);
   PyModule_AddObject(module, "InvalidChannelException", InvalidChannelException);

   InvalidPullException = PyErr_NewException("RPi.GPIO.InvalidPullException", NULL, NULL);
   PyModule_AddObject(module, "InvalidPullException", InvalidPullException);

   ModeNotSetException = PyErr_NewException("RPi.GPIO.ModeNotSetException", NULL, NULL);
   PyModule_AddObject(module, "ModeNotSetException", ModeNotSetException);

   SetupException = PyErr_NewException("RPi.GPIO.SetupException", NULL, NULL);
   PyModule_AddObject(module, "SetupException", SetupException);

   high = Py_BuildValue("i", HIGH);
   PyModule_AddObject(module, "HIGH", high);

   low = Py_BuildValue("i", LOW);
   PyModule_AddObject(module, "LOW", low);

   output = Py_BuildValue("i", OUTPUT);
   PyModule_AddObject(module, "OUT", output);

   input = Py_BuildValue("i", INPUT);
   PyModule_AddObject(module, "IN", input);

   board = Py_BuildValue("i", BOARD);
   PyModule_AddObject(module, "BOARD", board);

   bcm = Py_BuildValue("i", BCM);
   PyModule_AddObject(module, "BCM", bcm);
   
   pud_off = Py_BuildValue("i", PUD_OFF);
   PyModule_AddObject(module, "PUD_OFF", pud_off);
   
   pud_up = Py_BuildValue("i", PUD_UP);
   PyModule_AddObject(module, "PUD_UP", pud_up);
   
   pud_down = Py_BuildValue("i", PUD_DOWN);
   PyModule_AddObject(module, "PUD_DOWN", pud_down);

   if (module_setup() != SETUP_OK)
   {
#if PY_MAJOR_VERSION > 2
      return NULL;
#else
      return;
#endif
   }
      
   if (Py_AtExit(gpio_cleanup) != 0)
      gpio_cleanup();
      goto exit;

exit:
#if PY_MAJOR_VERSION > 2
   return module;
#else
   return;
#endif
}
Example #4
0
int main(int argc, char **argv)
{
	int opt;
	bool bluetooth = FALSE;
	bool mk3 = TRUE;
	bool camera = TRUE;
	int hw_version = 0;
	int gpio_number = 18;
	const char *port = "/dev/ttyAMA0";
	char *startup = NULL;
	int cdcinterval = 0;
	bool gpio_changed = FALSE;

	mainloop_init();

	while ((opt = getopt(argc, argv, "c:g:s:v:bhmr")) != -1)
	{
		switch (opt)
		{
			case 'b':
				bluetooth = 1;
				break;
			case 'c':
				cdcinterval = atoi(optarg);
				break;
			case 'g':
				gpio_number = atoi(optarg);
				gpio_changed = TRUE;
				break;
			case 'm':
				mk3 = 0;
				break;
			case 'r':
				camera = 0;
				break;
			case 's':
				startup = strdup(optarg);
				break;
			case 'v':
				hw_version = atoi(optarg);
				break;
			case 'h':
			default:
				fprintf(stderr,
					"Usage: %s [flags] [serial-port]\n"
					"\n"
					"Flags:\n"
					"\t-b           Car has bluetooth, don't use Phone and Speak buttons\n"
					"\t-c <time>    Force CDC-info replies every <time> seconds\n"
					"\t-g <number>  GPIO number to use for IBUS line monitor (0 = Use TH3122)\n"
					"\t-m           Do not do MK3 style CDC announcements\n"
					"\t-r           Do not switch to camera in reverse gear\n"
					"\t-s <string>  Send extra string to IBUS at startup\n"
					"\t-v <number>  Set PiBUS hardware version\n"
					"\n",
					argv[0]);
				return -1;
		}
	}

	if (argc > optind)
	{
		port = argv[optind];
	}

	if (!gpio_changed && hw_version >= 4)
	{
		gpio_number = 17;
	}

	if (gpio_init() != 0)
	{
		fprintf(stderr, "Can't init gpio\r\n");
		return -4;
	}

	if (ibus_init(port, startup, bluetooth, camera, mk3, cdcinterval, gpio_number, hw_version) != 0)
	{
		return -2;
	}

	if (keyboard_init() != 0)
	{
		fprintf(stderr, "Can't open keyboard\r\n");
		return -3;
	}

	mainloop();

	gpio_cleanup();
	ibus_cleanup();
	keyboard_cleanup();

	return 0;
}