int digitalRead(const char *pin) { const struct pin *p = getPin(pin, strlen(pin)); if(checkPin(p, 0) == 0) { printf("Error: %s is not a digital pin\n", pin); return 0; } char buf[29]; snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%i/value", p->gpio); FILE *file = fopen(buf, "r"); char value = fgetc(file); fclose(file); if(value == '1') return 1; return 0; }
void eMainMenu::sel_setup() { int setuppin=0; eConfig::getInstance()->getKey("/elitedvb/pins/setuplock", setuppin); if ( checkPin( setuppin, _("setup") ) ) { hide(); int i=0; do { eZapSetup setup; #ifndef DISABLE_LCD setup.setLCD(LCDTitle, LCDElement); #endif setup.show(); i=setup.exec(); setup.hide(); } while(i==-1); // to redisplay Setup after language change if ( !simpleMainmenu ) setActive(active); // --"-- show(); } }
/* Starts an analog measurement on the pin. * It returns inmediately, read value with readSingle(). * If the pin is incorrect it returns ADC_ERROR_VALUE. */ bool ADC_Module::startSingleRead(uint8_t pin) { // check whether the pin is correct if(!checkPin(pin)) { fail_flag |= ADC_ERROR_WRONG_PIN; return false; } if (calibrating) wait_for_cal(); // save the current state of the ADC in case it's in use adcWasInUse = isConverting(); // is the ADC running now? if(adcWasInUse) { // this means we're interrupting a conversion // save the current conversion config, the adc isr will restore the adc __disable_irq(); saveConfig(&adc_config); __enable_irq(); } // no continuous mode singleMode(); // start measurement startReadFast(pin); return true; }
int main(int argc, char* argv[]) { #ifdef DEBUG printf("argument count : %d\n", argc); printf("argument[0] : %c\n", argv[0]); #endif int opt; int argumentCount = 0; while ((opt = getopt(argc, argv, "s:g:")) != -1) { switch (opt) { case 's': argumentCount++; break; case 'g': argumentCount++; if (!checkPin(optarg)) { exit(1); } break; } } #ifdef DEBUG printf("argumentCount >>> %d\n", argumentCount); #endif if (argumentCount != 2) { printf("Usage : sudo ./Raspberry_DHT11 -s [model] -g [G:PIN|W:PIN]\n"); printf("Example : sudo ./Raspberry_DHT11 -s DHT11 -g G:18 => DHT11, GPIO_18\n"); printf("Example : sudo ./Raspberry_DHT11 -s DHT11 -g W:1 => DHT11, WiringPi 1\n"); exit(1); } if ( wiringPiSetup() == -1 ) { printMessage(false, "Setup fail."); exit(1); } while (1) { if (read_dht11_dat()) { printMessage(true, NULL); #ifndef DEBUG break; #endif } #ifdef DEBUG delay( 1000 ); /* wait 1sec to refresh */ #endif } return(0); }
void eParentalSetup::changePin(eButton *p) { const char *text = ( p == changeParentalPin ) ? _("parental") : _("setup"); int oldpin = (p == changeParentalPin) ? parentalpin : setuppin; int ret = 0; if ( oldpin ) // let enter the oldpin.. and validate { if ( !checkPin( oldpin, text ) ) return; } int newPin=0; do { { ParentalLockWindow w(eString().sprintf(_("New %s PIN"),text).c_str(), 0 ); w.show(); newPin = w.exec(); w.hide(); if ( newPin == -1 ) // cancel pressed return; } ParentalLockWindow w(eString().sprintf(_("Reenter %s PIN"),text).c_str(), 0 ); w.show(); ret = w.exec(); w.hide(); if ( ret == -1 ) // cancel pressed return; else if ( ret != newPin ) { int ret = eMessageBox::ShowBox(_("The PINs are not equal!\n\nDo you want to retry?"), _("PIN validation failed"), eMessageBox::btYes|eMessageBox::btNo|eMessageBox::iconQuestion, eMessageBox::btYes ); if ( ret == eMessageBox::btNo || ret == -1 ) return; } } while ( newPin != ret ); if ( p == changeParentalPin ) parentalpin = newPin; else setuppin = newPin; eMessageBox::ShowBox(_("PIN change completed"), _("PIN changed"), eMessageBox::btOK|eMessageBox::iconInfo, eMessageBox::btOK ); }
void eParentalSetup::slockChecked(int i) { if ( i && !changeSetupPin->isVisible() ) changeSetupPin->show(); else { if ( checkPin( setuppin, _("setup") ) ) { setuppin=0; changeSetupPin->hide(); } else setuplock->setCheck(1); } }
void unexportGpio(const char *pin) { const struct pin *p = getPin(pin, strlen(pin)); if(checkPin(p, 0) == 0) { printf("Error: %s is not a gpio pin\n", pin); return; } FILE *file = fopen("/sys/class/gpio/unexport", "w"); char num[3]; snprintf(num, sizeof(num), "%i", p->gpio); fputs(num, file); fclose(file); }
void pwmWrite(const char* pin, int frequency, int percent, int isrun) { const struct pin *p = getPin(pin, strlen(pin)); if(checkPin(p, 3) == 0) { printf("Error: %s is not pwm capable\n", pin); return; } char pwm[26] = "/sys/class/pwm/"; strcat(pwm, getPin(pin, strlen(pin))->pwm); char duty_percent[39] = ""; strcat(duty_percent, pwm); strcat(duty_percent, "/duty_percent"); char period_freq[38] = ""; strcat(period_freq, pwm); strcat(period_freq, "/period_freq"); char run[30] = ""; strcat(run, pwm); strcat(run, "/run"); FILE *file = fopen(period_freq, "w"); fprintf(file,"%i", frequency); fclose(file); file = fopen(duty_percent, "w"); fprintf(file, "%i", percent); fclose(file); if(isrun == 1) { file = fopen(run, "w"); fputs("1", file); fclose(file); } else { file = fopen(run, "w"); fputs("0", file); fclose(file); } }
void digitalWrite(const char *pin, int value) { const struct pin *p = getPin(pin, strlen(pin)); if(checkPin(p, 0) == 0) { printf("Error: %s is not a digital pin\n", pin); return; } char buf[29]; snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%i/value", p->gpio); FILE *file = fopen(buf, "w"); if(value) fputc('1', file); else fputc('0', file); fclose(file); }
void muxPin(const char* pin, int mode) { const struct pin *p = getPin(pin, strlen(pin)); if(checkPin(p, 0) == 0) { printf("Error: pin %s cannot be muxed\n", pin); return; } char buf[44]; snprintf(buf, sizeof(buf), "/sys/kernel/debug/omap_mux/%s", getPin(pin, strlen(pin))->mux); FILE *file = fopen(buf, "w"); fprintf(file, "%x", mode); fclose(file); }
void digitalMode(const char *pin, int mode) { const struct pin *p = getPin(pin, strlen(pin)); if(checkPin(p, 0) == 0) { printf("Error: %s is not a digital pin\n", pin); return; } char buf[33]; snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%i/direction", p->gpio); FILE *file = fopen(buf, "w"); if(mode) fputs("out", file); else fputs("in", file); fclose(file); }
/* Starts continuous conversion on the pin * It returns as soon as the ADC is set, use analogReadContinuous() to read the values * Set the resolution, number of averages and voltage reference using the appropriate functions BEFORE calling this function */ bool ADC_Module::startContinuous(uint8_t pin) { // check whether the pin is correct if(!checkPin(pin)) { fail_flag |= ADC_ERROR_WRONG_PIN; return false; } // check for calibration before setting channels, if (calibrating) wait_for_cal(); // increase the counter of measurements num_measurements++; // set continuous conversion flag continuousMode(); startReadFast(pin); return true; }
void eParentalSetup::plockChecked(int i) { if ( i && !changeParentalPin->isVisible() ) { changeParentalPin->show(); hidelocked->show(); } else { if ( checkPin( parentalpin, _("parental") ) ) { parentalpin=0; changeParentalPin->hide(); hidelocked->hide(); } else { hidelocked->show(); parentallock->setCheck(1); } } }
bool CRaspiProcessInterface::initialise(bool m_bInputOrOutput){ wiringPiSetup(); CIEC_INT param; param.fromString(PARAMS().getValue()); mPinNumber = param; if(true == checkPin()){ if(m_bInputOrOutput){ //if pin used as input pullUpDnControl(mPinNumber, PUD_DOWN); //pull-down the input-pin. use PUD_UP for pull-up. CRaspiProcessInterface::smPinUsage[mPinNumber] = CRaspiProcessInterface::enInput; pinMode(mPinNumber, INPUT); }else{ CRaspiProcessInterface::smPinUsage[mPinNumber] = CRaspiProcessInterface::enOutput; pinMode(mPinNumber, OUTPUT); } STATUS() = scmInitDeinitOK; return true; }else{ STATUS() = scmPinInUse; return false; } }
/* Reads the analog value of the pin. * It waits until the value is read and then returns the result. * If a comparison has been set up and fails, it will return ADC_ERROR_VALUE. * Set the resolution, number of averages and voltage reference using the appropriate functions. */ int ADC_Module::analogRead(uint8_t pin) { //digitalWriteFast(LED_BUILTIN, HIGH); // check whether the pin is correct if(!checkPin(pin)) { fail_flag |= ADC_ERROR_WRONG_PIN; return ADC_ERROR_VALUE; } // increase the counter of measurements num_measurements++; //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN)); if (calibrating) wait_for_cal(); //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN)); // check if we are interrupting a measurement, store setting if so. // vars to save the current state of the ADC in case it's in use ADC_Config old_config = {0}; uint8_t wasADCInUse = isConverting(); // is the ADC running now? if(wasADCInUse) { // this means we're interrupting a conversion // save the current conversion config, we don't want any other interrupts messing up the configs __disable_irq(); //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN) ); saveConfig(&old_config); __enable_irq(); } // no continuous mode singleMode(); startReadFast(pin); // start single read // wait for the ADC to finish while(isConverting()) { yield(); } // it's done, check if the comparison (if any) was true int32_t result; __disable_irq(); // make sure nothing interrupts this part if (isComplete()) { // conversion succeded result = (uint16_t)*ADC_RA; } else { // comparison was false fail_flag |= ADC_ERROR_COMPARISON; result = ADC_ERROR_VALUE; } __enable_irq(); // if we interrupted a conversion, set it again if (wasADCInUse) { //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN) ); __disable_irq(); loadConfig(&old_config); __enable_irq(); } num_measurements--; return result; } // analogRead