/* * Function: pud_test * Description: be used to test input pull-down and pull-up related functions */ void pud_test(void) { uint8_t value; int i; printf("--------------->Start Test Pin PUD<---------------\n"); for(i=0; i < ARRAYLEN; i++) { bcm2835_gpio_fsel(bplusGpio[i], BCM2835_GPIO_FSEL_INPT); //input } for(i=0; i< ARRAYLEN; i++) { bcm2835_gpio_set_pud(bplusGpio[i], BCM2835_GPIO_PUD_UP); bcm2835_delay(500); value = bcm2835_gpio_lev(bplusGpio[i]); if(1 != value) printf("BCM pin%d:expect value %d and actual value %d\n", bplusGpio[i], 1, value); bcm2835_gpio_set_pud(bplusGpio[i], BCM2835_GPIO_PUD_DOWN); bcm2835_delay(500); value = bcm2835_gpio_lev(bplusGpio[i]); if(0 != value) printf("BCM pin%d:expect value %d and actual value %d\n", bplusGpio[i], 0, value); } printf("--------------->Test Over Of Pin PUD<---------------\n"); }
void getButtons(void) { int i; while(updateState == 1) { bcm2835_gpio_write(gpioSnesLatch, HIGH); bcm2835_delayMicroseconds(12); bcm2835_gpio_write(gpioSnesLatch, LOW); bcm2835_delayMicroseconds(6); buttonStatesAct[0] = !bcm2835_gpio_lev(gpioSnesData); for(i=1; i<16; i++) { bcm2835_gpio_write(gpioSnesClock, LOW); bcm2835_delayMicroseconds(6); bcm2835_gpio_write(gpioSnesClock, HIGH); bcm2835_delayMicroseconds(6); buttonStatesAct[i] = !bcm2835_gpio_lev(gpioSnesData); } for(i=0; i<12; i++) { if(buttonStatesAct[i] && (!buttonStatesLast[i]) && (callbacks_pressed[i] != NULL)) (*(callbacks_pressed[i]))(); else if((!buttonStatesAct[i]) && buttonStatesLast[i] && (callbacks_released[i] != NULL)) (*(callbacks_released[i]))(); buttonStatesLast[i] = buttonStatesAct[i]; } bcm2835_delay(16); } }
int quarterTurn_tt(Turntable_t* turntable, int direction, int quarters) { int target = turntable->currentEncoder + (turntable->maxEncoder/4)*quarters; int prev_encoder = bcm2835_gpio_lev(ENCODER); printf("target = %d\r\ncurrentEncoder = %d\r\n",target,turntable->currentEncoder); motorStart(direction); while(target > turntable->currentEncoder) { if(prev_encoder != bcm2835_gpio_lev(ENCODER)) { prev_encoder = !prev_encoder; turntable->currentEncoder++; } if(!bcm2835_gpio_lev(SWITCH)) { if(target > turntable->maxEncoder) { target = target - turntable->maxEncoder; turntable->currentEncoder = 0; break; } } } motorStop(); return 0; }
/** * capture and return time in us * * so rude... need kernel module... */ double hc_sr04_capture(void) { double time = 0; // void start_capture { bcm2835_gpio_write(PIN_TRIGGER, HIGH); usleep(10); bcm2835_gpio_write(PIN_TRIGGER, LOW); } // double read_distance { hc_sr04_clear_timeout(); while(HIGH != bcm2835_gpio_lev(PIN_ECHO_BACK) || hc_sr04_is_timeout(5)) { usleep(5); } hc_sr04_clear_timeout(); while(LOW != bcm2835_gpio_lev(PIN_ECHO_BACK) || hc_sr04_is_timeout(100)) { usleep(100); time += 100; } } return time; }
/* check the state of each button and each controller */ void updateButtons( snespad *pad, buttonstates* buttons ) { int i; bcm2835_gpio_write( pad->strobe, HIGH ); delayMicroseconds(2); bcm2835_gpio_write( pad->strobe, LOW ); delayMicroseconds(2); buttons->buttons1 = 0; buttons->buttons2 = 0; for (i = 0; i < 16; i++) { uint8_t curpin1 = bcm2835_gpio_lev(pad->data1); uint8_t curpin2 = bcm2835_gpio_lev(pad->data2); #ifdef DEBUG printf("1: %d 2: %d\n",curpin1,curpin2); #endif bcm2835_gpio_write( pad->clock, HIGH ); delayMicroseconds(2); bcm2835_gpio_write( pad->clock, LOW ); delayMicroseconds(2); if( curpin1==LOW ) { buttons->buttons1 |= (1<<i); } if( curpin2==LOW ) { buttons->buttons2 |= (1<<i); } } }
int calibrate_tt(Turntable_t* turntable) { // This function must be run immediately after reset to calibrate the encoder readings // Reset max encoder value turntable->maxEncoder = 0; int prev_encoder = bcm2835_gpio_lev(ENCODER); motorStart(1); while(!bcm2835_gpio_lev(SWITCH)) { // Waits for falling edge if(prev_encoder != bcm2835_gpio_lev(ENCODER)) { prev_encoder = !prev_encoder; turntable->maxEncoder++; } } while(bcm2835_gpio_lev(SWITCH)) { // waits for rising edge if(prev_encoder != bcm2835_gpio_lev(ENCODER)) { prev_encoder = !prev_encoder; turntable->maxEncoder++; } } motorStop(); // reset current encoder to start turntable->currentEncoder = 0; printf("Max encoder = %d\r\n", turntable->maxEncoder); return 0; }
int main(int argc, char **argv) { if (!bcm2835_init()) return 1; bcm2835_gpio_fsel(PIN0, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_fsel(PIN1, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_set_pud(PIN0, BCM2835_GPIO_PUD_UP); bcm2835_gpio_set_pud(PIN1, BCM2835_GPIO_PUD_UP); bcm2835_gpio_fsel(POUT1, BCM2835_GPIO_FSEL_ALT5); bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_256); bcm2835_pwm_set_mode(0, 1, 1); bcm2835_pwm_set_range(0, 1024); int data=0; while (1) { uint8_t in0 = bcm2835_gpio_lev(PIN0); uint8_t in1 = bcm2835_gpio_lev(PIN1); if (in1 == 0) break; data ++; if(data > 1023) data=0; bcm2835_pwm_set_data(0, data); bcm2835_delay(2); } bcm2835_pwm_set_data(0, 0); bcm2835_gpio_fsel(POUT1, BCM2835_GPIO_FSEL_INPT); bcm2835_close(); return 0; }
int main(){ time_t mytime; printf("Start at "); mytime = time(NULL); printf(ctime(&mytime), "\n\n"); // Чисто отладочная запись в консоль, когда была запущена программа bcm2835_close(); // Это надо на всякий случай ибо процесс прекращается по ctrl+c и до этой функции дело не доходит if (!bcm2835_init()) // Инициализация GPIO и выход с кодом "1" при неудаче return 1; bcm2835_gpio_set_pud(PIR_SIGNAL, BCM2835_GPIO_PUD_DOWN); // Подтяжка ножки к нулю bcm2835_gpio_fsel(PIR_SIGNAL, BCM2835_GPIO_FSEL_INPT); // Установка ножки для PIR на прием //printf("%u\n", bcm2835_gpio_lev(PIR_SIGNAL)); // Вывод значения ножки /////////Начало цикла слежения///////// while (1) { // Бесконечный цикл... Надо почитать про прерывания... while(!bcm2835_gpio_lev(PIR_SIGNAL)) {/* Ждем первого срабатывания */} time_to_file_first(); // см. выше printf("PIR activated at "); mytime = time(NULL); printf(ctime(&mytime), "\n\n"); // То было в файл, а это в консоль // Пауза в мс. Аналог чувствительности датчика. Нужна чтобы отсечь ложные срабатывания bcm2835_delay(2000); // Проверяем сигнал после паузы (PIR либо потухнет, либо останется активен) // Если сигнал остался, то в консоль выводим время и ждем пока на ножке не появится ноль if(bcm2835_gpio_lev(PIR_SIGNAL)) { printf("Movement detected at "); mytime = time(NULL); printf(ctime(&mytime), "\n"); //Вывод даты и времени time_to_file_on(); // А заодно и в файл while (bcm2835_gpio_lev(PIR_SIGNAL)) {/* ждем пока сигнал не прекратится */} printf("Movement stopped at "); mytime = time(NULL); printf(ctime(&mytime), "\n"); printf("\n\n"); //Вывод даты и времени time_to_file_off(); // И опять в файл } } // До сюда обычно дело не доходит. Но пусть пока останется. printf("End\n"); return (bcm2835_close()); }
int main(int argc, char **argv) { PiSwitchConfig config; if(!TryGetPiSwitchConfig(CONFIG_FILE, argc, argv, &config)) { fprintf(stderr, "Configuration error\n"); return -1; } // Open the log file openlog(LOG_IDENTITY, LOG_PID, config.RunAsDaemon ? LOG_DAEMON : LOG_USER); syslog(LOG_INFO, "Listening on gpio: %u, Writing to gpio: %u, Poll frequency: %u", config.GpioIn, config.GpioOut, config.PollFrequency); bcm2835_set_debug((uint8_t) config.DebugEnabled); running = true; TryStartDaemon(&config); SetupSignals(); SetupGpio(config.GpioIn, config.GpioOut); bool powerOff = false; while (running) { if(bcm2835_gpio_lev(config.GpioIn) == HIGH) { // Check it again! bcm2835_delay(500); if(bcm2835_gpio_lev(config.GpioIn) == HIGH) { powerOff = true; break; } } bcm2835_delay(config.PollFrequency); } if(powerOff) { syslog(LOG_INFO, "gpio %u set to HIGH, shutting system down", config.GpioIn); } else { syslog(LOG_INFO, "signal reveived, stopping"); } closelog(); bcm2835_close(); if(powerOff) { system("poweroff"); } TryStopDaemon(&config); return EXIT_SUCCESS; }
int main(int argc, char **argv) { // If you call this, it will not actually access the GPIO // Use for testing // bcm2835_set_debug(1); int a[MAX_SAMPLES]; int k; for (k=0;k<MAX_SAMPLES;++k) a[k] = 100000 + k; struct timeval tv ; gettimeofday (&tv, NULL) ; epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ; if (!bcm2835_init()) return 1; // Set the pin to be an input bcm2835_gpio_fsel(PIN_15, BCM2835_GPIO_FSEL_INPT); // Ausgang IR Empfänger // Funktioniert als IR Repeater. IR Empfangsmodul ist an IN PIN_15 // OUT PIN_22 steuert 38kHz Oszillator while (1) { int i=0; int msBegin = millis(); int msEnd = 0; a[i] = bcm2835_gpio_lev(PIN_15); if (a[i] == 0) { ++i; while (i < MAX_SAMPLES) { a[i] = bcm2835_gpio_lev(PIN_15); ++i; int p,q; for (p=0; p < 1000; ++p) q=p; } msEnd = millis(); printf("%d - %d, %d samples, %d ms per 100 samples\n", msBegin, msEnd, MAX_SAMPLES, (msEnd-msBegin)*100/MAX_SAMPLES); int j; int nLastPrint = 0; for (j=0;j<MAX_SAMPLES;++j) { if (j>0 && a[j] != a[j-1] && a[j] == 0) { printf(" (%d)\n", j-nLastPrint); nLastPrint=j; } printf("%d", (a[j]) ? 0 : 1); } printf("\n"); } } return 0; }
//-------------------------------------------------------------------------------------------------- // Name: UpdateButtons // Function: Read button states and save them to the variable "Button" and "ButtonPressed" // // Parameter: // Return: //-------------------------------------------------------------------------------------------------- void UpdateButtons(void) { ButtonMem = Button; // Save last State Button = 0; if(!bcm2835_gpio_lev(PinButton[0])) Button |= (1<<0); if(!bcm2835_gpio_lev(PinButton[1])) Button |= (1<<1); if(!bcm2835_gpio_lev(PinButton[2])) Button |= (1<<2); if(!bcm2835_gpio_lev(PinButton[3])) Button |= (1<<3); if(!bcm2835_gpio_lev(PinButton[4])) Button |= (1<<4); ButtonPressed = (Button ^ ButtonMem) & Button; // Set by Pressing a Button }
uint8_t getButton( uint8_t pin ){ if ( bcm2835_gpio_lev ( pin ) ) { delay ( 50 ); if ( bcm2835_gpio_lev ( pin ) ) { return 1; //sig_send_val ( getppid (), SIGUSR1, 1 ); do { while ( bcm2835_gpio_lev ( pin ) ) delay ( 1 ); delay ( 50 );} while ( bcm2835_gpio_lev ( pin ) ); return 1; //sig_send_val ( getppid (), SIGUSR1, 1 ); }else; }else; delay ( 1 ); }
int main(int argc, char **argv) { if (!bcm2835_init()) return 1; bcm2835_gpio_fsel(PIN0, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_fsel(PIN1, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_set_pud(PIN0, BCM2835_GPIO_PUD_UP); bcm2835_gpio_set_pud(PIN1, BCM2835_GPIO_PUD_UP); bcm2835_gpio_fsel(POUT0, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(POUT1, BCM2835_GPIO_FSEL_OUTP); while (1) { uint8_t in0 = bcm2835_gpio_lev(PIN0); uint8_t in1 = bcm2835_gpio_lev(PIN1); if (in0 == 0) { bcm2835_gpio_write(POUT0, HIGH); } else { bcm2835_gpio_write(POUT0, LOW); } if (in1 == 0) break; } for (int i=0; i<5; i++) { bcm2835_gpio_write(POUT0, HIGH); bcm2835_delay(500); bcm2835_gpio_write(POUT0, LOW); bcm2835_gpio_write(POUT1, HIGH); bcm2835_delay(500); bcm2835_gpio_write(POUT0, HIGH); bcm2835_delay(500); bcm2835_gpio_write(POUT0, LOW); bcm2835_gpio_write(POUT1, LOW); bcm2835_delay(500); } bcm2835_close(); return 0; }
int SlushMotor::busyCheck(void) { if (m_bUseSpiBusy) { if (getParam(L6470_PARAM_STATUS) & L6470_STATUS_BUSY) { return 0; } else { return 1; } } else { if (!m_bIsBusy) { if (getParam(L6470_PARAM_STATUS) & L6470_STATUS_BUSY) { return 0; } else { m_bIsBusy = true; return 1; } } // By default, the BUSY pin is forced low when the device is performing a command if (bcm2835_gpio_lev(m_nBusyPin) == HIGH) { m_bIsBusy = false; return 0; } else { return 1; } } }
char* record_burst(int time,int rate){ sample_interval=(1000*1000/rate); //microseconds sample_size = time*rate; bcm2835_gpio_fsel(PIN_RX,BCM2835_GPIO_FSEL_INPT); char* placeholder=calloc(sample_size,sizeof(char)); printf("Recording in 3\n"); fflush(stdout); bcm2835_delay(1000); printf("Recording in 2\n"); bcm2835_delay(1000); printf("Recording in 1\n"); bcm2835_delay(1000); printf("Recording for %d seconds\n",time); printf("sample_size: %d\n",sample_size); fflush(stdout); for (i=0;i<sample_size;i++){ placeholder[i]=(char)bcm2835_gpio_lev(PIN_RX); bcm2835_delayMicroseconds(sample_interval); } printf("record_burst returning\n"); return placeholder; }
// ***************************************************************************** // receive 1 char from bus // Input // send: 1=nack, (last byte) 0 = ack (get another) // ***************************************************************************** uint8_t bcm2835_i2cbb_read(struct bcm2835_i2cbb *bb, uint8_t ack) { uint8_t j, data=0; for(j=0;j<8;j++) { data<<= 1; // shift in bcm2835_i2cbb_bitdelay(bb->clock_delay); bcm2835_i2cbb_sclH(bb);; // set clock high to get data bcm2835_i2cbb_bitdelay(bb->clock_delay); // delay for slave if(bcm2835_gpio_lev(bb->sda)) data++; // get data bcm2835_i2cbb_sclL(bb); // clock back to low } // clock has been left low at this point // send ack or nack bcm2835_i2cbb_bitdelay(bb->clock_delay); if(ack) bcm2835_i2cbb_sdaH(bb); else bcm2835_i2cbb_sdaL(bb); bcm2835_i2cbb_bitdelay(bb->clock_delay); bcm2835_i2cbb_sclH(bb); // clock it in bcm2835_i2cbb_bitdelay(bb->clock_delay); bcm2835_i2cbb_sclL(bb); // bak to low bcm2835_i2cbb_sdaH(bb); // release data line return data; }
// ***************************************************************************** // sends a byte to the bus, this is an 8 bit unit so could be address or data // msb first // returns 1 for NACK and 0 for ACK (0 is good) // ***************************************************************************** int bcm2835_i2cbb_send(struct bcm2835_i2cbb *bb, uint8_t value) { uint32_t rv; uint8_t j, mask=0x80; // clock is already low from start condition for(j=0;j<8;j++) { bcm2835_i2cbb_bitdelay(bb->clock_delay); if(value & mask) bcm2835_i2cbb_sdaH(bb); else bcm2835_i2cbb_sdaL(bb); // clock out data bcm2835_i2cbb_sclH(bb); // clock it out bcm2835_i2cbb_bitdelay(bb->clock_delay); bcm2835_i2cbb_sclL(bb); // back to low so data can change mask>>= 1; // next bit along } // release bus for slave ack or nack bcm2835_i2cbb_sdaH(bb); bcm2835_i2cbb_bitdelay(bb->clock_delay); bcm2835_i2cbb_sclH(bb); // and clock high tels slave to NACK/ACK bcm2835_i2cbb_bitdelay(bb->clock_delay); // delay for slave to act rv=bcm2835_gpio_lev(bb->sda); // get ACK, NACK from slave bcm2835_i2cbb_sclL(bb); // low to keep hold of bus as start condition // bcm2835_i2cbb_bitdelay(bb->clock_delay); // bcm2835_i2cbb_scl(bb, 1); // idle state ready for stop or start return rv; }
/** * Read data from controller. * * @return value state of all eight buttons. */ uint8_t read_buttons() { // Toggle the latch 12us then wait 6us bcm2835_gpio_write(NES_LATCH,HIGH); delayMicroseconds(12); bcm2835_gpio_write(NES_LATCH,LOW); delayMicroseconds(6); uint8_t value = 0; // Will hold value of all buttons. uint8_t tmp; // Temporary storage for button data. // Grab data from all eight buttons by pulsing the clock for 6us (12us for full pulse). int i; for (i = 0; i < 8; i++) { tmp = bcm2835_gpio_lev(NES_DATA); bcm2835_gpio_write(NES_CLOCK,HIGH); delayMicroseconds(6); bcm2835_gpio_write(NES_CLOCK,LOW); delayMicroseconds(6); value |= (!tmp << i); } return value; }
int main(int argc, char **argv) { uint8_t value,i; if (!bcm2835_init()) return 1; for(i=0;i<5;i++) { bcm2835_gpio_fsel(KEY[i], BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_set_pud(KEY[i], BCM2835_GPIO_PUD_UP); } while (1) { for(i=0;i<5;i++) { if(bcm2835_gpio_lev(KEY[i])==0) { printf("press the key: %d\n", i); delay(500); } } } bcm2835_close(); return 0; }
int main(int argc, char **argv) { // If you call this, it will not actually access the GPIO // Use for testing // bcm2835_set_debug(1); if (!bcm2835_init()) return 1; // Set RPI pin P1-15 to be an input bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_INPT); // with a pullup bcm2835_gpio_set_pud(PIN, BCM2835_GPIO_PUD_UP); // Blink while (1) { // Read some data uint8_t value = bcm2835_gpio_lev(PIN); printf("read from pin 15: %d\n", value); // wait a bit delay(500); } bcm2835_close(); return 0; }
void bcm2835_gpio_show_pins(uint8_t watched_pin) { int i; for(i = 1; i <= 17; i++) { int pin = -1; switch(i) { case 1: pin = RPI_GPIO_P1_03; break; case 2: pin = RPI_GPIO_P1_05; break; case 3: pin = RPI_GPIO_P1_07; break; case 4: pin = RPI_GPIO_P1_08; break; case 5: pin = RPI_GPIO_P1_10; break; case 6: pin = RPI_GPIO_P1_11; break; case 7: pin = RPI_GPIO_P1_12; break; case 8: pin = RPI_GPIO_P1_13; break; case 9: pin = RPI_GPIO_P1_15; break; case 10: pin = RPI_GPIO_P1_16; break; case 11: pin = RPI_GPIO_P1_18; break; case 12: pin = RPI_GPIO_P1_19; break; case 13: pin = RPI_GPIO_P1_21; break; case 14: pin = RPI_GPIO_P1_22; break; case 15: pin = RPI_GPIO_P1_23; break; case 16: pin = RPI_GPIO_P1_24; break; case 17: pin = RPI_GPIO_P1_26; break; } if (pin == watched_pin) printf("\033[35m"); if (pin >= 0) printf("%1d", bcm2835_gpio_lev(pin)); else printf("-"); if (pin == watched_pin) printf("\033[37m"); } printf("\n"); }
int main(int argc, char** argv){ if (!bcm2835_init()) return 1; //Set PIN_OUT to be output bcm2835_gpio_fsel(PIN_OUT, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_write(PIN_OUT, HIGH); // Set PIN_IN to be input with pulldown bcm2835_gpio_fsel(PIN_IN, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_set_pud(PIN_IN, BCM2835_GPIO_PUD_DOWN); run = 1; counter = 0; status = LOW; while(run){ //Check if door is open or closed uint8_t value = bcm2835_gpio_lev(PIN_IN); if(value == HIGH) on_high(); else on_low(); //Sleep counter++; delay(SLEEP_TIME); } return 1; }
int main(int argc, char **argv) { if (!bcm2835_init()) { return 1; } bcm2835_gpio_fsel(LED_PIN, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(BUZ_PIN, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(TLT_PIN, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_set_pud(TLT_PIN, BCM2835_GPIO_PUD_DOWN); for (int i = 0; i < 100; i++) { uint8_t value = bcm2835_gpio_lev(TLT_PIN); printf("read from pin %d : %d\n", TLT_PIN, value); if (value == 0) { bcm2835_gpio_write(LED_PIN, HIGH); bcm2835_gpio_write(BUZ_PIN, LOW); } else { bcm2835_gpio_write(LED_PIN, LOW); bcm2835_gpio_write(BUZ_PIN, HIGH); } bcm2835_delay(500); } bcm2835_gpio_write(LED_PIN, LOW); bcm2835_close(); return 0; }
/** * @brief Configures the pins as input and returns the pin status. * @param pin Pin to be configured, Available pin definitons are defined in the Utitlies.h * @return status Pin level can be HIGH or LOW. */ PinLevel_t ReadPinStatus(PIN_t pin) { bcm2835_gpio_fsel(pin,BCM2835_GPIO_FSEL_INPT); // Set the pin to be an input bcm2835_gpio_set_pud(pin, BCM2835_GPIO_PUD_DOWN); // Enable pull up. PinLevel_t pinLevel = (PinLevel_t)bcm2835_gpio_lev(pin); //read voltage level on MFP pin return pinLevel; }
int main(int argc, char **argv) { int usepin; if (!bcm2835_init()) return 1; // check board revision and set the pin to be an output if (get_rpi_revision()==1) { usepin = PIN; } else { usepin = PIN_V2; } bcm2835_gpio_fsel(usepin, BCM2835_GPIO_FSEL_INPT); while (1) { if (bcm2835_gpio_lev(usepin)==LOW) { printf("Button state: NOT PRESSED\n"); } else { printf("Button state: PRESSED\n"); } // wait a bit delay(500); } return 0; }
unsigned char MaxDetect1Wire(unsigned char type, unsigned char pin, unsigned char param) { unsigned char bits[40]; unsigned char data[5] = {0,0,0,0,0}; unsigned char bitid = 0; bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT); if (bcm2835_gpio_lev(pin) == 0 && bitDetect(pin, 0, 10000) == 0) return 0; bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_write(pin, 0); delay(18); bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT); if (bitDetect(pin, 1, 100) == 0) return 0; if (bitDetect(pin, 0, 100) == 0) return 0; if (bitDetect(pin, 1, 100) == 0) return 0; for (unsigned char i=0; i<40; i++) { if (bitDetect(pin, 0, 100) == 0) return 0; bits[i] = bitDetect(pin, 1, 100); if (bits[i] == 0) return 0; bitid++; } if (bitid == 40) { unsigned char j=0; for (int i=0; i<bitid; i++) { data[j/8] <<= 1; if (bits[i] > 40) data[j/8] |= 1; j++; if (param == debug) printf("bit %d: %d ms\n", i, bits[i]); } if (param == debug) printf("Data (%d): 0x%x 0x%x 0x%x 0x%x 0x%x\n", j, data[0], data[1], data[2], data[3], data[4]); if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) { if (type == DHT11) { if (param == simple) printf("%d.%d %d.%d\n", data[2], data[3], data[0], data[1]); else printf("Temperature = %d.%d *C Humidity = %d.%d %%\n", data[2], data[3], data[0], data[1]); } else if (type == DHT22) { float f, h; h = data[0] * 256 + data[1]; h /= 10; f = (data[2] & 0x7F)* 256 + data[3]; f /= 10.0; if (data[2] & 0x80) f *= -1; if (param == simple) printf("%.1f %.1f\n", f, h); else printf("Temperature = %.1f *C Humidity = %.1f %%\n", f, h); } return 1; } else if (param == debug) { printf("CRC doesn't match\n"); } } return 0; }
unsigned char bitDetect(unsigned char pin, unsigned char bitTest, unsigned short maxTime) { unsigned short counter = 0; while (bcm2835_gpio_lev(pin) == bitTest) { delayMicroseconds(1); counter++; if (counter == maxTime) return 0; } return counter; }
int max98306_check_headphone(void) { int value=0; value=bcm2835_gpio_lev(HEADPHONE_GPIO); return value; }
static void read_port(void *arg, long period) { int n; for (n = 0; n < npins; n++) { if ((~dir_map & RTAPI_BIT(n)) && (~exclude_map & RTAPI_BIT(n))) *port_data[n] = bcm2835_gpio_lev(gpios[n]); } }
//uint8_t bcm2835_gpio_lev(uint8_t pin); /// Call bcm2835_gpio_lev with 1 parameter /// \par Refer /// \par Modify void ope_gpio_lev(void) { uint8_t ret; get_byte_code(); ret = bcm2835_gpio_lev( *((uint8_t *)(buff+1)) ); set_ope_code( OPE_GPIO_LEV ); set_byte_code( ret ); put_reply(); mark_sync(); }