/* Demo for read and set mode via new led driver */ #include #include #include #include #include #include /* The GPIO device */ #define GPIO_DEVICE "/dev/gpio0" #define GPIO_NUM_DEV 23 /* gpio 0..7 and led 8..22 */ void gpio_get_mode(int fh,int id) { int i, mode[3]; /* mode[0] = id */ /* mode[1] = mode (> 128 if invert) */ /* mode[2] = input value, output unused */ mode[0] = id; if (ioctl(fh, 2, mode)) { /* ioctl 2 = get mode or input value*/ printf("Error by read.\n"); return; } printf("GPIO Pin:%i, mode:%i",mode[0],(mode[1] & 0x7F)); if (mode[1] > 0x7F) printf(", inverted"); if ((mode[1] == 0) || (mode[1] == 0x80)) printf(", input:%i",mode[2]); printf("\n"); } void gpio_set_mode (int fh, int id, int mode) { int mode_array[3]; /* mode[0] = id */ /* mode[1] = mode (invert + 128) */ /* mode[2] = unused */ mode_array[0] = id; mode_array[1] = mode; if (ioctl(fh, 3, mode_array)) { /* ioctl 3 = set mode */ printf("Error setting mode.\n"); return; } printf("GPIO Pin %i is set to mode %i\n",mode_array[0],mode_array[1]); } int main (int argc, char *argv[]) { int fh,id,mode,i; i = argc - 1; printf("%s %i\n", argv[0],i); switch (i) { case 0 : printf("%s usage:\n", argv[0]); printf("%s id : show the mode\n", argv[0]); printf("%s id mode : set the mode of id\n", argv[0]); return -1; break; case 1 : fh = open(GPIO_DEVICE, O_NONBLOCK); if (fh < 0) return -2; id = atoi(argv[1]); gpio_get_mode(fh,id); break; case 2 : fh = open(GPIO_DEVICE, O_NONBLOCK); if (fh < 0) return -2; id = atoi(argv[1]); mode = atoi(argv[2]); gpio_set_mode(fh,id,mode); break; default: ; } close(fh); return 0; }