GpIoPin::GpIoPin(uint pinNumber, bool isOut, QObject *parent) : QObject(parent){ _currentValue = 2; // Make it out of range to force write if out pin _ioFd = -1; // Make it invalid to force initial open _pinNumber = pinNumber; exportPin(); setDirection(isOut); }
void IRQ(int pin) { char pVal[16]; int timeoutTime = -1; // Never timeout int fd_pinValue; exportPin(pin); // Export Pin pinMode(pin, PIN_INPUT); setEdge(pin); memset(pinPath, 0 , sizeof(pinPath)); sprintf(pinPath, "%s/gpio%d/value", GPIO_PATH, pin); if ((fd_pinValue = open(pinPath, O_RDONLY | O_NONBLOCK)) == -1 ) { perror("IRQ getting value fd: "); } struct pollfd fdset[1]; struct sched_param sched; memset (&sched, 0, sizeof(sched)); sched.sched_priority = 55; // High Priority sched_setscheduler (0, SCHED_RR, &sched); while (1) { memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = fd_pinValue; fdset[0].events = POLLPRI; int rc = poll(fdset, 1, timeoutTime); // BLOCK 4EVA~! if (rc < 0) { printf("poll(): got a -1... something broke!\n"); } if (rc == 0) { printf("poll(): got a timeout... should be -1?\n"); } if (fdset[0].revents & POLLPRI) { memset(pVal, 0, 16); int size = read(fdset[0].fd, pVal, 16); // go to function pointer printf("\npoll() GPIO %d interrupt occurred\n", pin); } } close(fd_pinValue); }
/* pwm0=Pin49 pwm1=Pin50 pwm2=Pin55 pwm3=Pin58 */ int main(){ /* echo ?? > /sys/class/gpio/export echo (out||in) > /sys/class/gpio/gpio??/direction echo (1||0) > /sys/class/gpio/gpio??/value */ exportPin(57); pinMode(1,57); setValue(1,57); return 0; }
// runled.o 1000 10 1 void main(int argc, char **argv) { int iMicroSecs = 0, iCount = 0, iPattern = 1, i = 0; //check user inputs if (argc == 4) //valid inputs form user { iMicroSecs = strtol(argv[1], NULL, 10) * 1000; iCount = strtol(argv[2], NULL, 10); iPattern = strtol(argv[3], NULL, 10); } else { //invalid inputs, use default values printf("\nNo/Invalid Arguments ! Running Default:"); iMicroSecs = 1000000; iCount = 50; iPattern = 1; printf("\nBlink Rate = %d ms\nCount = %d\nPattern = %d\n", iMicroSecs, iCount, iPattern); } //Initialise GPIO pins for out operation //if (0 != setPinMode(P8_32, PIN_MODE_DEFAULT)) // exit(EXIT_FAILURE); if (0 != exportPin(P8_32)) exit(EXIT_FAILURE); if (0 != setPinDirection(P8_32, GPIO_DIR_OUT)) exit(EXIT_FAILURE); //if (0 != setPinMode(P8_33, PIN_MODE_DEFAULT)) // exit(EXIT_FAILURE); if (0 != exportPin(P8_33)) exit(EXIT_FAILURE); if (0 != setPinDirection(P8_33, GPIO_DIR_OUT)) exit(EXIT_FAILURE); //if (0 != setPinMode(P8_38, PIN_MODE_DEFAULT)) // exit(EXIT_FAILURE); if (0 != exportPin(P8_38)) exit(EXIT_FAILURE); if (0 != setPinDirection(P8_38, GPIO_DIR_OUT)) exit(EXIT_FAILURE); //if (0 != setPinMode(P8_39, PIN_MODE_DEFAULT)) // exit(EXIT_FAILURE); if (0 != exportPin(P8_39)) exit(EXIT_FAILURE); if (0 != setPinDirection(P8_39, GPIO_DIR_OUT)) exit(EXIT_FAILURE); switch(iPattern) { case 1: { for (i = 0; i < iCount; i++) { if (0 != setPinValue(P8_32, GPIO_HIGH)) exit(EXIT_FAILURE); if (0 != setPinValue(P8_33, GPIO_HIGH)) exit(EXIT_FAILURE); if (0 != setPinValue(P8_38, GPIO_HIGH)) exit(EXIT_FAILURE); if (0 != setPinValue(P8_39, GPIO_HIGH)) exit(EXIT_FAILURE); usleep(iMicroSecs); if (0 != setPinValue(P8_32, GPIO_LOW)) exit(EXIT_FAILURE); if (0 != setPinValue(P8_33, GPIO_LOW)) exit(EXIT_FAILURE); if (0 != setPinValue(P8_38, GPIO_LOW)) exit(EXIT_FAILURE); if (0 != setPinValue(P8_39, GPIO_LOW)) exit(EXIT_FAILURE); usleep(iMicroSecs); } break; } case 2: { for (i = 0; i < iCount; i++) { // all led OFF if (0 != setPinValue(P8_39, GPIO_LOW)) exit(EXIT_FAILURE); if (0 != setPinValue(P8_32, GPIO_LOW)) exit(EXIT_FAILURE); if (0 != setPinValue(P8_33, GPIO_LOW)) exit(EXIT_FAILURE); if (0 != setPinValue(P8_38, GPIO_LOW)) exit(EXIT_FAILURE); usleep(iMicroSecs); // led 1 : ON if (0 != setPinValue(P8_32, GPIO_HIGH)) exit(EXIT_FAILURE); usleep(iMicroSecs); // led 2 : ON if (0 != setPinValue(P8_33, GPIO_HIGH)) exit(EXIT_FAILURE); usleep(iMicroSecs); // led 3 : ON if (0 != setPinValue(P8_38, GPIO_HIGH)) exit(EXIT_FAILURE); usleep(iMicroSecs); // led 4 : ON if (0 != setPinValue(P8_39, GPIO_HIGH)) exit(EXIT_FAILURE); usleep(iMicroSecs); } break; } case 3: { for (i = 0; i < iCount; i++) { // led 1 : ON if (0 != setPinValue(P8_32, GPIO_HIGH)) exit(EXIT_FAILURE); usleep(iMicroSecs); // led 2 : ON, led 1 : OFF if (0 != setPinValue(P8_33, GPIO_HIGH)) exit(EXIT_FAILURE); if (0 != setPinValue(P8_32, GPIO_LOW)) exit(EXIT_FAILURE); usleep(iMicroSecs); // led 3 : ON, led 2 : OFF if (0 != setPinValue(P8_38, GPIO_HIGH)) exit(EXIT_FAILURE); if (0 != setPinValue(P8_33, GPIO_LOW)) exit(EXIT_FAILURE); usleep(iMicroSecs); // led 4 : ON, led 3 : OFF if (0 != setPinValue(P8_39, GPIO_HIGH)) exit(EXIT_FAILURE); if (0 != setPinValue(P8_38, GPIO_LOW)) exit(EXIT_FAILURE); usleep(iMicroSecs); //led 4 : OFF if (0 != setPinValue(P8_39, GPIO_LOW)) exit(EXIT_FAILURE); } break; } default: { //never reaches here } } }
Input::Input(Pin::List pin) : _pin(pin) { exportPin(pin); setDirection(pin, Direction::IN); _fd = open(pin, Direction::IN); }
Output::Output(Pin::List pin) : _pin(pin) { exportPin(pin); setDirection(pin, Direction::OUT); _fd = open(pin, Direction::OUT); }